summary refs log tree commit diff
path: root/src/Quaternions.cpp
blob: 6fe85bff3e4c128e69cdbc03c4add533e2fc0286 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <cmath>

#include "Quaternions.h"

void CrossProduct(XYZ P, XYZ Q, XYZ *V){
	V->x = P.y * Q.z - P.z * Q.y;
	V->y = P.z * Q.x - P.x * Q.z;
	V->z = P.x * Q.y - P.y * Q.x;
}

void Normalise(XYZ *vectory) {
	float d = sqrt(vectory->x*vectory->x+vectory->y*vectory->y+vectory->z*vectory->z);
	if(d==0){return;}
	vectory->x /= d;
	vectory->y /= d;
	vectory->z /= d;
}

extern float u0, u1, u2;
extern float v0, v1, v2;
extern float a, b;
extern int i, j;
extern bool bInter;
extern float pointv[3];
extern float p1v[3];
extern float p2v[3];
extern float p3v[3];
extern float normalv[3];

bool PointInTriangle(XYZ *p, XYZ normal, XYZ *p1, XYZ *p2, XYZ *p3)
{
	bInter=0;

	pointv[0]=p->x;
	pointv[1]=p->y;
	pointv[2]=p->z;

	p1v[0]=p1->x;
	p1v[1]=p1->y;
	p1v[2]=p1->z;

	p2v[0]=p2->x;
	p2v[1]=p2->y;
	p2v[2]=p2->z;

	p3v[0]=p3->x;
	p3v[1]=p3->y;
	p3v[2]=p3->z;

	normalv[0]=normal.x;
	normalv[1]=normal.y;
	normalv[2]=normal.z;

#define ABS(X) (((X)<0.f)?-(X):(X) )
#define MAX(A, B) (((A)<(B))?(B):(A))
	float max = MAX(MAX(ABS(normalv[0]), ABS(normalv[1])), ABS(normalv[2]));
#undef MAX
	if (max == ABS(normalv[0])) {i = 1; j = 2;} // y, z
	if (max == ABS(normalv[1])) {i = 0; j = 2;} // x, z
	if (max == ABS(normalv[2])) {i = 0; j = 1;} // x, y
#undef ABS

	u0 = pointv[i] - p1v[i];
	v0 = pointv[j] - p1v[j];
	u1 = p2v[i] - p1v[i];
	v1 = p2v[j] - p1v[j];
	u2 = p3v[i] - p1v[i];
	v2 = p3v[j] - p1v[j];

	if (u1 > -1.0e-05f && u1 < 1.0e-05f)// == 0.0f)
	{
		b = u0 / u2;
		if (0.0f <= b && b <= 1.0f)
		{
			a = (v0 - b * v2) / v1;
			if ((a >= 0.0f) && (( a + b ) <= 1.0f))
				bInter = 1;
		}
	}
	else
	{
		b = (v0 * u1 - u0 * v1) / (v2 * u1 - u2 * v1);
		if (0.0f <= b && b <= 1.0f)
		{
			a = (u0 - b * u2) / u1;
			if ((a >= 0.0f) && (( a + b ) <= 1.0f ))
				bInter = 1;
		}
	}

	return bInter;
}

extern float d;
extern float a1,a2,a3;
extern float total,denom,mu;
extern XYZ pa1,pa2,pa3,n;

float LineFacetd(XYZ p1,XYZ p2,XYZ pa,XYZ pb,XYZ pc, XYZ n, XYZ *p)
{

   //Calculate the parameters for the plane
   d = - n.x * pa.x - n.y * pa.y - n.z * pa.z;

   //Calculate the position on the line that intersects the plane
   denom = n.x * (p2.x - p1.x) + n.y * (p2.y - p1.y) + n.z * (p2.z - p1.z);
   if (abs(denom) < 0.0000001)        // Line and plane don't intersect
      return 0;
   mu = - (d + n.x * p1.x + n.y * p1.y + n.z * p1.z) / denom;
   p->x = p1.x + mu * (p2.x - p1.x);
   p->y = p1.y + mu * (p2.y - p1.y);
   p->z = p1.z + mu * (p2.z - p1.z);
   if (mu < 0 || mu > 1)   // Intersection not along line segment
      return 0;

   if(!PointInTriangle( p, n, &pa, &pb, &pc)){return 0;}
   return 1;
}

