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
29namespace WFMath {
30
31void 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<>
76bool 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 auto 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
94bool Polygon<2>::isValid() const
95{
96 for(const auto & m_point : m_points) {
97 if (!m_point.isValid()) {
98 return false;
99 }
100 }
101 return true;
102}
103
104//template<>
105Polygon<2>& Polygon<2>::shift(const Vector<2>& v)
106{
107 for(auto & point : m_points)
108 point += v;
109
110 return *this;
111}
112
113//template<>
114Polygon<2>& Polygon<2>::rotatePoint(const RotMatrix<2>& m, const Point<2>& p)
115{
116 for(auto & point : m_points)
117 point.rotate(m, p);
118
119 return *this;
120}
121
122//template<>
123Polygon<2> Polygon<2>::toParentCoords(const Point<2>& origin,
124 const RotMatrix<2>& rotation) const
125{
126 Polygon out;
127 out.m_points.resize(m_points.size());
128 for(unsigned i = 0; i < m_points.size(); ++i)
129 out.m_points[i] = m_points[i].toParentCoords(origin, rotation);
130 return out;
131}
132
133//template<>
134Polygon<2> Polygon<2>::toParentCoords(const AxisBox<2>& coords) const
135{
136 Polygon out;
137 out.m_points.resize(m_points.size());
138 for(unsigned i = 0; i < m_points.size(); ++i)
139 out.m_points[i] = m_points[i].toParentCoords(coords);
140 return out;
141}
142
143//template<>
144Polygon<2> Polygon<2>::toParentCoords(const RotBox<2>& coords) const
145{
146 Polygon out;
147 out.m_points.resize(m_points.size());
148 for(unsigned i = 0; i < m_points.size(); ++i)
149 out.m_points[i] = m_points[i].toParentCoords(coords);
150 return out;
151}
152
153//template<>
154Polygon<2> Polygon<2>::toLocalCoords(const Point<2>& origin,
155 const RotMatrix<2>& rotation) const
156{
157 Polygon out;
158 out.m_points.resize(m_points.size());
159 for(unsigned i = 0; i < m_points.size(); ++i)
160 out.m_points[i] = m_points[i].toLocalCoords(origin, rotation);
161 return out;
162}
163
164//template<>
165Polygon<2> Polygon<2>::toLocalCoords(const AxisBox<2>& coords) const
166{
167 Polygon out;
168 out.m_points.resize(m_points.size());
169 for(unsigned i = 0; i < m_points.size(); ++i)
170 out.m_points[i] = m_points[i].toLocalCoords(coords);
171 return out;
172}
173
174//template<>
175Polygon<2> Polygon<2>::toLocalCoords(const RotBox<2>& coords) const
176{
177 Polygon out;
178 out.m_points.resize(m_points.size());
179 for(unsigned i = 0; i < m_points.size(); ++i)
180 out.m_points[i] = m_points[i].toLocalCoords(coords);
181 return out;
182}
183
184template class Polygon<3>;
185template class Poly2Orient<3>;
186
187static_assert(std::is_standard_layout<Polygon<3>>::value, "Polygon should be standard layout.");
188static_assert(std::is_standard_layout<Polygon<2>>::value, "Polygon should be standard layout.");
189}
Generic library namespace.
Definition: shape.h:41
double CoordType
Basic floating point type.
Definition: const.h:140