winding.cpp 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. #pragma warning(disable:4018) //amckern - 64bit - '<' Singed/Unsigned Mismatch
  2. #include "winding.h"
  3. #include "cmdlib.h"
  4. #include "log.h"
  5. #include "mathlib.h"
  6. #include "hlassert.h"
  7. #undef BOGUS_RANGE
  8. #undef ON_EPSILON
  9. #define BOGUS_RANGE 10000.0
  10. #define ON_EPSILON 0.01
  11. //
  12. // Winding Public Methods
  13. //
  14. void Winding::Print() const
  15. {
  16. UINT32 x;
  17. for (x=0; x<m_NumPoints; x++)
  18. {
  19. Log("(%5.2f, %5.2f, %5.2f)\n", m_Points[x][0], m_Points[x][1], m_Points[x][2]);
  20. }
  21. }
  22. void Winding::getPlane(dplane_t& plane) const
  23. {
  24. vec3_t v1, v2;
  25. vec3_t plane_normal;
  26. //hlassert(m_NumPoints >= 3);
  27. if (m_NumPoints >= 3)
  28. {
  29. VectorSubtract(m_Points[2], m_Points[1], v1);
  30. VectorSubtract(m_Points[0], m_Points[1], v2);
  31. CrossProduct(v2, v1, plane_normal);
  32. VectorNormalize(plane_normal);
  33. VectorCopy(plane_normal, plane.normal); // change from vec_t
  34. plane.dist = DotProduct(m_Points[0], plane.normal);
  35. }
  36. else
  37. {
  38. VectorClear(plane.normal);
  39. plane.dist = 0.0;
  40. }
  41. }
  42. void Winding::getPlane(vec3_t& normal, vec_t& dist) const
  43. {
  44. vec3_t v1, v2;
  45. //hlassert(m_NumPoints >= 3);
  46. if (m_NumPoints >= 3)
  47. {
  48. VectorSubtract(m_Points[1], m_Points[0], v1);
  49. VectorSubtract(m_Points[2], m_Points[0], v2);
  50. CrossProduct(v2, v1, normal);
  51. VectorNormalize(normal);
  52. dist = DotProduct(m_Points[0], normal);
  53. }
  54. else
  55. {
  56. VectorClear(normal);
  57. dist = 0.0;
  58. }
  59. }
  60. vec_t Winding::getArea() const
  61. {
  62. unsigned int i;
  63. vec3_t d1, d2, cross;
  64. vec_t total;
  65. //hlassert(m_NumPoints >= 3);
  66. total = 0.0;
  67. if (m_NumPoints >= 3)
  68. {
  69. for (i = 2; i < m_NumPoints; i++)
  70. {
  71. VectorSubtract(m_Points[i - 1], m_Points[0], d1);
  72. VectorSubtract(m_Points[i], m_Points[0], d2);
  73. CrossProduct(d1, d2, cross);
  74. total += 0.5 * VectorLength(cross);
  75. }
  76. }
  77. return total;
  78. }
  79. void Winding::getBounds(vec3_t& mins, vec3_t& maxs) const
  80. {
  81. BoundingBox bounds;
  82. unsigned x;
  83. for (x=0; x<m_NumPoints; x++)
  84. {
  85. bounds.add(m_Points[x]);
  86. }
  87. VectorCopy(bounds.m_Mins, mins);
  88. VectorCopy(bounds.m_Maxs, maxs);
  89. }
  90. void Winding::getBounds(BoundingBox& bounds) const
  91. {
  92. bounds.reset();
  93. unsigned x;
  94. for (x=0; x<m_NumPoints; x++)
  95. {
  96. bounds.add(m_Points[x]);
  97. }
  98. }
  99. void Winding::getCenter(vec3_t& center) const
  100. {
  101. unsigned int i;
  102. vec_t scale;
  103. if (m_NumPoints > 0)
  104. {
  105. VectorCopy(vec3_origin, center);
  106. for (i = 0; i < m_NumPoints; i++)
  107. {
  108. VectorAdd(m_Points[i], center, center);
  109. }
  110. scale = 1.0 / m_NumPoints;
  111. VectorScale(center, scale, center);
  112. }
  113. else
  114. {
  115. VectorClear(center);
  116. }
  117. }
  118. Winding* Winding::Copy() const
  119. {
  120. Winding* newWinding = new Winding(*this);
  121. return newWinding;
  122. }
  123. void Winding::Check() const
  124. {
  125. unsigned int i, j;
  126. vec_t* p1;
  127. vec_t* p2;
  128. vec_t d, edgedist;
  129. vec3_t dir, edgenormal, facenormal;
  130. vec_t area;
  131. vec_t facedist;
  132. if (m_NumPoints < 3)
  133. {
  134. Error("Winding::Check : %i points", m_NumPoints);
  135. }
  136. area = getArea();
  137. if (area < 1)
  138. {
  139. Error("Winding::Check : %f area", area);
  140. }
  141. getPlane(facenormal, facedist);
  142. for (i = 0; i < m_NumPoints; i++)
  143. {
  144. p1 = m_Points[i];
  145. for (j = 0; j < 3; j++)
  146. {
  147. if (p1[j] > BOGUS_RANGE || p1[j] < -BOGUS_RANGE)
  148. {
  149. Error("Winding::Check : BOGUS_RANGE: %f", p1[j]);
  150. }
  151. }
  152. j = i + 1 == m_NumPoints ? 0 : i + 1;
  153. // check the point is on the face plane
  154. d = DotProduct(p1, facenormal) - facedist;
  155. if (d < -ON_EPSILON || d > ON_EPSILON)
  156. {
  157. Error("Winding::Check : point off plane");
  158. }
  159. // check the edge isn't degenerate
  160. p2 = m_Points[j];
  161. VectorSubtract(p2, p1, dir);
  162. if (VectorLength(dir) < ON_EPSILON)
  163. {
  164. Error("Winding::Check : degenerate edge");
  165. }
  166. CrossProduct(facenormal, dir, edgenormal);
  167. VectorNormalize(edgenormal);
  168. edgedist = DotProduct(p1, edgenormal);
  169. edgedist += ON_EPSILON;
  170. // all other points must be on front side
  171. for (j = 0; j < m_NumPoints; j++)
  172. {
  173. if (j == i)
  174. {
  175. continue;
  176. }
  177. d = DotProduct(m_Points[j], edgenormal);
  178. if (d > edgedist)
  179. {
  180. Error("Winding::Check : non-convex");
  181. }
  182. }
  183. }
  184. }
  185. bool Winding::Valid() const
  186. {
  187. // TODO: Check to ensure there are 3 non-colinear points
  188. if ((m_NumPoints < 3) || (!m_Points))
  189. {
  190. return false;
  191. }
  192. return true;
  193. }
  194. //
  195. // Construction
  196. //
  197. Winding::Winding()
  198. {
  199. m_Points = NULL;
  200. m_NumPoints = m_MaxPoints = 0;
  201. }
  202. Winding::Winding(vec3_t *points, UINT32 numpoints)
  203. {
  204. hlassert(numpoints >= 3);
  205. m_NumPoints = numpoints;
  206. m_MaxPoints = (m_NumPoints + 3) & ~3; // groups of 4
  207. m_Points = new vec3_t[m_MaxPoints];
  208. memcpy(m_Points, points, sizeof(vec3_t) * m_NumPoints);
  209. }
  210. void Winding::initFromPoints(vec3_t *points, UINT32 numpoints)
  211. {
  212. hlassert(numpoints >= 3);
  213. Reset();
  214. m_NumPoints = numpoints;
  215. m_MaxPoints = (m_NumPoints + 3) & ~3; // groups of 4
  216. m_Points = new vec3_t[m_MaxPoints];
  217. memcpy(m_Points, points, sizeof(vec3_t) * m_NumPoints);
  218. }
  219. Winding& Winding::operator=(const Winding& other)
  220. {
  221. delete[] m_Points;
  222. m_NumPoints = other.m_NumPoints;
  223. m_MaxPoints = (m_NumPoints + 3) & ~3; // groups of 4
  224. m_Points = new vec3_t[m_MaxPoints];
  225. memcpy(m_Points, other.m_Points, sizeof(vec3_t) * m_NumPoints);
  226. return *this;
  227. }
  228. Winding::Winding(UINT32 numpoints)
  229. {
  230. hlassert(numpoints >= 3);
  231. m_NumPoints = numpoints;
  232. m_MaxPoints = (m_NumPoints + 3) & ~3; // groups of 4
  233. m_Points = new vec3_t[m_MaxPoints];
  234. memset(m_Points, 0, sizeof(vec3_t) * m_NumPoints);
  235. }
  236. Winding::Winding(const Winding& other)
  237. {
  238. m_NumPoints = other.m_NumPoints;
  239. m_MaxPoints = (m_NumPoints + 3) & ~3; // groups of 4
  240. m_Points = new vec3_t[m_MaxPoints];
  241. memcpy(m_Points, other.m_Points, sizeof(vec3_t) * m_NumPoints);
  242. }
  243. Winding::~Winding()
  244. {
  245. delete[] m_Points;
  246. }
  247. void Winding::initFromPlane(const vec3_t normal, const vec_t dist)
  248. {
  249. int i;
  250. vec_t max, v;
  251. vec3_t org, vright, vup;
  252. // find the major axis
  253. max = -BOGUS_RANGE;
  254. int x = -1;
  255. for (i = 0; i < 3; i++)
  256. {
  257. v = fabs(normal[i]);
  258. if (v > max)
  259. {
  260. max = v;
  261. x = i;
  262. }
  263. }
  264. if (x == -1)
  265. {
  266. Error("Winding::initFromPlane no major axis found\n");
  267. }
  268. VectorCopy(vec3_origin, vup);
  269. switch (x)
  270. {
  271. case 0:
  272. case 1:
  273. vup[2] = 1;
  274. break;
  275. case 2:
  276. vup[0] = 1;
  277. break;
  278. }
  279. v = DotProduct(vup, normal);
  280. VectorMA(vup, -v, normal, vup);
  281. VectorNormalize(vup);
  282. VectorScale(normal, dist, org);
  283. CrossProduct(vup, normal, vright);
  284. VectorScale(vup, BOGUS_RANGE, vup);
  285. VectorScale(vright, BOGUS_RANGE, vright);
  286. // project a really big axis aligned box onto the plane
  287. m_NumPoints = 4;
  288. m_Points = new vec3_t[m_NumPoints];
  289. VectorSubtract(org, vright, m_Points[0]);
  290. VectorAdd(m_Points[0], vup, m_Points[0]);
  291. VectorAdd(org, vright, m_Points[1]);
  292. VectorAdd(m_Points[1], vup, m_Points[1]);
  293. VectorAdd(org, vright, m_Points[2]);
  294. VectorSubtract(m_Points[2], vup, m_Points[2]);
  295. VectorSubtract(org, vright, m_Points[3]);
  296. VectorSubtract(m_Points[3], vup, m_Points[3]);
  297. }
  298. Winding::Winding(const vec3_t normal, const vec_t dist)
  299. {
  300. initFromPlane(normal, dist);
  301. }
  302. Winding::Winding(const dface_t& face)
  303. {
  304. int se;
  305. dvertex_t* dv;
  306. int v;
  307. m_NumPoints = face.numedges;
  308. m_Points = new vec3_t[m_NumPoints];
  309. unsigned i;
  310. for (i = 0; i < face.numedges; i++)
  311. {
  312. se = g_dsurfedges[face.firstedge + i];
  313. if (se < 0)
  314. {
  315. v = g_dedges[-se].v[1];
  316. }
  317. else
  318. {
  319. v = g_dedges[se].v[0];
  320. }
  321. dv = &g_dvertexes[v];
  322. VectorCopy(dv->point, m_Points[i]);
  323. }
  324. RemoveColinearPoints();
  325. }
  326. Winding::Winding(const dplane_t& plane)
  327. {
  328. vec3_t normal;
  329. vec_t dist;
  330. VectorCopy(plane.normal, normal);
  331. dist = plane.dist;
  332. initFromPlane(normal, dist);
  333. }
  334. //
  335. // Specialized Functions
  336. //
  337. #ifdef COMMON_HULLU
  338. void Winding::RemoveColinearPoints()
  339. {
  340. unsigned int i;
  341. unsigned int nump;
  342. int j;
  343. vec3_t v1, v2;
  344. vec3_t p[128];
  345. //JK: Did the optimizations...
  346. if (m_NumPoints>1)
  347. {
  348. VectorSubtract(m_Points[0], m_Points[m_NumPoints - 1], v2);
  349. VectorNormalize(v2);
  350. }
  351. nump=0;
  352. for (i = 0; i < m_NumPoints; i++)
  353. {
  354. j = (i + 1) % m_NumPoints; // i + 1
  355. VectorSubtract(m_Points[i], m_Points[j], v1);
  356. VectorNormalize(v1);
  357. VectorAdd(v1, v2, v2);
  358. if (!VectorCompare(v2, vec3_origin))
  359. {
  360. VectorCopy(m_Points[i], p[nump]);
  361. nump++;
  362. }
  363. #if 0
  364. else
  365. {
  366. Log("v3 was (%4.3f %4.3f %4.3f)\n", v2[0], v2[1], v2[2]);
  367. }
  368. #endif
  369. //Set v2 for next round
  370. v2[0]=-v1[0];
  371. v2[1]=-v1[1];
  372. v2[2]=-v1[2];
  373. }
  374. if (nump == m_NumPoints)
  375. {
  376. return;
  377. }
  378. #if 0
  379. Warning("RemoveColinearPoints: Removed %u points, from %u to %u\n", m_NumPoints - nump, m_NumPoints, nump);
  380. Warning("Before :\n");
  381. Print();
  382. #endif
  383. m_NumPoints = nump;
  384. memcpy(m_Points, p, nump * sizeof(vec3_t));
  385. #if 0
  386. Warning("After :\n");
  387. Print();
  388. Warning("==========\n");
  389. #endif
  390. }
  391. #else /*COMMON_HULLU*/
  392. void Winding::RemoveColinearPoints()
  393. {
  394. unsigned int i;
  395. unsigned int nump;
  396. int j, k;
  397. vec3_t v1, v2, v3;
  398. vec3_t p[128];
  399. // TODO: OPTIMIZE: this could be 1/2 the number of vectornormalize calls by caching one of the previous values through the loop
  400. // TODO: OPTIMIZE: Remove the modulo operations inside the loop and replace with compares
  401. nump = 0;
  402. for (i = 0; i < m_NumPoints; i++)
  403. {
  404. j = (i + 1) % m_NumPoints; // i + 1
  405. k = (i + m_NumPoints - 1) % m_NumPoints; // i - 1
  406. VectorSubtract(m_Points[i], m_Points[j], v1);
  407. VectorSubtract(m_Points[i], m_Points[k], v2);
  408. VectorNormalize(v1);
  409. VectorNormalize(v2);
  410. VectorAdd(v1, v2, v3);
  411. if (!VectorCompare(v3, vec3_origin))
  412. {
  413. VectorCopy(m_Points[i], p[nump]);
  414. nump++;
  415. }
  416. #if 0
  417. else
  418. {
  419. Log("v3 was (%4.3f %4.3f %4.3f)\n", v3[0], v3[1], v3[2]);
  420. }
  421. #endif
  422. }
  423. if (nump == m_NumPoints)
  424. {
  425. return;
  426. }
  427. #if 0
  428. Warning("RemoveColinearPoints: Removed %u points, from %u to %u\n", m_NumPoints - nump, m_NumPoints, nump);
  429. Warning("Before :\n");
  430. Print();
  431. #endif
  432. m_NumPoints = nump;
  433. memcpy(m_Points, p, nump * sizeof(vec3_t));
  434. #if 0
  435. Warning("After :\n");
  436. Print();
  437. Warning("==========\n");
  438. #endif
  439. }
  440. #endif /*!COMMON_HULLU*/
  441. void Winding::Clip(const dplane_t& plane, Winding** front, Winding** back)
  442. {
  443. vec3_t normal;
  444. vec_t dist;
  445. VectorCopy(plane.normal, normal);
  446. dist = plane.dist;
  447. Clip(normal, dist, front, back);
  448. }
  449. void Winding::Clip(const vec3_t normal, const vec_t dist, Winding** front, Winding** back)
  450. {
  451. vec_t dists[MAX_POINTS_ON_WINDING + 4];
  452. int sides[MAX_POINTS_ON_WINDING + 4];
  453. int counts[3];
  454. vec_t dot;
  455. unsigned int i, j;
  456. unsigned int maxpts;
  457. counts[0] = counts[1] = counts[2] = 0;
  458. // determine sides for each point
  459. for (i = 0; i < m_NumPoints; i++)
  460. {
  461. dot = DotProduct(m_Points[i], normal);
  462. dot -= dist;
  463. dists[i] = dot;
  464. if (dot > ON_EPSILON)
  465. {
  466. sides[i] = SIDE_FRONT;
  467. }
  468. else if (dot < -ON_EPSILON)
  469. {
  470. sides[i] = SIDE_BACK;
  471. }
  472. else
  473. {
  474. sides[i] = SIDE_ON;
  475. }
  476. counts[sides[i]]++;
  477. }
  478. sides[i] = sides[0];
  479. dists[i] = dists[0];
  480. if (!counts[0])
  481. {
  482. *front = NULL;
  483. *back = new Winding(*this);
  484. return;
  485. }
  486. if (!counts[1])
  487. {
  488. *front = new Winding(*this);
  489. *back = NULL;
  490. return;
  491. }
  492. maxpts = m_NumPoints + 4; // can't use counts[0]+2 because
  493. // of fp grouping errors
  494. Winding* f = new Winding(maxpts);
  495. Winding* b = new Winding(maxpts);
  496. *front = f;
  497. *back = b;
  498. f->m_NumPoints = 0;
  499. b->m_NumPoints = 0;
  500. for (i = 0; i < m_NumPoints; i++)
  501. {
  502. vec_t* p1 = m_Points[i];
  503. if (sides[i] == SIDE_ON)
  504. {
  505. VectorCopy(p1, f->m_Points[f->m_NumPoints]);
  506. VectorCopy(p1, b->m_Points[b->m_NumPoints]);
  507. f->m_NumPoints++;
  508. b->m_NumPoints++;
  509. continue;
  510. }
  511. else if (sides[i] == SIDE_FRONT)
  512. {
  513. VectorCopy(p1, f->m_Points[f->m_NumPoints]);
  514. f->m_NumPoints++;
  515. }
  516. else if (sides[i] == SIDE_BACK)
  517. {
  518. VectorCopy(p1, b->m_Points[b->m_NumPoints]);
  519. b->m_NumPoints++;
  520. }
  521. if ((sides[i + 1] == SIDE_ON) | (sides[i + 1] == sides[i])) // | instead of || for branch optimization
  522. {
  523. continue;
  524. }
  525. // generate a split point
  526. vec3_t mid;
  527. unsigned int tmp = i + 1;
  528. if (tmp >= m_NumPoints)
  529. {
  530. tmp = 0;
  531. }
  532. vec_t* p2 = m_Points[tmp];
  533. dot = dists[i] / (dists[i] - dists[i + 1]);
  534. #if 0
  535. for (j = 0; j < 3; j++)
  536. { // avoid round off error when possible
  537. if (normal[j] < 1.0 - NORMAL_EPSILON)
  538. {
  539. if (normal[j] > -1.0 + NORMAL_EPSILON)
  540. {
  541. mid[j] = p1[j] + dot * (p2[j] - p1[j]);
  542. }
  543. else
  544. {
  545. mid[j] = -dist;
  546. }
  547. }
  548. else
  549. {
  550. mid[j] = dist;
  551. }
  552. }
  553. #else
  554. for (j = 0; j < 3; j++)
  555. { // avoid round off error when possible
  556. if (normal[j] == 1)
  557. mid[j] = dist;
  558. else if (normal[j] == -1)
  559. mid[j] = -dist;
  560. else
  561. mid[j] = p1[j] + dot * (p2[j] - p1[j]);
  562. }
  563. #endif
  564. VectorCopy(mid, f->m_Points[f->m_NumPoints]);
  565. VectorCopy(mid, b->m_Points[b->m_NumPoints]);
  566. f->m_NumPoints++;
  567. b->m_NumPoints++;
  568. }
  569. if ((f->m_NumPoints > maxpts) | (b->m_NumPoints > maxpts)) // | instead of || for branch optimization
  570. {
  571. Error("Winding::Clip : points exceeded estimate");
  572. }
  573. if ((f->m_NumPoints > MAX_POINTS_ON_WINDING) | (b->m_NumPoints > MAX_POINTS_ON_WINDING)) // | instead of || for branch optimization
  574. {
  575. Error("Winding::Clip : MAX_POINTS_ON_WINDING");
  576. }
  577. f->RemoveColinearPoints();
  578. b->RemoveColinearPoints();
  579. }
  580. bool Winding::Chop(const vec3_t normal, const vec_t dist)
  581. {
  582. Winding* f;
  583. Winding* b;
  584. Clip(normal, dist, &f, &b);
  585. if (b)
  586. {
  587. delete b;
  588. }
  589. if (f)
  590. {
  591. delete[] m_Points;
  592. m_NumPoints = f->m_NumPoints;
  593. m_Points = f->m_Points;
  594. f->m_Points = NULL;
  595. delete f;
  596. return true;
  597. }
  598. else
  599. {
  600. m_NumPoints = 0;
  601. delete[] m_Points;
  602. m_Points = NULL;
  603. return false;
  604. }
  605. }
  606. int Winding::WindingOnPlaneSide(const vec3_t normal, const vec_t dist)
  607. {
  608. bool front, back;
  609. unsigned int i;
  610. vec_t d;
  611. front = false;
  612. back = false;
  613. for (i = 0; i < m_NumPoints; i++)
  614. {
  615. d = DotProduct(m_Points[i], normal) - dist;
  616. if (d < -ON_EPSILON)
  617. {
  618. if (front)
  619. {
  620. return SIDE_CROSS;
  621. }
  622. back = true;
  623. continue;
  624. }
  625. if (d > ON_EPSILON)
  626. {
  627. if (back)
  628. {
  629. return SIDE_CROSS;
  630. }
  631. front = true;
  632. continue;
  633. }
  634. }
  635. if (back)
  636. {
  637. return SIDE_BACK;
  638. }
  639. if (front)
  640. {
  641. return SIDE_FRONT;
  642. }
  643. return SIDE_ON;
  644. }
  645. bool Winding::Clip(const dplane_t& split, bool keepon)
  646. {
  647. vec_t dists[MAX_POINTS_ON_WINDING];
  648. int sides[MAX_POINTS_ON_WINDING];
  649. int counts[3];
  650. vec_t dot;
  651. int i, j;
  652. counts[0] = counts[1] = counts[2] = 0;
  653. // determine sides for each point
  654. // do this exactly, with no epsilon so tiny portals still work
  655. for (i = 0; i < m_NumPoints; i++)
  656. {
  657. dot = DotProduct(m_Points[i], split.normal);
  658. dot -= split.dist;
  659. dists[i] = dot;
  660. if (dot > ON_EPSILON)
  661. {
  662. sides[i] = SIDE_FRONT;
  663. }
  664. else if (dot < ON_EPSILON)
  665. {
  666. sides[i] = SIDE_BACK;
  667. }
  668. else
  669. {
  670. sides[i] = SIDE_ON;
  671. }
  672. counts[sides[i]]++;
  673. }
  674. sides[i] = sides[0];
  675. dists[i] = dists[0];
  676. if (keepon && !counts[0] && !counts[1])
  677. {
  678. return true;
  679. }
  680. if (!counts[0])
  681. {
  682. delete[] m_Points;
  683. m_Points = NULL;
  684. m_NumPoints = 0;
  685. return false;
  686. }
  687. if (!counts[1])
  688. {
  689. return true;
  690. }
  691. unsigned maxpts = m_NumPoints + 4; // can't use counts[0]+2 because of fp grouping errors
  692. unsigned newNumPoints = 0;
  693. vec3_t* newPoints = new vec3_t[maxpts];
  694. memset(newPoints, 0, sizeof(vec3_t) * maxpts);
  695. for (i = 0; i < m_NumPoints; i++)
  696. {
  697. vec_t* p1 = m_Points[i];
  698. if (sides[i] == SIDE_ON)
  699. {
  700. VectorCopy(p1, newPoints[newNumPoints]);
  701. newNumPoints++;
  702. continue;
  703. }
  704. else if (sides[i] == SIDE_FRONT)
  705. {
  706. VectorCopy(p1, newPoints[newNumPoints]);
  707. newNumPoints++;
  708. }
  709. if (sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i])
  710. {
  711. continue;
  712. }
  713. // generate a split point
  714. vec3_t mid;
  715. unsigned int tmp = i + 1;
  716. if (tmp >= m_NumPoints)
  717. {
  718. tmp = 0;
  719. }
  720. vec_t* p2 = m_Points[tmp];
  721. dot = dists[i] / (dists[i] - dists[i + 1]);
  722. for (j = 0; j < 3; j++)
  723. { // avoid round off error when possible
  724. if (split.normal[j] < 1.0 - NORMAL_EPSILON)
  725. {
  726. if (split.normal[j] > -1.0 + NORMAL_EPSILON)
  727. {
  728. mid[j] = p1[j] + dot * (p2[j] - p1[j]);
  729. }
  730. else
  731. {
  732. mid[j] = -split.dist;
  733. }
  734. }
  735. else
  736. {
  737. mid[j] = split.dist;
  738. }
  739. }
  740. VectorCopy(mid, newPoints[newNumPoints]);
  741. newNumPoints++;
  742. }
  743. if (newNumPoints > maxpts)
  744. {
  745. Error("Winding::Clip : points exceeded estimate");
  746. }
  747. delete[] m_Points;
  748. m_Points = newPoints;
  749. m_NumPoints = newNumPoints;
  750. RemoveColinearPoints();
  751. return true;
  752. }
  753. /*
  754. * ==================
  755. * Divide
  756. * Divides a winding by a plane, producing one or two windings. The
  757. * original winding is not damaged or freed. If only on one side, the
  758. * returned winding will be the input winding. If on both sides, two
  759. * new windings will be created.
  760. * ==================
  761. */
  762. void Winding::Divide(const dplane_t& split, Winding** front, Winding** back)
  763. {
  764. vec_t dists[MAX_POINTS_ON_WINDING];
  765. int sides[MAX_POINTS_ON_WINDING];
  766. int counts[3];
  767. vec_t dot;
  768. int i, j;
  769. int maxpts;
  770. counts[0] = counts[1] = counts[2] = 0;
  771. // determine sides for each point
  772. for (i = 0; i < m_NumPoints; i++)
  773. {
  774. dot = DotProduct(m_Points[i], split.normal);
  775. dot -= split.dist;
  776. dists[i] = dot;
  777. if (dot > ON_EPSILON)
  778. {
  779. sides[i] = SIDE_FRONT;
  780. }
  781. else if (dot < -ON_EPSILON)
  782. {
  783. sides[i] = SIDE_BACK;
  784. }
  785. else
  786. {
  787. sides[i] = SIDE_ON;
  788. }
  789. counts[sides[i]]++;
  790. }
  791. sides[i] = sides[0];
  792. dists[i] = dists[0];
  793. *front = *back = NULL;
  794. if (!counts[0])
  795. {
  796. *back = this; // Makes this function non-const
  797. return;
  798. }
  799. if (!counts[1])
  800. {
  801. *front = this; // Makes this function non-const
  802. return;
  803. }
  804. maxpts = m_NumPoints + 4; // can't use counts[0]+2 because
  805. // of fp grouping errors
  806. Winding* f = new Winding(maxpts);
  807. Winding* b = new Winding(maxpts);
  808. *front = f;
  809. *back = b;
  810. f->m_NumPoints = 0;
  811. b->m_NumPoints = 0;
  812. for (i = 0; i < m_NumPoints; i++)
  813. {
  814. vec_t* p1 = m_Points[i];
  815. if (sides[i] == SIDE_ON)
  816. {
  817. VectorCopy(p1, f->m_Points[f->m_NumPoints]);
  818. VectorCopy(p1, b->m_Points[b->m_NumPoints]);
  819. f->m_NumPoints++;
  820. b->m_NumPoints++;
  821. continue;
  822. }
  823. else if (sides[i] == SIDE_FRONT)
  824. {
  825. VectorCopy(p1, f->m_Points[f->m_NumPoints]);
  826. f->m_NumPoints++;
  827. }
  828. else if (sides[i] == SIDE_BACK)
  829. {
  830. VectorCopy(p1, b->m_Points[b->m_NumPoints]);
  831. b->m_NumPoints++;
  832. }
  833. if (sides[i + 1] == SIDE_ON || sides[i + 1] == sides[i])
  834. {
  835. continue;
  836. }
  837. // generate a split point
  838. vec3_t mid;
  839. unsigned int tmp = i + 1;
  840. if (tmp >= m_NumPoints)
  841. {
  842. tmp = 0;
  843. }
  844. vec_t* p2 = m_Points[tmp];
  845. dot = dists[i] / (dists[i] - dists[i + 1]);
  846. for (j = 0; j < 3; j++)
  847. { // avoid round off error when possible
  848. if (split.normal[j] < 1.0 - NORMAL_EPSILON)
  849. {
  850. if (split.normal[j] > -1.0 + NORMAL_EPSILON)
  851. {
  852. mid[j] = p1[j] + dot * (p2[j] - p1[j]);
  853. }
  854. else
  855. {
  856. mid[j] = -split.dist;
  857. }
  858. }
  859. else
  860. {
  861. mid[j] = split.dist;
  862. }
  863. }
  864. VectorCopy(mid, f->m_Points[f->m_NumPoints]);
  865. VectorCopy(mid, b->m_Points[b->m_NumPoints]);
  866. f->m_NumPoints++;
  867. b->m_NumPoints++;
  868. }
  869. if (f->m_NumPoints > maxpts || b->m_NumPoints > maxpts)
  870. {
  871. Error("Winding::Divide : points exceeded estimate");
  872. }
  873. f->RemoveColinearPoints();
  874. b->RemoveColinearPoints();
  875. }
  876. void Winding::addPoint(const vec3_t newpoint)
  877. {
  878. if (m_NumPoints >= m_MaxPoints)
  879. {
  880. resize(m_NumPoints + 1);
  881. }
  882. VectorCopy(newpoint, m_Points[m_NumPoints]);
  883. m_NumPoints++;
  884. }
  885. void Winding::insertPoint(const vec3_t newpoint, const unsigned int offset)
  886. {
  887. if (offset >= m_NumPoints)
  888. {
  889. addPoint(newpoint);
  890. }
  891. else
  892. {
  893. if (m_NumPoints >= m_MaxPoints)
  894. {
  895. resize(m_NumPoints + 1);
  896. }
  897. unsigned x;
  898. for (x = m_NumPoints; x>offset; x--)
  899. {
  900. VectorCopy(m_Points[x-1], m_Points[x]);
  901. }
  902. VectorCopy(newpoint, m_Points[x]);
  903. m_NumPoints++;
  904. }
  905. }
  906. void Winding::resize(UINT32 newsize)
  907. {
  908. newsize = (newsize + 3) & ~3; // groups of 4
  909. vec3_t* newpoints = new vec3_t[newsize];
  910. m_NumPoints = min(newsize, m_NumPoints);
  911. memcpy(newpoints, m_Points, m_NumPoints);
  912. delete[] m_Points;
  913. m_Points = newpoints;
  914. m_MaxPoints = newsize;
  915. }
  916. void Winding::CopyPoints(vec3_t *points, int &numpoints)
  917. {
  918. if(!points)
  919. {
  920. numpoints = 0;
  921. return;
  922. }
  923. memcpy(points, m_Points, sizeof(vec3_t) * m_NumPoints);
  924. numpoints = m_NumPoints;
  925. }
  926. void Winding::Reset(void)
  927. {
  928. if(m_Points)
  929. {
  930. delete [] m_Points;
  931. m_Points = NULL;
  932. }
  933. m_NumPoints = m_MaxPoints = 0;
  934. }