mPlane.h 22.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------

#ifndef _MPLANE_H_
#define _MPLANE_H_

#ifndef _MPOINT3_H_
#include "math/mPoint3.h"
#endif

#ifndef _MBOX_H_
#include "math/mBox.h"
#endif

#ifndef _MORIENTEDBOX_H_
#include "math/mOrientedBox.h"
#endif

#ifndef _MSPHERE_H_
#include "math/mSphere.h"
#endif


/// A 3D plane defined by a normal and a distance along the normal.
///
/// @note The distance is stored negative.
class PlaneF : public Point3F
{
   public:

      /// *NEGATIVE* distance along the xyz normal.
      F32 d;

      /// Return the plane's normal.
      const Point3F& getNormal() const { return *this; }

      /// Return the plane's position.
      Point3F getPosition() const { return Point3F( x, y, z ) * -d; }

      bool isHorizontal() const;
      bool isVertical() const;

      bool isParallelTo( const PlaneF& plane, F32 epsilon = POINT_EPSILON ) const;

      bool isPerpendicularTo( const PlaneF& plane, F32 epsilon = POINT_EPSILON ) const;

      /// @name Initialization
      /// @{

      PlaneF() {}
      PlaneF( const Point3F& p, const Point3F& n );
      /// NOTE: d is the NEGATIVE distance along the xyz normal.
      PlaneF( F32 _x, F32 _y, F32 _z, F32 _d);
      PlaneF( const Point3F& j, const Point3F& k, const Point3F& l );

      void set( F32 _x, F32 _y, F32 _z );
      /// NOTE: d is the NEGATIVE distance along the xyz normal.
      void set( F32 _x, F32 _y, F32 _z, F32 _d );
      void set( const Point3F& p, const Point3F& n);
      void set( const Point3F& k, const Point3F& j, const Point3F& l );
      void setPoint(const Point3F &p); // assumes the x,y,z fields are already set
                                           // creates an un-normalized plane

      void setXY(F32 zz);
      void setYZ(F32 xx);
      void setXZ(F32 yy);
      void setXY(const Point3F& P, F32 dir);
      void setYZ(const Point3F& P, F32 dir);
      void setXZ(const Point3F& P, F32 dir);

      /// @}

      void shiftX(F32 xx);
      void shiftY(F32 yy);
      void shiftZ(F32 zz);
      void invert();
      void neg();

      /// Return the distance of the given point from the plane.
      F32 distToPlane( const Point3F& cp ) const;

      /// Project the given point onto the plane.
      Point3F project(const Point3F &pt) const;

      /// @name Intersection
      /// @{

      enum Side
      {
         Front = 1,
         On    = 0,
         Back  = -1
      };

      /// Compute on which side of the plane the given point lies.
      Side whichSide( const Point3F& cp ) const;

      /// Compute which side the given sphere lies on.
      Side whichSide( const SphereF& sphere ) const;

      /// Compute which side the given AABB lies on.
      Side whichSide( const Box3F& aabb ) const;

      /// Compute which side the given OBB lies on.
      Side whichSide( const OrientedBox3F& obb ) const;

      /// Compute which side the given box lies on.
      ///
      /// @param center Center point.
      /// @param axisx X-Axis vector.  Length is box half-extent along X.
      /// @param axisy Y-Axis vector.  Length is box half-extent along Y.
      /// @param axisz Z-Axis vector.  Length is box half-extent along Z.
      ///
      /// @return Side that the given box is on.
      Side whichSideBox( const Point3F& center,
                         const Point3F& axisx,
                         const Point3F& axisy,
                         const Point3F& axisz ) const;

      /// @}

      /// @name Intersection
      /// @{

      /// Compute the distance at which the ray traveling from @a start in the direction
      /// of @end intersects the plane
      /// @param start Starting point of the ray.
      /// @param end Point in the direction of which the ray travels from @a start.
      /// @return The distance (as a fraction/multiple of the length of the vector between
      ///   @a start and @a end) at which the ray intersects the plane or PARALLEL_PLANE if the
      ///   ray is parallel to the plane.
      F32 intersect( const Point3F &start, const Point3F &end ) const;

