OKVIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Duration.hpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Modified and adapted by
5  * Copyright (c) 2015, Autonomous Systems Lab / ETH Zurich
6  *
7  * Original Copyright (c) 2010, Willow Garage, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of Willow Garage, Inc. nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *********************************************************************/
37 /*
38  * Modified: Stefan Leutenegger (s.leutenegger@imperial.ac.uk)
39  * Modified: Andreas Forster (an.forster@gmail.com)
40  */
41 
50 #ifndef INCLUDE_OKVIS_IMPLEMENTATION_DURATION_HPP_
51 #define INCLUDE_OKVIS_IMPLEMENTATION_DURATION_HPP_
52 
53 #include <okvis/Duration.hpp>
54 //#include <ros/rate.h>
55 
57 namespace okvis {
58 //
59 // DurationBase template member function implementation
60 //
61 template<class T>
62 DurationBase<T>::DurationBase(int32_t _sec, int32_t _nsec)
63  : sec(_sec),
64  nsec(_nsec) {
66 }
67 
68 template<class T>
70 #ifdef HAVE_TRUNC
71  sec = (int32_t)trunc(d);
72 #else
73  // (morgan: why doesn't win32 provide trunc? argh. hacked this together
74  // without much thought. need to test this conversion.
75  if (d >= 0.0)
76  sec = (int32_t) floor(d);
77  else
78  sec = (int32_t) floor(d) + 1;
79 #endif
80  nsec = (int32_t)((d - (double) sec) * 1000000000);
81  return *static_cast<T*>(this);
82 }
83 
84 template<class T>
86  sec = (int32_t)(t / 1000000000);
87  nsec = (int32_t)(t % 1000000000);
88 
89  normalizeSecNSecSigned(sec, nsec);
90 
91  return *static_cast<T*>(this);
92 }
93 
94 template<class T>
95 T DurationBase<T>::operator+(const T &rhs) const {
96  return T(sec + rhs.sec, nsec + rhs.nsec);
97 }
98 
99 template<class T>
100 T DurationBase<T>::operator*(double scale) const {
101  return T(toSec() * scale);
102 }
103 
104 template<class T>
105 T DurationBase<T>::operator-(const T &rhs) const {
106  return T(sec - rhs.sec, nsec - rhs.nsec);
107 }
108 
109 template<class T>
111  return T(-sec, -nsec);
112 }
113 
114 template<class T>
115 T& DurationBase<T>::operator+=(const T &rhs) {
116  *this = *this + rhs;
117  return *static_cast<T*>(this);
118 }
119 
120 template<class T>
121 T& DurationBase<T>::operator-=(const T &rhs) {
122  *this += (-rhs);
123  return *static_cast<T*>(this);
124 }
125 
126 template<class T>
127 T& DurationBase<T>::operator*=(double scale) {
128  fromSec(toSec() * scale);
129  return *static_cast<T*>(this);
130 }
131 
132 template<class T>
133 bool DurationBase<T>::operator<(const T &rhs) const {
134  if (sec < rhs.sec)
135  return true;
136  else if (sec == rhs.sec && nsec < rhs.nsec)
137  return true;
138  return false;
139 }
140 
141 template<class T>
142 bool DurationBase<T>::operator>(const T &rhs) const {
143  if (sec > rhs.sec)
144  return true;
145  else if (sec == rhs.sec && nsec > rhs.nsec)
146  return true;
147  return false;
148 }
149 
150 template<class T>
151 bool DurationBase<T>::operator<=(const T &rhs) const {
152  if (sec < rhs.sec)
153  return true;
154  else if (sec == rhs.sec && nsec <= rhs.nsec)
155  return true;
156  return false;
157 }
158 
159 template<class T>
160 bool DurationBase<T>::operator>=(const T &rhs) const {
161  if (sec > rhs.sec)
162  return true;
163  else if (sec == rhs.sec && nsec >= rhs.nsec)
164  return true;
165  return false;
166 }
167 
168 template<class T>
169 bool DurationBase<T>::operator==(const T &rhs) const {
170  return sec == rhs.sec && nsec == rhs.nsec;
171 }
172 
173 template<class T>
175  return sec == 0 && nsec == 0;
176 }
177 }
178 
179 #endif // INCLUDE_OKVIS_IMPLEMENTATION_DURATION_HPP_
DurationBase()
Definition: Duration.hpp:85
bool operator==(const T &rhs) const
Definition: Duration.hpp:169
bool isZero()
Definition: Duration.hpp:174
bool operator<(const T &rhs) const
Definition: Duration.hpp:133
bool operator<=(const T &rhs) const
Definition: Duration.hpp:151
Header file for the DurationBase, Duration and WallDuration class.
bool operator>(const T &rhs) const
Definition: Duration.hpp:142
T & fromNSec(int64_t t)
Definition: Duration.hpp:85
bool operator>=(const T &rhs) const
Definition: Duration.hpp:160
T & operator-=(const T &rhs)
Definition: Duration.hpp:121
T operator+(const T &rhs) const
Definition: Duration.hpp:95
void normalizeSecNSecSigned(int64_t &sec, int64_t &nsec)
Definition: Duration.cpp:55
int32_t nsec
Definition: Duration.hpp:84
T operator-() const
Definition: Duration.hpp:110
T & fromSec(double t)
Definition: Duration.hpp:69
T operator*(double scale) const
Definition: Duration.hpp:100
T & operator*=(double scale)
Definition: Duration.hpp:127
int32_t sec
Definition: Duration.hpp:84
T & operator+=(const T &rhs)
Definition: Duration.hpp:115