-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
155 lines (127 loc) · 2.33 KB
/
Copy pathUtils.cpp
File metadata and controls
155 lines (127 loc) · 2.33 KB
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
#include <cmath>
#include "./Utils.h"
float* Array3(float a, float b, float c)
{
static float array[4];
array[0] = a;
array[1] = b;
array[2] = c;
array[3] = 1.;
return array;
}
// Math helper functions //
float Dot(glm::vec3 v1, glm::vec3 v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
void Cross(glm::vec3 v1, glm::vec3 v2, glm::vec3 vout)
{
glm::vec3 tmp;
tmp[0] = v1[1] * v2[2] - v2[1] * v1[2];
tmp[1] = v2[0] * v1[2] - v1[0] * v2[2];
tmp[2] = v1[0] * v2[1] - v2[0] * v1[1];
vout[0] = tmp[0];
vout[1] = tmp[1];
vout[2] = tmp[2];
}
float Unit(float v[3])
{
float dist;
dist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
if (dist > 0.0)
{
dist = sqrt(dist);
v[0] /= dist;
v[1] /= dist;
v[2] /= dist;
}
return dist;
}
float Unit(glm::vec3 vin, glm::vec3 vout)
{
float dist = vin[0] * vin[0] + vin[1] * vin[1] + vin[2] * vin[2];
if (dist > 0.0)
{
dist = sqrt(dist);
vout[0] = vin[0] / dist;
vout[1] = vin[1] / dist;
vout[2] = vin[2] / dist;
}
else
{
vout[0] = vin[0];
vout[1] = vin[1];
vout[2] = vin[2];
}
return dist;
}
glm::vec3 Unit(glm::vec3 vin)
{
float dist = vin.x * vin.x + vin.y * vin.y + vin.z * vin.z;
if (dist > 0.0)
{
dist = sqrt(dist);
return glm::vec3(vin.x / dist, vin.y / dist, vin.z / dist);
}
else
{
return vin;
}
}
void HsvRgb(float hsv[3], float rgb[3])
{
// guarantee valid input:
float h = hsv[0] / 60.f;
while (h >= 6.) h -= 6.;
while (h < 0.) h += 6.;
float s = hsv[1];
if (s < 0.)
s = 0.;
if (s > 1.)
s = 1.;
float v = hsv[2];
if (v < 0.)
v = 0.;
if (v > 1.)
v = 1.;
// if sat==0, then is a gray:
if (s == 0.0)
{
rgb[0] = rgb[1] = rgb[2] = v;
return;
}
// get an rgb from the hue itself:
float i = floor(h);
float f = h - i;
float p = v * (1.f - s);
float q = v * (1.f - s*f);
float t = v * (1.f - (s * (1.f - f)));
float r, g, b; // red, green, blue
switch ((int)i)
{
case 0:
r = v; g = t; b = p;
break;
case 1:
r = q; g = v; b = p;
break;
case 2:
r = p; g = v; b = t;
break;
case 3:
r = p; g = q; b = v;
break;
case 4:
r = t; g = p; b = v;
break;
case 5:
r = v; g = p; b = q;
break;
}
rgb[0] = r;
rgb[1] = g;
rgb[2] = b;
}
float Dist(glm::vec3 target, glm::vec3 position) {
return sqrt(pow((target.x - position.x), 2) + pow((target.y - position.y), 2) + pow((target.z - position.z), 2));
}