wfmath  1.0.3
A math library for the Worldforge system.
polygon.cpp
1 // polygon.cpp (Polygon<> implementation)
2 //
3 // The WorldForge Project
4 // Copyright (C) 2002 The WorldForge Project
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // For information about WorldForge and its authors, please contact
21 // the Worldforge Web Site at http://www.worldforge.org.
22 
23 // Author: Ron Steinke
24 // Created: 2002-1-4
25 
26 #include "polygon_funcs.h"
27 #include "rotbox.h"
28 
29 namespace WFMath {
30 
31 void _Poly2Reorient::reorient(Polygon<2>& poly, size_t skip) const
32 {
33  size_t end = poly.numCorners();
34 
35  switch(m_type) {
36  case _WFMATH_POLY2REORIENT_NONE:
37  return;
38  case _WFMATH_POLY2REORIENT_CLEAR_AXIS2:
39  for(size_t i = 0; i != end; ++i) {
40  if(i == skip)
41  continue;
42  (poly[i])[1] = 0;
43  }
44  return;
45  case _WFMATH_POLY2REORIENT_CLEAR_BOTH_AXES:
46  for(size_t i = 0; i != end; ++i) {
47  if(i == skip)
48  continue;
49  (poly[i])[0] = 0;
50  (poly[i])[1] = 0;
51  }
52  return;
53  case _WFMATH_POLY2REORIENT_MOVE_AXIS2_TO_AXIS1:
54  for(size_t i = 0; i != end; ++i) {
55  if(i == skip)
56  continue;
57  (poly[i])[0] = (poly[i])[1];
58  (poly[i])[1] = 0;
59  }
60  return;
61  case _WFMATH_POLY2REORIENT_SCALE1_CLEAR2:
62  for(size_t i = 0; i != end; ++i) {
63  if(i == skip)
64  continue;
65  (poly[i])[0] *= m_scale;
66  (poly[i])[1] = 0;
67  }
68  return;
69  default:
70  assert(false);
71  return;
72  }
73 }
74 
75 //template<>
76 bool Polygon<2>::isEqualTo(const Polygon<2>& p, CoordType epsilon) const
77 {
78  if(m_points.size() != p.m_points.size())
79  return false;
80 
81  Polygon<2>::theConstIter i1 = m_points.begin(), i2 = p.m_points.begin(),
82  end = m_points.end();
83 
84  while(i1 != end) {
85  if(!Equal(*i1, *i2, epsilon))
86  return false;
87  ++i1;
88  ++i2;
89  }
90 
91  return true;
92 }
93 
94 bool Polygon<2>::isValid() const
95 {
96  for(theConstIter i = m_points.begin(); i != m_points.end(); ++i)
97  if(!i->isValid())
98  return false;
99 
100  return true;
101 }
102 
103 //template<>
104 Polygon<2>& Polygon<2>::shift(const Vector<2>& v)
105 {
106  for(theIter i = m_points.begin(); i != m_points.end(); ++i)
107  *i += v;
108 
109  return *this;
110 }
111 
112 //template<>
113 Polygon<2>& Polygon<2>::rotatePoint(const RotMatrix<2>& m, const Point<2>& p)
114 {
115  for(theIter i = m_points.begin(); i != m_points.end(); ++i)
116  i->rotate(m, p);
117 
118  return *this;
119 }
120 
121 //template<>
122 Polygon<2> Polygon<2>::toParentCoords(const Point<2>& origin,
123  const RotMatrix<2>& rotation) const
124 {
125  Polygon out;
126  out.m_points.resize(m_points.size());
127  for(unsigned i = 0; i < m_points.size(); ++i)
128  out.m_points[i] = m_points[i].toParentCoords(origin, rotation);
129  return out;
130 }
131 
132 //template<>
133 Polygon<2> Polygon<2>::toParentCoords(const AxisBox<2>& coords) const
134 {
135  Polygon out;
136  out.m_points.resize(m_points.size());
137  for(unsigned i = 0; i < m_points.size(); ++i)
138  out.m_points[i] = m_points[i].toParentCoords(coords);
139  return out;
140 }
141 
142 //template<>
143 Polygon<2> Polygon<2>::toParentCoords(const RotBox<2>& coords) const
144 {
145  Polygon out;
146  out.m_points.resize(m_points.size());
147  for(unsigned i = 0; i < m_points.size(); ++i)
148  out.m_points[i] = m_points[i].toParentCoords(coords);
149  return out;
150 }
151 
152 //template<>
153 Polygon<2> Polygon<2>::toLocalCoords(const Point<2>& origin,
154  const RotMatrix<2>& rotation) const
155 {
156  Polygon out;
157  out.m_points.resize(m_points.size());
158  for(unsigned i = 0; i < m_points.size(); ++i)
159  out.m_points[i] = m_points[i].toLocalCoords(origin, rotation);
160  return out;
161 }
162 
163 //template<>
164 Polygon<2> Polygon<2>::toLocalCoords(const AxisBox<2>& coords) const
165 {
166  Polygon out;
167  out.m_points.resize(m_points.size());
168  for(unsigned i = 0; i < m_points.size(); ++i)
169  out.m_points[i] = m_points[i].toLocalCoords(coords);
170  return out;
171 }
172 
173 //template<>
174 Polygon<2> Polygon<2>::toLocalCoords(const RotBox<2>& coords) const
175 {
176  Polygon out;
177  out.m_points.resize(m_points.size());
178  for(unsigned i = 0; i < m_points.size(); ++i)
179  out.m_points[i] = m_points[i].toLocalCoords(coords);
180  return out;
181 }
182 
183 template class Polygon<3>;
184 template class _Poly2Orient<3>;
185 
186 }
WFMath
Generic library namespace.
Definition: shape.h:41
WFMath::CoordType
double CoordType
Basic floating point type.
Definition: const.h:140