Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / misc / plane.cpp
blob7da91745990122922b7db3223295af373b522a4b
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdmisc.h"
19 #include "nel/misc/plane.h"
20 #include "nel/misc/uv.h"
22 #ifdef DEBUG_NEW
23 #define new DEBUG_NEW
24 #endif
26 namespace NLMISC
30 //============================================================
31 void CPlane::make(const CVector &normal, const CVector &p)
33 CVector v= normal.normed();
34 a= v.x;
35 b= v.y;
36 c= v.z;
37 d=-(v*p); // d=- (ax+by+cz).
39 void CPlane::make(const CVector &p0, const CVector &p1, const CVector &p2)
41 CVector v;
43 v=(p1-p0)^(p2-p1);
44 make(v,p1);
48 //============================================================
49 bool CPlane::clipSegmentBack(CVector &p0, CVector &p1) const
51 float d0,d1,decal;
52 CVector proj;
54 d0= (*this)*p0;
55 d1= (*this)*p1;
56 if(d0<0 && d1<0)
57 return true;
58 if(d0>=0 && d1>=0)
59 return false;
60 // Clip line.
61 decal= (0-d0) / (d1-d0);
62 proj= p0+ (p1-p0)*decal;
63 if(d0>=0)
64 p0=proj;
65 else
66 p1=proj;
67 return true;
69 bool CPlane::clipSegmentFront(CVector &p0, CVector &p1) const
71 float d0,d1,decal;
72 CVector proj;
74 d0= (*this)*p0;
75 d1= (*this)*p1;
76 if(d0>=0 && d1>=0)
77 return true;
78 if(d0<0 && d1<0)
79 return false;
80 // Clip line.
81 decal= (0-d0) / (d1-d0);
82 proj= p0+ (p1-p0)*decal;
83 if(d0<0)
84 p0=proj;
85 else
86 p1=proj;
87 return true;
90 //============================================================
91 sint CPlane::clipPolygonBack(CVector in[], CVector out[], sint nIn) const
93 sint nOut=0,s,p,i;
94 if(nIn<=2) return 0;
96 s=nIn-1;
98 for (i=0;i<nIn;i++)
100 p=i;
101 if ( (*this)*in[p] < 0 )
103 if ( (*this)*in[s] >= 0 )
104 out[nOut++]= intersect(in[s],in[p]);
105 out[nOut++]=in[p];
107 else
109 if ( (*this)*in[s] < 0 )
110 out[nOut++]= intersect(in[s],in[p]);
112 s=p;
115 return nOut;
118 //============================================================
119 sint CPlane::clipPolygonBack(const CVector in[], const CUV inUV[], CVector out[], CUV outUV[], sint nIn) const
121 sint nOut=0,s,p,i;
122 if(nIn<=2) return 0;
124 s=nIn-1;
126 for (i=0;i<nIn;i++)
128 p=i;
129 float dp3Curr = (*this)*in[p];
130 float dp3Prev = (*this)*in[s];
131 if ( dp3Curr < 0 )
133 if (dp3Prev >= 0 )
135 float lambda = favoid0((float) ((double) dp3Prev / ((double) dp3Prev - (double) dp3Curr)));
136 out[nOut] = blend(in[s], in[p], lambda);
137 outUV[nOut++] = blend(inUV[s], inUV[p], lambda);
139 out[nOut]=in[p];
140 outUV[nOut++]=inUV[p];
142 else
144 if (dp3Prev < 0 )
146 float lambda = favoid0((float) ((double) dp3Prev / ((double) dp3Prev - (double) dp3Curr)));
147 out[nOut] = blend(in[s], in[p], lambda);
148 outUV[nOut++] = blend(inUV[s], inUV[p], lambda);
151 s=p;
154 return nOut;
157 //============================================================
158 sint CPlane::clipPolygonFront(CVector in[], CVector out[], sint nIn) const
160 sint nOut=0,s,p,i;
161 if(nIn<=2) return 0;
163 s=nIn-1;
165 for (i=0;i<nIn;i++)
167 p=i;
168 if ( (*this)*in[p] > 0 )
170 if ( (*this)*in[s] <= 0 )
171 out[nOut++]= intersect(in[s],in[p]);
172 out[nOut++]=in[p];
174 else
176 if ( (*this)*in[s] > 0 )
177 out[nOut++]= intersect(in[s],in[p]);
179 s=p;
182 return nOut;
186 //============================================================
187 CPlane CPlane::inverted() const
189 return CPlane(-a, -b, -c, -d);
192 //============================================================
193 void CPlane::invert()
195 a = -a;
196 b = -b;
197 c = -c;
198 d = -d;