-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdafx.cpp
246 lines (195 loc) · 6.57 KB
/
stdafx.cpp
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
235
236
237
238
239
240
241
242
243
244
245
246
// stdafx.cpp : source file that includes just the standard includes
// GL Test.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
#include <sstream>
void clearCurrentSelection()
{
selectedID = -1;
}
void log(string * text)
{
// This function takes in a string pointer and prints that string to log.txt
FILE * pFile = fopen("log.txt", "a");
fputs(text->data(), pFile);
fclose(pFile);
}
void log(string text)
{
// This function takes in a string and prints that string to log.txt
FILE * pFile = fopen("log.txt", "a");
fputs(text.data(), pFile);
fclose(pFile);
}
string dtos(double number)
{
//This function takes in a double and returns that double as a string object
std::ostringstream stream;
stream << number;
string str = stream.str();
return str;
}
string itos(int number)
{
//This function takes in an integer and returns that integer as a string object
std::ostringstream stream;
stream << number;
string str = stream.str();
return str;
}
double getDecimalRandom(int randBound)
{
//Returns a random decimal between 0 and randBound
return (rand() % (randBound*100))/100.0;
}
double getRandomAsD(int randBound)
{
//Will generate a random double between 0 and randBound
return rand() % (randBound+1);
}
int getRandomAsI(int randBound)
{
//Will generate a random integer between 0 and randBound
return (int)(rand() % (randBound+1));
}
bool choose(float probability)
{
//Return a value of true based on the probability and return false otherwise.
double decision = getRandomAsD(100);
if(decision < probability*100) return true;
else return false;
}
double findDistance(double startX, double startZ, double endX, double endZ)
{
return sqrt(pow(endX - startX,2) + pow(endZ - startZ,2));
}
double find3dDistance(Vector3 v1, Vector3 v2)
{
Vector3 v;
setVector(v, v1.x-v2.x, v1.y-v2.y, v1.z-v2.z);
return sqrt((v.x*v.x) + (v.y*v.y) + (v.z*v.z));
}
double find3dDistance(Vector3 v)
{
return sqrt((v.x*v.x) + (v.y*v.y) + (v.z*v.z));
}
double cosineInterpolate(double a, double b, double x)
{
//This function takes in two number values 'a' and 'b' and a variable x
//that represents a normalized distance between the location of 'a' and 'b'.
// (Makes the assumption that the location of 'a' is closer to 0 than 'b')
//Using the cosine trig function, an intermediate value between 'a' and 'b'
//is returned. This intermediate value is closer to 'a' when 'x' is
//closer to 0 and it is closer to 'b' when 'x' is closer to 1.
double f = (1-cos(x * PI)) * .5;
return a*(1-f)+b*f;
}
Vector3 crossProduct(Vector3 v1, Vector3 v2)
{
Vector3 result;
result.x = v1.y*v2.z - v1.z*v2.y;
result.y = v1.z*v2.x - v1.x*v2.z;
result.z = v1.x*v2.y - v1.y*v2.x;
return result;
}
double dotProduct(Vector3 v1, Vector3 v2)
{
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}
void normalize(Vector3 &v)
{
double length = sqrt((v.x*v.x) + (v.y*v.y) + (v.z*v.z));
if(length == 0)
return; //Vector is zero vector
v.x = v.x / length;
v.y = v.y / length;
v.z = v.z / length;
}
void setVector(Vector4 &v, double x, double y, double z, double a)
{
v.x = x;
v.y = y;
v.z = z;
v.a = a;
}
void setVector(Vector3 &v, double x, double y, double z)
{
v.x = x;
v.y = y;
v.z = z;
}
void setCoord(Vector2 &v, double x, double y)
{
v.x = x;
v.y = y;
}
//This function takes in:
// -A triange given by three 3d points in space
// -A line given by two 3d points in space
// -A target point
//
//This function determines if the line intersects with the triangle
//If they do intersect then values are given to the target vector on the exact coordinates that the line crosses the triangle
BOOL checkLineIntersect(Vector3 tp1, Vector3 tp2, Vector3 tp3, Vector3 lp1, Vector3 lp2, Vector3 &intersect)
{
//tp1 must be the 90 degree angle in the triangle
Vector3 n1, n2; //Vectors for holding temporary data
Vector3 intersectPos; //Hit Point
//n1 = tp2 - tp1
//n2 = tp3 - tp1
setVector(n1, tp2.x-tp1.x, tp2.y-tp1.y, tp2.z-tp1.z);
setVector(n2, tp3.x-tp1.x, tp3.y-tp1.y, tp3.z-tp1.z);
//Get normal vector for the triangle
Vector3 normal = crossProduct(n1, n2);
normalize(normal);
//n1 = lp1 - tp1
//n2 = lp2 - tp1
setVector(n1, lp1.x-tp1.x, lp1.y-tp1.y, lp1.z-tp1.z);
setVector(n2, lp2.x-tp1.x, lp2.y-tp1.y, lp2.z-tp1.z);
//Calculate distance between the two points on the line and the plane
double dist1 = dotProduct(n1, normal);
double dist2 = dotProduct(n2, normal);
if ( (dist1 * dist2) >= 0.0f) return FALSE; // line doesn't cross the triangle.
if ( dist1 == dist2) return FALSE;// line and plane are parallel
//Find the point where the line intersects the plane
//n1 = lp1 + (lp2-lp1) * ( -dist1/(dist2-dist1) )
n1.x = lp1.x + (lp2.x-lp1.x)*(-dist1/(dist2-dist1));
n1.y = lp1.y + (lp2.y-lp1.y)*(-dist1/(dist2-dist1));
n1.z = lp1.z + (lp2.z-lp1.z)*(-dist1/(dist2-dist1));
setVector(intersectPos, n1.x, n1.y, n1.z);
//Find if the intersection point lies within all bounds of the triangle
/*Vector3 vTest;
setVector(n1, tp2.x-tp1.x, tp2.y-tp1.y, tp2.z-tp1.z);
vTest = crossProduct(normal, n1);
setVector(n1, intersectPos.x-tp1.x, intersectPos.y-tp1.y, intersectPos.z-tp1.z);
double dp = dotProduct(vTest, n1);
if(dp < 0) return FALSE;
setVector(n1, tp3.x-tp2.x, tp3.y-tp2.y, tp3.z-tp2.z);
vTest = crossProduct(normal, n1);
setVector(n1, intersectPos.x-tp2.x, intersectPos.y-tp2.y, intersectPos.z-tp2.z);
dp = dotProduct(vTest, n1);
if(dp < 0) return FALSE;
setVector(n1, tp1.x-tp3.x, tp1.y-tp3.y, tp1.z-tp3.z);
vTest = crossProduct(normal, n1);
setVector(n1, intersectPos.x-tp3.x, intersectPos.y-tp3.y, intersectPos.z-tp3.z);
dp = dotProduct(vTest, n1);
if(dp < 0) return FALSE;*/
Vector3 vTest;
setVector(n1, tp2.x-tp1.x, tp2.y-tp1.y, tp2.z-tp1.z);
setVector(n2, intersectPos.x-tp1.x, intersectPos.y-tp1.y, intersectPos.z-tp1.z);
vTest = crossProduct(n1, n2);
float dp = ceilf(dotProduct(vTest, normal)*1000)/1000; //Round to nearest 4 decimals to avoid computation errors
if(dp < 0) return FALSE;
setVector(n1, tp3.x-tp2.x, tp3.y-tp2.y, tp3.z-tp2.z);
setVector(n2, intersectPos.x-tp2.x, intersectPos.y-tp2.y, intersectPos.z-tp2.z);
vTest = crossProduct(n1, n2);
dp = ceilf(dotProduct(vTest, normal)*1000)/1000; //Round to nearest 4 decimals to avoid computation errors
if(dp < 0) return FALSE;
setVector(n1, tp1.x-tp3.x, tp1.y-tp3.y, tp1.z-tp3.z);
setVector(n2, intersectPos.x-tp3.x, intersectPos.y-tp3.y, intersectPos.z-tp3.z);
vTest = crossProduct(n1, n2);
dp = ceilf(dotProduct(vTest, normal)*1000)/1000; //Round to nearest 4 decimals to avoid computation errors
if(dp < 0) return FALSE;
intersect = intersectPos;
return TRUE;
}