/usr/include/wfmath-0.3/wfmath/polygon_funcs.h is in libwfmath-0.3-dev 0.3.12-3ubuntu2.
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 | // polygon_funcs.h (line polygon implementation)
//
// The WorldForge Project
// Copyright (C) 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
#ifndef WFMATH_POLYGON_FUNCS_H
#define WFMATH_POLYGON_FUNCS_H
#include <wfmath/polygon.h>
#include <wfmath/vector.h>
#include <wfmath/point.h>
#include <wfmath/ball.h>
#include <cmath>
#include <cassert>
#include <limits>
namespace WFMath {
template<int dim>
inline _Poly2Orient<dim>& _Poly2Orient<dim>::operator=(const _Poly2Orient<dim>& a)
{
m_origin = a.m_origin;
for(int i = 0; i < 2; ++i)
m_axes[i] = a.m_axes[i];
return *this;
}
template<int dim>
inline bool Polygon<dim>::isEqualTo(const Polygon<dim>& p, double epsilon) const
{
// The same polygon can be expressed in different ways in the interal
// format, so we have to call getCorner();
int size = m_poly.numCorners();
if(size != p.m_poly.numCorners())
return false;
for(int i = 0; i < size; ++i)
if(!Equal(getCorner(i), p.getCorner(i), epsilon))
return false;
return true;
}
template<int dim>
inline Point<dim> _Poly2Orient<dim>::convert(const Point<2>& p) const
{
assert(m_origin.isValid());
Point<dim> out = m_origin;
for(int j = 0; j < 2; ++j) {
if(m_axes[j].isValid())
out += m_axes[j] * p[j];
else
assert(p[j] == 0);
}
out.setValid(p.isValid());
return out;
}
template<int dim>
bool _Poly2Orient<dim>::expand(const Point<dim>& pd, Point<2>& p2, double epsilon)
{
p2[0] = p2[1] = 0; // initialize
p2.setValid();
if(!m_origin.isValid()) { // Adding to an empty list
m_origin = pd;
m_origin.setValid();
return true;
}
Vector<dim> shift = pd - m_origin, start_shift = shift;
CoordType bound = (CoordType)(shift.sqrMag() * epsilon);
int j = 0;
while(true) {
if(Dot(shift, start_shift) <= bound) // shift is effectively zero
return true;
if(j == 2) { // Have two axes, shift doesn't lie in their plane
p2.setValid(false);
return false;
}
if(!m_axes[j].isValid()) { // Didn't span this previously, now we do
p2[j] = shift.mag();
m_axes[j] = shift / p2[j];
m_axes[j].setValid();
return true;
}
p2[j] = Dot(shift, m_axes[j]);
shift -= m_axes[j] * p2[j]; // shift is now perpendicular to m_axes[j]
++j;
}
}
template<int dim>
_Poly2Reorient _Poly2Orient<dim>::reduce(const Polygon<2>& poly, int skip)
{
if(poly.numCorners() <= ((skip == 0) ? 1 : 0)) { // No corners left
m_origin.setValid(false);
m_axes[0].setValid(false);
m_axes[1].setValid(false);
return _WFMATH_POLY2REORIENT_NONE;
}
assert(m_origin.isValid());
if(!m_axes[0].isValid())
return _WFMATH_POLY2REORIENT_NONE;
// Check that we still span both axes
bool still_valid[2] = {false,}, got_ratio = false;
CoordType ratio = std::numeric_limits<CoordType>::max();
CoordType size = std::numeric_limits<CoordType>::max();
double epsilon;
int i, end = poly.numCorners();
// scale epsilon
for(i = 0; i < end; ++i) {
if(i == skip)
continue;
const Point<2> &p = poly[i];
CoordType x = std::fabs(p[0]),
y = std::fabs(p[1]),
max = (x > y) ? x : y;
if(i == 0 || max < size)
size = max;
}
int exponent;
(void) frexp(size, &exponent);
epsilon = ldexp(WFMATH_EPSILON, exponent);
i = 0;
if(skip == 0)
++i;
assert(i != end);
Point<2> first_point = poly[i];
first_point.setValid(); // in case someone stuck invalid points in the poly
while(++i != end) {
if(i == skip)
continue;
Vector<2> diff = poly[i] - first_point;
if(diff.sqrMag() < epsilon * epsilon) // No addition to span
continue;
if(!m_axes[1].isValid()) // We span 1D
return _WFMATH_POLY2REORIENT_NONE;
for(int j = 0; j < 2; ++j) {
if(fabs(diff[j]) < epsilon) {
assert(diff[j ? 0 : 1] >= epsilon); // because diff != 0
if(still_valid[j ? 0 : 1] || got_ratio) // We span a 2D space
return _WFMATH_POLY2REORIENT_NONE;
still_valid[j] = true;
}
}
// The point has both elements nonzero
if(still_valid[0] || still_valid[1]) // We span a 2D space
return _WFMATH_POLY2REORIENT_NONE;
CoordType new_ratio = diff[1] / diff[0];
if(!got_ratio) {
ratio = new_ratio;
got_ratio = true;
continue;
}
if(!Equal(ratio, new_ratio)) // We span a 2D space
return _WFMATH_POLY2REORIENT_NONE;
}
// Okay, we don't span both vectors. What now?
if(still_valid[0]) {
assert(m_axes[1].isValid());
assert(!still_valid[1]);
assert(!got_ratio);
// This is easy, m_axes[1] is just invalid
m_origin += m_axes[1] * first_point[1];
m_axes[1].setValid(false);
return _WFMATH_POLY2REORIENT_CLEAR_AXIS2;
}
if(still_valid[1]) {
assert(m_axes[1].isValid());
assert(!got_ratio);
// This is a little harder, m_axes[0] is invalid, must swap axes
m_origin += m_axes[0] * first_point[0];
m_axes[0] = m_axes[1];
m_axes[1].setValid(false);
return _WFMATH_POLY2REORIENT_MOVE_AXIS2_TO_AXIS1;
}
// The !m_axes[1].isValid() case reducing to a point falls into here
if(!got_ratio) { // Nothing's valid, all points are equal
m_origin += m_axes[0] * first_point[0];
if(m_axes[1].isValid())
m_origin += m_axes[1] * first_point[1];
m_axes[0].setValid(false);
m_axes[1].setValid(false);
return _WFMATH_POLY2REORIENT_CLEAR_BOTH_AXES;
}
assert(m_axes[1].isValid());
// All the points are colinear, along some line which is not parallel
// to either of the original axes
Vector<dim> new0;
new0 = m_axes[0] + m_axes[1] * ratio;
CoordType norm = new0.mag();
new0 /= norm;
// Vector diff = m_axes[0] * first_point[0] + m_axes[1] * first_point[1];
// // Causes Dot(diff, m_axes[0]) to vanish, so the point on the line
// // with x coordinate zero is the new origin
// diff -= new0 * (first_point[0] * norm);
// m_origin += diff;
// or, equivalently
m_origin += m_axes[1] * (first_point[1] - ratio * first_point[0]);
m_axes[0] = new0;
m_axes[1].setValid(false);
return _Poly2Reorient(_WFMATH_POLY2REORIENT_SCALE1_CLEAR2, norm);
}
template<int dim>
inline void _Poly2Orient<dim>::rotate(const RotMatrix<dim>& m, const Point<dim>& p)
{
m_origin.rotate(m, p);
for(int j = 0; j < 2; ++j)
m_axes[j] = Prod(m_axes[j], m);
}
template<int dim>
void _Poly2Orient<dim>::rotate2(const RotMatrix<dim>& m, const Point<2>& p)
{
assert(m_origin.isValid());
if(!m_axes[0].isValid()) {
assert(p[0] == 0 && p[1] == 0);
return;
}
Vector<dim> shift = m_axes[0] * p[0];
m_axes[0] = Prod(m_axes[0], m);
if(m_axes[1].isValid()) {
shift += m_axes[1] * p[1];
m_axes[1] = Prod(m_axes[1], m);
}
else
assert(p[1] == 0);
m_origin += shift - Prod(shift, m);
}
template<>
inline void _Poly2Orient<3>::rotate(const Quaternion& q, const Point<3>& p)
{
m_origin.rotate(q, p);
for(int j = 0; j < 2; ++j)
m_axes[j].rotate(q);
}
template<>
inline void _Poly2Orient<3>::rotate2(const Quaternion& q, const Point<2>& p)
{
assert(m_origin.isValid());
if(!m_axes[0].isValid()) {
assert(p[0] == 0 && p[1] == 0);
return;
}
Vector<3> shift = m_axes[0] * p[0];
m_axes[0].rotate(q);
if(m_axes[1].isValid()) {
shift += m_axes[1] * p[1];
m_axes[1].rotate(q);
}
else
assert(p[1] == 0);
m_origin += shift - shift.rotate(q);
}
template<int dim>
inline bool Polygon<dim>::addCorner(int i, const Point<dim>& p, double epsilon)
{
Point<2> p2;
bool succ = m_orient.expand(p, p2, epsilon);
if(succ)
m_poly.addCorner(i, p2, epsilon);
return succ;
}
template<int dim>
inline void Polygon<dim>::removeCorner(int i)
{
m_poly.removeCorner(i);
_Poly2Reorient r = m_orient.reduce(m_poly);
r.reorient(m_poly);
}
template<int dim>
inline bool Polygon<dim>::moveCorner(int i, const Point<dim>& p, double epsilon)
{
_Poly2Orient<dim> try_orient = m_orient;
_Poly2Reorient r = try_orient.reduce(m_poly, i);
Point<2> p2;
if(!try_orient.expand(p, p2, epsilon))
return false;
r.reorient(m_poly, i);
m_poly[i] = p2;
m_orient = try_orient;
return true;
}
template<int dim>
AxisBox<dim> Polygon<dim>::boundingBox() const
{
assert(m_poly.numCorners() > 0);
Point<dim> min = m_orient.convert(m_poly[0]), max = min;
bool valid = min.isValid();
for(int i = 1; i != m_poly.numCorners(); ++i) {
Point<dim> p = m_orient.convert(m_poly[i]);
valid = valid && p.isValid();
for(int j = 0; j < dim; ++j) {
if(p[j] < min[j])
min[j] = p[j];
if(p[j] > max[j])
max[j] = p[j];
}
}
min.setValid(valid);
max.setValid(valid);
return AxisBox<dim>(min, max, true);
}
template<int dim>
inline Ball<dim> Polygon<dim>::boundingSphere() const
{
Ball<2> b = m_poly.boundingSphere();
return Ball<dim>(m_orient.convert(b.center()), b.radius());
}
template<int dim>
inline Ball<dim> Polygon<dim>::boundingSphereSloppy() const
{
Ball<2> b = m_poly.boundingSphereSloppy();
return Ball<dim>(m_orient.convert(b.center()), b.radius());
}
} // namespace WFMath
#endif // WFMATH_POLYGON_FUNCS_H
|