1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "winding.h"
23
24 #include <algorithm>
25
26 #include "math/line.h"
27
plane3_distance_to_point(const Plane3 & plane,const DoubleVector3 & point)28 inline double plane3_distance_to_point (const Plane3& plane, const DoubleVector3& point)
29 {
30 return point.dot(plane.normal()) - plane.dist();
31 }
32
plane3_distance_to_point(const Plane3 & plane,const Vector3 & point)33 inline double plane3_distance_to_point (const Plane3& plane, const Vector3& point)
34 {
35 return point.dot(plane.normal()) - plane.dist();
36 }
37
38 /// \brief Returns the point at which \p line intersects \p plane, or an undefined value if there is no intersection.
line_intersect_plane(const DoubleLine & line,const Plane3 & plane)39 inline DoubleVector3 line_intersect_plane (const DoubleLine& line, const Plane3& plane)
40 {
41 return line.origin + line.direction * (-plane3_distance_to_point(plane, line.origin) / line.direction.dot(
42 plane.normal()));
43 }
44
float_is_largest_absolute(double axis,double other)45 inline bool float_is_largest_absolute (double axis, double other)
46 {
47 return fabs(axis) > fabs(other);
48 }
49
50 /// \brief Returns the index of the component of \p v that has the largest absolute value.
vector3_largest_absolute_component_index(const DoubleVector3 & v)51 inline int vector3_largest_absolute_component_index (const DoubleVector3& v)
52 {
53 return (float_is_largest_absolute(v[1], v[0])) ? (float_is_largest_absolute(v[1], v[2])) ? 1 : 2
54 : (float_is_largest_absolute(v[0], v[2])) ? 0 : 2;
55 }
56
57 /// \brief Returns the infinite line that is the intersection of \p plane and \p other.
plane3_intersect_plane3(const Plane3 & plane,const Plane3 & other)58 inline DoubleLine plane3_intersect_plane3 (const Plane3& plane, const Plane3& other)
59 {
60 DoubleLine line;
61 line.direction = plane.normal().crossProduct(other.normal());
62 switch (vector3_largest_absolute_component_index(line.direction)) {
63 case 0:
64 line.origin.x() = 0;
65 line.origin.y() = (-other.dist() * plane.normal().z() - -plane.dist() * other.normal().z())
66 / line.direction.x();
67 line.origin.z() = (-plane.dist() * other.normal().y() - -other.dist() * plane.normal().y())
68 / line.direction.x();
69 break;
70 case 1:
71 line.origin.x() = (-plane.dist() * other.normal().z() - -other.dist() * plane.normal().z())
72 / line.direction.y();
73 line.origin.y() = 0;
74 line.origin.z() = (-other.dist() * plane.normal().x() - -plane.dist() * other.normal().x())
75 / line.direction.y();
76 break;
77 case 2:
78 line.origin.x() = (-other.dist() * plane.normal().y() - -plane.dist() * other.normal().y())
79 / line.direction.z();
80 line.origin.y() = (-plane.dist() * other.normal().x() - -other.dist() * plane.normal().x())
81 / line.direction.z();
82 line.origin.z() = 0;
83 break;
84 default:
85 break;
86 }
87
88 return line;
89 }
90
91 /// \brief Keep the value of \p infinity as small as possible to improve precision in Winding_Clip.
Winding_createInfinite(FixedWinding & winding,const Plane3 & plane,double infinity)92 void Winding_createInfinite (FixedWinding& winding, const Plane3& plane, double infinity)
93 {
94 double max = -infinity;
95 int x = -1;
96 for (int i = 0; i < 3; i++) {
97 const double d = fabs(plane.normal()[i]);
98 if (d > max) {
99 x = i;
100 max = d;
101 }
102 }
103 if (x == -1) {
104 globalErrorStream() << "invalid plane\n";
105 return;
106 }
107
108 DoubleVector3 vup = g_vector3_identity;
109 switch (x) {
110 case 0:
111 case 1:
112 vup[2] = 1;
113 break;
114 case 2:
115 vup[0] = 1;
116 break;
117 }
118
119 vup += plane.normal() * (-vup.dot(plane.normal()));
120 vector3_normalise(vup);
121
122 DoubleVector3 org = plane.normal() * plane.dist();
123
124 DoubleVector3 vright = vup.crossProduct(plane.normal());
125
126 vup *= infinity;
127 vright *= infinity;
128
129 // project a really big axis aligned box onto the plane
130
131 DoubleLine r1, r2, r3, r4;
132 r1.origin = (org - vright) + vup;
133 r1.direction = vright.getNormalised();
134 winding.push_back(FixedWindingVertex(r1.origin, r1, c_brush_maxFaces));
135 r2.origin = org + vright + vup;
136 r2.direction = (-vup).getNormalised();
137 winding.push_back(FixedWindingVertex(r2.origin, r2, c_brush_maxFaces));
138 r3.origin = (org + vright) - vup;
139 r3.direction = (-vright).getNormalised();
140 winding.push_back(FixedWindingVertex(r3.origin, r3, c_brush_maxFaces));
141 r4.origin = (org - vright) - vup;
142 r4.direction = vup.getNormalised();
143 winding.push_back(FixedWindingVertex(r4.origin, r4, c_brush_maxFaces));
144 }
145
Winding_ClassifyDistance(const double distance,const double epsilon)146 inline PlaneClassification Winding_ClassifyDistance (const double distance, const double epsilon)
147 {
148 if (distance > epsilon) {
149 return ePlaneFront;
150 }
151 if (distance < -epsilon) {
152 return ePlaneBack;
153 }
154 return ePlaneOn;
155 }
156
157 /// \brief Returns true if
158 /// !flipped && winding is completely BACK or ON
159 /// or flipped && winding is completely FRONT or ON
Winding_TestPlane(const Winding & winding,const Plane3 & plane,bool flipped)160 bool Winding_TestPlane (const Winding& winding, const Plane3& plane, bool flipped)
161 {
162 const int test = (flipped) ? ePlaneBack : ePlaneFront;
163 for (Winding::const_iterator i = winding.begin(); i != winding.end(); ++i) {
164 if (test == Winding_ClassifyDistance(plane3_distance_to_point(plane, (*i).vertex), ON_EPSILON)) {
165 return false;
166 }
167 }
168 return true;
169 }
170
171 /// \brief Returns true if any point in \p w1 is in front of plane2, or any point in \p w2 is in front of plane1
Winding_PlanesConcave(const Winding & w1,const Winding & w2,const Plane3 & plane1,const Plane3 & plane2)172 bool Winding_PlanesConcave (const Winding& w1, const Winding& w2, const Plane3& plane1, const Plane3& plane2)
173 {
174 return !Winding_TestPlane(w1, plane2, false) || !Winding_TestPlane(w2, plane1, false);
175 }
176
Winding_ClassifyPlane(const Winding & winding,const Plane3 & plane)177 BrushSplitType Winding_ClassifyPlane (const Winding& winding, const Plane3& plane)
178 {
179 BrushSplitType split;
180 for (Winding::const_iterator i = winding.begin(); i != winding.end(); ++i) {
181 ++split.counts[Winding_ClassifyDistance(plane3_distance_to_point(plane, (*i).vertex), ON_EPSILON)];
182 }
183 return split;
184 }
185
186 const double DEBUG_EPSILON_SQUARED = ON_EPSILON * ON_EPSILON;
187
188 #define WINDING_DEBUG 0
189
190 /// \brief Clip \p winding which lies on \p plane by \p clipPlane, resulting in \p clipped.
191 /// If \p winding is completely in front of the plane, \p clipped will be identical to \p winding.
192 /// If \p winding is completely in back of the plane, \p clipped will be empty.
193 /// If \p winding intersects the plane, the edge of \p clipped which lies on \p clipPlane will store the value of \p adjacent.
Winding_Clip(const FixedWinding & winding,const Plane3 & plane,const Plane3 & clipPlane,std::size_t adjacent,FixedWinding & clipped)194 void Winding_Clip (const FixedWinding& winding, const Plane3& plane, const Plane3& clipPlane, std::size_t adjacent,
195 FixedWinding& clipped)
196 {
197 PlaneClassification classification = Winding_ClassifyDistance(plane3_distance_to_point(clipPlane,
198 winding.back().vertex), ON_EPSILON);
199 PlaneClassification nextClassification;
200 // for each edge
201 for (std::size_t next = 0, i = winding.size() - 1; next != winding.size(); i = next, ++next, classification
202 = nextClassification) {
203 nextClassification = Winding_ClassifyDistance(plane3_distance_to_point(clipPlane, winding[next].vertex),
204 ON_EPSILON);
205 const FixedWindingVertex& vertex = winding[i];
206
207 // if first vertex of edge is ON
208 if (classification == ePlaneOn) {
209 // append first vertex to output winding
210 if (nextClassification == ePlaneBack) {
211 // this edge lies on the clip plane
212 clipped.push_back(
213 FixedWindingVertex(vertex.vertex, plane3_intersect_plane3(plane, clipPlane), adjacent));
214 } else {
215 clipped.push_back(vertex);
216 }
217 continue;
218 }
219
220 // if first vertex of edge is FRONT
221 if (classification == ePlaneFront) {
222 // add first vertex to output winding
223 clipped.push_back(vertex);
224 }
225 // if second vertex of edge is ON
226 if (nextClassification == ePlaneOn) {
227 continue;
228 }
229 // else if second vertex of edge is same as first
230 else if (nextClassification == classification) {
231 continue;
232 }
233 // else if first vertex of edge is FRONT and there are only two edges
234 else if (classification == ePlaneFront && winding.size() == 2) {
235 continue;
236 }
237 // else first vertex is FRONT and second is BACK or vice versa
238 else {
239 // append intersection point of line and plane to output winding
240 DoubleVector3 mid(line_intersect_plane(vertex.edge, clipPlane));
241
242 if (classification == ePlaneFront) {
243 // this edge lies on the clip plane
244 clipped.push_back(FixedWindingVertex(mid, plane3_intersect_plane3(plane, clipPlane), adjacent));
245 } else {
246 clipped.push_back(FixedWindingVertex(mid, vertex.edge, vertex.adjacent));
247 }
248 }
249 }
250 }
251
Winding_FindAdjacent(const Winding & winding,std::size_t face)252 std::size_t Winding_FindAdjacent (const Winding& winding, std::size_t face)
253 {
254 for (std::size_t i = 0; i < winding.size(); ++i) {
255 ASSERT_MESSAGE(winding[i].adjacent != c_brush_maxFaces, "edge connectivity data is invalid");
256 if (winding[i].adjacent == face) {
257 return i;
258 }
259 }
260 return c_brush_maxFaces;
261 }
262
Winding_Opposite(const Winding & winding,const std::size_t & index,const std::size_t & other)263 std::size_t Winding_Opposite (const Winding& winding, const std::size_t& index, const std::size_t& other)
264 {
265 ASSERT_MESSAGE(index < winding.size() && other < winding.size(), "Winding_Opposite: index out of range");
266
267 double dist_best = 0;
268 std::size_t index_best = c_brush_maxFaces;
269
270 Ray edge(ray_for_points(winding[index].vertex, winding[other].vertex));
271
272 for (std::size_t i = 0; i < winding.size(); ++i) {
273 if (i == index || i == other) {
274 continue;
275 }
276
277 double dist_squared = ray_squared_distance_to_point(edge, winding[i].vertex);
278
279 if (dist_squared > dist_best) {
280 dist_best = dist_squared;
281 index_best = i;
282 }
283 }
284 return index_best;
285 }
286
Winding_Opposite(const Winding & winding,const std::size_t index)287 std::size_t Winding_Opposite (const Winding& winding, const std::size_t index)
288 {
289 return Winding_Opposite(winding, index, winding.next(index));
290 }
291
292 /// \brief Calculate the \p centroid of the polygon defined by \p winding which lies on plane \p plane.
Winding_Centroid(const Winding & winding,const Plane3 & plane,Vector3 & centroid)293 void Winding_Centroid (const Winding& winding, const Plane3& plane, Vector3& centroid)
294 {
295 double area2 = 0, x_sum = 0, y_sum = 0;
296 const ProjectionAxis axis = projectionaxis_for_normal(plane.normal());
297 const indexremap_t remap = indexremap_for_projectionaxis(axis);
298 for (std::size_t i = winding.size() - 1, j = 0; j < winding.size(); i = j, ++j) {
299 const double ai = winding[i].vertex[remap.x] * winding[j].vertex[remap.y] - winding[j].vertex[remap.x]
300 * winding[i].vertex[remap.y];
301 area2 += ai;
302 x_sum += (winding[j].vertex[remap.x] + winding[i].vertex[remap.x]) * ai;
303 y_sum += (winding[j].vertex[remap.y] + winding[i].vertex[remap.y]) * ai;
304 }
305
306 centroid[remap.x] = static_cast<float> (x_sum / (3 * area2));
307 centroid[remap.y] = static_cast<float> (y_sum / (3 * area2));
308 {
309 Ray ray(Vector3(0, 0, 0), Vector3(0, 0, 0));
310 ray.origin[remap.x] = centroid[remap.x];
311 ray.origin[remap.y] = centroid[remap.y];
312 ray.direction[remap.z] = 1;
313 centroid[remap.z] = static_cast<float> (ray_distance_to_plane(ray, plane));
314 }
315 }
316