      /// Compute the intersection of the ray that emanates at @a start in the direction
      /// @dir.
      /// @param start Point where the ray emanates.
      /// @param dir Direction in which the ray travels.  Must be normalized.
      /// @param outHit Resulting intersection point.  Only set if there indeed is a hit.
      /// @return True if the ray intersects the plane or false if not.
      bool intersect( const Point3F &start, const Point3F &dir, Point3F *outHit ) const;

      /// Compute the intersection between two planes.
      ///
      /// @param plane Plane to intersect with.
      /// @param outLineOrigin Used to store the origin of the resulting intersection line.
      /// @param outLineDirection Used to store the direction of the resulting intersection line.
      ///
      /// @return True if there is an intersection or false if the two planes are coplanar.
      bool intersect( const PlaneF& plane, Point3F& outLineOrigin, Point3F& outLineDirection ) const;

      /// @}

      /// @name Clipping
      /// @{

      /// Clip a convex polygon by the plane.
      ///
      /// The resulting polygon will be the input polygon minus the part on the negative side
      /// of the plane.  The input polygon must be convex and @a inVertices must be in CCW or CW order.
      ///
      /// @param inVertices Array holding the vertices of the input polygon.
      /// @param inNumVertices Number of vertices in @a inVertices.
      /// @param outVertices Array to hold the vertices of the clipped polygon.  Must have space for one additional
      ///   vertex in case the polygon is split by the plane such that an additional vertex appears.  Must not
      ///   be the same as @a inVertices.
      /// @return Number of vertices in the clipped polygon, i.e. number of vertices in @a outVertices.
      ///
      /// @note Be aware that if the polygon fully lies on the negative side of the plane,
      ///   the resulting @a outNumVertices will be zero, i.e. no polygon will result from the clip.
      U32 clipPolygon( const Point3F* inVertices, U32 inNumVertices, Point3F* outVertices ) const;

      /// Clip a line segment by the plane.
      ///
      /// @param start Start point of the line segment.
      /// @param end End point of the line segment.
      /// @param outNewEnd New end point if there is an intersection with the plane.
      ///
      /// @return True
      bool clipSegment( const Point3F& start, const Point3F& end, Point3F& outNewEnd ) const;

      /// @}
};

#define PARALLEL_PLANE  1e20f

#define PlaneSwitchCode(s, e) (s * 3 + e)


//---------------------------------------------------------------------------

inline PlaneF::PlaneF( F32 _x, F32 _y, F32 _z, F32 _d )
{
   set( _x, _y, _z, _d );
}

//-----------------------------------------------------------------------------

inline PlaneF::PlaneF( const Point3F& p, const Point3F& n )
{
   set(p,n);
}

//-----------------------------------------------------------------------------

inline PlaneF::PlaneF( const Point3F& j, const Point3F& k, const Point3F& l )
{
   set(j,k,l);
}

//-----------------------------------------------------------------------------

inline void PlaneF::setXY( F32 zz )
{
   x = y = 0.0f; z = 1.0f; d = -zz;
}

//-----------------------------------------------------------------------------

inline void PlaneF::setYZ( F32 xx )
{
   x = 1.0f; z = y = 0.0f; d = -xx;
}

//-----------------------------------------------------------------------------

inline void PlaneF::setXZ( F32 yy )
{
   x = z = 0.0f; y = 1.0f; d = -yy;
}

//-----------------------------------------------------------------------------

inline void PlaneF::setXY(const Point3F& point, F32 dir)       // Normal = (0, 0, -1|1)
{
   x = y = 0.0f;
   d = -((z = dir) * point.z);
}

//-----------------------------------------------------------------------------

inline void PlaneF::setYZ(const Point3F& point, F32 dir)       // Normal = (-1|1, 0, 0)
{
   z = y = 0.0f;
   d = -((x = dir) * point.x);
}

