00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "math.h"
00044
00045 #include "OSGConfig.h"
00046
00047 #include <assert.h>
00048
00049 #include "OSGPlane.h"
00050 #include "OSGLine.h"
00051
00052 OSG_USING_NAMESPACE
00053
00079
00080
00081 Plane::Plane(void) :
00082 _normalVec (0.f, 0.f, 0.f),
00083 _distance (0.f ),
00084 _directionIndex (0)
00085 {
00086 }
00087
00088
00089 Plane::Plane(const Plane &obj) :
00090 _normalVec (obj._normalVec ),
00091 _distance (obj._distance ),
00092 _directionIndex (obj._directionIndex)
00093 {
00094 updateDirectionIndex();
00095 }
00096
00097
00103 Plane::Plane(const Pnt3f &p0, const Pnt3f &p1, const Pnt3f &p2)
00104 {
00105 Vec3f vec2(p2 - p0);
00106
00107 _normalVec = p1 - p0;
00108
00109 _normalVec.crossThis(vec2);
00110 _normalVec.normalize();
00111
00112 _distance = _normalVec.dot(p0);
00113
00114 updateDirectionIndex();
00115 }
00116
00117
00118 Plane::Plane(const Vec3f &normal, Real32 distance) :
00119 _normalVec(normal ),
00120 _distance (distance)
00121 {
00122 _normalVec.normalize();
00123
00124 updateDirectionIndex();
00125 }
00126
00127
00128 Plane::Plane(const Vec3f &normal, const Pnt3f &point) :
00129 _normalVec(normal)
00130 {
00131 _normalVec.normalize();
00132
00133 _distance = _normalVec.dot(point);
00134
00135 updateDirectionIndex();
00136 }
00137
00138
00139
00140 Plane::~Plane(void)
00141 {
00142 }
00143
00144
00148 void Plane::offset(Real32 d)
00149 {
00150 _distance += d;
00151 }
00152
00153
00154
00161 bool Plane::intersect(const Plane &pl, Line &is) const
00162 {
00163 Vec3f dir = _normalVec.cross(pl.getNormal());
00164 Pnt3f pnt;
00165
00166 Real32 len = dir.length();
00167
00168 if(len < Eps)
00169 return false;
00170
00171
00172
00173 Real32 abs;
00174 Real32 maxabs = osgabs(dir[0]);
00175 UInt16 index = 0;
00176
00177 if((abs = osgabs(dir[1])) > maxabs)
00178 {
00179 maxabs = abs;
00180 index = 1;
00181 }
00182
00183 if((abs = osgabs(dir[2])) > maxabs)
00184 {
00185 maxabs = abs;
00186 index = 2;
00187 }
00188
00189 switch(index)
00190 {
00191 case 0:
00192 pnt.setValues(
00193 0.f,
00194 (pl.getNormal ()[2] * _distance -
00195 pl.getDistanceFromOrigin() * _normalVec[2]) / dir[0],
00196 (pl.getDistanceFromOrigin() * _normalVec[1] -
00197 pl.getNormal ()[1] * _distance ) / dir[0]);
00198 break;
00199
00200 case 1:
00201 pnt.setValues(
00202 (pl.getDistanceFromOrigin() * _normalVec[2] -
00203 pl.getNormal ()[2] * _distance ) / dir[1],
00204 0.f,
00205 (pl.getNormal ()[0] * _distance -
00206 pl.getDistanceFromOrigin() * _normalVec[0]) / dir[1]);
00207 break;
00208
00209 case 2:
00210 pnt.setValues(
00211 (pl.getNormal ()[1] * _distance -
00212 pl.getDistanceFromOrigin() * _normalVec[1]) / dir[2],
00213 (pl.getDistanceFromOrigin() * _normalVec[0] -
00214 pl.getNormal ()[0] * _distance) / dir[2],
00215 0.f);
00216 break;
00217
00218 default:
00219 return false;
00220 }
00221
00222
00223
00224 dir *= 1.f / len;
00225
00226 is.setValue(pnt, dir);
00227
00228 return true;
00229 }
00230
00235 bool Plane::intersect(const Line &line, Pnt3f &point) const
00236 {
00237 Real32 t;
00238
00239 if(intersect(line, t) == true)
00240 {
00241 point = line.getPosition() + t * line.getDirection();
00242
00243 return true;
00244 }
00245 else
00246 {
00247 return false;
00248 }
00249 }
00250
00255 bool Plane::intersect(const Line &line, Real32 &t) const
00256 {
00257 if(intersectInfinite(line, t) == false || t < 0.f)
00258 {
00259 return false;
00260 }
00261
00262 return true;
00263 }
00264
00271 bool Plane::intersectInfinite(const Line &line, Real32 &t) const
00272 {
00273 Real32 a;
00274
00275 a = _normalVec.dot(line.getDirection());
00276
00277 if(a != 0.0f)
00278 {
00279 t = _normalVec.dot(
00280 Pnt3f(_normalVec * _distance) - line.getPosition()) / a;
00281
00282 return true;
00283 }
00284 else
00285 {
00286 if(_normalVec.dot(line.getPosition()) - _distance == 0.f)
00287 {
00288 t = 0.f;
00289
00290 return true;
00291 }
00292 }
00293
00294 return false;
00295 }
00296
00301 bool Plane::intersectInfinite(const Line &line, Pnt3f &point) const
00302 {
00303 Real32 t;
00304
00305 if(intersectInfinite(line, t) == true)
00306 {
00307 point = line.getPosition() + t * line.getDirection();
00308
00309 return true;
00310 }
00311 else
00312 {
00313 return false;
00314 }
00315 }
00316
00321 int Plane::clip(Pnt3f *polyIn, Pnt3f *polyOut, int count) const
00322 {
00323 Pnt3f i, s, p;
00324 int j, n;
00325
00326 n = 0;
00327 s = polyIn[count-1];
00328
00329 for (j = 0; j < count; j++) {
00330 p = polyIn[j];
00331
00332 if (isInHalfSpace(p)) {
00333 if (isInHalfSpace(s))
00334 polyOut[n++] = p;
00335 else {
00336 Line lp(s, p);
00337 if (intersectInfinite(lp, i)) {
00338 polyOut[n++] = i;
00339 polyOut[n++] = p;
00340 }
00341 }
00342 }
00343 else if (isInHalfSpace(s)) {
00344 Line ls(s, p);
00345 if (intersectInfinite(ls, i))
00346 polyOut[n++] = i;
00347 }
00348
00349 s = p;
00350 }
00351
00352 return n;
00353 }
00354
00355
00356 void Plane::transform(const Matrix &matrix)
00357 {
00358 matrix.mult(_normalVec);
00359
00360 _normalVec.normalize();
00361
00362 Vec3f trans;
00363
00364 trans[0] = matrix[3][0];
00365 trans[1] = matrix[3][1];
00366 trans[2] = matrix[3][2];
00367
00368 trans.projectTo(_normalVec);
00369
00370 UInt32 uiValNorm = getMaxIndexAbs3(_normalVec);
00371 UInt32 uiValPoint = getMaxIndexAbs3( trans);
00372
00373 if(trans[uiValPoint] > Eps || trans[uiValPoint] < -Eps)
00374 {
00375 if((_normalVec[uiValNorm ] < 0.f &&
00376 trans [uiValPoint] < 0.f ) ||
00377 (_normalVec[uiValNorm ] > 0.f &&
00378 trans [uiValPoint] > 0.f ))
00379 {
00380 _distance -= trans.length();
00381 }
00382 else
00383 {
00384 _distance += trans.length();
00385 }
00386 }
00387
00388 updateDirectionIndex();
00389 }
00390
00391 void Plane::updateDirectionIndex(void)
00392 {
00393 UInt8 ind = 0;
00394
00395 if(_normalVec[0] > 0)
00396 ind |= 0x1;
00397 if(_normalVec[1] > 0)
00398 ind |= 0x2;
00399 if(_normalVec[2] > 0)
00400 ind |= 0x4;
00401
00402 _directionIndex = ind;
00403 }
00404
00405
00406 OSG_BEGIN_NAMESPACE
00407
00408
00409 OSG_BASE_DLLMAPPING
00410 bool operator ==(const Plane &p1, const Plane &p2)
00411 {
00412 return ((p1._distance == p2._distance ) &&
00413 (p1._normalVec == p2._normalVec) );
00414 }
00415
00416 OSG_BASE_DLLMAPPING
00417 std::ostream &operator <<(std::ostream &outStream, const Plane &obj)
00418 {
00419 return outStream << obj.getNormal() << ":" << obj.getDistanceFromOrigin();
00420 }
00421
00422 OSG_END_NAMESPACE