# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

plane.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "stdmisc.h"
00027 
00028 #include "nel/misc/plane.h"
00029 
00030 
00031 namespace       NLMISC
00032 {
00033 
00034 
00035 //============================================================
00036 void    CPlane::make(const CVector &normal, const CVector &p)
00037 {
00038         CVector v= normal.normed();
00039         a= v.x;
00040         b= v.y;
00041         c= v.z;
00042         d=-(v*p);               // d=- (ax+by+cz).
00043 }
00044 void    CPlane::make(const CVector &p0, const CVector &p1, const CVector &p2)
00045 {
00046         CVector v;
00047 
00048         v=(p1-p0)^(p2-p1);
00049         make(v,p1);
00050 }
00051 
00052 
00053 //============================================================
00054 bool    CPlane::clipSegmentBack(CVector &p0, CVector &p1) const
00055 {
00056         float   d0,d1,decal;
00057         CVector proj;
00058 
00059         d0= (*this)*p0;
00060         d1= (*this)*p1;
00061         if(d0<0 && d1<0)
00062                 return true;
00063         if(d0>=0 && d1>=0)
00064                 return false;
00065         // Clip line.
00066         decal= (0-d0) / (d1-d0);
00067         proj= p0+ (p1-p0)*decal;
00068         if(d0>=0)
00069                 p0=proj;
00070         else
00071                 p1=proj;
00072         return true;
00073 }
00074 bool    CPlane::clipSegmentFront(CVector &p0, CVector &p1) const
00075 {
00076         float   d0,d1,decal;
00077         CVector proj;
00078 
00079         d0= (*this)*p0;
00080         d1= (*this)*p1;
00081         if(d0>=0 && d1>=0)
00082                 return true;
00083         if(d0<0 && d1<0)
00084                 return false;
00085         // Clip line.
00086         decal= (0-d0) / (d1-d0);
00087         proj= p0+ (p1-p0)*decal;
00088         if(d0<0)
00089                 p0=proj;
00090         else
00091                 p1=proj;
00092         return true;
00093 }
00094 
00095 
00096 //============================================================
00097 sint    CPlane::clipPolygonBack(CVector in[], CVector out[], sint nIn) const
00098 {
00099         sint nOut=0,s,p,i;
00100         if(nIn<=2) return 0;
00101 
00102         s=nIn-1;
00103 
00104         for (i=0;i<nIn;i++)
00105         {
00106                 p=i;
00107                 if ( (*this)*in[p] < 0 )
00108                 {
00109                         if ( (*this)*in[s] > 0 ) 
00110                                 out[nOut++]= intersect(in[s],in[p]);
00111                         out[nOut++]=in[p];
00112                 }
00113                 else
00114                 {
00115                         if ( (*this)*in[s] < 0 ) 
00116                                 out[nOut++]= intersect(in[s],in[p]);
00117                 }
00118                 s=p;
00119         }
00120 
00121         return nOut;
00122 }
00123 
00124 //============================================================
00125 sint    CPlane::clipPolygonFront(CVector in[], CVector out[], sint nIn) const
00126 {
00127         sint nOut=0,s,p,i;
00128         if(nIn<=2) return 0;
00129 
00130         s=nIn-1;
00131 
00132         for (i=0;i<nIn;i++)
00133         {
00134                 p=i;
00135                 if ( (*this)*in[p] > 0 )
00136                 {
00137                         if ( (*this)*in[s] < 0 ) 
00138                                 out[nOut++]= intersect(in[s],in[p]);
00139                         out[nOut++]=in[p];
00140                 }
00141                 else
00142                 {
00143                         if ( (*this)*in[s] > 0 ) 
00144                                 out[nOut++]= intersect(in[s],in[p]);
00145                 }
00146                 s=p;
00147         }
00148 
00149         return nOut;
00150 }
00151 
00152 
00153 //============================================================
00154 CPlane  CPlane::inverted() const
00155 {
00156         return CPlane(-a, -b, -c, -d);
00157 }
00158                 
00159 //============================================================
00160 void    CPlane::invert()
00161 {
00162         a = -a;
00163         b = -b;
00164         c = -c;
00165         d = -d;
00166 }
00167 
00168 
00169 
00170 
00171 }
00172