//-----------------------------------------------------------------------------

inline void PlaneF::setXZ(const Point3F& point, F32 dir)       // Normal = (0, -1|1, 0)
{
   x = z = 0.0f;
   d = -((y = dir) * point.y);
}

//-----------------------------------------------------------------------------

inline void PlaneF::shiftX( F32 xx )
{
   d -= xx * x;
}

//-----------------------------------------------------------------------------

inline void PlaneF::shiftY( F32 yy )
{
   d -= yy * y;
}

//-----------------------------------------------------------------------------

inline void PlaneF::shiftZ( F32 zz )
{
   d -= zz * z;
}

//-----------------------------------------------------------------------------

inline bool PlaneF::isHorizontal() const
{
   return (x == 0.0f && y == 0.0f) ? true : false;
}

//-----------------------------------------------------------------------------

inline bool PlaneF::isVertical() const
{
    return ((x != 0.0f || y != 0.0f) && z == 0.0f) ? true : false;
}

//-----------------------------------------------------------------------------

inline Point3F PlaneF::project(const Point3F &pt) const
{
   F32 dist = distToPlane(pt);
   return Point3F(pt.x - x * dist, pt.y - y * dist, pt.z - z * dist);
}

//-----------------------------------------------------------------------------

inline F32 PlaneF::distToPlane( const Point3F& cp ) const
{
   // return mDot(*this,cp) + d;
   return (x * cp.x + y * cp.y + z * cp.z) + d;
}

//-----------------------------------------------------------------------------

inline PlaneF::Side PlaneF::whichSide(const Point3F& cp) const
{
   F32 dist = distToPlane(cp);
   if (dist >= 0.005f)                 // if (mFabs(dist) < 0.005f)
      return Front;                    //    return On;
   else if (dist <= -0.005f)           // else if (dist > 0.0f)
      return Back;                     //    return Front;
   else                                // else
      return On;                       //    return Back;
}

//-----------------------------------------------------------------------------

inline PlaneF::Side PlaneF::whichSide( const SphereF& sphere ) const
{
   const F32 dist = distToPlane( sphere.center );
   if( dist > sphere.radius )
      return Front;
   else if( dist < - sphere.radius )
      return Back;
   else
      return On;
}

//-----------------------------------------------------------------------------

inline PlaneF::Side PlaneF::whichSide( const Box3F& aabb ) const
{
   // See Graphics Gems IV, 1.7 and "A Faster Overlap Test for a Plane and a Bounding Box"
   // (http://replay.waybackmachine.org/19981203032829/http://www.cs.unc.edu/~hoff/research/vfculler/boxplane.html)
   // for details.

   Point3F pVertex;
   Point3F nVertex;

   pVertex.x = ( x > 0.0f ) ? aabb.maxExtents.x : aabb.minExtents.x;
   pVertex.y = ( y > 0.0f ) ? aabb.maxExtents.y : aabb.minExtents.y;
   pVertex.z = ( z > 0.0f ) ? aabb.maxExtents.z : aabb.minExtents.z;

   if( whichSide( pVertex ) == Back )
      return Back;

   nVertex.x = ( x > 0.0f ) ? aabb.minExtents.x : aabb.maxExtents.x;
   nVertex.y = ( y > 0.0f ) ? aabb.minExtents.y : aabb.maxExtents.y;
   nVertex.z = ( z > 0.0f ) ? aabb.minExtents.z : aabb.maxExtents.z;

   if( whichSide( nVertex ) == Front )
      return Front;

   return On;
}

//-----------------------------------------------------------------------------

