polylib.c 14 KB

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