26 #ifndef WFMATH_STREAM_H
27 #define WFMATH_STREAM_H
29 #include <wfmath/vector.h>
30 #include <wfmath/rotmatrix.h>
31 #include <wfmath/point.h>
32 #include <wfmath/axisbox.h>
33 #include <wfmath/ball.h>
34 #include <wfmath/segment.h>
35 #include <wfmath/rotbox.h>
36 #include <wfmath/polygon.h>
37 #include <wfmath/error.h>
48 namespace _IOWrapper {
54 virtual ~BaseRead() {}
56 virtual void read(std::istream& is) = 0;
61 virtual ~BaseWrite() {}
63 virtual void write(std::ostream& os)
const = 0;
67 class ImplRead :
public BaseRead {
69 ImplRead(C& c) : m_data(c) {}
70 virtual ~ImplRead() {}
72 virtual void read(std::istream& is) {is >> m_data;}
79 class ImplWrite :
public BaseWrite {
81 ImplWrite(
const C& c) : m_data(c) {}
82 virtual ~ImplWrite() {}
84 virtual void write(std::ostream& os)
const {os << m_data;}
90 std::string ToStringImpl(
const BaseWrite& b,
int precision);
91 void FromStringImpl(BaseRead& b,
const std::string& s,
int precision);
99 inline std::string
ToString(
const C& c,
unsigned int precision = 6)
101 return _IOWrapper::ToStringImpl(_IOWrapper::ImplWrite<C>(c), 6);
109 inline void FromString(C& c,
const std::string& s,
unsigned int precision = 6)
111 _IOWrapper::ImplRead<C> i(c);
112 _IOWrapper::FromStringImpl(i, s, 6);
115 void _ReadCoordList(std::istream& is,
CoordType* d,
const int num);
116 void _WriteCoordList(std::ostream& os,
const CoordType* d,
const int num);
120 inline std::ostream& operator<<(std::ostream& os, const Vector<dim>& v)
122 _WriteCoordList(os, v.m_elem, dim);
127 inline std::istream& operator>>(std::istream& is, Vector<dim>& v)
129 _ReadCoordList(is, v.m_elem, dim);
135 inline std::ostream& operator<<(std::ostream& os, const RotMatrix<dim>& m)
139 for(
int i = 0; i < dim; ++i) {
140 _WriteCoordList(os, m.m_elem[i], dim);
141 os << (i < (dim - 1) ?
',' :
')');
148 inline std::istream& operator>>(std::istream& is, RotMatrix<dim>& m)
157 for(
int i = 0; i < dim; ++i) {
158 _ReadCoordList(is, d + i * dim, dim);
160 char want = (i == dim - 1) ?
')' :
',';
165 if(!m._setVals(d, FloatMax(WFMATH_EPSILON, _GetEpsilon(is))))
172 inline std::ostream& operator<<(std::ostream& os, const Point<dim>& p)
174 _WriteCoordList(os, p.m_elem, dim);
179 inline std::istream& operator>>(std::istream& is, Point<dim>& p)
181 _ReadCoordList(is, p.m_elem, dim);
187 inline std::ostream& operator<<(std::ostream& os, const AxisBox<dim>& a)
189 return os <<
"AxisBox: m_low = " << a.m_low <<
", m_high = " << a.m_high;
193 inline std::istream& operator>>(std::istream& is, AxisBox<dim>& a)
199 }
while(next !=
'=');
205 }
while(next !=
'=');
213 inline std::ostream& operator<<(std::ostream& os, const Ball<dim>& b)
215 return os <<
"Ball: m_center = " << b.m_center <<
216 +
", m_radius = " << b.m_radius;
220 inline std::istream& operator>>(std::istream& is, Ball<dim>& b)
226 }
while(next !=
'=');
232 }
while(next !=
'=');
240 inline std::ostream& operator<<(std::ostream& os, const Segment<dim>& s)
242 return os <<
"Segment: m_p1 = " << s.m_p1 <<
", m_p2 = " << s.m_p2;
246 inline std::istream& operator>>(std::istream& is, Segment<dim>& s)
252 }
while(next !=
'=');
258 }
while(next !=
'=');
266 inline std::ostream& operator<<(std::ostream& os, const RotBox<dim>& r)
268 return os <<
"RotBox: m_corner0 = " << r.m_corner0
269 <<
", m_size = " << r.m_size
270 <<
", m_orient = " << r.m_orient;
274 inline std::istream& operator>>(std::istream& is, RotBox<dim>& r)
280 }
while(next !=
'=');
286 }
while(next !=
'=');
292 }
while(next !=
'=');
299 template<> std::ostream& operator<<(std::ostream& os, const Polygon<2>& r);
300 template<> std::istream& operator>>(std::istream& is, Polygon<2>& r);
303 inline std::ostream& operator<<(std::ostream& os, const Polygon<dim>& r)
305 int size = r.m_poly.numCorners();
314 for(
int i = 0; i < size; ++i)
315 os << r.getCorner(i) << (i < (dim - 1) ?
',' :
')');
322 template<
int dim>
struct _PolyReader
329 std::istream& operator>>(std::istream& is, Polygon<dim>& r)
332 _PolyReader<dim> read;
333 std::list<_PolyReader<dim> > read_list;
342 }
while(next !=
'>');
345 }
while(next !=
'(');
349 read_list.push_back(read);
362 typename std::list<_PolyReader<dim> >::iterator i, end = read_list.end();
365 int str_prec = is.precision();
367 while(--str_prec > 0)
369 double epsilon = DoubleMax(str_eps, WFMATH_EPSILON);
371 r.m_orient = _Poly2Orient<dim>();
373 if(read_list.size() < 3) {
374 for(i = read_list.begin(); i != end; ++i) {
375 succ = r.m_orient.expand(i->pd, i->p2, epsilon);
380 typename std::list<_PolyReader<dim> >::iterator p1 = end, p2 = end, p3 = end, j;
383 for(i = read_list.begin(); i != end; ++i) {
384 for(j = i, ++j; j != end; ++j) {
385 CoordType new_dist = SloppyDistance(i->pd, j->pd);
386 if(new_dist > dist) {
399 for(i = read_list.begin(); i != end; ++i) {
401 if(i == p1 || i == p2)
403 CoordType new_dist = FloatMin(SloppyDistance(i->pd, p1->pd),
404 SloppyDistance(i->pd, p2->pd));
405 if(new_dist > dist) {
415 succ = r.m_orient.expand(p1->pd, p1->p2, epsilon);
417 succ = r.m_orient.expand(p2->pd, p2->p2, epsilon);
419 succ = r.m_orient.expand(p3->pd, p3->p2, epsilon);
424 for(i = read_list.begin(); i != end; ++i) {
425 if(i == p1 || i == p2 || i == p3)
427 succ = r.m_orient.expand(i->pd, i->p2, epsilon);
437 r.m_poly.resize(read_list.size());
440 for(i = read_list.begin(), pnum = 0; i != end; ++i, ++pnum)
441 r.m_poly[pnum] = i->p2;
448 #endif // WFMATH_STREAM_H
void FromString(C &c, const std::string &s, unsigned int precision=6)
Parse a WFMath type from a string.
Definition: stream.h:109
float CoordType
Basic floating point type.
Definition: const.h:79
std::string ToString(const C &c, unsigned int precision=6)
Output a WFMath type as a string.
Definition: stream.h:99