inline PlaneF::Side PlaneF::whichSide( const OrientedBox3F& obb ) const
{
   // Project the box onto the line defined by the plane center and normal.
   // See "3D Game Engine Design" chapter 4.3.2.

   const F32 r = obb.getHalfExtents().x * mFabs( mDot( obb.getAxis( 0 ), *this ) ) +
                 obb.getHalfExtents().y * mFabs( mDot( obb.getAxis( 1 ), *this ) ) +
                 obb.getHalfExtents().z * mFabs( mDot( obb.getAxis( 2 ), *this ) );

   const F32 dist = distToPlane( obb.getCenter() );
   if( dist > r )
      return Front;
   else if( dist < - r )
      return Back;
   else
      return On;
}

//-----------------------------------------------------------------------------

inline PlaneF::Side PlaneF::whichSideBox(const Point3F& center,
                                         const Point3F& axisx,
                                         const Point3F& axisy,
                                         const Point3F& axisz) const
{
   F32 baseDist = distToPlane(center);

   F32 compDist = mFabs(mDot(axisx, *this)) +
      mFabs(mDot(axisy, *this)) +
      mFabs(mDot(axisz, *this));

   if (baseDist >= compDist)
      return Front;
   else if (baseDist <= -compDist)
      return Back;
   else
      return On;
}

inline void PlaneF::set( F32 _x, F32 _y, F32 _z )
{
    Point3F::set(_x,_y,_z);
}

inline void PlaneF::set( F32 _x, F32 _y, F32 _z, F32 _d )
{
    Point3F::set(_x,_y,_z);
    d = _d;
}

//---------------------------------------------------------------------------
/// Calculate the coefficients of the plane passing through
/// a point with the given normal.
inline void PlaneF::setPoint(const Point3F &p)
{
   d = -(p.x * x + p.y * y + p.z * z);
}

inline void PlaneF::set( const Point3F& p, const Point3F& n )
{
   x = n.x; y = n.y; z = n.z;
   normalize();

   // Calculate the last plane coefficient.

   d = -(p.x * x + p.y * y + p.z * z);
}

//---------------------------------------------------------------------------
// Calculate the coefficients of the plane passing through
// three points.  Basically it calculates the normal to the three
// points then calculates a plane through the middle point with that
// normal.
inline void PlaneF::set( const Point3F& k, const Point3F& j, const Point3F& l )
{
   // Point3F kj, lj, pv;
   // kj = k - j;
   // lj = l - j;
   // mCross( kj, lj, &pv );
   // set(j, pv);

   // Inline for speed...
   const F32 ax = k.x-j.x;
   const F32 ay = k.y-j.y;
   const F32 az = k.z-j.z;
   const F32 bx = l.x-j.x;
   const F32 by = l.y-j.y;
   const F32 bz = l.z-j.z;
   x = ay*bz - az*by;
   y = az*bx - ax*bz;
   z = ax*by - ay*bx;
      
   m_point3F_normalize( (F32 *)(&x) );
   d = -(j.x * x + j.y * y + j.z * z);
}

inline void PlaneF::invert()
{
   x = -x;
   y = -y;
   z = -z;
   d = -d;
}

inline void PlaneF::neg()
{
   invert();
}

inline F32 PlaneF::intersect(const Point3F &p1, const Point3F &p2) const
{
   const F32 den = mDot(p2 - p1, *this);
   if( mIsZero( den ) )
      return PARALLEL_PLANE;
   return -distToPlane(p1) / den;
}

inline bool PlaneF::intersect( const Point3F &start, const Point3F &dir, Point3F *outHit ) const
{
   const F32 den = mDot( dir, *this );
   if ( mIsZero( den ) )
      return false;

   F32 dist = -distToPlane( start ) / den;
   *outHit = start + dir * dist;
   return true;
}

class PlaneD: public Point3D
{
public:
   /// NOTE: d is the NEGATIVE distance along the xyz normal.
   F64 d;

   PlaneD();
   PlaneD( const PlaneF& copy);
   PlaneD( const Point3D& p, const Point3D& n );
   /// NOTE: d is the NEGATIVE distance along the xyz normal.
   PlaneD( F64 _x, F64 _y, F64 _z, F64 _d);
   PlaneD( const Point3D& j, const Point3D& k, const Point3D& l );

