Types.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Nate Koenig
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef SDFORMAT_TYPES_HH_
18 #define SDFORMAT_TYPES_HH_
19 
20 #include <algorithm>
21 #include <cmath>
22 #include <cstdint>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 
27 #include <sdf/sdf_config.h>
28 #include "sdf/system_util.hh"
29 #include "sdf/Error.hh"
30 
31 #if defined(__GNUC__)
32 #define SDF_DEPRECATED(version) __attribute__((deprecated))
33 #define SDF_FORCEINLINE __attribute__((always_inline))
34 #elif defined(MSVC)
35 #define SDF_DEPRECATED(version)
36 #define SDF_FORCEINLINE __forceinline
37 #else
38 #define SDF_DEPRECATED(version)
39 #define SDF_FORCEINLINE
40 #endif
41 
42 namespace sdf
43 {
44  // Inline bracket to help doxygen filtering.
45  inline namespace SDF_VERSION_NAMESPACE {
46  //
47 
53  std::vector<std::string> split(const std::string &_str,
54  const std::string &_splitter);
55 
60  std::string trim(const char *_in);
61 
66  template<typename T>
67  inline bool equal(const T &_a, const T &_b,
68  const T &_epsilon = 1e-6f)
69  {
70  return std::fabs(_a - _b) <= _epsilon;
71  }
72 
74  using Errors = std::vector<Error>;
75 
78  {
85  public: Color(float _r = 0.0f, float _g = 0.0f,
86  float _b = 0.0f, float _a = 1.0f) SDF_DEPRECATED(6.0)
87  : r(_r), g(_g), b(_b), a(_a)
88  {}
89 
94  public: friend std::ostream &operator<< (std::ostream &_out,
95  const Color &_pt)
96  {
97  _out << _pt.r << " " << _pt.g << " " << _pt.b << " " << _pt.a;
98  return _out;
99  }
100 
104  public: friend std::istream &operator>> (std::istream &_in, Color &_pt)
105  {
106  // Skip white spaces
107  _in.setf(std::ios_base::skipws);
108  _in >> _pt.r >> _pt.g >> _pt.b >> _pt.a;
109  return _in;
110  }
111 
115  public: bool operator ==(const Color &_clr) const
116  {
117  return equal(this->r, _clr.r) &&
118  equal(this->g, _clr.g) &&
119  equal(this->b, _clr.b) &&
120  equal(this->a, _clr.a);
121  }
122 
124  public: float r;
125 
127  public: float g;
128 
130  public: float b;
131 
133  public: float a;
134  };
135 
139  {
141  public: Time()
142  : sec(0), nsec(0)
143  {
144  }
145 
149  public: Time(int32_t _sec, int32_t _nsec)
150  : sec(_sec), nsec(_nsec)
151  {
152  }
153 
158  public: friend std::ostream &operator<<(std::ostream &_out,
159  const Time &_time)
160  {
161  _out << _time.sec << " " << _time.nsec;
162  return _out;
163  }
164 
169  public: friend std::istream &operator>>(std::istream &_in,
170  Time &_time)
171  {
172  // Skip white spaces
173  _in.setf(std::ios_base::skipws);
174  _in >> _time.sec >> _time.nsec;
175  return _in;
176  }
177 
181  public: bool operator ==(const Time &_time) const
182  {
183  return this->sec == _time.sec && this->nsec == _time.nsec;
184  }
185 
187  public: int32_t sec;
188 
190  public: int32_t nsec;
191  };
192 
195  {
196  public: double mass;
197  };
198 
202  std::string SDFORMAT_VISIBLE lowercase(const std::string &_in);
203  }
204 }
205 #endif
friend std::ostream & operator<<(std::ostream &_out, const Time &_time)
Stream insertion operator.
Definition: Types.hh:158
Time()
Constructor.
Definition: Types.hh:141
SDFORMAT_VISIBLE std::vector< std::string > split(const std::string &_str, const std::string &_splitter)
Split a string using the delimiter in splitter.
friend std::istream & operator>>(std::istream &_in, Time &_time)
Stream extraction operator.
Definition: Types.hh:169
float g
Green value.
Definition: Types.hh:127
Defines a color.
Definition: Types.hh:77
std::string SDFORMAT_VISIBLE lowercase(const std::string &_in)
Transforms a string to its lowercase equivalent.
double mass
Definition: Types.hh:196
int32_t sec
Seconds.
Definition: Types.hh:187
float a
Alpha value.
Definition: Types.hh:133
std::vector< Error > Errors
A vector of Error.
Definition: Types.hh:74
#define SDFORMAT_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system_util.hh:48
float r
Red value.
Definition: Types.hh:124
bool equal(const T &_a, const T &_b, const T &_epsilon=1e-6f)
check if two values are equal, within a tolerance
Definition: Types.hh:67
A Time class, can be used to hold wall- or sim-time.
Definition: Types.hh:138
A class for inertial information about a link.
Definition: Types.hh:194
namespace for Simulation Description Format parser
Definition: Actor.hh:32
int32_t nsec
Nanoseconds.
Definition: Types.hh:190
Color(float _r=0.0f, float _g=0.0f, float _b=0.0f, float _a=1.0f)
Constructor.
Definition: Types.hh:85
SDFORMAT_VISIBLE std::string trim(const char *_in)
Trim leading and trailing whitespace from a string.
#define SDF_DEPRECATED(version)
Definition: Types.hh:38
float b
Blue value.
Definition: Types.hh:130
std::ostream & operator<<(std::ostream &os, ParamStreamer< T > s)
Definition: Param.hh:75
Time(int32_t _sec, int32_t _nsec)
Constructor.
Definition: Types.hh:149