Winding.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../precompiled.h"
  21. #pragma hdrstop
  22. //===============================================================
  23. //
  24. // idWinding
  25. //
  26. //===============================================================
  27. /*
  28. =============
  29. idWinding::ReAllocate
  30. =============
  31. */
  32. bool idWinding::ReAllocate( int n, bool keep ) {
  33. idVec5 *oldP;
  34. oldP = p;
  35. n = (n+3) & ~3; // align up to multiple of four
  36. p = new idVec5[n];
  37. if ( oldP ) {
  38. if ( keep ) {
  39. memcpy( p, oldP, numPoints * sizeof(p[0]) );
  40. }
  41. delete[] oldP;
  42. }
  43. allocedSize = n;
  44. return true;
  45. }
  46. /*
  47. =============
  48. idWinding::BaseForPlane
  49. =============
  50. */
  51. void idWinding::BaseForPlane( const idVec3 &normal, const float dist ) {
  52. idVec3 org, vright, vup;
  53. org = normal * dist;
  54. normal.NormalVectors( vup, vright );
  55. vup *= MAX_WORLD_SIZE;
  56. vright *= MAX_WORLD_SIZE;
  57. EnsureAlloced( 4 );
  58. numPoints = 4;
  59. p[0].ToVec3() = org - vright + vup;
  60. p[0].s = p[0].t = 0.0f;
  61. p[1].ToVec3() = org + vright + vup;
  62. p[1].s = p[1].t = 0.0f;
  63. p[2].ToVec3() = org + vright - vup;
  64. p[2].s = p[2].t = 0.0f;
  65. p[3].ToVec3() = org - vright - vup;
  66. p[3].s = p[3].t = 0.0f;
  67. }
  68. /*
  69. =============
  70. idWinding::Split
  71. =============
  72. */
  73. int idWinding::Split( const idPlane &plane, const float epsilon, idWinding **front, idWinding **back ) const {
  74. float * dists;
  75. byte * sides;
  76. int counts[3];
  77. float dot;
  78. int i, j;
  79. const idVec5 * p1, *p2;
  80. idVec5 mid;
  81. idWinding * f, *b;
  82. int maxpts;
  83. assert( this );
  84. dists = (float *) _alloca( (numPoints+4) * sizeof( float ) );
  85. sides = (byte *) _alloca( (numPoints+4) * sizeof( byte ) );
  86. counts[0] = counts[1] = counts[2] = 0;
  87. // determine sides for each point
  88. for ( i = 0; i < numPoints; i++ ) {
  89. dists[i] = dot = plane.Distance( p[i].ToVec3() );
  90. if ( dot > epsilon ) {
  91. sides[i] = SIDE_FRONT;
  92. } else if ( dot < -epsilon ) {
  93. sides[i] = SIDE_BACK;
  94. } else {
  95. sides[i] = SIDE_ON;
  96. }
  97. counts[sides[i]]++;
  98. }
  99. sides[i] = sides[0];
  100. dists[i] = dists[0];
  101. *front = *back = NULL;
  102. // if coplanar, put on the front side if the normals match
  103. if ( !counts[SIDE_FRONT] && !counts[SIDE_BACK] ) {
  104. idPlane windingPlane;
  105. GetPlane( windingPlane );
  106. if ( windingPlane.Normal() * plane.Normal() > 0.0f ) {
  107. *front = Copy();
  108. return SIDE_FRONT;
  109. } else {
  110. *back = Copy();
  111. return SIDE_BACK;
  112. }
  113. }
  114. // if nothing at the front of the clipping plane
  115. if ( !counts[SIDE_FRONT] ) {
  116. *back = Copy();
  117. return SIDE_BACK;
  118. }
  119. // if nothing at the back of the clipping plane
  120. if ( !counts[SIDE_BACK] ) {
  121. *front = Copy();
  122. return SIDE_FRONT;
  123. }
  124. maxpts = numPoints+4; // cant use counts[0]+2 because of fp grouping errors
  125. *front = f = new idWinding(maxpts);
  126. *back = b = new idWinding(maxpts);
  127. for (i = 0; i < numPoints; i++) {
  128. p1 = &p[i];
  129. if ( sides[i] == SIDE_ON ) {
  130. f->p[f->numPoints] = *p1;
  131. f->numPoints++;
  132. b->p[b->numPoints] = *p1;
  133. b->numPoints++;
  134. continue;
  135. }
  136. if ( sides[i] == SIDE_FRONT ) {
  137. f->p[f->numPoints] = *p1;
  138. f->numPoints++;
  139. }
  140. if ( sides[i] == SIDE_BACK ) {
  141. b->p[b->numPoints] = *p1;
  142. b->numPoints++;
  143. }
  144. if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) {
  145. continue;
  146. }
  147. // generate a split point
  148. p2 = &p[(i+1)%numPoints];
  149. // always calculate the split going from the same side
  150. // or minor epsilon issues can happen
  151. if ( sides[i] == SIDE_FRONT ) {
  152. dot = dists[i] / ( dists[i] - dists[i+1] );
  153. for ( j = 0; j < 3; j++ ) {
  154. // avoid round off error when possible
  155. if ( plane.Normal()[j] == 1.0f ) {
  156. mid[j] = plane.Dist();
  157. } else if ( plane.Normal()[j] == -1.0f ) {
  158. mid[j] = -plane.Dist();
  159. } else {
  160. mid[j] = (*p1)[j] + dot * ( (*p2)[j] - (*p1)[j] );
  161. }
  162. }
  163. mid.s = p1->s + dot * ( p2->s - p1->s );
  164. mid.t = p1->t + dot * ( p2->t - p1->t );
  165. } else {
  166. dot = dists[i+1] / ( dists[i+1] - dists[i] );
  167. for ( j = 0; j < 3; j++ ) {
  168. // avoid round off error when possible
  169. if ( plane.Normal()[j] == 1.0f ) {
  170. mid[j] = plane.Dist();
  171. } else if ( plane.Normal()[j] == -1.0f ) {
  172. mid[j] = -plane.Dist();
  173. } else {
  174. mid[j] = (*p2)[j] + dot * ( (*p1)[j] - (*p2)[j] );
  175. }
  176. }
  177. mid.s = p2->s + dot * ( p1->s - p2->s );
  178. mid.t = p2->t + dot * ( p1->t - p2->t );
  179. }
  180. f->p[f->numPoints] = mid;
  181. f->numPoints++;
  182. b->p[b->numPoints] = mid;
  183. b->numPoints++;
  184. }
  185. if ( f->numPoints > maxpts || b->numPoints > maxpts ) {
  186. idLib::common->FatalError( "idWinding::Split: points exceeded estimate." );
  187. }
  188. return SIDE_CROSS;
  189. }
  190. /*
  191. =============
  192. idWinding::Clip
  193. =============
  194. */
  195. idWinding *idWinding::Clip( const idPlane &plane, const float epsilon, const bool keepOn ) {
  196. float * dists;
  197. byte * sides;
  198. idVec5 * newPoints;
  199. int newNumPoints;
  200. int counts[3];
  201. float dot;
  202. int i, j;
  203. idVec5 * p1, *p2;
  204. idVec5 mid;
  205. int maxpts;
  206. assert( this );
  207. dists = (float *) _alloca( (numPoints+4) * sizeof( float ) );
  208. sides = (byte *) _alloca( (numPoints+4) * sizeof( byte ) );
  209. counts[SIDE_FRONT] = counts[SIDE_BACK] = counts[SIDE_ON] = 0;
  210. // determine sides for each point
  211. for ( i = 0; i < numPoints; i++ ) {
  212. dists[i] = dot = plane.Distance( p[i].ToVec3() );
  213. if ( dot > epsilon ) {
  214. sides[i] = SIDE_FRONT;
  215. } else if ( dot < -epsilon ) {
  216. sides[i] = SIDE_BACK;
  217. } else {
  218. sides[i] = SIDE_ON;
  219. }
  220. counts[sides[i]]++;
  221. }
  222. sides[i] = sides[0];
  223. dists[i] = dists[0];
  224. // if the winding is on the plane and we should keep it
  225. if ( keepOn && !counts[SIDE_FRONT] && !counts[SIDE_BACK] ) {
  226. return this;
  227. }
  228. // if nothing at the front of the clipping plane
  229. if ( !counts[SIDE_FRONT] ) {
  230. delete this;
  231. return NULL;
  232. }
  233. // if nothing at the back of the clipping plane
  234. if ( !counts[SIDE_BACK] ) {
  235. return this;
  236. }
  237. maxpts = numPoints + 4; // cant use counts[0]+2 because of fp grouping errors
  238. newPoints = (idVec5 *) _alloca16( maxpts * sizeof( idVec5 ) );
  239. newNumPoints = 0;
  240. for ( i = 0; i < numPoints; i++ ) {
  241. p1 = &p[i];
  242. if ( newNumPoints+1 > maxpts ) {
  243. return this; // can't split -- fall back to original
  244. }
  245. if ( sides[i] == SIDE_ON ) {
  246. newPoints[newNumPoints] = *p1;
  247. newNumPoints++;
  248. continue;
  249. }
  250. if ( sides[i] == SIDE_FRONT ) {
  251. newPoints[newNumPoints] = *p1;
  252. newNumPoints++;
  253. }
  254. if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) {
  255. continue;
  256. }
  257. if ( newNumPoints+1 > maxpts ) {
  258. return this; // can't split -- fall back to original
  259. }
  260. // generate a split point
  261. p2 = &p[(i+1)%numPoints];
  262. dot = dists[i] / (dists[i] - dists[i+1]);
  263. for ( j = 0; j < 3; j++ ) {
  264. // avoid round off error when possible
  265. if ( plane.Normal()[j] == 1.0f ) {
  266. mid[j] = plane.Dist();
  267. } else if ( plane.Normal()[j] == -1.0f ) {
  268. mid[j] = -plane.Dist();
  269. } else {
  270. mid[j] = (*p1)[j] + dot * ( (*p2)[j] - (*p1)[j] );
  271. }
  272. }
  273. mid.s = p1->s + dot * ( p2->s - p1->s );
  274. mid.t = p1->t + dot * ( p2->t - p1->t );
  275. newPoints[newNumPoints] = mid;
  276. newNumPoints++;
  277. }
  278. if ( !EnsureAlloced( newNumPoints, false ) ) {
  279. return this;
  280. }
  281. numPoints = newNumPoints;
  282. memcpy( p, newPoints, newNumPoints * sizeof(idVec5) );
  283. return this;
  284. }
  285. /*
  286. =============
  287. idWinding::ClipInPlace
  288. =============
  289. */
  290. bool idWinding::ClipInPlace( const idPlane &plane, const float epsilon, const bool keepOn ) {
  291. float* dists;
  292. byte * sides;
  293. idVec5 * newPoints;
  294. int newNumPoints;
  295. int counts[3];
  296. float dot;
  297. int i, j;
  298. idVec5 * p1, *p2;
  299. idVec5 mid;
  300. int maxpts;
  301. assert( this );
  302. dists = (float *) _alloca( (numPoints+4) * sizeof( float ) );
  303. sides = (byte *) _alloca( (numPoints+4) * sizeof( byte ) );
  304. counts[SIDE_FRONT] = counts[SIDE_BACK] = counts[SIDE_ON] = 0;
  305. // determine sides for each point
  306. for ( i = 0; i < numPoints; i++ ) {
  307. dists[i] = dot = plane.Distance( p[i].ToVec3() );
  308. if ( dot > epsilon ) {
  309. sides[i] = SIDE_FRONT;
  310. } else if ( dot < -epsilon ) {
  311. sides[i] = SIDE_BACK;
  312. } else {
  313. sides[i] = SIDE_ON;
  314. }
  315. counts[sides[i]]++;
  316. }
  317. sides[i] = sides[0];
  318. dists[i] = dists[0];
  319. // if the winding is on the plane and we should keep it
  320. if ( keepOn && !counts[SIDE_FRONT] && !counts[SIDE_BACK] ) {
  321. return true;
  322. }
  323. // if nothing at the front of the clipping plane
  324. if ( !counts[SIDE_FRONT] ) {
  325. numPoints = 0;
  326. return false;
  327. }
  328. // if nothing at the back of the clipping plane
  329. if ( !counts[SIDE_BACK] ) {
  330. return true;
  331. }
  332. maxpts = numPoints + 4; // cant use counts[0]+2 because of fp grouping errors
  333. newPoints = (idVec5 *) _alloca16( maxpts * sizeof( idVec5 ) );
  334. newNumPoints = 0;
  335. for ( i = 0; i < numPoints; i++ ) {
  336. p1 = &p[i];
  337. if ( newNumPoints+1 > maxpts ) {
  338. return true; // can't split -- fall back to original
  339. }
  340. if ( sides[i] == SIDE_ON ) {
  341. newPoints[newNumPoints] = *p1;
  342. newNumPoints++;
  343. continue;
  344. }
  345. if ( sides[i] == SIDE_FRONT ) {
  346. newPoints[newNumPoints] = *p1;
  347. newNumPoints++;
  348. }
  349. if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) {
  350. continue;
  351. }
  352. if ( newNumPoints+1 > maxpts ) {
  353. return true; // can't split -- fall back to original
  354. }
  355. // generate a split point
  356. p2 = &p[(i+1)%numPoints];
  357. dot = dists[i] / (dists[i] - dists[i+1]);
  358. for ( j = 0; j < 3; j++ ) {
  359. // avoid round off error when possible
  360. if ( plane.Normal()[j] == 1.0f ) {
  361. mid[j] = plane.Dist();
  362. } else if ( plane.Normal()[j] == -1.0f ) {
  363. mid[j] = -plane.Dist();
  364. } else {
  365. mid[j] = (*p1)[j] + dot * ( (*p2)[j] - (*p1)[j] );
  366. }
  367. }
  368. mid.s = p1->s + dot * ( p2->s - p1->s );
  369. mid.t = p1->t + dot * ( p2->t - p1->t );
  370. newPoints[newNumPoints] = mid;
  371. newNumPoints++;
  372. }
  373. if ( !EnsureAlloced( newNumPoints, false ) ) {
  374. return true;
  375. }
  376. numPoints = newNumPoints;
  377. memcpy( p, newPoints, newNumPoints * sizeof(idVec5) );
  378. return true;
  379. }
  380. /*
  381. =============
  382. idWinding::Copy
  383. =============
  384. */
  385. idWinding *idWinding::Copy( void ) const {
  386. idWinding *w;
  387. w = new idWinding( numPoints );
  388. w->numPoints = numPoints;
  389. memcpy( w->p, p, numPoints * sizeof(p[0]) );
  390. return w;
  391. }
  392. /*
  393. =============
  394. idWinding::Reverse
  395. =============
  396. */
  397. idWinding *idWinding::Reverse( void ) const {
  398. idWinding *w;
  399. int i;
  400. w = new idWinding( numPoints );
  401. w->numPoints = numPoints;
  402. for ( i = 0; i < numPoints; i++ ) {
  403. w->p[ numPoints - i - 1 ] = p[i];
  404. }
  405. return w;
  406. }
  407. /*
  408. =============
  409. idWinding::ReverseSelf
  410. =============
  411. */
  412. void idWinding::ReverseSelf( void ) {
  413. idVec5 v;
  414. int i;
  415. for ( i = 0; i < (numPoints>>1); i++ ) {
  416. v = p[i];
  417. p[i] = p[numPoints - i - 1];
  418. p[numPoints - i - 1] = v;
  419. }
  420. }
  421. /*
  422. =============
  423. idWinding::Check
  424. =============
  425. */
  426. bool idWinding::Check( bool print ) const {
  427. int i, j;
  428. float d, edgedist;
  429. idVec3 dir, edgenormal;
  430. float area;
  431. idPlane plane;
  432. if ( numPoints < 3 ) {
  433. if ( print ) {
  434. idLib::common->Printf( "idWinding::Check: only %i points.", numPoints );
  435. }
  436. return false;
  437. }
  438. area = GetArea();
  439. if ( area < 1.0f ) {
  440. if ( print ) {
  441. idLib::common->Printf( "idWinding::Check: tiny area: %f", area );
  442. }
  443. return false;
  444. }
  445. GetPlane( plane );
  446. for ( i = 0; i < numPoints; i++ ) {
  447. const idVec3 &p1 = p[i].ToVec3();
  448. // check if the winding is huge
  449. for ( j = 0; j < 3; j++ ) {
  450. if ( p1[j] >= MAX_WORLD_COORD || p1[j] <= MIN_WORLD_COORD ) {
  451. if ( print ) {
  452. idLib::common->Printf( "idWinding::Check: point %d outside world %c-axis: %f", i, 'X'+j, p1[j] );
  453. }
  454. return false;
  455. }
  456. }
  457. j = i + 1 == numPoints ? 0 : i + 1;
  458. // check if the point is on the face plane
  459. d = p1 * plane.Normal() + plane[3];
  460. if ( d < -ON_EPSILON || d > ON_EPSILON ) {
  461. if ( print ) {
  462. idLib::common->Printf( "idWinding::Check: point %d off plane.", i );
  463. }
  464. return false;
  465. }
  466. // check if the edge isn't degenerate
  467. const idVec3 &p2 = p[j].ToVec3();
  468. dir = p2 - p1;
  469. if ( dir.Length() < ON_EPSILON) {
  470. if ( print ) {
  471. idLib::common->Printf( "idWinding::Check: edge %d is degenerate.", i );
  472. }
  473. return false;
  474. }
  475. // check if the winding is convex
  476. edgenormal = plane.Normal().Cross( dir );
  477. edgenormal.Normalize();
  478. edgedist = p1 * edgenormal;
  479. edgedist += ON_EPSILON;
  480. // all other points must be on front side
  481. for ( j = 0; j < numPoints; j++ ) {
  482. if ( j == i ) {
  483. continue;
  484. }
  485. d = p[j].ToVec3() * edgenormal;
  486. if ( d > edgedist ) {
  487. if ( print ) {
  488. idLib::common->Printf( "idWinding::Check: non-convex." );
  489. }
  490. return false;
  491. }
  492. }
  493. }
  494. return true;
  495. }
  496. /*
  497. =============
  498. idWinding::GetArea
  499. =============
  500. */
  501. float idWinding::GetArea( void ) const {
  502. int i;
  503. idVec3 d1, d2, cross;
  504. float total;
  505. total = 0.0f;
  506. for ( i = 2; i < numPoints; i++ ) {
  507. d1 = p[i-1].ToVec3() - p[0].ToVec3();
  508. d2 = p[i].ToVec3() - p[0].ToVec3();
  509. cross = d1.Cross( d2 );
  510. total += cross.Length();
  511. }
  512. return total * 0.5f;
  513. }
  514. /*
  515. =============
  516. idWinding::GetRadius
  517. =============
  518. */
  519. float idWinding::GetRadius( const idVec3 &center ) const {
  520. int i;
  521. float radius, r;
  522. idVec3 dir;
  523. radius = 0.0f;
  524. for ( i = 0; i < numPoints; i++ ) {
  525. dir = p[i].ToVec3() - center;
  526. r = dir * dir;
  527. if ( r > radius ) {
  528. radius = r;
  529. }
  530. }
  531. return idMath::Sqrt( radius );
  532. }
  533. /*
  534. =============
  535. idWinding::GetCenter
  536. =============
  537. */
  538. idVec3 idWinding::GetCenter( void ) const {
  539. int i;
  540. idVec3 center;
  541. center.Zero();
  542. for ( i = 0; i < numPoints; i++ ) {
  543. center += p[i].ToVec3();
  544. }
  545. center *= ( 1.0f / numPoints );
  546. return center;
  547. }
  548. /*
  549. =============
  550. idWinding::GetPlane
  551. =============
  552. */
  553. void idWinding::GetPlane( idVec3 &normal, float &dist ) const {
  554. idVec3 v1, v2, center;
  555. if ( numPoints < 3 ) {
  556. normal.Zero();
  557. dist = 0.0f;
  558. return;
  559. }
  560. center = GetCenter();
  561. v1 = p[0].ToVec3() - center;
  562. v2 = p[1].ToVec3() - center;
  563. normal = v2.Cross( v1 );
  564. normal.Normalize();
  565. dist = p[0].ToVec3() * normal;
  566. }
  567. /*
  568. =============
  569. idWinding::GetPlane
  570. =============
  571. */
  572. void idWinding::GetPlane( idPlane &plane ) const {
  573. idVec3 v1, v2;
  574. idVec3 center;
  575. if ( numPoints < 3 ) {
  576. plane.Zero();
  577. return;
  578. }
  579. center = GetCenter();
  580. v1 = p[0].ToVec3() - center;
  581. v2 = p[1].ToVec3() - center;
  582. plane.SetNormal( v2.Cross( v1 ) );
  583. plane.Normalize();
  584. plane.FitThroughPoint( p[0].ToVec3() );
  585. }
  586. /*
  587. =============
  588. idWinding::GetBounds
  589. =============
  590. */
  591. void idWinding::GetBounds( idBounds &bounds ) const {
  592. int i;
  593. if ( !numPoints ) {
  594. bounds.Clear();
  595. return;
  596. }
  597. bounds[0] = bounds[1] = p[0].ToVec3();
  598. for ( i = 1; i < numPoints; i++ ) {
  599. if ( p[i].x < bounds[0].x ) {
  600. bounds[0].x = p[i].x;
  601. } else if ( p[i].x > bounds[1].x ) {
  602. bounds[1].x = p[i].x;
  603. }
  604. if ( p[i].y < bounds[0].y ) {
  605. bounds[0].y = p[i].y;
  606. } else if ( p[i].y > bounds[1].y ) {
  607. bounds[1].y = p[i].y;
  608. }
  609. if ( p[i].z < bounds[0].z ) {
  610. bounds[0].z = p[i].z;
  611. } else if ( p[i].z > bounds[1].z ) {
  612. bounds[1].z = p[i].z;
  613. }
  614. }
  615. }
  616. /*
  617. =============
  618. idWinding::RemoveEqualPoints
  619. =============
  620. */
  621. void idWinding::RemoveEqualPoints( const float epsilon ) {
  622. int i, j;
  623. for ( i = 0; i < numPoints; i++ ) {
  624. if ( (p[i].ToVec3() - p[(i+numPoints-1)%numPoints].ToVec3()).LengthSqr() >= Square( epsilon ) ) {
  625. continue;
  626. }
  627. numPoints--;
  628. for ( j = i; j < numPoints; j++ ) {
  629. p[j] = p[j+1];
  630. }
  631. i--;
  632. }
  633. }
  634. /*
  635. =============
  636. idWinding::RemoveColinearPoints
  637. =============
  638. */
  639. void idWinding::RemoveColinearPoints( const idVec3 &normal, const float epsilon ) {
  640. int i, j;
  641. idVec3 edgeNormal;
  642. float dist;
  643. if ( numPoints <= 3 ) {
  644. return;
  645. }
  646. for ( i = 0; i < numPoints; i++ ) {
  647. // create plane through edge orthogonal to winding plane
  648. edgeNormal = (p[i].ToVec3() - p[(i+numPoints-1)%numPoints].ToVec3()).Cross( normal );
  649. edgeNormal.Normalize();
  650. dist = edgeNormal * p[i].ToVec3();
  651. if ( idMath::Fabs( edgeNormal * p[(i+1)%numPoints].ToVec3() - dist ) > epsilon ) {
  652. continue;
  653. }
  654. numPoints--;
  655. for ( j = i; j < numPoints; j++ ) {
  656. p[j] = p[j+1];
  657. }
  658. i--;
  659. }
  660. }
  661. /*
  662. =============
  663. idWinding::AddToConvexHull
  664. Adds the given winding to the convex hull.
  665. Assumes the current winding already is a convex hull with three or more points.
  666. =============
  667. */
  668. void idWinding::AddToConvexHull( const idWinding *winding, const idVec3 &normal, const float epsilon ) {
  669. int i, j, k;
  670. idVec3 dir;
  671. float d;
  672. int maxPts;
  673. idVec3 * hullDirs;
  674. bool * hullSide;
  675. bool outside;
  676. int numNewHullPoints;
  677. idVec5 * newHullPoints;
  678. if ( !winding ) {
  679. return;
  680. }
  681. maxPts = this->numPoints + winding->numPoints;
  682. if ( !this->EnsureAlloced( maxPts, true ) ) {
  683. return;
  684. }
  685. newHullPoints = (idVec5 *) _alloca( maxPts * sizeof( idVec5 ) );
  686. hullDirs = (idVec3 *) _alloca( maxPts * sizeof( idVec3 ) );
  687. hullSide = (bool *) _alloca( maxPts * sizeof( bool ) );
  688. for ( i = 0; i < winding->numPoints; i++ ) {
  689. const idVec5 &p1 = winding->p[i];
  690. // calculate hull edge vectors
  691. for ( j = 0; j < this->numPoints; j++ ) {
  692. dir = this->p[ (j + 1) % this->numPoints ].ToVec3() - this->p[ j ].ToVec3();
  693. dir.Normalize();
  694. hullDirs[j] = normal.Cross( dir );
  695. }
  696. // calculate side for each hull edge
  697. outside = false;
  698. for ( j = 0; j < this->numPoints; j++ ) {
  699. dir = p1.ToVec3() - this->p[j].ToVec3();
  700. d = dir * hullDirs[j];
  701. if ( d >= epsilon ) {
  702. outside = true;
  703. }
  704. if ( d >= -epsilon ) {
  705. hullSide[j] = true;
  706. } else {
  707. hullSide[j] = false;
  708. }
  709. }
  710. // if the point is effectively inside, do nothing
  711. if ( !outside ) {
  712. continue;
  713. }
  714. // find the back side to front side transition
  715. for ( j = 0; j < this->numPoints; j++ ) {
  716. if ( !hullSide[ j ] && hullSide[ (j + 1) % this->numPoints ] ) {
  717. break;
  718. }
  719. }
  720. if ( j >= this->numPoints ) {
  721. continue;
  722. }
  723. // insert the point here
  724. newHullPoints[0] = p1;
  725. numNewHullPoints = 1;
  726. // copy over all points that aren't double fronts
  727. j = (j+1) % this->numPoints;
  728. for ( k = 0; k < this->numPoints; k++ ) {
  729. if ( hullSide[ (j+k) % this->numPoints ] && hullSide[ (j+k+1) % this->numPoints ] ) {
  730. continue;
  731. }
  732. newHullPoints[numNewHullPoints] = this->p[ (j+k+1) % this->numPoints ];
  733. numNewHullPoints++;
  734. }
  735. this->numPoints = numNewHullPoints;
  736. memcpy( this->p, newHullPoints, numNewHullPoints * sizeof(idVec5) );
  737. }
  738. }
  739. /*
  740. =============
  741. idWinding::AddToConvexHull
  742. Add a point to the convex hull.
  743. The current winding must be convex but may be degenerate and can have less than three points.
  744. =============
  745. */
  746. void idWinding::AddToConvexHull( const idVec3 &point, const idVec3 &normal, const float epsilon ) {
  747. int j, k, numHullPoints;
  748. idVec3 dir;
  749. float d;
  750. idVec3 * hullDirs;
  751. bool * hullSide;
  752. idVec5 * hullPoints;
  753. bool outside;
  754. switch( numPoints ) {
  755. case 0: {
  756. p[0] = point;
  757. numPoints++;
  758. return;
  759. }
  760. case 1: {
  761. // don't add the same point second
  762. if ( p[0].ToVec3().Compare( point, epsilon ) ) {
  763. return;
  764. }
  765. p[1].ToVec3() = point;
  766. numPoints++;
  767. return;
  768. }
  769. case 2: {
  770. // don't add a point if it already exists
  771. if ( p[0].ToVec3().Compare( point, epsilon ) || p[1].ToVec3().Compare( point, epsilon ) ) {
  772. return;
  773. }
  774. // if only two points make sure we have the right ordering according to the normal
  775. dir = point - p[0].ToVec3();
  776. dir = dir.Cross( p[1].ToVec3() - p[0].ToVec3() );
  777. if ( dir[0] == 0.0f && dir[1] == 0.0f && dir[2] == 0.0f ) {
  778. // points don't make a plane
  779. return;
  780. }
  781. if ( dir * normal > 0.0f ) {
  782. p[2].ToVec3() = point;
  783. }
  784. else {
  785. p[2] = p[1];
  786. p[1].ToVec3() = point;
  787. }
  788. numPoints++;
  789. return;
  790. }
  791. }
  792. hullDirs = (idVec3 *) _alloca( numPoints * sizeof( idVec3 ) );
  793. hullSide = (bool *) _alloca( numPoints * sizeof( bool ) );
  794. // calculate hull edge vectors
  795. for ( j = 0; j < numPoints; j++ ) {
  796. dir = p[(j + 1) % numPoints].ToVec3() - p[j].ToVec3();
  797. hullDirs[j] = normal.Cross( dir );
  798. }
  799. // calculate side for each hull edge
  800. outside = false;
  801. for ( j = 0; j < numPoints; j++ ) {
  802. dir = point - p[j].ToVec3();
  803. d = dir * hullDirs[j];
  804. if ( d >= epsilon ) {
  805. outside = true;
  806. }
  807. if ( d >= -epsilon ) {
  808. hullSide[j] = true;
  809. } else {
  810. hullSide[j] = false;
  811. }
  812. }
  813. // if the point is effectively inside, do nothing
  814. if ( !outside ) {
  815. return;
  816. }
  817. // find the back side to front side transition
  818. for ( j = 0; j < numPoints; j++ ) {
  819. if ( !hullSide[ j ] && hullSide[ (j + 1) % numPoints ] ) {
  820. break;
  821. }
  822. }
  823. if ( j >= numPoints ) {
  824. return;
  825. }
  826. hullPoints = (idVec5 *) _alloca( (numPoints+1) * sizeof( idVec5 ) );
  827. // insert the point here
  828. hullPoints[0] = point;
  829. numHullPoints = 1;
  830. // copy over all points that aren't double fronts
  831. j = (j+1) % numPoints;
  832. for ( k = 0; k < numPoints; k++ ) {
  833. if ( hullSide[ (j+k) % numPoints ] && hullSide[ (j+k+1) % numPoints ] ) {
  834. continue;
  835. }
  836. hullPoints[numHullPoints] = p[ (j+k+1) % numPoints ];
  837. numHullPoints++;
  838. }
  839. if ( !EnsureAlloced( numHullPoints, false ) ) {
  840. return;
  841. }
  842. numPoints = numHullPoints;
  843. memcpy( p, hullPoints, numHullPoints * sizeof(idVec5) );
  844. }
  845. /*
  846. =============
  847. idWinding::TryMerge
  848. =============
  849. */
  850. #define CONTINUOUS_EPSILON 0.005f
  851. idWinding *idWinding::TryMerge( const idWinding &w, const idVec3 &planenormal, int keep ) const {
  852. idVec3 *p1, *p2, *p3, *p4, *back;
  853. idWinding *newf;
  854. const idWinding *f1, *f2;
  855. int i, j, k, l;
  856. idVec3 normal, delta;
  857. float dot;
  858. bool keep1, keep2;
  859. f1 = this;
  860. f2 = &w;
  861. //
  862. // find a idLib::common edge
  863. //
  864. p1 = p2 = NULL; // stop compiler warning
  865. j = 0;
  866. for ( i = 0; i < f1->numPoints; i++ ) {
  867. p1 = &f1->p[i].ToVec3();
  868. p2 = &f1->p[(i+1) % f1->numPoints].ToVec3();
  869. for ( j = 0; j < f2->numPoints; j++ ) {
  870. p3 = &f2->p[j].ToVec3();
  871. p4 = &f2->p[(j+1) % f2->numPoints].ToVec3();
  872. for (k = 0; k < 3; k++ ) {
  873. if ( idMath::Fabs((*p1)[k] - (*p4)[k]) > 0.1f ) {
  874. break;
  875. }
  876. if ( idMath::Fabs((*p2)[k] - (*p3)[k]) > 0.1f ) {
  877. break;
  878. }
  879. }
  880. if ( k == 3 ) {
  881. break;
  882. }
  883. }
  884. if ( j < f2->numPoints ) {
  885. break;
  886. }
  887. }
  888. if ( i == f1->numPoints ) {
  889. return NULL; // no matching edges
  890. }
  891. //
  892. // check slope of connected lines
  893. // if the slopes are colinear, the point can be removed
  894. //
  895. back = &f1->p[(i+f1->numPoints-1)%f1->numPoints].ToVec3();
  896. delta = (*p1) - (*back);
  897. normal = planenormal.Cross(delta);
  898. normal.Normalize();
  899. back = &f2->p[(j+2)%f2->numPoints].ToVec3();
  900. delta = (*back) - (*p1);
  901. dot = delta * normal;
  902. if ( dot > CONTINUOUS_EPSILON ) {
  903. return NULL; // not a convex polygon
  904. }
  905. keep1 = (bool)(dot < -CONTINUOUS_EPSILON);
  906. back = &f1->p[(i+2)%f1->numPoints].ToVec3();
  907. delta = (*back) - (*p2);
  908. normal = planenormal.Cross( delta );
  909. normal.Normalize();
  910. back = &f2->p[(j+f2->numPoints-1)%f2->numPoints].ToVec3();
  911. delta = (*back) - (*p2);
  912. dot = delta * normal;
  913. if ( dot > CONTINUOUS_EPSILON ) {
  914. return NULL; // not a convex polygon
  915. }
  916. keep2 = (bool)(dot < -CONTINUOUS_EPSILON);
  917. //
  918. // build the new polygon
  919. //
  920. newf = new idWinding( f1->numPoints + f2->numPoints );
  921. // copy first polygon
  922. for ( k = (i+1) % f1->numPoints; k != i; k = (k+1) % f1->numPoints ) {
  923. if ( !keep && k == (i+1) % f1->numPoints && !keep2 ) {
  924. continue;
  925. }
  926. newf->p[newf->numPoints] = f1->p[k];
  927. newf->numPoints++;
  928. }
  929. // copy second polygon
  930. for ( l = (j+1) % f2->numPoints; l != j; l = (l+1) % f2->numPoints ) {
  931. if ( !keep && l == (j+1) % f2->numPoints && !keep1 ) {
  932. continue;
  933. }
  934. newf->p[newf->numPoints] = f2->p[l];
  935. newf->numPoints++;
  936. }
  937. return newf;
  938. }
  939. /*
  940. =============
  941. idWinding::RemovePoint
  942. =============
  943. */
  944. void idWinding::RemovePoint( int point ) {
  945. if ( point < 0 || point >= numPoints ) {
  946. idLib::common->FatalError( "idWinding::removePoint: point out of range" );
  947. }
  948. if ( point < numPoints - 1) {
  949. memmove(&p[point], &p[point+1], (numPoints - point - 1) * sizeof(p[0]) );
  950. }
  951. numPoints--;
  952. }
  953. /*
  954. =============
  955. idWinding::InsertPoint
  956. =============
  957. */
  958. void idWinding::InsertPoint( const idVec3 &point, int spot ) {
  959. int i;
  960. if ( spot > numPoints ) {
  961. idLib::common->FatalError( "idWinding::insertPoint: spot > numPoints" );
  962. }
  963. if ( spot < 0 ) {
  964. idLib::common->FatalError( "idWinding::insertPoint: spot < 0" );
  965. }
  966. EnsureAlloced( numPoints+1, true );
  967. for ( i = numPoints; i > spot; i-- ) {
  968. p[i] = p[i-1];
  969. }
  970. p[spot] = point;
  971. numPoints++;
  972. }
  973. /*
  974. =============
  975. idWinding::InsertPointIfOnEdge
  976. =============
  977. */
  978. bool idWinding::InsertPointIfOnEdge( const idVec3 &point, const idPlane &plane, const float epsilon ) {
  979. int i;
  980. float dist, dot;
  981. idVec3 normal;
  982. // point may not be too far from the winding plane
  983. if ( idMath::Fabs( plane.Distance( point ) ) > epsilon ) {
  984. return false;
  985. }
  986. for ( i = 0; i < numPoints; i++ ) {
  987. // create plane through edge orthogonal to winding plane
  988. normal = (p[(i+1)%numPoints].ToVec3() - p[i].ToVec3()).Cross( plane.Normal() );
  989. normal.Normalize();
  990. dist = normal * p[i].ToVec3();
  991. if ( idMath::Fabs( normal * point - dist ) > epsilon ) {
  992. continue;
  993. }
  994. normal = plane.Normal().Cross( normal );
  995. dot = normal * point;
  996. dist = dot - normal * p[i].ToVec3();
  997. if ( dist < epsilon ) {
  998. // if the winding already has the point
  999. if ( dist > -epsilon ) {
  1000. return false;
  1001. }
  1002. continue;
  1003. }
  1004. dist = dot - normal * p[(i+1)%numPoints].ToVec3();
  1005. if ( dist > -epsilon ) {
  1006. // if the winding already has the point
  1007. if ( dist < epsilon ) {
  1008. return false;
  1009. }
  1010. continue;
  1011. }
  1012. InsertPoint( point, i+1 );
  1013. return true;
  1014. }
  1015. return false;
  1016. }
  1017. /*
  1018. =============
  1019. idWinding::IsTiny
  1020. =============
  1021. */
  1022. #define EDGE_LENGTH 0.2f
  1023. bool idWinding::IsTiny( void ) const {
  1024. int i;
  1025. float len;
  1026. idVec3 delta;
  1027. int edges;
  1028. edges = 0;
  1029. for ( i = 0; i < numPoints; i++ ) {
  1030. delta = p[(i+1)%numPoints].ToVec3() - p[i].ToVec3();
  1031. len = delta.Length();
  1032. if ( len > EDGE_LENGTH ) {
  1033. if ( ++edges == 3 ) {
  1034. return false;
  1035. }
  1036. }
  1037. }
  1038. return true;
  1039. }
  1040. /*
  1041. =============
  1042. idWinding::IsHuge
  1043. =============
  1044. */
  1045. bool idWinding::IsHuge( void ) const {
  1046. int i, j;
  1047. for ( i = 0; i < numPoints; i++ ) {
  1048. for ( j = 0; j < 3; j++ ) {
  1049. if ( p[i][j] <= MIN_WORLD_COORD || p[i][j] >= MAX_WORLD_COORD ) {
  1050. return true;
  1051. }
  1052. }
  1053. }
  1054. return false;
  1055. }
  1056. /*
  1057. =============
  1058. idWinding::Print
  1059. =============
  1060. */
  1061. void idWinding::Print( void ) const {
  1062. int i;
  1063. for ( i = 0; i < numPoints; i++ ) {
  1064. idLib::common->Printf( "(%5.1f, %5.1f, %5.1f)\n", p[i][0], p[i][1], p[i][2] );
  1065. }
  1066. }
  1067. /*
  1068. =============
  1069. idWinding::PlaneDistance
  1070. =============
  1071. */
  1072. float idWinding::PlaneDistance( const idPlane &plane ) const {
  1073. int i;
  1074. float d, min, max;
  1075. min = idMath::INFINITY;
  1076. max = -min;
  1077. for ( i = 0; i < numPoints; i++ ) {
  1078. d = plane.Distance( p[i].ToVec3() );
  1079. if ( d < min ) {
  1080. min = d;
  1081. if ( FLOATSIGNBITSET( min ) & FLOATSIGNBITNOTSET( max ) ) {
  1082. return 0.0f;
  1083. }
  1084. }
  1085. if ( d > max ) {
  1086. max = d;
  1087. if ( FLOATSIGNBITSET( min ) & FLOATSIGNBITNOTSET( max ) ) {
  1088. return 0.0f;
  1089. }
  1090. }
  1091. }
  1092. if ( FLOATSIGNBITNOTSET( min ) ) {
  1093. return min;
  1094. }
  1095. if ( FLOATSIGNBITSET( max ) ) {
  1096. return max;
  1097. }
  1098. return 0.0f;
  1099. }
  1100. /*
  1101. =============
  1102. idWinding::PlaneSide
  1103. =============
  1104. */
  1105. int idWinding::PlaneSide( const idPlane &plane, const float epsilon ) const {
  1106. bool front, back;
  1107. int i;
  1108. float d;
  1109. front = false;
  1110. back = false;
  1111. for ( i = 0; i < numPoints; i++ ) {
  1112. d = plane.Distance( p[i].ToVec3() );
  1113. if ( d < -epsilon ) {
  1114. if ( front ) {
  1115. return SIDE_CROSS;
  1116. }
  1117. back = true;
  1118. continue;
  1119. }
  1120. else if ( d > epsilon ) {
  1121. if ( back ) {
  1122. return SIDE_CROSS;
  1123. }
  1124. front = true;
  1125. continue;
  1126. }
  1127. }
  1128. if ( back ) {
  1129. return SIDE_BACK;
  1130. }
  1131. if ( front ) {
  1132. return SIDE_FRONT;
  1133. }
  1134. return SIDE_ON;
  1135. }
  1136. /*
  1137. =============
  1138. idWinding::PlanesConcave
  1139. =============
  1140. */
  1141. #define WCONVEX_EPSILON 0.2f
  1142. bool idWinding::PlanesConcave( const idWinding &w2, const idVec3 &normal1, const idVec3 &normal2, float dist1, float dist2 ) const {
  1143. int i;
  1144. // check if one of the points of winding 1 is at the back of the plane of winding 2
  1145. for ( i = 0; i < numPoints; i++ ) {
  1146. if ( normal2 * p[i].ToVec3() - dist2 > WCONVEX_EPSILON ) {
  1147. return true;
  1148. }
  1149. }
  1150. // check if one of the points of winding 2 is at the back of the plane of winding 1
  1151. for ( i = 0; i < w2.numPoints; i++ ) {
  1152. if ( normal1 * w2.p[i].ToVec3() - dist1 > WCONVEX_EPSILON ) {
  1153. return true;
  1154. }
  1155. }
  1156. return false;
  1157. }
  1158. /*
  1159. =============
  1160. idWinding::PointInside
  1161. =============
  1162. */
  1163. bool idWinding::PointInside( const idVec3 &normal, const idVec3 &point, const float epsilon ) const {
  1164. int i;
  1165. idVec3 dir, n, pointvec;
  1166. for ( i = 0; i < numPoints; i++ ) {
  1167. dir = p[(i+1) % numPoints].ToVec3() - p[i].ToVec3();
  1168. pointvec = point - p[i].ToVec3();
  1169. n = dir.Cross( normal );
  1170. if ( pointvec * n < -epsilon ) {
  1171. return false;
  1172. }
  1173. }
  1174. return true;
  1175. }
  1176. /*
  1177. =============
  1178. idWinding::LineIntersection
  1179. =============
  1180. */
  1181. bool idWinding::LineIntersection( const idPlane &windingPlane, const idVec3 &start, const idVec3 &end, bool backFaceCull ) const {
  1182. float front, back, frac;
  1183. idVec3 mid;
  1184. front = windingPlane.Distance( start );
  1185. back = windingPlane.Distance( end );
  1186. // if both points at the same side of the plane
  1187. if ( front < 0.0f && back < 0.0f ) {
  1188. return false;
  1189. }
  1190. if ( front > 0.0f && back > 0.0f ) {
  1191. return false;
  1192. }
  1193. // if back face culled
  1194. if ( backFaceCull && front < 0.0f ) {
  1195. return false;
  1196. }
  1197. // get point of intersection with winding plane
  1198. if ( idMath::Fabs(front - back) < 0.0001f ) {
  1199. mid = end;
  1200. }
  1201. else {
  1202. frac = front / (front - back);
  1203. mid[0] = start[0] + (end[0] - start[0]) * frac;
  1204. mid[1] = start[1] + (end[1] - start[1]) * frac;
  1205. mid[2] = start[2] + (end[2] - start[2]) * frac;
  1206. }
  1207. return PointInside( windingPlane.Normal(), mid, 0.0f );
  1208. }
  1209. /*
  1210. =============
  1211. idWinding::RayIntersection
  1212. =============
  1213. */
  1214. bool idWinding::RayIntersection( const idPlane &windingPlane, const idVec3 &start, const idVec3 &dir, float &scale, bool backFaceCull ) const {
  1215. int i;
  1216. bool side, lastside = false;
  1217. idPluecker pl1, pl2;
  1218. scale = 0.0f;
  1219. pl1.FromRay( start, dir );
  1220. for ( i = 0; i < numPoints; i++ ) {
  1221. pl2.FromLine( p[i].ToVec3(), p[(i+1)%numPoints].ToVec3() );
  1222. side = pl1.PermutedInnerProduct( pl2 ) > 0.0f;
  1223. if ( i && side != lastside ) {
  1224. return false;
  1225. }
  1226. lastside = side;
  1227. }
  1228. if ( !backFaceCull || lastside ) {
  1229. windingPlane.RayIntersection( start, dir, scale );
  1230. return true;
  1231. }
  1232. return false;
  1233. }
  1234. /*
  1235. =================
  1236. idWinding::TriangleArea
  1237. =================
  1238. */
  1239. float idWinding::TriangleArea( const idVec3 &a, const idVec3 &b, const idVec3 &c ) {
  1240. idVec3 v1, v2;
  1241. idVec3 cross;
  1242. v1 = b - a;
  1243. v2 = c - a;
  1244. cross = v1.Cross( v2 );
  1245. return 0.5f * cross.Length();
  1246. }
  1247. //===============================================================
  1248. //
  1249. // idFixedWinding
  1250. //
  1251. //===============================================================
  1252. /*
  1253. =============
  1254. idFixedWinding::ReAllocate
  1255. =============
  1256. */
  1257. bool idFixedWinding::ReAllocate( int n, bool keep ) {
  1258. assert( n <= MAX_POINTS_ON_WINDING );
  1259. if ( n > MAX_POINTS_ON_WINDING ) {
  1260. idLib::common->Printf("WARNING: idFixedWinding -> MAX_POINTS_ON_WINDING overflowed\n");
  1261. return false;
  1262. }
  1263. return true;
  1264. }
  1265. /*
  1266. =============
  1267. idFixedWinding::Split
  1268. =============
  1269. */
  1270. int idFixedWinding::Split( idFixedWinding *back, const idPlane &plane, const float epsilon ) {
  1271. int counts[3];
  1272. float dists[MAX_POINTS_ON_WINDING+4];
  1273. byte sides[MAX_POINTS_ON_WINDING+4];
  1274. float dot;
  1275. int i, j;
  1276. idVec5 *p1, *p2;
  1277. idVec5 mid;
  1278. idFixedWinding out;
  1279. counts[SIDE_FRONT] = counts[SIDE_BACK] = counts[SIDE_ON] = 0;
  1280. // determine sides for each point
  1281. for ( i = 0; i < numPoints; i++ ) {
  1282. dists[i] = dot = plane.Distance( p[i].ToVec3() );
  1283. if ( dot > epsilon ) {
  1284. sides[i] = SIDE_FRONT;
  1285. } else if ( dot < -epsilon ) {
  1286. sides[i] = SIDE_BACK;
  1287. } else {
  1288. sides[i] = SIDE_ON;
  1289. }
  1290. counts[sides[i]]++;
  1291. }
  1292. if ( !counts[SIDE_BACK] ) {
  1293. if ( !counts[SIDE_FRONT] ) {
  1294. return SIDE_ON;
  1295. }
  1296. else {
  1297. return SIDE_FRONT;
  1298. }
  1299. }
  1300. if ( !counts[SIDE_FRONT] ) {
  1301. return SIDE_BACK;
  1302. }
  1303. sides[i] = sides[0];
  1304. dists[i] = dists[0];
  1305. out.numPoints = 0;
  1306. back->numPoints = 0;
  1307. for ( i = 0; i < numPoints; i++ ) {
  1308. p1 = &p[i];
  1309. if ( !out.EnsureAlloced( out.numPoints+1, true ) ) {
  1310. return SIDE_FRONT; // can't split -- fall back to original
  1311. }
  1312. if ( !back->EnsureAlloced( back->numPoints+1, true ) ) {
  1313. return SIDE_FRONT; // can't split -- fall back to original
  1314. }
  1315. if ( sides[i] == SIDE_ON ) {
  1316. out.p[out.numPoints] = *p1;
  1317. out.numPoints++;
  1318. back->p[back->numPoints] = *p1;
  1319. back->numPoints++;
  1320. continue;
  1321. }
  1322. if ( sides[i] == SIDE_FRONT ) {
  1323. out.p[out.numPoints] = *p1;
  1324. out.numPoints++;
  1325. }
  1326. if ( sides[i] == SIDE_BACK ) {
  1327. back->p[back->numPoints] = *p1;
  1328. back->numPoints++;
  1329. }
  1330. if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) {
  1331. continue;
  1332. }
  1333. if ( !out.EnsureAlloced( out.numPoints+1, true ) ) {
  1334. return SIDE_FRONT; // can't split -- fall back to original
  1335. }
  1336. if ( !back->EnsureAlloced( back->numPoints+1, true ) ) {
  1337. return SIDE_FRONT; // can't split -- fall back to original
  1338. }
  1339. // generate a split point
  1340. j = i + 1;
  1341. if ( j >= numPoints ) {
  1342. p2 = &p[0];
  1343. }
  1344. else {
  1345. p2 = &p[j];
  1346. }
  1347. dot = dists[i] / (dists[i] - dists[i+1]);
  1348. for ( j = 0; j < 3; j++ ) {
  1349. // avoid round off error when possible
  1350. if ( plane.Normal()[j] == 1.0f ) {
  1351. mid[j] = plane.Dist();
  1352. } else if ( plane.Normal()[j] == -1.0f ) {
  1353. mid[j] = -plane.Dist();
  1354. } else {
  1355. mid[j] = (*p1)[j] + dot * ( (*p2)[j] - (*p1)[j] );
  1356. }
  1357. }
  1358. mid.s = p1->s + dot * ( p2->s - p1->s );
  1359. mid.t = p1->t + dot * ( p2->t - p1->t );
  1360. out.p[out.numPoints] = mid;
  1361. out.numPoints++;
  1362. back->p[back->numPoints] = mid;
  1363. back->numPoints++;
  1364. }
  1365. for ( i = 0; i < out.numPoints; i++ ) {
  1366. p[i] = out.p[i];
  1367. }
  1368. numPoints = out.numPoints;
  1369. return SIDE_CROSS;
  1370. }