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>
41 #include <wfmath/line.h>
47 #ifndef ATLAS_MESSAGE_ELEMENT_H
48 #error "You must include Atlas/Message/Element.h before wfmath/atlasconv.h"
51 typedef Atlas::Message::WrongTypeException _AtlasBadParse;
56 AtlasInType(
const Atlas::Message::Element& val) : m_val(val) {}
58 template<
class C> AtlasInType(C c) : m_obj(c), m_val(m_obj) {}
59 operator const Atlas::Message::Element&()
const {
return m_val;}
60 bool IsList()
const {
return m_val.isList();}
61 const Atlas::Message::ListType& AsList()
const {
return m_val.asList();}
63 Atlas::Message::Element m_obj;
64 const Atlas::Message::Element& m_val;
70 AtlasOutType(
const Atlas::Message::ListType& l) : m_val(l) {}
71 AtlasOutType(
const Atlas::Message::MapType& l) : m_val(l) {}
72 operator Atlas::Message::Element&() {
return m_val;}
73 operator const Atlas::Message::Element&()
const {
return m_val;}
75 Atlas::Message::Element m_val;
78 inline AtlasOutType _ArrayToAtlas(
const CoordType* array,
unsigned len)
80 Atlas::Message::ListType a(len);
82 for(
unsigned i = 0; i < len; ++i)
88 inline void _ArrayFromAtlas(
CoordType* array,
unsigned len,
const AtlasInType& a)
91 throw _AtlasBadParse();
93 const Atlas::Message::ListType& list(a.AsList());
95 if(list.size() != (
unsigned int) len)
96 throw _AtlasBadParse();
98 for(
unsigned i = 0; i < len; ++i)
99 array[i] = list[i].asNum();
111 _ArrayFromAtlas(m_elem, dim, a);
118 return _ArrayToAtlas(m_elem, dim);
124 throw _AtlasBadParse();
127 const Atlas::Message::ListType& list(a.AsList());
130 throw _AtlasBadParse();
133 for(
int i = 0; i < 3; ++i)
134 m_vec[i] = list[i].asNum();
136 m_w = list[3].asNum();
140 if (norm <= numeric_constants<CoordType>::epsilon()) {
156 Atlas::Message::ListType a(4);
158 for(
int i = 0; i < 3; ++i)
174 _ArrayFromAtlas(m_elem, dim, a);
181 return _ArrayToAtlas(m_elem, dim);
194 throw _AtlasBadParse();
196 const Atlas::Message::ListType& list(a.AsList());
198 switch(list.size()) {
204 for(
int i = 0; i < dim; ++i) {
205 m_low[i] = list[i].asNum();
206 m_high[i] = list[i+dim].asNum();
212 throw _AtlasBadParse();
215 for(
int i = 0; i < dim; ++i) {
216 if(m_low[i] > m_high[i]) {
218 m_low[i] = m_high[i];
229 for(i = 0; i < dim; ++i)
234 return m_high.toAtlas();
238 Atlas::Message::ListType a(2*dim);
239 for(i = 0; i < dim; ++i) {
241 a[dim+i] = m_high[i];
250 const Atlas::Message::Element& message(a);
251 if (message.isMap()) {
252 const Atlas::Message::MapType& shapeElement(message.asMap());
254 Atlas::Message::MapType::const_iterator shape_I = shapeElement.find(
"radius");
255 if (shape_I != shapeElement.end()) {
256 const Atlas::Message::Element& shapeRadiusElem(shape_I->second);
257 if (shapeRadiusElem.isNum()) {
258 m_radius = shapeRadiusElem.asNum();
261 Atlas::Message::MapType::const_iterator pos_I = shapeElement.find(
"position");
262 if (pos_I != shapeElement.end()) {
263 const Atlas::Message::Element& posElem(pos_I->second);
264 if (posElem.isList()) {
265 m_center.fromAtlas(posElem);
274 Atlas::Message::MapType map;
275 map.insert(Atlas::Message::MapType::value_type(
"radius", m_radius));
276 map.insert(Atlas::Message::MapType::value_type(
"position", m_center.toAtlas()));
287 inline bool _ListNumCheck(
const Atlas::Message::ListType & list,
int dim)
289 for(
int i = 0; i < dim; ++i) {
290 if (!list[i].isNum()) {
297 template<
template <
int>
class ShapeT>
298 inline void _AddCorner(ShapeT<3> & shape,
299 const Atlas::Message::ListType & point)
301 Point<3> wpt(point[0].asNum(), point[1].asNum(), point[2].asNum());
302 shape.addCorner(shape.numCorners(), wpt);
305 template<
template <
int>
class ShapeT>
306 inline void _AddCorner(ShapeT<2> & shape,
307 const Atlas::Message::ListType & point)
309 Point<2> wpt(point[0].asNum(), point[1].asNum());
310 shape.addCorner(shape.numCorners(), wpt);
313 template<
template <
int>
class ShapeT,
int dim>
314 inline void _CornersFromAtlas(ShapeT<dim> & shape,
315 const Atlas::Message::Element& message)
317 if (message.isList()) {
318 const Atlas::Message::ListType& pointsData(message.asList());
320 for (
size_t p = 0; p < pointsData.size(); ++p) {
321 if (!pointsData[p].isList()) {
325 const Atlas::Message::ListType& point(pointsData[p].asList());
326 if ((point.size() < dim) || !_ListNumCheck(point, dim)) {
330 _AddCorner(shape, point);
337 const Atlas::Message::Element& message(a);
338 if (message.isMap()) {
339 const Atlas::Message::MapType& shapeElement(message.asMap());
340 Atlas::Message::MapType::const_iterator it = shapeElement.find(
"points");
341 if ((it != shapeElement.end()) && it->second.isList()) {
342 _CornersFromAtlas(*
this, it->second);
343 if (numCorners() > 2) {
347 }
else if (message.isList()) {
348 _CornersFromAtlas(*
this, message);
349 if (numCorners() > 2) {
353 throw _AtlasBadParse();
358 Atlas::Message::ListType points;
359 for (theConstIter I = m_points.begin(); I != m_points.end(); ++I)
361 points.push_back(I->toAtlas());
363 Atlas::Message::MapType map;
364 map.insert(Atlas::Message::MapType::value_type(
"points", points));
371 const Atlas::Message::Element& message(a);
372 if (message.isMap()) {
373 const Atlas::Message::MapType& shapeElement(message.asMap());
374 Atlas::Message::MapType::const_iterator it = shapeElement.find(
"points");
375 if ((it != shapeElement.end()) && it->second.isList()) {
376 _CornersFromAtlas(*
this, it->second);
377 if (numCorners() > 0) {
381 }
else if (message.isList()) {
382 _CornersFromAtlas(*
this, message);
383 if (numCorners() > 0) {
387 throw _AtlasBadParse();
393 Atlas::Message::ListType points;
394 for (const_iterator I = m_points.begin(); I != m_points.end(); ++I)
396 points.push_back(I->toAtlas());
398 Atlas::Message::MapType map;
399 map.insert(Atlas::Message::MapType::value_type(
"points", points));
411 const Atlas::Message::Element& message(a);
412 if (message.isMap()) {
413 const Atlas::Message::MapType& shapeElement(message.asMap());
415 Atlas::Message::MapType::const_iterator shape_I = shapeElement.find(
"point");
416 if (shape_I != shapeElement.end()) {
417 const Atlas::Message::Element& shapePointElem(shape_I->second);
421 shape_I = shapeElement.find(
"size");
422 if (shape_I != shapeElement.end()) {
423 const Atlas::Message::Element& shapeVectorElem(shape_I->second);
426 m_corner0 = shapePoint;
427 m_size = shapeVector;
433 throw _AtlasBadParse();
439 Atlas::Message::MapType map;
440 map.insert(Atlas::Message::MapType::value_type(
"point", m_corner0.toAtlas()));
441 map.insert(Atlas::Message::MapType::value_type(
"size", m_size.toAtlas()));
453 #endif // WFMATH_ATLAS_CONV_H