1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
19 #include "nel/misc/triangle.h"
20 #include "nel/misc/plane.h"
21 #include "nel/misc/matrix.h"
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
;
42 float lambda
= (plane
.d
+np1
)/np2
;
44 // Checks the intersection belongs to the segment
45 if (lambda
< -EPSILON
|| lambda
> 1.0f
+EPSILON
)
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
;
73 // compute triangle in 2D.
76 tri2D
.V1
.x
= (V1
-V0
)*locI
;
77 tri2D
.V1
.y
= (V1
-V0
)*locJ
;
79 tri2D
.V2
.x
= (V2
-V0
)*locI
;
80 tri2D
.V2
.y
= (V2
-V0
)*locJ
;
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
;
90 float gd
= dx02
*dy01
- dx01
*dy02
;
96 OOgd
= 1; // for now, do not manage correctly this case.
98 gx
= (dc02
*dy01
- dc01
*dy02
) * OOgd
;
99 gy
= (dc01
*dx02
- dc02
*dx01
) * OOgd
;
102 grad
= locI
*gx
+ locJ
*gy
;
105 // ***************************************************************************
106 void CTriangle::applyMatrix(const CMatrix
&m
, CTriangle
&dest
) const