/usr/include/wfmath-1.0/wfmath/stream.h is in libwfmath-1.0-dev 1.0.2+dfsg1-0.4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | // stream.h (Functions in the WFMath library that use streams)
//
// The WorldForge Project
// Copyright (C) 2001,2002 The WorldForge Project
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// For information about WorldForge and its authors, please contact
// the Worldforge Web Site at http://www.worldforge.org.
// Author: Ron Steinke
// Created: 2001-12-7
#ifndef WFMATH_STREAM_H
#define WFMATH_STREAM_H
#include <wfmath/vector.h>
#include <wfmath/rotmatrix.h>
#include <wfmath/point.h>
#include <wfmath/axisbox.h>
#include <wfmath/ball.h>
#include <wfmath/segment.h>
#include <wfmath/rotbox.h>
#include <wfmath/polygon.h>
#include <wfmath/line.h>
#include <wfmath/error.h>
#include <string>
#include <iostream>
#include <list> // For Polygon<>::operator>>()
#include <cassert>
namespace WFMath {
// sstream vs. strstream compatibility wrapper
namespace _IOWrapper {
// Need separate read/write classes, since one is const C& and the other is C&
class BaseRead {
public:
virtual ~BaseRead() {}
virtual void read(std::istream& is) = 0;
};
class BaseWrite {
public:
virtual ~BaseWrite() {}
virtual void write(std::ostream& os) const = 0;
};
template<class C>
class ImplRead : public BaseRead {
public:
ImplRead(C& c) : m_data(c) {}
virtual ~ImplRead() {}
virtual void read(std::istream& is) {is >> m_data;}
private:
C &m_data;
};
template<class C>
class ImplWrite : public BaseWrite {
public:
ImplWrite(const C& c) : m_data(c) {}
virtual ~ImplWrite() {}
virtual void write(std::ostream& os) const {os << m_data;}
private:
const C &m_data;
};
std::string ToStringImpl(const BaseWrite& b, std::streamsize precision);
void FromStringImpl(BaseRead& b, const std::string& s, std::streamsize precision);
}
/// Output a WFMath type as a string
/**
* This uses operator<<() in its backend.
**/
template<class C>
inline std::string ToString(const C& c, std::streamsize precision = 6)
{
return _IOWrapper::ToStringImpl(_IOWrapper::ImplWrite<C>(c), precision);
}
/// Parse a WFMath type from a string
/**
* This uses operator>>() in its backend.
**/
template<class C>
inline void FromString(C& c, const std::string& s, std::streamsize = 6)
{
_IOWrapper::ImplRead<C> i(c);
_IOWrapper::FromStringImpl(i, s, 6);
}
void _ReadCoordList(std::istream& is, CoordType* d, const int num);
void _WriteCoordList(std::ostream& os, const CoordType* d, const int num);
CoordType _GetEpsilon(std::istream& is);
template<int dim>
inline std::ostream& operator<<(std::ostream& os, const Vector<dim>& v)
{
_WriteCoordList(os, v.m_elem, dim);
return os;
}
template<int dim>
inline std::istream& operator>>(std::istream& is, Vector<dim>& v)
{
_ReadCoordList(is, v.m_elem, dim);
v.m_valid = true;
return is;
}
template<int dim>
inline std::ostream& operator<<(std::ostream& os, const RotMatrix<dim>& m)
{
os << '(';
for(int i = 0; i < dim; ++i) {
_WriteCoordList(os, m.m_elem[i], dim);
os << (i < (dim - 1) ? ',' : ')');
}
return os;
}
template<int dim>
inline std::istream& operator>>(std::istream& is, RotMatrix<dim>& m)
{
CoordType d[dim*dim];
char next;
is >> next;
if(next != '(')
throw ParseError();
for(int i = 0; i < dim; ++i) {
_ReadCoordList(is, d + i * dim, dim);
is >> next;
char want = (i == dim - 1) ? ')' : ',';
if(next != want)
throw ParseError();
}
if(!m._setVals(d, FloatMax(numeric_constants<CoordType>::epsilon(), _GetEpsilon(is))))
throw ParseError();
return is;
}
template<int dim>
inline std::ostream& operator<<(std::ostream& os, const Point<dim>& p)
{
_WriteCoordList(os, p.m_elem, dim);
return os;
}
template<int dim>
inline std::istream& operator>>(std::istream& is, Point<dim>& p)
{
_ReadCoordList(is, p.m_elem, dim);
p.m_valid = true;
return is;
}
template<int dim>
inline std::ostream& operator<<(std::ostream& os, const AxisBox<dim>& a)
{
return os << "AxisBox: m_low = " << a.m_low << ", m_high = " << a.m_high;
}
template<int dim>
inline std::istream& operator>>(std::istream& is, AxisBox<dim>& a)
{
char next;
do {
is >> next;
} while(next != '=');
is >> a.m_low;
do {
is >> next;
} while(next != '=');
is >> a.m_high;
return is;
}
template<int dim>
inline std::ostream& operator<<(std::ostream& os, const Ball<dim>& b)
{
return os << "Ball: m_center = " << b.m_center <<
+ ", m_radius = " << b.m_radius;
}
template<int dim>
inline std::istream& operator>>(std::istream& is, Ball<dim>& b)
{
char next;
do {
is >> next;
} while(next != '=');
is >> b.m_center;
do {
is >> next;
} while(next != '=');
is >> b.m_radius;
return is;
}
template<int dim>
inline std::ostream& operator<<(std::ostream& os, const Segment<dim>& s)
{
return os << "Segment: m_p1 = " << s.m_p1 << ", m_p2 = " << s.m_p2;
}
template<int dim>
inline std::istream& operator>>(std::istream& is, Segment<dim>& s)
{
char next;
do {
is >> next;
} while(next != '=');
is >> s.m_p1;
do {
is >> next;
} while(next != '=');
is >> s.m_p2;
return is;
}
template<int dim>
inline std::ostream& operator<<(std::ostream& os, const RotBox<dim>& r)
{
return os << "RotBox: m_corner0 = " << r.m_corner0
<< ", m_size = " << r.m_size
<< ", m_orient = " << r.m_orient;
}
template<int dim>
inline std::istream& operator>>(std::istream& is, RotBox<dim>& r)
{
char next;
do {
is >> next;
} while(next != '=');
is >> r.m_corner0;
do {
is >> next;
} while(next != '=');
is >> r.m_size;
do {
is >> next;
} while(next != '=');
is >> r.m_orient;
return is;
}
template<> std::ostream& operator<<(std::ostream& os, const Polygon<2>& r);
template<> std::istream& operator>>(std::istream& is, Polygon<2>& r);
template<int dim>
inline std::ostream& operator<<(std::ostream& os, const Polygon<dim>& r)
{
size_t size = r.m_poly.numCorners();
if(size == 0) {
os << "<empty>";
return os;
}
os << "Polygon: (";
for(size_t i = 0; i < size; ++i)
os << r.getCorner(i) << (i < (dim - 1) ? ',' : ')');
return os;
}
// Can't stick this in operator>>(std::istream&, Polygon<>&), because
// we use it as a template argument for list<>. Why isn't that allowed?
template<int dim> struct _PolyReader
{
Point<dim> pd;
Point<2> p2;
};
template<int dim>
std::istream& operator>>(std::istream& is, Polygon<dim>& r)
{
char next;
_PolyReader<dim> read;
std::list<_PolyReader<dim> > read_list;
// Read in the points
do {
is >> next;
if(next == '<') { // empty polygon
do {
is >> next;
} while(next != '>');
return is;
}
} while(next != '(');
while(true) {
is >> read.pd;
read_list.push_back(read);
is >> next;
if(next == ')')
break;
if(next != ',')
throw ParseError();
}
// Convert to internal format. Be careful about the order points are
// added to the orientation. If the first few points are too close together,
// round off error can skew the plane, and later points that are further
// away may fail.
typename std::list<_PolyReader<dim> >::iterator i, end = read_list.end();
bool succ;
std::streamsize str_prec = is.precision();
float str_eps = 1;
while(--str_prec > 0) // Precision of 6 gives epsilon = 1e-5
str_eps /= 10;
CoordType epsilon = FloatMax(str_eps, numeric_constants<CoordType>::epsilon());
r.m_orient = _Poly2Orient<dim>();
if(read_list.size() < 3) { // This will always work
for(i = read_list.begin(); i != end; ++i) {
succ = r.m_orient.expand(i->pd, i->p2, epsilon);
assert(succ);
}
}
else { // Find the three furthest apart points
typename std::list<_PolyReader<dim> >::iterator p1 = end, p2 = end, p3 = end, j; // invalid values
CoordType dist = -1;
for(i = read_list.begin(); i != end; ++i) {
for(j = i, ++j; j != end; ++j) {
CoordType new_dist = SloppyDistance(i->pd, j->pd);
if(new_dist > dist) {
p1 = i;
p2 = j;
dist = new_dist;
}
}
}
assert(p1 != end);
assert(p2 != end);
dist = -1;
for(i = read_list.begin(); i != end; ++i) {
// Don't want to be near either p1 or p2
if(i == p1 || i == p2)
continue;
CoordType new_dist = FloatMin(SloppyDistance(i->pd, p1->pd),
SloppyDistance(i->pd, p2->pd));
if(new_dist > dist) {
p3 = i;
dist = new_dist;
}
}
assert(p3 != end);
// Add p1, p2, p3 first
succ = r.m_orient.expand(p1->pd, p1->p2, epsilon);
assert(succ);
succ = r.m_orient.expand(p2->pd, p2->p2, epsilon);
assert(succ);
succ = r.m_orient.expand(p3->pd, p3->p2, epsilon);
assert(succ);
// Try to add the rest
for(i = read_list.begin(); i != end; ++i) {
if(i == p1 || i == p2 || i == p3) // Did these already
continue;
succ = r.m_orient.expand(i->pd, i->p2, epsilon);
if(!succ) {
r.clear();
throw ParseError();
}
}
}
// Got valid points, add them to m_poly
r.m_poly.resize(read_list.size());
int pnum;
for(i = read_list.begin(), pnum = 0; i != end; ++i, ++pnum)
r.m_poly[pnum] = i->p2;
return is;
}
template<int dim>
inline std::ostream& operator<<(std::ostream& os, const Line<dim>& r)
{
size_t size = r.numCorners();
if(size == 0) {
os << "<empty>";
return os;
}
os << "Line: (";
for(size_t i = 0; i < size; ++i)
os << r.getCorner(i) << (i < (dim - 1) ? ',' : ')');
return os;
}
} // namespace WFMath
#endif // WFMATH_STREAM_H
|