   // Methods
   //using Point3D::set;
    void set(const F64 _x, const F64 _y, const F64 _z);

   void     set( const Point3D& p, const Point3D& n);
   void     set( const Point3D& k, const Point3D& j, const Point3D& l );
   void     setPoint(const Point3D &p); // assumes the x,y,z fields are already set
                                        // creates an un-normalized plane

   void     setXY(F64 zz);
   void     setYZ(F64 xx);
   void     setXZ(F64 yy);
   void     setXY(const Point3D& P, F64 dir);
   void     setYZ(const Point3D& P, F64 dir);
   void     setXZ(const Point3D& P, F64 dir);
   void     shiftX(F64 xx);
   void     shiftY(F64 yy);
   void     shiftZ(F64 zz);
   void     invert();
   void     neg();
   Point3D  project(const Point3D &pt) const; // projects the point onto the plane.

   F64      distToPlane( const Point3D& cp ) const;

   enum Side {
      Front = 1,
      On    = 0,
      Back  = -1
   };

   Side whichSide(const Point3D& cp) const;
   F64  intersect(const Point3D &start, const Point3D &end) const;
   //DLLAPI bool     split( const Poly3F& poly, Poly3F* front, Poly3F* back );

   bool     isHorizontal() const;
   bool     isVertical() const;

   Side whichSideBox(const Point3D& center,
                     const Point3D& axisx,
                     const Point3D& axisy,
                     const Point3D& axisz) const;
};
//#define PARALLEL_PLANE  1e20f

//#define PlaneSwitchCode(s, e) (s * 3 + e)


//---------------------------------------------------------------------------

inline PlaneD::PlaneD()
{
}

inline PlaneD::
   PlaneD( F64 _x, F64 _y, F64 _z, F64 _d )
{
   x = _x; y = _y; z = _z; d = _d;
}

inline PlaneD::PlaneD( const PlaneF& copy)
{
   x = copy.x; y = copy.y; z = copy.z; d = copy.d;
}

inline PlaneD::PlaneD( const Point3D& p, const Point3D& n )
{
   set(p,n);
}

inline PlaneD::PlaneD( const Point3D& j, const Point3D& k, const Point3D& l )
{
   set(j,k,l);
}

inline void PlaneD::setXY( F64 zz )
{
   x = y = 0; z = 1; d = -zz;
}

inline void PlaneD::setYZ( F64 xx )
{
   x = 1; z = y = 0; d = -xx;
}

inline void PlaneD::setXZ( F64 yy )
{
   x = z = 0; y = 1; d = -yy;
}

inline void PlaneD::setXY(const Point3D& point, F64 dir)       // Normal = (0, 0, -1|1)
{
   x = y = 0; 
   d = -((z = dir) * point.z);
}

inline void PlaneD::setYZ(const Point3D& point, F64 dir)       // Normal = (-1|1, 0, 0)
{
   z = y = 0; 
   d = -((x = dir) * point.x);
}

inline void PlaneD::setXZ(const Point3D& point, F64 dir)       // Normal = (0, -1|1, 0)
{
   x = z = 0; 
   d = -((y = dir) * point.y);
}

inline void PlaneD::shiftX( F64 xx )
{
   d -= xx * x;
}

inline void PlaneD::shiftY( F64 yy )
{
   d -= yy * y;
}

inline void PlaneD::shiftZ( F64 zz )
{
   d -= zz * z;
}

inline bool PlaneD::isHorizontal() const
{
   return (x == 0 && y == 0) ? true : false;
}

inline bool PlaneD::isVertical() const
{
    return ((x != 0 || y != 0) && z == 0) ? true : false;
}

inline Point3D PlaneD::project(const Point3D &pt) const
{
   F64 dist = distToPlane(pt);
   return Point3D(pt.x - x * dist, pt.y - y * dist, pt.z - z * dist);
}

