123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602 |
- /*
- ===========================================================================
- Doom 3 GPL Source Code
- Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
- This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
- Doom 3 Source Code is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- Doom 3 Source Code is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
- 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.
- 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.
- ===========================================================================
- */
- #include "../precompiled.h"
- #pragma hdrstop
- //===============================================================
- //
- // idWinding
- //
- //===============================================================
- /*
- =============
- idWinding::ReAllocate
- =============
- */
- bool idWinding::ReAllocate( int n, bool keep ) {
- idVec5 *oldP;
- oldP = p;
- n = (n+3) & ~3; // align up to multiple of four
- p = new idVec5[n];
- if ( oldP ) {
- if ( keep ) {
- memcpy( p, oldP, numPoints * sizeof(p[0]) );
- }
- delete[] oldP;
- }
- allocedSize = n;
- return true;
- }
- /*
- =============
- idWinding::BaseForPlane
- =============
- */
- void idWinding::BaseForPlane( const idVec3 &normal, const float dist ) {
- idVec3 org, vright, vup;
- org = normal * dist;
- normal.NormalVectors( vup, vright );
- vup *= MAX_WORLD_SIZE;
- vright *= MAX_WORLD_SIZE;
- EnsureAlloced( 4 );
- numPoints = 4;
- p[0].ToVec3() = org - vright + vup;
- p[0].s = p[0].t = 0.0f;
- p[1].ToVec3() = org + vright + vup;
- p[1].s = p[1].t = 0.0f;
- p[2].ToVec3() = org + vright - vup;
- p[2].s = p[2].t = 0.0f;
- p[3].ToVec3() = org - vright - vup;
- p[3].s = p[3].t = 0.0f;
- }
- /*
- =============
- idWinding::Split
- =============
- */
- int idWinding::Split( const idPlane &plane, const float epsilon, idWinding **front, idWinding **back ) const {
- float * dists;
- byte * sides;
- int counts[3];
- float dot;
- int i, j;
- const idVec5 * p1, *p2;
- idVec5 mid;
- idWinding * f, *b;
- int maxpts;
- assert( this );
- dists = (float *) _alloca( (numPoints+4) * sizeof( float ) );
- sides = (byte *) _alloca( (numPoints+4) * sizeof( byte ) );
- counts[0] = counts[1] = counts[2] = 0;
- // determine sides for each point
- for ( i = 0; i < numPoints; i++ ) {
- dists[i] = dot = plane.Distance( p[i].ToVec3() );
- if ( dot > epsilon ) {
- sides[i] = SIDE_FRONT;
- } else if ( dot < -epsilon ) {
- sides[i] = SIDE_BACK;
- } else {
- sides[i] = SIDE_ON;
- }
- counts[sides[i]]++;
- }
- sides[i] = sides[0];
- dists[i] = dists[0];
-
- *front = *back = NULL;
- // if coplanar, put on the front side if the normals match
- if ( !counts[SIDE_FRONT] && !counts[SIDE_BACK] ) {
- idPlane windingPlane;
- GetPlane( windingPlane );
- if ( windingPlane.Normal() * plane.Normal() > 0.0f ) {
- *front = Copy();
- return SIDE_FRONT;
- } else {
- *back = Copy();
- return SIDE_BACK;
- }
- }
- // if nothing at the front of the clipping plane
- if ( !counts[SIDE_FRONT] ) {
- *back = Copy();
- return SIDE_BACK;
- }
- // if nothing at the back of the clipping plane
- if ( !counts[SIDE_BACK] ) {
- *front = Copy();
- return SIDE_FRONT;
- }
- maxpts = numPoints+4; // cant use counts[0]+2 because of fp grouping errors
- *front = f = new idWinding(maxpts);
- *back = b = new idWinding(maxpts);
-
- for (i = 0; i < numPoints; i++) {
- p1 = &p[i];
-
- if ( sides[i] == SIDE_ON ) {
- f->p[f->numPoints] = *p1;
- f->numPoints++;
- b->p[b->numPoints] = *p1;
- b->numPoints++;
- continue;
- }
-
- if ( sides[i] == SIDE_FRONT ) {
- f->p[f->numPoints] = *p1;
- f->numPoints++;
- }
- if ( sides[i] == SIDE_BACK ) {
- b->p[b->numPoints] = *p1;
- b->numPoints++;
- }
- if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) {
- continue;
- }
-
- // generate a split point
- p2 = &p[(i+1)%numPoints];
-
- // always calculate the split going from the same side
- // or minor epsilon issues can happen
- if ( sides[i] == SIDE_FRONT ) {
- dot = dists[i] / ( dists[i] - dists[i+1] );
- for ( j = 0; j < 3; j++ ) {
- // avoid round off error when possible
- if ( plane.Normal()[j] == 1.0f ) {
- mid[j] = plane.Dist();
- } else if ( plane.Normal()[j] == -1.0f ) {
- mid[j] = -plane.Dist();
- } else {
- mid[j] = (*p1)[j] + dot * ( (*p2)[j] - (*p1)[j] );
- }
- }
- mid.s = p1->s + dot * ( p2->s - p1->s );
- mid.t = p1->t + dot * ( p2->t - p1->t );
- } else {
- dot = dists[i+1] / ( dists[i+1] - dists[i] );
- for ( j = 0; j < 3; j++ ) {
- // avoid round off error when possible
- if ( plane.Normal()[j] == 1.0f ) {
- mid[j] = plane.Dist();
- } else if ( plane.Normal()[j] == -1.0f ) {
- mid[j] = -plane.Dist();
- } else {
- mid[j] = (*p2)[j] + dot * ( (*p1)[j] - (*p2)[j] );
- }
- }
- mid.s = p2->s + dot * ( p1->s - p2->s );
- mid.t = p2->t + dot * ( p1->t - p2->t );
- }
- f->p[f->numPoints] = mid;
- f->numPoints++;
- b->p[b->numPoints] = mid;
- b->numPoints++;
- }
-
- if ( f->numPoints > maxpts || b->numPoints > maxpts ) {
- idLib::common->FatalError( "idWinding::Split: points exceeded estimate." );
- }
- return SIDE_CROSS;
- }
- /*
- =============
- idWinding::Clip
- =============
- */
- idWinding *idWinding::Clip( const idPlane &plane, const float epsilon, const bool keepOn ) {
- float * dists;
- byte * sides;
- idVec5 * newPoints;
- int newNumPoints;
- int counts[3];
- float dot;
- int i, j;
- idVec5 * p1, *p2;
- idVec5 mid;
- int maxpts;
- assert( this );
- dists = (float *) _alloca( (numPoints+4) * sizeof( float ) );
- sides = (byte *) _alloca( (numPoints+4) * sizeof( byte ) );
- counts[SIDE_FRONT] = counts[SIDE_BACK] = counts[SIDE_ON] = 0;
- // determine sides for each point
- for ( i = 0; i < numPoints; i++ ) {
- dists[i] = dot = plane.Distance( p[i].ToVec3() );
- if ( dot > epsilon ) {
- sides[i] = SIDE_FRONT;
- } else if ( dot < -epsilon ) {
- sides[i] = SIDE_BACK;
- } else {
- sides[i] = SIDE_ON;
- }
- counts[sides[i]]++;
- }
- sides[i] = sides[0];
- dists[i] = dists[0];
- // if the winding is on the plane and we should keep it
- if ( keepOn && !counts[SIDE_FRONT] && !counts[SIDE_BACK] ) {
- return this;
- }
- // if nothing at the front of the clipping plane
- if ( !counts[SIDE_FRONT] ) {
- delete this;
- return NULL;
- }
- // if nothing at the back of the clipping plane
- if ( !counts[SIDE_BACK] ) {
- return this;
- }
- maxpts = numPoints + 4; // cant use counts[0]+2 because of fp grouping errors
- newPoints = (idVec5 *) _alloca16( maxpts * sizeof( idVec5 ) );
- newNumPoints = 0;
-
- for ( i = 0; i < numPoints; i++ ) {
- p1 = &p[i];
- if ( newNumPoints+1 > maxpts ) {
- return this; // can't split -- fall back to original
- }
- if ( sides[i] == SIDE_ON ) {
- newPoints[newNumPoints] = *p1;
- newNumPoints++;
- continue;
- }
- if ( sides[i] == SIDE_FRONT ) {
- newPoints[newNumPoints] = *p1;
- newNumPoints++;
- }
- if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) {
- continue;
- }
- if ( newNumPoints+1 > maxpts ) {
- return this; // can't split -- fall back to original
- }
- // generate a split point
- p2 = &p[(i+1)%numPoints];
- dot = dists[i] / (dists[i] - dists[i+1]);
- for ( j = 0; j < 3; j++ ) {
- // avoid round off error when possible
- if ( plane.Normal()[j] == 1.0f ) {
- mid[j] = plane.Dist();
- } else if ( plane.Normal()[j] == -1.0f ) {
- mid[j] = -plane.Dist();
- } else {
- mid[j] = (*p1)[j] + dot * ( (*p2)[j] - (*p1)[j] );
- }
- }
- mid.s = p1->s + dot * ( p2->s - p1->s );
- mid.t = p1->t + dot * ( p2->t - p1->t );
- newPoints[newNumPoints] = mid;
- newNumPoints++;
- }
-
- if ( !EnsureAlloced( newNumPoints, false ) ) {
- return this;
- }
- numPoints = newNumPoints;
- memcpy( p, newPoints, newNumPoints * sizeof(idVec5) );
- return this;
- }
- /*
- =============
- idWinding::ClipInPlace
- =============
- */
- bool idWinding::ClipInPlace( const idPlane &plane, const float epsilon, const bool keepOn ) {
- float* dists;
- byte * sides;
- idVec5 * newPoints;
- int newNumPoints;
- int counts[3];
- float dot;
- int i, j;
- idVec5 * p1, *p2;
- idVec5 mid;
- int maxpts;
- assert( this );
- dists = (float *) _alloca( (numPoints+4) * sizeof( float ) );
- sides = (byte *) _alloca( (numPoints+4) * sizeof( byte ) );
- counts[SIDE_FRONT] = counts[SIDE_BACK] = counts[SIDE_ON] = 0;
- // determine sides for each point
- for ( i = 0; i < numPoints; i++ ) {
- dists[i] = dot = plane.Distance( p[i].ToVec3() );
- if ( dot > epsilon ) {
- sides[i] = SIDE_FRONT;
- } else if ( dot < -epsilon ) {
- sides[i] = SIDE_BACK;
- } else {
- sides[i] = SIDE_ON;
- }
- counts[sides[i]]++;
- }
- sides[i] = sides[0];
- dists[i] = dists[0];
-
- // if the winding is on the plane and we should keep it
- if ( keepOn && !counts[SIDE_FRONT] && !counts[SIDE_BACK] ) {
- return true;
- }
- // if nothing at the front of the clipping plane
- if ( !counts[SIDE_FRONT] ) {
- numPoints = 0;
- return false;
- }
- // if nothing at the back of the clipping plane
- if ( !counts[SIDE_BACK] ) {
- return true;
- }
- maxpts = numPoints + 4; // cant use counts[0]+2 because of fp grouping errors
- newPoints = (idVec5 *) _alloca16( maxpts * sizeof( idVec5 ) );
- newNumPoints = 0;
- for ( i = 0; i < numPoints; i++ ) {
- p1 = &p[i];
- if ( newNumPoints+1 > maxpts ) {
- return true; // can't split -- fall back to original
- }
-
- if ( sides[i] == SIDE_ON ) {
- newPoints[newNumPoints] = *p1;
- newNumPoints++;
- continue;
- }
-
- if ( sides[i] == SIDE_FRONT ) {
- newPoints[newNumPoints] = *p1;
- newNumPoints++;
- }
- if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) {
- continue;
- }
-
- if ( newNumPoints+1 > maxpts ) {
- return true; // can't split -- fall back to original
- }
- // generate a split point
- p2 = &p[(i+1)%numPoints];
-
- dot = dists[i] / (dists[i] - dists[i+1]);
- for ( j = 0; j < 3; j++ ) {
- // avoid round off error when possible
- if ( plane.Normal()[j] == 1.0f ) {
- mid[j] = plane.Dist();
- } else if ( plane.Normal()[j] == -1.0f ) {
- mid[j] = -plane.Dist();
- } else {
- mid[j] = (*p1)[j] + dot * ( (*p2)[j] - (*p1)[j] );
- }
- }
- mid.s = p1->s + dot * ( p2->s - p1->s );
- mid.t = p1->t + dot * ( p2->t - p1->t );
-
- newPoints[newNumPoints] = mid;
- newNumPoints++;
- }
- if ( !EnsureAlloced( newNumPoints, false ) ) {
- return true;
- }
- numPoints = newNumPoints;
- memcpy( p, newPoints, newNumPoints * sizeof(idVec5) );
- return true;
- }
- /*
- =============
- idWinding::Copy
- =============
- */
- idWinding *idWinding::Copy( void ) const {
- idWinding *w;
- w = new idWinding( numPoints );
- w->numPoints = numPoints;
- memcpy( w->p, p, numPoints * sizeof(p[0]) );
- return w;
- }
- /*
- =============
- idWinding::Reverse
- =============
- */
- idWinding *idWinding::Reverse( void ) const {
- idWinding *w;
- int i;
- w = new idWinding( numPoints );
- w->numPoints = numPoints;
- for ( i = 0; i < numPoints; i++ ) {
- w->p[ numPoints - i - 1 ] = p[i];
- }
- return w;
- }
- /*
- =============
- idWinding::ReverseSelf
- =============
- */
- void idWinding::ReverseSelf( void ) {
- idVec5 v;
- int i;
- for ( i = 0; i < (numPoints>>1); i++ ) {
- v = p[i];
- p[i] = p[numPoints - i - 1];
- p[numPoints - i - 1] = v;
- }
- }
- /*
- =============
- idWinding::Check
- =============
- */
- bool idWinding::Check( bool print ) const {
- int i, j;
- float d, edgedist;
- idVec3 dir, edgenormal;
- float area;
- idPlane plane;
- if ( numPoints < 3 ) {
- if ( print ) {
- idLib::common->Printf( "idWinding::Check: only %i points.", numPoints );
- }
- return false;
- }
-
- area = GetArea();
- if ( area < 1.0f ) {
- if ( print ) {
- idLib::common->Printf( "idWinding::Check: tiny area: %f", area );
- }
- return false;
- }
- GetPlane( plane );
-
- for ( i = 0; i < numPoints; i++ ) {
- const idVec3 &p1 = p[i].ToVec3();
- // check if the winding is huge
- for ( j = 0; j < 3; j++ ) {
- if ( p1[j] >= MAX_WORLD_COORD || p1[j] <= MIN_WORLD_COORD ) {
- if ( print ) {
- idLib::common->Printf( "idWinding::Check: point %d outside world %c-axis: %f", i, 'X'+j, p1[j] );
- }
- return false;
- }
- }
- j = i + 1 == numPoints ? 0 : i + 1;
-
- // check if the point is on the face plane
- d = p1 * plane.Normal() + plane[3];
- if ( d < -ON_EPSILON || d > ON_EPSILON ) {
- if ( print ) {
- idLib::common->Printf( "idWinding::Check: point %d off plane.", i );
- }
- return false;
- }
-
- // check if the edge isn't degenerate
- const idVec3 &p2 = p[j].ToVec3();
- dir = p2 - p1;
-
- if ( dir.Length() < ON_EPSILON) {
- if ( print ) {
- idLib::common->Printf( "idWinding::Check: edge %d is degenerate.", i );
- }
- return false;
- }
- // check if the winding is convex
- edgenormal = plane.Normal().Cross( dir );
- edgenormal.Normalize();
- edgedist = p1 * edgenormal;
- edgedist += ON_EPSILON;
-
- // all other points must be on front side
- for ( j = 0; j < numPoints; j++ ) {
- if ( j == i ) {
- continue;
- }
- d = p[j].ToVec3() * edgenormal;
- if ( d > edgedist ) {
- if ( print ) {
- idLib::common->Printf( "idWinding::Check: non-convex." );
- }
- return false;
- }
- }
- }
- return true;
- }
- /*
- =============
- idWinding::GetArea
- =============
- */
- float idWinding::GetArea( void ) const {
- int i;
- idVec3 d1, d2, cross;
- float total;
- total = 0.0f;
- for ( i = 2; i < numPoints; i++ ) {
- d1 = p[i-1].ToVec3() - p[0].ToVec3();
- d2 = p[i].ToVec3() - p[0].ToVec3();
- cross = d1.Cross( d2 );
- total += cross.Length();
- }
- return total * 0.5f;
- }
- /*
- =============
- idWinding::GetRadius
- =============
- */
- float idWinding::GetRadius( const idVec3 ¢er ) const {
- int i;
- float radius, r;
- idVec3 dir;
- radius = 0.0f;
- for ( i = 0; i < numPoints; i++ ) {
- dir = p[i].ToVec3() - center;
- r = dir * dir;
- if ( r > radius ) {
- radius = r;
- }
- }
- return idMath::Sqrt( radius );
- }
- /*
- =============
- idWinding::GetCenter
- =============
- */
- idVec3 idWinding::GetCenter( void ) const {
- int i;
- idVec3 center;
- center.Zero();
- for ( i = 0; i < numPoints; i++ ) {
- center += p[i].ToVec3();
- }
- center *= ( 1.0f / numPoints );
- return center;
- }
- /*
- =============
- idWinding::GetPlane
- =============
- */
- void idWinding::GetPlane( idVec3 &normal, float &dist ) const {
- idVec3 v1, v2, center;
- if ( numPoints < 3 ) {
- normal.Zero();
- dist = 0.0f;
- return;
- }
- center = GetCenter();
- v1 = p[0].ToVec3() - center;
- v2 = p[1].ToVec3() - center;
- normal = v2.Cross( v1 );
- normal.Normalize();
- dist = p[0].ToVec3() * normal;
- }
- /*
- =============
- idWinding::GetPlane
- =============
- */
- void idWinding::GetPlane( idPlane &plane ) const {
- idVec3 v1, v2;
- idVec3 center;
- if ( numPoints < 3 ) {
- plane.Zero();
- return;
- }
- center = GetCenter();
- v1 = p[0].ToVec3() - center;
- v2 = p[1].ToVec3() - center;
- plane.SetNormal( v2.Cross( v1 ) );
- plane.Normalize();
- plane.FitThroughPoint( p[0].ToVec3() );
- }
- /*
- =============
- idWinding::GetBounds
- =============
- */
- void idWinding::GetBounds( idBounds &bounds ) const {
- int i;
- if ( !numPoints ) {
- bounds.Clear();
- return;
- }
- bounds[0] = bounds[1] = p[0].ToVec3();
- for ( i = 1; i < numPoints; i++ ) {
- if ( p[i].x < bounds[0].x ) {
- bounds[0].x = p[i].x;
- } else if ( p[i].x > bounds[1].x ) {
- bounds[1].x = p[i].x;
- }
- if ( p[i].y < bounds[0].y ) {
- bounds[0].y = p[i].y;
- } else if ( p[i].y > bounds[1].y ) {
- bounds[1].y = p[i].y;
- }
- if ( p[i].z < bounds[0].z ) {
- bounds[0].z = p[i].z;
- } else if ( p[i].z > bounds[1].z ) {
- bounds[1].z = p[i].z;
- }
- }
- }
- /*
- =============
- idWinding::RemoveEqualPoints
- =============
- */
- void idWinding::RemoveEqualPoints( const float epsilon ) {
- int i, j;
- for ( i = 0; i < numPoints; i++ ) {
- if ( (p[i].ToVec3() - p[(i+numPoints-1)%numPoints].ToVec3()).LengthSqr() >= Square( epsilon ) ) {
- continue;
- }
- numPoints--;
- for ( j = i; j < numPoints; j++ ) {
- p[j] = p[j+1];
- }
- i--;
- }
- }
- /*
- =============
- idWinding::RemoveColinearPoints
- =============
- */
- void idWinding::RemoveColinearPoints( const idVec3 &normal, const float epsilon ) {
- int i, j;
- idVec3 edgeNormal;
- float dist;
- if ( numPoints <= 3 ) {
- return;
- }
- for ( i = 0; i < numPoints; i++ ) {
- // create plane through edge orthogonal to winding plane
- edgeNormal = (p[i].ToVec3() - p[(i+numPoints-1)%numPoints].ToVec3()).Cross( normal );
- edgeNormal.Normalize();
- dist = edgeNormal * p[i].ToVec3();
- if ( idMath::Fabs( edgeNormal * p[(i+1)%numPoints].ToVec3() - dist ) > epsilon ) {
- continue;
- }
- numPoints--;
- for ( j = i; j < numPoints; j++ ) {
- p[j] = p[j+1];
- }
- i--;
- }
- }
- /*
- =============
- idWinding::AddToConvexHull
- Adds the given winding to the convex hull.
- Assumes the current winding already is a convex hull with three or more points.
- =============
- */
- void idWinding::AddToConvexHull( const idWinding *winding, const idVec3 &normal, const float epsilon ) {
- int i, j, k;
- idVec3 dir;
- float d;
- int maxPts;
- idVec3 * hullDirs;
- bool * hullSide;
- bool outside;
- int numNewHullPoints;
- idVec5 * newHullPoints;
- if ( !winding ) {
- return;
- }
- maxPts = this->numPoints + winding->numPoints;
- if ( !this->EnsureAlloced( maxPts, true ) ) {
- return;
- }
- newHullPoints = (idVec5 *) _alloca( maxPts * sizeof( idVec5 ) );
- hullDirs = (idVec3 *) _alloca( maxPts * sizeof( idVec3 ) );
- hullSide = (bool *) _alloca( maxPts * sizeof( bool ) );
- for ( i = 0; i < winding->numPoints; i++ ) {
- const idVec5 &p1 = winding->p[i];
- // calculate hull edge vectors
- for ( j = 0; j < this->numPoints; j++ ) {
- dir = this->p[ (j + 1) % this->numPoints ].ToVec3() - this->p[ j ].ToVec3();
- dir.Normalize();
- hullDirs[j] = normal.Cross( dir );
- }
- // calculate side for each hull edge
- outside = false;
- for ( j = 0; j < this->numPoints; j++ ) {
- dir = p1.ToVec3() - this->p[j].ToVec3();
- d = dir * hullDirs[j];
- if ( d >= epsilon ) {
- outside = true;
- }
- if ( d >= -epsilon ) {
- hullSide[j] = true;
- } else {
- hullSide[j] = false;
- }
- }
- // if the point is effectively inside, do nothing
- if ( !outside ) {
- continue;
- }
- // find the back side to front side transition
- for ( j = 0; j < this->numPoints; j++ ) {
- if ( !hullSide[ j ] && hullSide[ (j + 1) % this->numPoints ] ) {
- break;
- }
- }
- if ( j >= this->numPoints ) {
- continue;
- }
- // insert the point here
- newHullPoints[0] = p1;
- numNewHullPoints = 1;
- // copy over all points that aren't double fronts
- j = (j+1) % this->numPoints;
- for ( k = 0; k < this->numPoints; k++ ) {
- if ( hullSide[ (j+k) % this->numPoints ] && hullSide[ (j+k+1) % this->numPoints ] ) {
- continue;
- }
- newHullPoints[numNewHullPoints] = this->p[ (j+k+1) % this->numPoints ];
- numNewHullPoints++;
- }
- this->numPoints = numNewHullPoints;
- memcpy( this->p, newHullPoints, numNewHullPoints * sizeof(idVec5) );
- }
- }
- /*
- =============
- idWinding::AddToConvexHull
- Add a point to the convex hull.
- The current winding must be convex but may be degenerate and can have less than three points.
- =============
- */
- void idWinding::AddToConvexHull( const idVec3 &point, const idVec3 &normal, const float epsilon ) {
- int j, k, numHullPoints;
- idVec3 dir;
- float d;
- idVec3 * hullDirs;
- bool * hullSide;
- idVec5 * hullPoints;
- bool outside;
- switch( numPoints ) {
- case 0: {
- p[0] = point;
- numPoints++;
- return;
- }
- case 1: {
- // don't add the same point second
- if ( p[0].ToVec3().Compare( point, epsilon ) ) {
- return;
- }
- p[1].ToVec3() = point;
- numPoints++;
- return;
- }
- case 2: {
- // don't add a point if it already exists
- if ( p[0].ToVec3().Compare( point, epsilon ) || p[1].ToVec3().Compare( point, epsilon ) ) {
- return;
- }
- // if only two points make sure we have the right ordering according to the normal
- dir = point - p[0].ToVec3();
- dir = dir.Cross( p[1].ToVec3() - p[0].ToVec3() );
- if ( dir[0] == 0.0f && dir[1] == 0.0f && dir[2] == 0.0f ) {
- // points don't make a plane
- return;
- }
- if ( dir * normal > 0.0f ) {
- p[2].ToVec3() = point;
- }
- else {
- p[2] = p[1];
- p[1].ToVec3() = point;
- }
- numPoints++;
- return;
- }
- }
- hullDirs = (idVec3 *) _alloca( numPoints * sizeof( idVec3 ) );
- hullSide = (bool *) _alloca( numPoints * sizeof( bool ) );
- // calculate hull edge vectors
- for ( j = 0; j < numPoints; j++ ) {
- dir = p[(j + 1) % numPoints].ToVec3() - p[j].ToVec3();
- hullDirs[j] = normal.Cross( dir );
- }
- // calculate side for each hull edge
- outside = false;
- for ( j = 0; j < numPoints; j++ ) {
- dir = point - p[j].ToVec3();
- d = dir * hullDirs[j];
- if ( d >= epsilon ) {
- outside = true;
- }
- if ( d >= -epsilon ) {
- hullSide[j] = true;
- } else {
- hullSide[j] = false;
- }
- }
- // if the point is effectively inside, do nothing
- if ( !outside ) {
- return;
- }
- // find the back side to front side transition
- for ( j = 0; j < numPoints; j++ ) {
- if ( !hullSide[ j ] && hullSide[ (j + 1) % numPoints ] ) {
- break;
- }
- }
- if ( j >= numPoints ) {
- return;
- }
- hullPoints = (idVec5 *) _alloca( (numPoints+1) * sizeof( idVec5 ) );
- // insert the point here
- hullPoints[0] = point;
- numHullPoints = 1;
- // copy over all points that aren't double fronts
- j = (j+1) % numPoints;
- for ( k = 0; k < numPoints; k++ ) {
- if ( hullSide[ (j+k) % numPoints ] && hullSide[ (j+k+1) % numPoints ] ) {
- continue;
- }
- hullPoints[numHullPoints] = p[ (j+k+1) % numPoints ];
- numHullPoints++;
- }
- if ( !EnsureAlloced( numHullPoints, false ) ) {
- return;
- }
- numPoints = numHullPoints;
- memcpy( p, hullPoints, numHullPoints * sizeof(idVec5) );
- }
- /*
- =============
- idWinding::TryMerge
- =============
- */
- #define CONTINUOUS_EPSILON 0.005f
- idWinding *idWinding::TryMerge( const idWinding &w, const idVec3 &planenormal, int keep ) const {
- idVec3 *p1, *p2, *p3, *p4, *back;
- idWinding *newf;
- const idWinding *f1, *f2;
- int i, j, k, l;
- idVec3 normal, delta;
- float dot;
- bool keep1, keep2;
- f1 = this;
- f2 = &w;
- //
- // find a idLib::common edge
- //
- p1 = p2 = NULL; // stop compiler warning
- j = 0;
-
- for ( i = 0; i < f1->numPoints; i++ ) {
- p1 = &f1->p[i].ToVec3();
- p2 = &f1->p[(i+1) % f1->numPoints].ToVec3();
- for ( j = 0; j < f2->numPoints; j++ ) {
- p3 = &f2->p[j].ToVec3();
- p4 = &f2->p[(j+1) % f2->numPoints].ToVec3();
- for (k = 0; k < 3; k++ ) {
- if ( idMath::Fabs((*p1)[k] - (*p4)[k]) > 0.1f ) {
- break;
- }
- if ( idMath::Fabs((*p2)[k] - (*p3)[k]) > 0.1f ) {
- break;
- }
- }
- if ( k == 3 ) {
- break;
- }
- }
- if ( j < f2->numPoints ) {
- break;
- }
- }
-
- if ( i == f1->numPoints ) {
- return NULL; // no matching edges
- }
- //
- // check slope of connected lines
- // if the slopes are colinear, the point can be removed
- //
- back = &f1->p[(i+f1->numPoints-1)%f1->numPoints].ToVec3();
- delta = (*p1) - (*back);
- normal = planenormal.Cross(delta);
- normal.Normalize();
-
- back = &f2->p[(j+2)%f2->numPoints].ToVec3();
- delta = (*back) - (*p1);
- dot = delta * normal;
- if ( dot > CONTINUOUS_EPSILON ) {
- return NULL; // not a convex polygon
- }
- keep1 = (bool)(dot < -CONTINUOUS_EPSILON);
-
- back = &f1->p[(i+2)%f1->numPoints].ToVec3();
- delta = (*back) - (*p2);
- normal = planenormal.Cross( delta );
- normal.Normalize();
- back = &f2->p[(j+f2->numPoints-1)%f2->numPoints].ToVec3();
- delta = (*back) - (*p2);
- dot = delta * normal;
- if ( dot > CONTINUOUS_EPSILON ) {
- return NULL; // not a convex polygon
- }
- keep2 = (bool)(dot < -CONTINUOUS_EPSILON);
- //
- // build the new polygon
- //
- newf = new idWinding( f1->numPoints + f2->numPoints );
-
- // copy first polygon
- for ( k = (i+1) % f1->numPoints; k != i; k = (k+1) % f1->numPoints ) {
- if ( !keep && k == (i+1) % f1->numPoints && !keep2 ) {
- continue;
- }
-
- newf->p[newf->numPoints] = f1->p[k];
- newf->numPoints++;
- }
-
- // copy second polygon
- for ( l = (j+1) % f2->numPoints; l != j; l = (l+1) % f2->numPoints ) {
- if ( !keep && l == (j+1) % f2->numPoints && !keep1 ) {
- continue;
- }
- newf->p[newf->numPoints] = f2->p[l];
- newf->numPoints++;
- }
- return newf;
- }
- /*
- =============
- idWinding::RemovePoint
- =============
- */
- void idWinding::RemovePoint( int point ) {
- if ( point < 0 || point >= numPoints ) {
- idLib::common->FatalError( "idWinding::removePoint: point out of range" );
- }
- if ( point < numPoints - 1) {
- memmove(&p[point], &p[point+1], (numPoints - point - 1) * sizeof(p[0]) );
- }
- numPoints--;
- }
- /*
- =============
- idWinding::InsertPoint
- =============
- */
- void idWinding::InsertPoint( const idVec3 &point, int spot ) {
- int i;
- if ( spot > numPoints ) {
- idLib::common->FatalError( "idWinding::insertPoint: spot > numPoints" );
- }
- if ( spot < 0 ) {
- idLib::common->FatalError( "idWinding::insertPoint: spot < 0" );
- }
- EnsureAlloced( numPoints+1, true );
- for ( i = numPoints; i > spot; i-- ) {
- p[i] = p[i-1];
- }
- p[spot] = point;
- numPoints++;
- }
- /*
- =============
- idWinding::InsertPointIfOnEdge
- =============
- */
- bool idWinding::InsertPointIfOnEdge( const idVec3 &point, const idPlane &plane, const float epsilon ) {
- int i;
- float dist, dot;
- idVec3 normal;
- // point may not be too far from the winding plane
- if ( idMath::Fabs( plane.Distance( point ) ) > epsilon ) {
- return false;
- }
- for ( i = 0; i < numPoints; i++ ) {
- // create plane through edge orthogonal to winding plane
- normal = (p[(i+1)%numPoints].ToVec3() - p[i].ToVec3()).Cross( plane.Normal() );
- normal.Normalize();
- dist = normal * p[i].ToVec3();
- if ( idMath::Fabs( normal * point - dist ) > epsilon ) {
- continue;
- }
- normal = plane.Normal().Cross( normal );
- dot = normal * point;
- dist = dot - normal * p[i].ToVec3();
- if ( dist < epsilon ) {
- // if the winding already has the point
- if ( dist > -epsilon ) {
- return false;
- }
- continue;
- }
- dist = dot - normal * p[(i+1)%numPoints].ToVec3();
- if ( dist > -epsilon ) {
- // if the winding already has the point
- if ( dist < epsilon ) {
- return false;
- }
- continue;
- }
- InsertPoint( point, i+1 );
- return true;
- }
- return false;
- }
- /*
- =============
- idWinding::IsTiny
- =============
- */
- #define EDGE_LENGTH 0.2f
- bool idWinding::IsTiny( void ) const {
- int i;
- float len;
- idVec3 delta;
- int edges;
- edges = 0;
- for ( i = 0; i < numPoints; i++ ) {
- delta = p[(i+1)%numPoints].ToVec3() - p[i].ToVec3();
- len = delta.Length();
- if ( len > EDGE_LENGTH ) {
- if ( ++edges == 3 ) {
- return false;
- }
- }
- }
- return true;
- }
- /*
- =============
- idWinding::IsHuge
- =============
- */
- bool idWinding::IsHuge( void ) const {
- int i, j;
- for ( i = 0; i < numPoints; i++ ) {
- for ( j = 0; j < 3; j++ ) {
- if ( p[i][j] <= MIN_WORLD_COORD || p[i][j] >= MAX_WORLD_COORD ) {
- return true;
- }
- }
- }
- return false;
- }
- /*
- =============
- idWinding::Print
- =============
- */
- void idWinding::Print( void ) const {
- int i;
- for ( i = 0; i < numPoints; i++ ) {
- idLib::common->Printf( "(%5.1f, %5.1f, %5.1f)\n", p[i][0], p[i][1], p[i][2] );
- }
- }
- /*
- =============
- idWinding::PlaneDistance
- =============
- */
- float idWinding::PlaneDistance( const idPlane &plane ) const {
- int i;
- float d, min, max;
- min = idMath::INFINITY;
- max = -min;
- for ( i = 0; i < numPoints; i++ ) {
- d = plane.Distance( p[i].ToVec3() );
- if ( d < min ) {
- min = d;
- if ( FLOATSIGNBITSET( min ) & FLOATSIGNBITNOTSET( max ) ) {
- return 0.0f;
- }
- }
- if ( d > max ) {
- max = d;
- if ( FLOATSIGNBITSET( min ) & FLOATSIGNBITNOTSET( max ) ) {
- return 0.0f;
- }
- }
- }
- if ( FLOATSIGNBITNOTSET( min ) ) {
- return min;
- }
- if ( FLOATSIGNBITSET( max ) ) {
- return max;
- }
- return 0.0f;
- }
- /*
- =============
- idWinding::PlaneSide
- =============
- */
- int idWinding::PlaneSide( const idPlane &plane, const float epsilon ) const {
- bool front, back;
- int i;
- float d;
- front = false;
- back = false;
- for ( i = 0; i < numPoints; i++ ) {
- d = plane.Distance( p[i].ToVec3() );
- if ( d < -epsilon ) {
- if ( front ) {
- return SIDE_CROSS;
- }
- back = true;
- continue;
- }
- else if ( d > epsilon ) {
- if ( back ) {
- return SIDE_CROSS;
- }
- front = true;
- continue;
- }
- }
- if ( back ) {
- return SIDE_BACK;
- }
- if ( front ) {
- return SIDE_FRONT;
- }
- return SIDE_ON;
- }
- /*
- =============
- idWinding::PlanesConcave
- =============
- */
- #define WCONVEX_EPSILON 0.2f
- bool idWinding::PlanesConcave( const idWinding &w2, const idVec3 &normal1, const idVec3 &normal2, float dist1, float dist2 ) const {
- int i;
- // check if one of the points of winding 1 is at the back of the plane of winding 2
- for ( i = 0; i < numPoints; i++ ) {
- if ( normal2 * p[i].ToVec3() - dist2 > WCONVEX_EPSILON ) {
- return true;
- }
- }
- // check if one of the points of winding 2 is at the back of the plane of winding 1
- for ( i = 0; i < w2.numPoints; i++ ) {
- if ( normal1 * w2.p[i].ToVec3() - dist1 > WCONVEX_EPSILON ) {
- return true;
- }
- }
- return false;
- }
- /*
- =============
- idWinding::PointInside
- =============
- */
- bool idWinding::PointInside( const idVec3 &normal, const idVec3 &point, const float epsilon ) const {
- int i;
- idVec3 dir, n, pointvec;
- for ( i = 0; i < numPoints; i++ ) {
- dir = p[(i+1) % numPoints].ToVec3() - p[i].ToVec3();
- pointvec = point - p[i].ToVec3();
- n = dir.Cross( normal );
- if ( pointvec * n < -epsilon ) {
- return false;
- }
- }
- return true;
- }
- /*
- =============
- idWinding::LineIntersection
- =============
- */
- bool idWinding::LineIntersection( const idPlane &windingPlane, const idVec3 &start, const idVec3 &end, bool backFaceCull ) const {
- float front, back, frac;
- idVec3 mid;
- front = windingPlane.Distance( start );
- back = windingPlane.Distance( end );
- // if both points at the same side of the plane
- if ( front < 0.0f && back < 0.0f ) {
- return false;
- }
- if ( front > 0.0f && back > 0.0f ) {
- return false;
- }
- // if back face culled
- if ( backFaceCull && front < 0.0f ) {
- return false;
- }
- // get point of intersection with winding plane
- if ( idMath::Fabs(front - back) < 0.0001f ) {
- mid = end;
- }
- else {
- frac = front / (front - back);
- mid[0] = start[0] + (end[0] - start[0]) * frac;
- mid[1] = start[1] + (end[1] - start[1]) * frac;
- mid[2] = start[2] + (end[2] - start[2]) * frac;
- }
- return PointInside( windingPlane.Normal(), mid, 0.0f );
- }
- /*
- =============
- idWinding::RayIntersection
- =============
- */
- bool idWinding::RayIntersection( const idPlane &windingPlane, const idVec3 &start, const idVec3 &dir, float &scale, bool backFaceCull ) const {
- int i;
- bool side, lastside = false;
- idPluecker pl1, pl2;
- scale = 0.0f;
- pl1.FromRay( start, dir );
- for ( i = 0; i < numPoints; i++ ) {
- pl2.FromLine( p[i].ToVec3(), p[(i+1)%numPoints].ToVec3() );
- side = pl1.PermutedInnerProduct( pl2 ) > 0.0f;
- if ( i && side != lastside ) {
- return false;
- }
- lastside = side;
- }
- if ( !backFaceCull || lastside ) {
- windingPlane.RayIntersection( start, dir, scale );
- return true;
- }
- return false;
- }
- /*
- =================
- idWinding::TriangleArea
- =================
- */
- float idWinding::TriangleArea( const idVec3 &a, const idVec3 &b, const idVec3 &c ) {
- idVec3 v1, v2;
- idVec3 cross;
- v1 = b - a;
- v2 = c - a;
- cross = v1.Cross( v2 );
- return 0.5f * cross.Length();
- }
- //===============================================================
- //
- // idFixedWinding
- //
- //===============================================================
- /*
- =============
- idFixedWinding::ReAllocate
- =============
- */
- bool idFixedWinding::ReAllocate( int n, bool keep ) {
- assert( n <= MAX_POINTS_ON_WINDING );
- if ( n > MAX_POINTS_ON_WINDING ) {
- idLib::common->Printf("WARNING: idFixedWinding -> MAX_POINTS_ON_WINDING overflowed\n");
- return false;
- }
- return true;
- }
- /*
- =============
- idFixedWinding::Split
- =============
- */
- int idFixedWinding::Split( idFixedWinding *back, const idPlane &plane, const float epsilon ) {
- int counts[3];
- float dists[MAX_POINTS_ON_WINDING+4];
- byte sides[MAX_POINTS_ON_WINDING+4];
- float dot;
- int i, j;
- idVec5 *p1, *p2;
- idVec5 mid;
- idFixedWinding out;
- counts[SIDE_FRONT] = counts[SIDE_BACK] = counts[SIDE_ON] = 0;
- // determine sides for each point
- for ( i = 0; i < numPoints; i++ ) {
- dists[i] = dot = plane.Distance( p[i].ToVec3() );
- if ( dot > epsilon ) {
- sides[i] = SIDE_FRONT;
- } else if ( dot < -epsilon ) {
- sides[i] = SIDE_BACK;
- } else {
- sides[i] = SIDE_ON;
- }
- counts[sides[i]]++;
- }
- if ( !counts[SIDE_BACK] ) {
- if ( !counts[SIDE_FRONT] ) {
- return SIDE_ON;
- }
- else {
- return SIDE_FRONT;
- }
- }
-
- if ( !counts[SIDE_FRONT] ) {
- return SIDE_BACK;
- }
- sides[i] = sides[0];
- dists[i] = dists[0];
-
- out.numPoints = 0;
- back->numPoints = 0;
- for ( i = 0; i < numPoints; i++ ) {
- p1 = &p[i];
- if ( !out.EnsureAlloced( out.numPoints+1, true ) ) {
- return SIDE_FRONT; // can't split -- fall back to original
- }
- if ( !back->EnsureAlloced( back->numPoints+1, true ) ) {
- return SIDE_FRONT; // can't split -- fall back to original
- }
- if ( sides[i] == SIDE_ON ) {
- out.p[out.numPoints] = *p1;
- out.numPoints++;
- back->p[back->numPoints] = *p1;
- back->numPoints++;
- continue;
- }
-
- if ( sides[i] == SIDE_FRONT ) {
- out.p[out.numPoints] = *p1;
- out.numPoints++;
- }
- if ( sides[i] == SIDE_BACK ) {
- back->p[back->numPoints] = *p1;
- back->numPoints++;
- }
-
- if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) {
- continue;
- }
-
- if ( !out.EnsureAlloced( out.numPoints+1, true ) ) {
- return SIDE_FRONT; // can't split -- fall back to original
- }
- if ( !back->EnsureAlloced( back->numPoints+1, true ) ) {
- return SIDE_FRONT; // can't split -- fall back to original
- }
- // generate a split point
- j = i + 1;
- if ( j >= numPoints ) {
- p2 = &p[0];
- }
- else {
- p2 = &p[j];
- }
- dot = dists[i] / (dists[i] - dists[i+1]);
- for ( j = 0; j < 3; j++ ) {
- // avoid round off error when possible
- if ( plane.Normal()[j] == 1.0f ) {
- mid[j] = plane.Dist();
- } else if ( plane.Normal()[j] == -1.0f ) {
- mid[j] = -plane.Dist();
- } else {
- mid[j] = (*p1)[j] + dot * ( (*p2)[j] - (*p1)[j] );
- }
- }
- mid.s = p1->s + dot * ( p2->s - p1->s );
- mid.t = p1->t + dot * ( p2->t - p1->t );
-
- out.p[out.numPoints] = mid;
- out.numPoints++;
- back->p[back->numPoints] = mid;
- back->numPoints++;
- }
- for ( i = 0; i < out.numPoints; i++ ) {
- p[i] = out.p[i];
- }
- numPoints = out.numPoints;
- return SIDE_CROSS;
- }
|