polylib.c 12 KB

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