Merge branch '138-toggle-free-look-with-hotkey' into 'main/atys-live'
[ryzomcore.git] / nel / src / misc / triangle.cpp
blob234eb05062ac587f8e90f0cb60ab84a487aa212d
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/triangle.h"
20 #include "nel/misc/plane.h"
21 #include "nel/misc/matrix.h"
23 #ifdef DEBUG_NEW
24 #define new DEBUG_NEW
25 #endif
27 namespace NLMISC
30 #define EPSILON 0.0001f
31 // ***************************************************************************
32 bool CTriangle::intersect (const CVector& p0, const CVector& p1, CVector& hit, const CPlane& plane) const
34 CVector normal = plane.getNormal();
36 float np1 = normal*p1;
37 float np2 = np1-normal*p0;
39 if (np2 == 0.0f)
40 return false;
42 float lambda = (plane.d+np1)/np2;
44 // Checks the intersection belongs to the segment
45 if (lambda < -EPSILON || lambda > 1.0f+EPSILON)
46 return false;
48 // The intersection on the plane
49 hit = p0*lambda+p1*(1.0f-lambda);
51 float d0 = ((V1-V0)^normal)*(hit-V0);
52 float d1 = ((V2-V1)^normal)*(hit-V1);
53 float d2 = ((V0-V2)^normal)*(hit-V2);
55 return (d0 < +EPSILON && d1 < +EPSILON && d2 < +EPSILON) ||
56 (d0 > -EPSILON && d1 > -EPSILON && d2 > -EPSILON);
61 // ***************************************************************************
62 void CTriangle::computeGradient(float c0, float c1, float c2, CVector &grad) const
64 // Compute basis for 2D triangle.
65 CVector locI, locJ, locK;
66 locI= V1-V0;
67 locJ= V2-V0;
68 locK= locI^locJ;
69 locK.normalize();
70 locI.normalize();
71 locJ= locK^locI;
73 // compute triangle in 2D.
74 CTriangle tri2D;
75 tri2D.V0.set(0,0,0);
76 tri2D.V1.x= (V1-V0)*locI;
77 tri2D.V1.y= (V1-V0)*locJ;
78 tri2D.V1.z= 0;
79 tri2D.V2.x= (V2-V0)*locI;
80 tri2D.V2.y= (V2-V0)*locJ;
81 tri2D.V2.z= 0;
83 // Compute 2 2D Gradients.
84 float dx01= tri2D.V0.x - tri2D.V2.x;
85 float dx02= tri2D.V1.x - tri2D.V2.x;
86 float dy01= tri2D.V0.y - tri2D.V2.y;
87 float dy02= tri2D.V1.y - tri2D.V2.y;
88 float dc01= c0 - c2;
89 float dc02= c1 - c2;
90 float gd= dx02*dy01 - dx01*dy02;
92 float OOgd;
93 if(gd!=0)
94 OOgd= 1.0f/gd;
95 else
96 OOgd= 1; // for now, do not manage correctly this case.
97 float gx, gy;
98 gx= (dc02*dy01 - dc01*dy02) * OOgd;
99 gy= (dc01*dx02 - dc02*dx01) * OOgd;
101 // transform in 3D.
102 grad= locI*gx + locJ*gy;
105 // ***************************************************************************
106 void CTriangle::applyMatrix(const CMatrix &m, CTriangle &dest) const
108 dest.V0 = m * V0;
109 dest.V1 = m * V1;
110 dest.V2 = m * V2;
115 } // NLMISC