2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // DWinding.cpp: implementation of the DWinding class.
22 //////////////////////////////////////////////////////////////////////
26 #include "gtkr_list.h"
32 //////////////////////////////////////////////////////////////////////
33 // Construction/Destruction
34 //////////////////////////////////////////////////////////////////////
48 //////////////////////////////////////////////////////////////////////
50 //////////////////////////////////////////////////////////////////////
52 #define BOGUS_RANGE 4096
54 void DWinding::AllocWinding(int points)
59 p = new vec3_t[points];
62 vec_t DWinding::WindingArea()
68 for (int i = 2; i < numpoints ; i++)
70 VectorSubtract (p[i-1], p[0], d1);
71 VectorSubtract (p[i], p[0], d2);
73 CrossProduct (d1, d2, cross);
75 total += 0.5f * VectorLength ( cross );
81 void DWinding::RemoveColinearPoints()
83 vec3_t p2[MAX_POINTS_ON_WINDING];
86 for (int i = 0; i < numpoints; i++)
88 int j = (i+1)%numpoints;
89 int k = (i+numpoints-1)%numpoints;
92 VectorSubtract (p[j], p[i], v1);
93 VectorSubtract (p[i], p[k], v2);
94 VectorNormalize(v1, v1);
95 VectorNormalize(v2, v2);
97 if (DotProduct(v1, v2) < 0.999)
99 VectorCopy (p[i], p2[nump]);
104 if (nump == numpoints)
108 memcpy (p, p2, nump*sizeof(vec3_t));
111 DPlane* DWinding::WindingPlane()
113 DPlane* newPlane = new DPlane(p[0], p[1], p[2], NULL);
117 void DWinding::WindingBounds(vec3_t mins, vec3_t maxs)
122 VectorCopy(mins, p[0]);
123 VectorCopy(maxs, p[0]);
125 for (int i = 1; i < numpoints ;i++)
127 for (int j = 0; j < 3; j++)
138 void DWinding::WindingCentre(vec3_t centre)
140 VectorCopy (vec3_origin, centre);
141 for (int i = 0; i < numpoints; i++)
142 VectorAdd (p[i], centre, centre);
144 float scale = 1.0f/numpoints;
145 VectorScale (centre, scale, centre);
149 DWinding* DWinding::CopyWinding()
151 DWinding* c = new DWinding;
152 c->AllocWinding(numpoints);
153 memcpy (c->p, p, numpoints*sizeof(vec3_t));
158 int DWinding::WindingOnPlaneSide(vec3_t normal, vec_t dist)
163 for (int i = 0; i < numpoints; i++)
165 vec_t d = DotProduct (p[i], normal) - dist;
189 void DWinding::CheckWinding()
193 vec3_t dir, edgenormal;
196 Sys_Printf ("CheckWinding: %i points", numpoints);
198 vec_t area = WindingArea();
200 Sys_Printf ("CheckWinding: %f area", area);
202 DPlane* wPlane = WindingPlane ();
204 for (i = 0; i < numpoints; i++)
209 for (j = 0; j < 3; j++)
210 if (p1[j] > BOGUS_RANGE || p1[j] < -BOGUS_RANGE)
211 Sys_Printf ("CheckFace: BUGUS_RANGE: %f", p1[j]);
213 j = i + 1 == numpoints ? 0 : i + 1;
215 // check the point is on the face plane
216 vec_t d = DotProduct (p1, wPlane->normal) - wPlane->_d;
217 if (d < -ON_EPSILON || d > ON_EPSILON)
218 Sys_Printf ("CheckWinding: point off plane");
220 // check the edge isnt degenerate
222 VectorSubtract (p2, p1, dir);
224 if (VectorLength (dir) < ON_EPSILON)
225 Sys_Printf ("CheckWinding: degenerate edge");
227 CrossProduct (wPlane->normal, dir, edgenormal);
228 VectorNormalize (edgenormal, edgenormal);
229 edgedist = DotProduct (p1, edgenormal);
231 // all other points must be on front side
232 for (j = 0 ; j < numpoints ; j++)
237 d = DotProduct (p[j], edgenormal);
238 if (d > (edgedist + ON_EPSILON))
239 Sys_Printf ("CheckWinding: non-convex");
246 DWinding* DWinding::ReverseWinding()
248 DWinding* c = new DWinding;
249 c->AllocWinding(numpoints);
251 for (int i = 0; i < numpoints ; i++)
252 VectorCopy (p[numpoints-1-i], c->p[i]);
257 bool DWinding::ChopWindingInPlace(DPlane* chopPlane, vec_t epsilon)
259 vec_t dists[MAX_POINTS_ON_WINDING+4];
260 int sides[MAX_POINTS_ON_WINDING+4];
265 counts[0] = counts[1] = counts[2] = 0;
267 // determine sides for each point
269 for (i = 0; i < numpoints; i++)
271 vec_t dot = DotProduct (p[i], chopPlane->normal);
272 dot -= chopPlane->_d;
276 sides[i] = SIDE_FRONT;
277 else if (dot < -epsilon)
278 sides[i] = SIDE_BACK;
296 int maxpts = numpoints+4; // cant use counts[0]+2 because
297 // of fp grouping errors
299 DWinding* f = new DWinding;
300 f->AllocWinding(maxpts);
303 for (i = 0; i < numpoints; i++)
307 if (sides[i] == SIDE_ON)
309 VectorCopy (p1, f->p[f->numpoints]);
314 if (sides[i] == SIDE_FRONT)
316 VectorCopy (p1, f->p[f->numpoints]);
320 if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
323 // generate a split point
324 p2 = p[(i+1)%numpoints];
326 vec_t dot = dists[i] / (dists[i]-dists[i+1]);
327 for (int j = 0; j < 3; j++)
329 if (chopPlane->normal[j] == 1)
330 mid[j] = chopPlane->_d;
331 else if (chopPlane->normal[j] == -1)
332 mid[j] = -chopPlane->_d;
334 mid[j] = p1[j] + dot*(p2[j]-p1[j]);
337 VectorCopy (mid, f->p[f->numpoints]);
341 if (f->numpoints > maxpts)
342 Sys_Printf ("ClipWinding: points exceeded estimate");
343 if (f->numpoints > MAX_POINTS_ON_WINDING)
344 Sys_Printf ("ClipWinding: MAX_POINTS_ON_WINDING");
353 void DWinding::ClipWindingEpsilon(DPlane* chopPlane, vec_t epsilon, DWinding **front, DWinding **back)
355 vec_t dists[MAX_POINTS_ON_WINDING+4];
356 int sides[MAX_POINTS_ON_WINDING+4];
361 counts[0] = counts[1] = counts[2] = 0;
363 // determine sides for each point
365 for (i = 0; i < numpoints; i++)
367 vec_t dot = -chopPlane->DistanceToPoint(p[i]);
371 sides[i] = SIDE_FRONT;
372 else if (dot < -epsilon)
373 sides[i] = SIDE_BACK;
382 *front = *back = NULL;
386 *back = CopyWinding();
391 *front = CopyWinding();
395 int maxpts = numpoints+4; // cant use counts[0]+2 because
396 // of fp grouping errors
398 DWinding* f = new DWinding;
399 DWinding* b = new DWinding;
401 f->AllocWinding(maxpts);
404 b->AllocWinding(maxpts);
410 for (i = 0; i < numpoints ; i++)
414 if (sides[i] == SIDE_ON)
416 VectorCopy (p1, f->p[f->numpoints]);
418 VectorCopy (p1, b->p[b->numpoints]);
423 if (sides[i] == SIDE_FRONT)
425 VectorCopy (p1, f->p[f->numpoints]);
428 if (sides[i] == SIDE_BACK)
430 VectorCopy (p1, b->p[b->numpoints]);
434 if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
437 // generate a split point
438 p2 = p[(i+1)%numpoints];
440 vec_t dot = dists[i] / (dists[i]-dists[i+1]);
441 for (int j = 0; j < 3; j++)
443 if (chopPlane->normal[j] == 1)
444 mid[j] = chopPlane->_d;
445 else if (chopPlane->normal[j] == -1)
446 mid[j] = -chopPlane->_d;
448 mid[j] = p1[j] + dot*(p2[j]-p1[j]);
451 VectorCopy (mid, f->p[f->numpoints]);
453 VectorCopy (mid, b->p[b->numpoints]);
457 if (f->numpoints > maxpts || b->numpoints > maxpts)
458 Sys_Printf ("ClipWinding: points exceeded estimate");
459 if (f->numpoints > MAX_POINTS_ON_WINDING || b->numpoints > MAX_POINTS_ON_WINDING)
460 Sys_Printf ("ClipWinding: MAX_POINTS_ON_WINDING");
463 bool DWinding::ChopWinding(DPlane* chopPlane)
467 ClipWindingEpsilon (chopPlane, (float)ON_EPSILON, &f, &b);
482 numpoints = f->numpoints;