cm_polylib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. // this is only used for visualization tools in cm_ debug functions
  19. #include "cm_local.h"
  20. // counters are only bumped when running single threaded,
  21. // because they are an awefull coherence problem
  22. int c_active_windings;
  23. int c_peak_windings;
  24. int c_winding_allocs;
  25. int c_winding_points;
  26. void pw(winding_t *w)
  27. {
  28. int i;
  29. for (i=0 ; i<w->numpoints ; i++)
  30. printf ("(%5.1f, %5.1f, %5.1f)\n",w->p[i][0], w->p[i][1],w->p[i][2]);
  31. }
  32. /*
  33. =============
  34. AllocWinding
  35. =============
  36. */
  37. winding_t *AllocWinding (int points)
  38. {
  39. winding_t *w;
  40. int s;
  41. c_winding_allocs++;
  42. c_winding_points += points;
  43. c_active_windings++;
  44. if (c_active_windings > c_peak_windings)
  45. c_peak_windings = c_active_windings;
  46. s = sizeof(vec_t)*3*points + sizeof(int);
  47. w = Z_Malloc (s);
  48. Com_Memset (w, 0, s);
  49. return w;
  50. }
  51. void FreeWinding (winding_t *w)
  52. {
  53. if (*(unsigned *)w == 0xdeaddead)
  54. Com_Error (ERR_FATAL, "FreeWinding: freed a freed winding");
  55. *(unsigned *)w = 0xdeaddead;
  56. c_active_windings--;
  57. Z_Free (w);
  58. }
  59. /*
  60. ============
  61. RemoveColinearPoints
  62. ============
  63. */
  64. int c_removed;
  65. void RemoveColinearPoints (winding_t *w)
  66. {
  67. int i, j, k;
  68. vec3_t v1, v2;
  69. int nump;
  70. vec3_t p[MAX_POINTS_ON_WINDING];
  71. nump = 0;
  72. for (i=0 ; i<w->numpoints ; i++)
  73. {
  74. j = (i+1)%w->numpoints;
  75. k = (i+w->numpoints-1)%w->numpoints;
  76. VectorSubtract (w->p[j], w->p[i], v1);
  77. VectorSubtract (w->p[i], w->p[k], v2);
  78. VectorNormalize2(v1,v1);
  79. VectorNormalize2(v2,v2);
  80. if (DotProduct(v1, v2) < 0.999)
  81. {
  82. VectorCopy (w->p[i], p[nump]);
  83. nump++;
  84. }
  85. }
  86. if (nump == w->numpoints)
  87. return;
  88. c_removed += w->numpoints - nump;
  89. w->numpoints = nump;
  90. Com_Memcpy (w->p, p, nump*sizeof(p[0]));
  91. }
  92. /*
  93. ============
  94. WindingPlane
  95. ============
  96. */
  97. void WindingPlane (winding_t *w, vec3_t normal, vec_t *dist)
  98. {
  99. vec3_t v1, v2;
  100. VectorSubtract (w->p[1], w->p[0], v1);
  101. VectorSubtract (w->p[2], w->p[0], v2);
  102. CrossProduct (v2, v1, normal);
  103. VectorNormalize2(normal, normal);
  104. *dist = DotProduct (w->p[0], normal);
  105. }
  106. /*
  107. =============
  108. WindingArea
  109. =============
  110. */
  111. vec_t WindingArea (winding_t *w)
  112. {
  113. int i;
  114. vec3_t d1, d2, cross;
  115. vec_t total;
  116. total = 0;
  117. for (i=2 ; i<w->numpoints ; i++)
  118. {
  119. VectorSubtract (w->p[i-1], w->p[0], d1);
  120. VectorSubtract (w->p[i], w->p[0], d2);
  121. CrossProduct (d1, d2, cross);
  122. total += 0.5 * VectorLength ( cross );
  123. }
  124. return total;
  125. }
  126. /*
  127. =============
  128. WindingBounds
  129. =============
  130. */
  131. void WindingBounds (winding_t *w, vec3_t mins, vec3_t maxs)
  132. {
  133. vec_t v;
  134. int i,j;
  135. mins[0] = mins[1] = mins[2] = MAX_MAP_BOUNDS;
  136. maxs[0] = maxs[1] = maxs[2] = -MAX_MAP_BOUNDS;
  137. for (i=0 ; i<w->numpoints ; i++)
  138. {
  139. for (j=0 ; j<3 ; j++)
  140. {
  141. v = w->p[i][j];
  142. if (v < mins[j])
  143. mins[j] = v;
  144. if (v > maxs[j])
  145. maxs[j] = v;
  146. }
  147. }
  148. }
  149. /*
  150. =============
  151. WindingCenter
  152. =============
  153. */
  154. void WindingCenter (winding_t *w, vec3_t center)
  155. {
  156. int i;
  157. float scale;
  158. VectorCopy (vec3_origin, center);
  159. for (i=0 ; i<w->numpoints ; i++)
  160. VectorAdd (w->p[i], center, center);
  161. scale = 1.0/w->numpoints;
  162. VectorScale (center, scale, center);
  163. }
  164. /*
  165. =================
  166. BaseWindingForPlane
  167. =================
  168. */
  169. winding_t *BaseWindingForPlane (vec3_t normal, vec_t dist)
  170. {
  171. int i, x;
  172. vec_t max, v;
  173. vec3_t org, vright, vup;
  174. winding_t *w;
  175. // find the major axis
  176. max = -MAX_MAP_BOUNDS;
  177. x = -1;
  178. for (i=0 ; i<3; i++)
  179. {
  180. v = fabs(normal[i]);
  181. if (v > max)
  182. {
  183. x = i;
  184. max = v;
  185. }
  186. }
  187. if (x==-1)
  188. Com_Error (ERR_DROP, "BaseWindingForPlane: no axis found");
  189. VectorCopy (vec3_origin, vup);
  190. switch (x)
  191. {
  192. case 0:
  193. case 1:
  194. vup[2] = 1;
  195. break;
  196. case 2:
  197. vup[0] = 1;
  198. break;
  199. }
  200. v = DotProduct (vup, normal);
  201. VectorMA (vup, -v, normal, vup);
  202. VectorNormalize2(vup, vup);
  203. VectorScale (normal, dist, org);
  204. CrossProduct (vup, normal, vright);
  205. VectorScale (vup, MAX_MAP_BOUNDS, vup);
  206. VectorScale (vright, MAX_MAP_BOUNDS, vright);
  207. // project a really big axis aligned box onto the plane
  208. w = AllocWinding (4);
  209. VectorSubtract (org, vright, w->p[0]);
  210. VectorAdd (w->p[0], vup, w->p[0]);
  211. VectorAdd (org, vright, w->p[1]);
  212. VectorAdd (w->p[1], vup, w->p[1]);
  213. VectorAdd (org, vright, w->p[2]);
  214. VectorSubtract (w->p[2], vup, w->p[2]);
  215. VectorSubtract (org, vright, w->p[3]);
  216. VectorSubtract (w->p[3], vup, w->p[3]);
  217. w->numpoints = 4;
  218. return w;
  219. }
  220. /*
  221. ==================
  222. CopyWinding
  223. ==================
  224. */
  225. winding_t *CopyWinding (winding_t *w)
  226. {
  227. int size;
  228. winding_t *c;
  229. c = AllocWinding (w->numpoints);
  230. size = (int)((winding_t *)0)->p[w->numpoints];
  231. Com_Memcpy (c, w, size);
  232. return c;
  233. }
  234. /*
  235. ==================
  236. ReverseWinding
  237. ==================
  238. */
  239. winding_t *ReverseWinding (winding_t *w)
  240. {
  241. int i;
  242. winding_t *c;
  243. c = AllocWinding (w->numpoints);
  244. for (i=0 ; i<w->numpoints ; i++)
  245. {
  246. VectorCopy (w->p[w->numpoints-1-i], c->p[i]);
  247. }
  248. c->numpoints = w->numpoints;
  249. return c;
  250. }
  251. /*
  252. =============
  253. ClipWindingEpsilon
  254. =============
  255. */
  256. void ClipWindingEpsilon (winding_t *in, vec3_t normal, vec_t dist,
  257. vec_t epsilon, winding_t **front, winding_t **back)
  258. {
  259. vec_t dists[MAX_POINTS_ON_WINDING+4];
  260. int sides[MAX_POINTS_ON_WINDING+4];
  261. int counts[3];
  262. static vec_t dot; // VC 4.2 optimizer bug if not static
  263. int i, j;
  264. vec_t *p1, *p2;
  265. vec3_t mid;
  266. winding_t *f, *b;
  267. int maxpts;
  268. counts[0] = counts[1] = counts[2] = 0;
  269. // determine sides for each point
  270. for (i=0 ; i<in->numpoints ; i++)
  271. {
  272. dot = DotProduct (in->p[i], normal);
  273. dot -= dist;
  274. dists[i] = dot;
  275. if (dot > epsilon)
  276. sides[i] = SIDE_FRONT;
  277. else if (dot < -epsilon)
  278. sides[i] = SIDE_BACK;
  279. else
  280. {
  281. sides[i] = SIDE_ON;
  282. }
  283. counts[sides[i]]++;
  284. }
  285. sides[i] = sides[0];
  286. dists[i] = dists[0];
  287. *front = *back = NULL;
  288. if (!counts[0])
  289. {
  290. *back = CopyWinding (in);
  291. return;
  292. }
  293. if (!counts[1])
  294. {
  295. *front = CopyWinding (in);
  296. return;
  297. }
  298. maxpts = in->numpoints+4; // cant use counts[0]+2 because
  299. // of fp grouping errors
  300. *front = f = AllocWinding (maxpts);
  301. *back = b = AllocWinding (maxpts);
  302. for (i=0 ; i<in->numpoints ; i++)
  303. {
  304. p1 = in->p[i];
  305. if (sides[i] == SIDE_ON)
  306. {
  307. VectorCopy (p1, f->p[f->numpoints]);
  308. f->numpoints++;
  309. VectorCopy (p1, b->p[b->numpoints]);
  310. b->numpoints++;
  311. continue;
  312. }
  313. if (sides[i] == SIDE_FRONT)
  314. {
  315. VectorCopy (p1, f->p[f->numpoints]);
  316. f->numpoints++;
  317. }
  318. if (sides[i] == SIDE_BACK)
  319. {
  320. VectorCopy (p1, b->p[b->numpoints]);
  321. b->numpoints++;
  322. }
  323. if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
  324. continue;
  325. // generate a split point
  326. p2 = in->p[(i+1)%in->numpoints];
  327. dot = dists[i] / (dists[i]-dists[i+1]);
  328. for (j=0 ; j<3 ; j++)
  329. { // avoid round off error when possible
  330. if (normal[j] == 1)
  331. mid[j] = dist;
  332. else if (normal[j] == -1)
  333. mid[j] = -dist;
  334. else
  335. mid[j] = p1[j] + dot*(p2[j]-p1[j]);
  336. }
  337. VectorCopy (mid, f->p[f->numpoints]);
  338. f->numpoints++;
  339. VectorCopy (mid, b->p[b->numpoints]);
  340. b->numpoints++;
  341. }
  342. if (f->numpoints > maxpts || b->numpoints > maxpts)
  343. Com_Error (ERR_DROP, "ClipWinding: points exceeded estimate");
  344. if (f->numpoints > MAX_POINTS_ON_WINDING || b->numpoints > MAX_POINTS_ON_WINDING)
  345. Com_Error (ERR_DROP, "ClipWinding: MAX_POINTS_ON_WINDING");
  346. }
  347. /*
  348. =============
  349. ChopWindingInPlace
  350. =============
  351. */
  352. void ChopWindingInPlace (winding_t **inout, vec3_t normal, vec_t dist, vec_t epsilon)
  353. {
  354. winding_t *in;
  355. vec_t dists[MAX_POINTS_ON_WINDING+4];
  356. int sides[MAX_POINTS_ON_WINDING+4];
  357. int counts[3];
  358. static vec_t dot; // VC 4.2 optimizer bug if not static
  359. int i, j;
  360. vec_t *p1, *p2;
  361. vec3_t mid;
  362. winding_t *f;
  363. int maxpts;
  364. in = *inout;
  365. counts[0] = counts[1] = counts[2] = 0;
  366. // determine sides for each point
  367. for (i=0 ; i<in->numpoints ; i++)
  368. {
  369. dot = DotProduct (in->p[i], normal);
  370. dot -= dist;
  371. dists[i] = dot;
  372. if (dot > epsilon)
  373. sides[i] = SIDE_FRONT;
  374. else if (dot < -epsilon)
  375. sides[i] = SIDE_BACK;
  376. else
  377. {
  378. sides[i] = SIDE_ON;
  379. }
  380. counts[sides[i]]++;
  381. }
  382. sides[i] = sides[0];
  383. dists[i] = dists[0];
  384. if (!counts[0])
  385. {
  386. FreeWinding (in);
  387. *inout = NULL;
  388. return;
  389. }
  390. if (!counts[1])
  391. return; // inout stays the same
  392. maxpts = in->numpoints+4; // cant use counts[0]+2 because
  393. // of fp grouping errors
  394. f = AllocWinding (maxpts);
  395. for (i=0 ; i<in->numpoints ; i++)
  396. {
  397. p1 = in->p[i];
  398. if (sides[i] == SIDE_ON)
  399. {
  400. VectorCopy (p1, f->p[f->numpoints]);
  401. f->numpoints++;
  402. continue;
  403. }
  404. if (sides[i] == SIDE_FRONT)
  405. {
  406. VectorCopy (p1, f->p[f->numpoints]);
  407. f->numpoints++;
  408. }
  409. if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
  410. continue;
  411. // generate a split point
  412. p2 = in->p[(i+1)%in->numpoints];
  413. dot = dists[i] / (dists[i]-dists[i+1]);
  414. for (j=0 ; j<3 ; j++)
  415. { // avoid round off error when possible
  416. if (normal[j] == 1)
  417. mid[j] = dist;
  418. else if (normal[j] == -1)
  419. mid[j] = -dist;
  420. else
  421. mid[j] = p1[j] + dot*(p2[j]-p1[j]);
  422. }
  423. VectorCopy (mid, f->p[f->numpoints]);
  424. f->numpoints++;
  425. }
  426. if (f->numpoints > maxpts)
  427. Com_Error (ERR_DROP, "ClipWinding: points exceeded estimate");
  428. if (f->numpoints > MAX_POINTS_ON_WINDING)
  429. Com_Error (ERR_DROP, "ClipWinding: MAX_POINTS_ON_WINDING");
  430. FreeWinding (in);
  431. *inout = f;
  432. }
  433. /*
  434. =================
  435. ChopWinding
  436. Returns the fragment of in that is on the front side
  437. of the cliping plane. The original is freed.
  438. =================
  439. */
  440. winding_t *ChopWinding (winding_t *in, vec3_t normal, vec_t dist)
  441. {
  442. winding_t *f, *b;
  443. ClipWindingEpsilon (in, normal, dist, ON_EPSILON, &f, &b);
  444. FreeWinding (in);
  445. if (b)
  446. FreeWinding (b);
  447. return f;
  448. }
  449. /*
  450. =================
  451. CheckWinding
  452. =================
  453. */
  454. void CheckWinding (winding_t *w)
  455. {
  456. int i, j;
  457. vec_t *p1, *p2;
  458. vec_t d, edgedist;
  459. vec3_t dir, edgenormal, facenormal;
  460. vec_t area;
  461. vec_t facedist;
  462. if (w->numpoints < 3)
  463. Com_Error (ERR_DROP, "CheckWinding: %i points",w->numpoints);
  464. area = WindingArea(w);
  465. if (area < 1)
  466. Com_Error (ERR_DROP, "CheckWinding: %f area", area);
  467. WindingPlane (w, facenormal, &facedist);
  468. for (i=0 ; i<w->numpoints ; i++)
  469. {
  470. p1 = w->p[i];
  471. for (j=0 ; j<3 ; j++)
  472. if (p1[j] > MAX_MAP_BOUNDS || p1[j] < -MAX_MAP_BOUNDS)
  473. Com_Error (ERR_DROP, "CheckFace: BUGUS_RANGE: %f",p1[j]);
  474. j = i+1 == w->numpoints ? 0 : i+1;
  475. // check the point is on the face plane
  476. d = DotProduct (p1, facenormal) - facedist;
  477. if (d < -ON_EPSILON || d > ON_EPSILON)
  478. Com_Error (ERR_DROP, "CheckWinding: point off plane");
  479. // check the edge isnt degenerate
  480. p2 = w->p[j];
  481. VectorSubtract (p2, p1, dir);
  482. if (VectorLength (dir) < ON_EPSILON)
  483. Com_Error (ERR_DROP, "CheckWinding: degenerate edge");
  484. CrossProduct (facenormal, dir, edgenormal);
  485. VectorNormalize2 (edgenormal, edgenormal);
  486. edgedist = DotProduct (p1, edgenormal);
  487. edgedist += ON_EPSILON;
  488. // all other points must be on front side
  489. for (j=0 ; j<w->numpoints ; j++)
  490. {
  491. if (j == i)
  492. continue;
  493. d = DotProduct (w->p[j], edgenormal);
  494. if (d > edgedist)
  495. Com_Error (ERR_DROP, "CheckWinding: non-convex");
  496. }
  497. }
  498. }
  499. /*
  500. ============
  501. WindingOnPlaneSide
  502. ============
  503. */
  504. int WindingOnPlaneSide (winding_t *w, vec3_t normal, vec_t dist)
  505. {
  506. qboolean front, back;
  507. int i;
  508. vec_t d;
  509. front = qfalse;
  510. back = qfalse;
  511. for (i=0 ; i<w->numpoints ; i++)
  512. {
  513. d = DotProduct (w->p[i], normal) - dist;
  514. if (d < -ON_EPSILON)
  515. {
  516. if (front)
  517. return SIDE_CROSS;
  518. back = qtrue;
  519. continue;
  520. }
  521. if (d > ON_EPSILON)
  522. {
  523. if (back)
  524. return SIDE_CROSS;
  525. front = qtrue;
  526. continue;
  527. }
  528. }
  529. if (back)
  530. return SIDE_BACK;
  531. if (front)
  532. return SIDE_FRONT;
  533. return SIDE_ON;
  534. }
  535. /*
  536. =================
  537. AddWindingToConvexHull
  538. Both w and *hull are on the same plane
  539. =================
  540. */
  541. #define MAX_HULL_POINTS 128
  542. void AddWindingToConvexHull( winding_t *w, winding_t **hull, vec3_t normal ) {
  543. int i, j, k;
  544. float *p, *copy;
  545. vec3_t dir;
  546. float d;
  547. int numHullPoints, numNew;
  548. vec3_t hullPoints[MAX_HULL_POINTS];
  549. vec3_t newHullPoints[MAX_HULL_POINTS];
  550. vec3_t hullDirs[MAX_HULL_POINTS];
  551. qboolean hullSide[MAX_HULL_POINTS];
  552. qboolean outside;
  553. if ( !*hull ) {
  554. *hull = CopyWinding( w );
  555. return;
  556. }
  557. numHullPoints = (*hull)->numpoints;
  558. Com_Memcpy( hullPoints, (*hull)->p, numHullPoints * sizeof(vec3_t) );
  559. for ( i = 0 ; i < w->numpoints ; i++ ) {
  560. p = w->p[i];
  561. // calculate hull side vectors
  562. for ( j = 0 ; j < numHullPoints ; j++ ) {
  563. k = ( j + 1 ) % numHullPoints;
  564. VectorSubtract( hullPoints[k], hullPoints[j], dir );
  565. VectorNormalize2( dir, dir );
  566. CrossProduct( normal, dir, hullDirs[j] );
  567. }
  568. outside = qfalse;
  569. for ( j = 0 ; j < numHullPoints ; j++ ) {
  570. VectorSubtract( p, hullPoints[j], dir );
  571. d = DotProduct( dir, hullDirs[j] );
  572. if ( d >= ON_EPSILON ) {
  573. outside = qtrue;
  574. }
  575. if ( d >= -ON_EPSILON ) {
  576. hullSide[j] = qtrue;
  577. } else {
  578. hullSide[j] = qfalse;
  579. }
  580. }
  581. // if the point is effectively inside, do nothing
  582. if ( !outside ) {
  583. continue;
  584. }
  585. // find the back side to front side transition
  586. for ( j = 0 ; j < numHullPoints ; j++ ) {
  587. if ( !hullSide[ j % numHullPoints ] && hullSide[ (j + 1) % numHullPoints ] ) {
  588. break;
  589. }
  590. }
  591. if ( j == numHullPoints ) {
  592. continue;
  593. }
  594. // insert the point here
  595. VectorCopy( p, newHullPoints[0] );
  596. numNew = 1;
  597. // copy over all points that aren't double fronts
  598. j = (j+1)%numHullPoints;
  599. for ( k = 0 ; k < numHullPoints ; k++ ) {
  600. if ( hullSide[ (j+k) % numHullPoints ] && hullSide[ (j+k+1) % numHullPoints ] ) {
  601. continue;
  602. }
  603. copy = hullPoints[ (j+k+1) % numHullPoints ];
  604. VectorCopy( copy, newHullPoints[numNew] );
  605. numNew++;
  606. }
  607. numHullPoints = numNew;
  608. Com_Memcpy( hullPoints, newHullPoints, numHullPoints * sizeof(vec3_t) );
  609. }
  610. FreeWinding( *hull );
  611. w = AllocWinding( numHullPoints );
  612. w->numpoints = numHullPoints;
  613. *hull = w;
  614. Com_Memcpy( w->p, hullPoints, numHullPoints * sizeof(vec3_t) );
  615. }