inline F64 PlaneD::distToPlane( const Point3D& cp ) const
{
   // return mDot(*this,cp) + d;
   return (x * cp.x + y * cp.y + z * cp.z) + d;
}

inline PlaneD::Side PlaneD::whichSide(const Point3D& cp) const
{
   F64 dist = distToPlane(cp);
   if (dist >= 0.005f)                 // if (mFabs(dist) < 0.005f)
      return Front;                    //    return On;
   else if (dist <= -0.005f)           // else if (dist > 0.0f)
      return Back;                     //    return Front;
   else                                // else
      return On;                       //    return Back;
}

inline void PlaneD::set(const F64 _x, const F64 _y, const F64 _z)
{
    Point3D::set(_x,_y,_z); 
}

//---------------------------------------------------------------------------
// Calculate the coefficients of the plane passing through 
// a point with the given normal.

////inline void PlaneD::set( const Point3D& p, const Point3D& n )
inline void PlaneD::setPoint(const Point3D &p)
{
   d = -(p.x * x + p.y * y + p.z * z);
}

inline void PlaneD::set( const Point3D& p, const Point3D& n )
{
   x = n.x; y = n.y; z = n.z;
   normalize();

   // Calculate the last plane coefficient.

   d = -(p.x * x + p.y * y + p.z * z);
}

//---------------------------------------------------------------------------
// Calculate the coefficients of the plane passing through 
// three points.  Basically it calculates the normal to the three
// points then calculates a plane through the middle point with that
// normal.

inline void PlaneD::set( const Point3D& k, const Point3D& j, const Point3D& l )
{
//   Point3D kj,lj,pv;
//   kj = k;
//   kj -= j;
//   lj = l;
//   lj -= j;
//   mCross( kj, lj, &pv );
//   set(j,pv);

// Above ends up making function calls up the %*#...
// This is called often enough to be a little more direct
// about it (sqrt should become intrinsic in the future)...
   F64 ax = k.x-j.x;
   F64 ay = k.y-j.y;
   F64 az = k.z-j.z;
   F64 bx = l.x-j.x;
   F64 by = l.y-j.y;
   F64 bz = l.z-j.z;
   x = ay*bz - az*by;
   y = az*bx - ax*bz;
   z = ax*by - ay*bx;
   F64 squared = x*x + y*y + z*z;
   AssertFatal(squared != 0.0, "Error, no plane possible!");

   // In non-debug mode
   if (squared != 0) 
   {
      F64 invSqrt = 1.0 / (F64)mSqrt(x*x + y*y + z*z);
      x *= invSqrt;
      y *= invSqrt;
      z *= invSqrt;
      d = -(j.x * x + j.y * y + j.z * z);
   }
   else 
   {
      x = 0;
      y = 0;
      z = 1;
      d = -(j.x * x + j.y * y + j.z * z);
   }
}

inline void PlaneD::invert()
{
   x = -x;
   y = -y;
   z = -z;
   d = -d;
}

inline void PlaneD::neg()
{
   invert();
}

inline F64 PlaneD::intersect(const Point3D &p1, const Point3D &p2) const
{
   F64 den = mDot(p2 - p1, *this);
   if(den == 0)
      return PARALLEL_PLANE;
   return -distToPlane(p1) / den;
}

inline PlaneD::Side PlaneD::whichSideBox(const Point3D& center,
                                         const Point3D& axisx,
                                         const Point3D& axisy,
                                         const Point3D& axisz) const
{
   F64 baseDist = distToPlane(center);

   F64 compDist = mFabs(mDot(axisx, *this)) +
                  mFabs(mDot(axisy, *this)) +
                  mFabs(mDot(axisz, *this));

   // Intersects
   if (baseDist >= compDist)
      return Front;
   else if (baseDist <= -compDist)
      return Back;
   else
      return On;

//   if (compDist > mFabs(baseDist))
//      return On;
//   else
//      return baseDist < 0.0 ? Back : Front;
}

#endif // _MPLANE_H_