2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
22 #if !defined(INCLUDED_WINDING_H)
23 #define INCLUDED_WINDING_H
25 #include "debugging/debugging.h"
29 #include "math/vector.h"
30 #include "container/array.h"
39 const float ProjectionAxisEpsilon = static_cast<float>(0.0001);
41 inline bool projectionaxis_better(float axis, float other)
43 return fabs(axis) > fabs(other) + ProjectionAxisEpsilon;
46 /// \brief Texture axis precedence: Z > X > Y
47 inline ProjectionAxis projectionaxis_for_normal(const Vector3& normal)
49 return (projectionaxis_better(normal[eProjectionAxisY], normal[eProjectionAxisX]))
50 ? (projectionaxis_better(normal[eProjectionAxisY], normal[eProjectionAxisZ]))
53 : (projectionaxis_better(normal[eProjectionAxisX], normal[eProjectionAxisZ]))
61 indexremap_t(std::size_t _x, std::size_t _y, std::size_t _z)
68 inline indexremap_t indexremap_for_projectionaxis(const ProjectionAxis axis)
72 case eProjectionAxisX: return indexremap_t(1, 2, 0);
73 case eProjectionAxisY: return indexremap_t(2, 0, 1);
74 default: return indexremap_t(0, 1, 2);
78 enum PlaneClassification
85 #define MAX_POINTS_ON_WINDING 64
86 const std::size_t c_brush_maxFaces = 1024;
103 typedef Array<WindingVertex> container_type;
105 std::size_t numpoints;
106 container_type points;
108 typedef container_type::iterator iterator;
109 typedef container_type::const_iterator const_iterator;
111 Winding() : numpoints(0)
114 Winding(std::size_t size) : numpoints(0), points(size)
117 void resize(std::size_t size)
125 return points.begin();
127 const_iterator begin() const
129 return points.begin();
133 return points.begin() + numpoints;
135 const_iterator end() const
137 return points.begin() + numpoints;
140 WindingVertex& operator[](std::size_t index)
142 ASSERT_MESSAGE(index < points.size(), "winding: index out of bounds");
143 return points[index];
145 const WindingVertex& operator[](std::size_t index) const
147 ASSERT_MESSAGE(index < points.size(), "winding: index out of bounds");
148 return points[index];
151 void push_back(const WindingVertex& point)
153 points[numpoints] = point;
156 void erase(iterator point)
158 for(iterator i = point + 1; i != end(); point = i, ++i)
166 typedef BasicVector3<double> DoubleVector3;
171 DoubleVector3 origin;
172 DoubleVector3 direction;
175 class FixedWindingVertex
178 DoubleVector3 vertex;
180 std::size_t adjacent;
182 FixedWindingVertex(const DoubleVector3& vertex_, const DoubleLine& edge_, std::size_t adjacent_)
183 : vertex(vertex_), edge(edge_), adjacent(adjacent_)
190 typedef std::vector<FixedWindingVertex> Points;
195 points.reserve(MAX_POINTS_ON_WINDING);
198 FixedWindingVertex& front()
200 return points.front();
202 const FixedWindingVertex& front() const
204 return points.front();
206 FixedWindingVertex& back()
208 return points.back();
210 const FixedWindingVertex& back() const
212 return points.back();
220 void push_back(const FixedWindingVertex& point)
222 points.push_back(point);
224 std::size_t size() const
226 return points.size();
229 FixedWindingVertex& operator[](std::size_t index)
231 //ASSERT_MESSAGE(index < MAX_POINTS_ON_WINDING, "winding: index out of bounds");
232 return points[index];
234 const FixedWindingVertex& operator[](std::size_t index) const
236 //ASSERT_MESSAGE(index < MAX_POINTS_ON_WINDING, "winding: index out of bounds");
237 return points[index];
243 inline void Winding_forFixedWinding(Winding& winding, const FixedWinding& fixed)
245 winding.resize(fixed.size());
246 winding.numpoints = fixed.size();
247 for(std::size_t i = 0; i < fixed.size(); ++i)
249 winding[i].vertex[0] = static_cast<float>(fixed[i].vertex[0]);
250 winding[i].vertex[1] = static_cast<float>(fixed[i].vertex[1]);
251 winding[i].vertex[2] = static_cast<float>(fixed[i].vertex[2]);
252 winding[i].adjacent = fixed[i].adjacent;
256 inline std::size_t Winding_wrap(const Winding& winding, std::size_t i)
258 ASSERT_MESSAGE(winding.numpoints != 0, "Winding_wrap: empty winding");
259 return i % winding.numpoints;
262 inline std::size_t Winding_next(const Winding& winding, std::size_t i)
264 return Winding_wrap(winding, ++i);
270 void Winding_createInfinite(FixedWinding& w, const Plane3& plane, double infinity);
272 const double ON_EPSILON = 1.0 / (1 << 8);
274 /// \brief Returns true if edge (\p x, \p y) is smaller than the epsilon used to classify winding points against a plane.
275 inline bool Edge_isDegenerate(const Vector3& x, const Vector3& y)
277 return vector3_length_squared(y - x) < (ON_EPSILON * ON_EPSILON);
280 void Winding_Clip(const FixedWinding& winding, const Plane3& plane, const Plane3& clipPlane, std::size_t adjacent, FixedWinding& clipped);
290 brushsplit_t& operator+=(const brushsplit_t& other)
292 counts[0] += other.counts[0];
293 counts[1] += other.counts[1];
294 counts[2] += other.counts[2];
297 std::size_t counts[3];
300 brushsplit_t Winding_ClassifyPlane(const Winding& w, const Plane3& plane);
302 bool Winding_PlanesConcave(const Winding& w1, const Winding& w2, const Plane3& plane1, const Plane3& plane2);
303 bool Winding_TestPlane(const Winding& w, const Plane3& plane, bool flipped);
305 std::size_t Winding_FindAdjacent(const Winding& w, std::size_t face);
307 std::size_t Winding_Opposite(const Winding& w, const std::size_t index, const std::size_t other);
308 std::size_t Winding_Opposite(const Winding& w, std::size_t index);
310 void Winding_Centroid(const Winding& w, const Plane3& plane, Vector3& centroid);
313 inline void Winding_printConnectivity(Winding& winding)
315 for(Winding::iterator i = winding.begin(); i != winding.end(); ++i)
317 std::size_t vertexIndex = std::distance(winding.begin(), i);
318 globalOutputStream() << "vertex: " << Unsigned(vertexIndex) << " adjacent: " << Unsigned((*i).adjacent) << "\n";