31 #ifndef WFMATH_ATLAS_CONV_H
32 #define WFMATH_ATLAS_CONV_H
34 #include <wfmath/point.h>
35 #include <wfmath/vector.h>
36 #include <wfmath/quaternion.h>
37 #include <wfmath/axisbox.h>
38 #include <wfmath/polygon.h>
39 #include <wfmath/ball.h>
40 #include <wfmath/rotbox.h>
46 #ifdef ATLAS_MESSAGE_ELEMENT_H
48 typedef Atlas::Message::WrongTypeException _AtlasBadParse;
49 typedef Atlas::Message::Element _AtlasMessageType;
50 typedef Atlas::Message::FloatType _AtlasFloatType;
51 typedef Atlas::Message::ListType _AtlasListType;
52 typedef Atlas::Message::MapType _AtlasMapType;
54 inline bool _isNum(
const _AtlasMessageType& a) {
return a.isNum();}
55 inline _AtlasFloatType _asNum(
const _AtlasMessageType& a) {
return a.asNum();}
58 #error "You must include Atlas/Message/Element.h before wfmath/atlasconv.h"
64 AtlasInType(
const _AtlasMessageType& val) : m_val(val) {}
66 template<
class C> AtlasInType(C c) : m_obj(c), m_val(m_obj) {}
67 operator const _AtlasMessageType&()
const {
return m_val;}
68 bool IsList()
const {
return m_val.isList();}
69 const _AtlasListType& AsList()
const {
return m_val.asList();}
71 _AtlasMessageType m_obj;
72 const _AtlasMessageType& m_val;
78 AtlasOutType(
const _AtlasListType& l) : m_val(l) {}
79 AtlasOutType(
const _AtlasMapType& l) : m_val(l) {}
80 operator _AtlasMessageType&() {
return m_val;}
81 operator const _AtlasMessageType&()
const {
return m_val;}
83 _AtlasMessageType m_val;
86 inline AtlasOutType _ArrayToAtlas(
const CoordType* array,
unsigned len)
88 _AtlasListType a(len);
90 for(
unsigned i = 0; i < len; ++i)
96 inline void _ArrayFromAtlas(
CoordType* array,
unsigned len,
const AtlasInType& a)
99 throw _AtlasBadParse();
101 const _AtlasListType& list(a.AsList());
103 if(list.size() != (
unsigned int) len)
104 throw _AtlasBadParse();
106 for(
unsigned i = 0; i < len; ++i)
107 array[i] = _asNum(list[i]);
119 _ArrayFromAtlas(m_elem, dim, a);
126 return _ArrayToAtlas(m_elem, dim);
132 throw _AtlasBadParse();
135 const _AtlasListType& list(a.AsList());
138 throw _AtlasBadParse();
141 for(
int i = 0; i < 3; ++i)
142 m_vec[i] = _asNum(list[i]);
144 m_w = _asNum(list[3]);
148 if (norm <= WFMATH_EPSILON) {
166 for(
int i = 0; i < 3; ++i)
182 _ArrayFromAtlas(m_elem, dim, a);
189 return _ArrayToAtlas(m_elem, dim);
202 throw _AtlasBadParse();
204 const _AtlasListType& list(a.AsList());
206 switch(list.size()) {
212 for(
int i = 0; i < dim; ++i) {
213 m_low[i] = _asNum(list[i]);
214 m_high[i] = _asNum(list[i+dim]);
220 throw _AtlasBadParse();
223 for(
int i = 0; i < dim; ++i) {
224 if(m_low[i] > m_high[i]) {
226 m_low[i] = m_high[i];
237 for(i = 0; i < dim; ++i)
242 return m_high.toAtlas();
246 _AtlasListType a(2*dim);
247 for(i = 0; i < dim; ++i) {
249 a[dim+i] = m_high[i];
258 const _AtlasMessageType& message(a);
259 if (message.isMap()) {
260 const Atlas::Message::MapType& shapeElement(message.asMap());
262 Atlas::Message::MapType::const_iterator shape_I = shapeElement.find(
"radius");
263 if (shape_I != shapeElement.end()) {
264 const Atlas::Message::Element& shapeRadiusElem(shape_I->second);
265 if (shapeRadiusElem.isNum()) {
266 m_radius = shapeRadiusElem.asNum();
269 Atlas::Message::MapType::const_iterator pos_I = shapeElement.find(
"position");
270 if (pos_I != shapeElement.end()) {
271 const Atlas::Message::Element& posElem(pos_I->second);
272 if (posElem.isList()) {
273 m_center.fromAtlas(posElem);
282 Atlas::Message::MapType map;
283 map.insert(Atlas::Message::MapType::value_type(
"radius", _AtlasFloatType(m_radius)));
284 map.insert(Atlas::Message::MapType::value_type(
"position", m_center.toAtlas()));
295 template<
template <
int>
class ShapeT,
int dim>
296 inline void _CornersFromAtlas(ShapeT<dim> & shape,
297 const _AtlasMessageType& message)
299 if (message.isList()) {
300 const Atlas::Message::ListType& pointsData(message.asList());
302 for (
size_t p = 0; p < pointsData.size(); ++p) {
303 if (!pointsData[p].isList()) {
307 const Atlas::Message::ListType& point(pointsData[p].asList());
308 if ((point.size() < dim) || !point[0].isNum() || !point[1].isNum()) {
313 shape.addCorner(shape.numCorners(), wpt);
320 const _AtlasMessageType& message(a);
321 if (message.isMap()) {
322 const Atlas::Message::MapType& shapeElement(message.asMap());
323 Atlas::Message::MapType::const_iterator it = shapeElement.find(
"points");
324 if ((it != shapeElement.end()) && it->second.isList()) {
325 _CornersFromAtlas(*
this, it->second);
326 if (numCorners() > 2) {
330 }
else if (message.isList()) {
331 _CornersFromAtlas(*
this, message);
332 if (numCorners() > 2) {
336 throw _AtlasBadParse();
341 Atlas::Message::ListType points;
342 for (theConstIter I = m_points.begin(); I != m_points.end(); ++I)
344 points.push_back(I->toAtlas());
346 Atlas::Message::MapType map;
347 map.insert(Atlas::Message::MapType::value_type(
"points", points));
355 const _AtlasMessageType& message(a);
356 if (message.isMap()) {
357 const Atlas::Message::MapType& shapeElement(message.asMap());
359 Atlas::Message::MapType::const_iterator shape_I = shapeElement.find(
"point");
360 if (shape_I != shapeElement.end()) {
361 const Atlas::Message::Element& shapePointElem(shape_I->second);
365 shape_I = shapeElement.find(
"size");
366 if (shape_I != shapeElement.end()) {
367 const Atlas::Message::Element& shapeVectorElem(shape_I->second);
370 m_corner0 = shapePoint;
371 m_size = shapeVector;
377 throw _AtlasBadParse();
383 Atlas::Message::MapType map;
384 map.insert(Atlas::Message::MapType::value_type(
"point", m_corner0.toAtlas()));
385 map.insert(Atlas::Message::MapType::value_type(
"size", m_size.toAtlas()));
397 #endif // WFMATH_ATLAS_CONV_H
Vector()
Construct an uninitialized vector.
Definition: vector.h:125
RotBox()
construct an uninitialized box
Definition: rotbox.h:50
void fromAtlas(const AtlasInType &a)
Set the box's value to that given by an Atlas object.
Definition: atlasconv.h:353
void fromAtlas(const AtlasInType &a)
Set the box's value to that given by an Atlas object.
Definition: atlasconv.h:256
A dim dimensional rotation matrix. Technically, a member of the group O(dim).
Definition: const.h:53
CoordType sqrMag() const
The squared magnitude of a vector.
Definition: vector_funcs.h:284
A polygon, all of whose points lie in a plane, embedded in dim dimensions.
Definition: const.h:51
AtlasOutType toAtlas() const
Create an Atlas object from the box.
Definition: atlasconv.h:280
AtlasOutType toAtlas() const
Create an Atlas object from the box.
Definition: atlasconv.h:381
void fromAtlas(const AtlasInType &a)
Set the point's value to that given by an Atlas object.
Definition: atlasconv.h:180
void setValid(bool valid=true)
make isValid() return true if you've initialized the vector by hand
Definition: vector.h:154
A dim dimensional vector.
Definition: const.h:55
Ball()
construct an uninitialized ball
Definition: ball.h:64
Point()
Construct an uninitialized point.
Definition: point.h:100
AtlasOutType toAtlas() const
Create an Atlas object from the point.
Definition: atlasconv.h:187
float CoordType
Basic floating point type.
Definition: const.h:79
AtlasOutType toAtlas() const
Create an Atlas object from the Quaternion.
Definition: atlasconv.h:162
AxisBox()
Construct an uninitialized box.
Definition: axisbox.h:66
void fromAtlas(const AtlasInType &a)
Set the box's value to that given by an Atlas object.
Definition: atlasconv.h:199
AtlasOutType toAtlas() const
Create an Atlas object from the vector.
Definition: atlasconv.h:124
void fromAtlas(const AtlasInType &a)
Set the vector's value to that given by an Atlas object.
Definition: atlasconv.h:117
void fromAtlas(const AtlasInType &a)
Set the Quaternion's value to that given by an Atlas object.
Definition: atlasconv.h:129
AtlasOutType toAtlas() const
Create an Atlas object from the box.
Definition: atlasconv.h:233
A dim dimensional point.
Definition: const.h:50