void ReflectVector(XYZ *vel, XYZ *n)
{
   XYZ vn;
   XYZ vt;
   float dotprod;

   dotprod=dotproduct(*n,*vel);
   vn.x=n->x*dotprod;
   vn.y=n->y*dotprod;
   vn.z=n->z*dotprod;

   vt.x=vel->x-vn.x;
   vt.y=vel->y-vn.y;
   vt.z=vel->z-vn.z;

   vel->x = vt.x - vn.x;
   vel->y = vt.y - vn.y;
   vel->z = vt.z - vn.z;
}

float dotproduct(XYZ point1, XYZ point2){
	return point1.x * point2.x + point1.y * point2.y + point1.z * point2.z;
}

float findDistance(XYZ point1, XYZ point2){
	return sqrt((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y)+(point1.z-point2.z)*(point1.z-point2.z));
}

float findLengthfast(XYZ point1){
	return((point1.x)*(point1.x)+(point1.y)*(point1.y)+(point1.z)*(point1.z));
}

float findDistancefast(XYZ point1, XYZ point2){
	return((point1.x-point2.x)*(point1.x-point2.x)+(point1.y-point2.y)*(point1.y-point2.y)+(point1.z-point2.z)*(point1.z-point2.z));
}

XYZ DoRotation(XYZ thePoint, float xang, float yang, float zang){
	XYZ newpoint;
	if(xang){
		xang*=6.283185;
		xang/=360;
	}
	if(yang){
		yang*=6.283185;
		yang/=360;
	}
	if(zang){
		zang*=6.283185;
		zang/=360;
	}

	if(yang){
	newpoint.z=thePoint.z*cos(yang)-thePoint.x*sin(yang);
	newpoint.x=thePoint.z*sin(yang)+thePoint.x*cos(yang);
	thePoint.z=newpoint.z;
	thePoint.x=newpoint.x;
	}

	if(zang){
	newpoint.x=thePoint.x*cos(zang)-thePoint.y*sin(zang);
	newpoint.y=thePoint.y*cos(zang)+thePoint.x*sin(zang);
	thePoint.x=newpoint.x;
	thePoint.y=newpoint.y;
	}

	if(xang){
	newpoint.y=thePoint.y*cos(xang)-thePoint.z*sin(xang);
	newpoint.z=thePoint.y*sin(xang)+thePoint.z*cos(xang);
	thePoint.z=newpoint.z;
	thePoint.y=newpoint.y;
	}

	return thePoint;
}

float square( float f ) { return (f*f) ;}

bool sphere_line_intersection(float x1, float y1, float z1,
	float x2, float y2, float z2, float x3, float y3, float z3, float r)
{

	 // x1,y1,z1  P1 coordinates (point of line)
	 // x2,y2,z2  P2 coordinates (point of line)
	 // x3,y3,z3, r  P3 coordinates and radius (sphere)
	 // x,y,z   intersection coordinates
	 //
	 // This function returns a pointer array which first index indicates
	 // the number of intersection point, followed by coordinate pairs.

	float a, b, c, i;

	if(x1>x3+r&&x2>x3+r)return(0);
	if(x1<x3-r&&x2<x3-r)return(0);
	if(y1>y3+r&&y2>y3+r)return(0);
	if(y1<y3-r&&y2<y3-r)return(0);
	if(z1>z3+r&&z2>z3+r)return(0);
	if(z1<z3-r&&z2<z3-r)return(0);
	 a =  square(x2 - x1) + square(y2 - y1) + square(z2 - z1);
	 b =  2* ( (x2 - x1)*(x1 - x3)
	      + (y2 - y1)*(y1 - y3)
	      + (z2 - z1)*(z1 - z3) ) ;
	 c =  square(x3) + square(y3) +
	      square(z3) + square(x1) +
	      square(y1) + square(z1) -
	      2* ( x3*x1 + y3*y1 + z3*z1 ) - square(r) ;
	 i =   b * b - 4 * a * c ;

	 if ( i < 0.0 )
	 {
	  // no intersection
	  return(0);
	 }

	return(1);
}