2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
21 #include "Math/XYZ.hpp"
23 bool PointInTriangle(XYZ* p, XYZ normal, XYZ* p1, XYZ* p2, XYZ* p3)
25 static float u0, u1, u2;
26 static float v0, v1, v2;
30 static bool bInter = 0;
31 static float pointv[3];
35 static float normalv[3];
55 normalv[0] = normal.x;
56 normalv[1] = normal.y;
57 normalv[2] = normal.z;
59 #define ABS(X) (((X) < 0.f) ? -(X) : (X))
60 #define MAX(A, B) (((A) < (B)) ? (B) : (A))
61 max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2]));
63 if (max == ABS(normalv[0])) {
67 if (max == ABS(normalv[1])) {
71 if (max == ABS(normalv[2])) {
77 u0 = pointv[i] - p1v[i];
78 v0 = pointv[j] - p1v[j];
84 if (u1 > -1.0e-05f && u1 < 1.0e-05f) { // == 0.0f)
86 if (0.0f <= b && b <= 1.0f) {
87 a = (v0 - b * v2) / v1;
88 if ((a >= 0.0f) && ((a + b) <= 1.0f))
92 b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1);
93 if (0.0f <= b && b <= 1.0f) {
94 a = (u0 - b * u2) / u1;
95 if ((a >= 0.0f) && ((a + b) <= 1.0f))
103 bool LineFacet(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ* p)
106 static float denom, mu;
109 //Calculate the parameters for the plane
110 n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y);
111 n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z);
112 n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x);
114 d = -n.x * pa.x - n.y * pa.y - n.z * pa.z;
116 //Calculate the position on the line that intersects the plane
117 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
118 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
120 mu = -(d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
121 p->x = p1.x + mu * (p2.x - p1.x);
122 p->y = p1.y + mu * (p2.y - p1.y);
123 p->z = p1.z + mu * (p2.z - p1.z);
124 if (mu < 0 || mu > 1) // Intersection not along line segment
127 if (!PointInTriangle(p, n, &pa, &pb, &pc)) {
134 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ* p)
137 static float denom, mu;
140 //Calculate the parameters for the plane
141 n.x = (pb.y - pa.y) * (pc.z - pa.z) - (pb.z - pa.z) * (pc.y - pa.y);
142 n.y = (pb.z - pa.z) * (pc.x - pa.x) - (pb.x - pa.x) * (pc.z - pa.z);
143 n.z = (pb.x - pa.x) * (pc.y - pa.y) - (pb.y - pa.y) * (pc.x - pa.x);
145 d = -n.x * pa.x - n.y * pa.y - n.z * pa.z;
147 //Calculate the position on the line that intersects the plane
148 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
149 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
151 mu = -(d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
152 p->x = p1.x + mu * (p2.x - p1.x);
153 p->y = p1.y + mu * (p2.y - p1.y);
154 p->z = p1.z + mu * (p2.z - p1.z);
155 if (mu < 0 || mu > 1) // Intersection not along line segment
158 if (!PointInTriangle(p, n, &pa, &pb, &pc)) {
165 float LineFacetd(XYZ p1, XYZ p2, XYZ pa, XYZ pb, XYZ pc, XYZ n, XYZ* p)
168 static float denom, mu;
170 //Calculate the parameters for the plane
171 d = -n.x * pa.x - n.y * pa.y - n.z * pa.z;
173 //Calculate the position on the line that intersects the plane
174 denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
175 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
177 mu = -(d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
178 p->x = p1.x + mu * (p2.x - p1.x);
179 p->y = p1.y + mu * (p2.y - p1.y);
180 p->z = p1.z + mu * (p2.z - p1.z);
181 if (mu < 0 || mu > 1) // Intersection not along line segment
184 if (!PointInTriangle(p, n, &pa, &pb, &pc)) {
190 float LineFacetd(XYZ* p1, XYZ* p2, XYZ* pa, XYZ* pb, XYZ* pc, XYZ* p)
193 static float denom, mu;
196 //Calculate the parameters for the plane
197 n.x = (pb->y - pa->y) * (pc->z - pa->z) - (pb->z - pa->z) * (pc->y - pa->y);
198 n.y = (pb->z - pa->z) * (pc->x - pa->x) - (pb->x - pa->x) * (pc->z - pa->z);
199 n.z = (pb->x - pa->x) * (pc->y - pa->y) - (pb->y - pa->y) * (pc->x - pa->x);
201 d = -n.x * pa->x - n.y * pa->y - n.z * pa->z;
203 //Calculate the position on the line that intersects the plane
204 denom = n.x * (p2->x - p1->x) + n.y * (p2->y - p1->y) + n.z * (p2->z - p1->z);
205 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
207 mu = -(d + n.x * p1->x + n.y * p1->y + n.z * p1->z) / denom;
208 p->x = p1->x + mu * (p2->x - p1->x);
209 p->y = p1->y + mu * (p2->y - p1->y);
210 p->z = p1->z + mu * (p2->z - p1->z);
211 if (mu < 0 || mu > 1) // Intersection not along line segment
214 if (!PointInTriangle(p, n, pa, pb, pc)) {
220 float LineFacetd(XYZ* p1, XYZ* p2, XYZ* pa, XYZ* pb, XYZ* pc, XYZ* n, XYZ* p)
223 static float denom, mu;
225 //Calculate the parameters for the plane
226 d = -n->x * pa->x - n->y * pa->y - n->z * pa->z;
228 //Calculate the position on the line that intersects the plane
229 denom = n->x * (p2->x - p1->x) + n->y * (p2->y - p1->y) + n->z * (p2->z - p1->z);
230 if (fabs(denom) < 0.0000001) // Line and plane don't intersect
232 mu = -(d + n->x * p1->x + n->y * p1->y + n->z * p1->z) / denom;
233 p->x = p1->x + mu * (p2->x - p1->x);
234 p->y = p1->y + mu * (p2->y - p1->y);
235 p->z = p1->z + mu * (p2->z - p1->z);
236 if (mu < 0 || mu > 1) // Intersection not along line segment
239 if (!PointInTriangle(p, *n, pa, pb, pc)) {