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
|
#include <BH/Math.h>
#include <string.h>
#include <math.h>
#define EPSILON 0.00001f
#define PI 3.14159265358979323846f
int BH_Ray2fIntersectLine(const float start[2],
const float direction[2],
const float line[3],
float *t,
float out[2])
{
float tmp1[2];
float denom, time;
/* Calculate intersection time */
denom = BH_Vec2fDot(direction, line);
time = (line[2] - BH_Vec2fDot(line, start)) / denom;
/* Check for ray/plane parallel to each other or point is behing the ray. */
if (fabsf(denom) < EPSILON || time < 0.0f)
return BH_ERROR;
/* Compute intersection point */
BH_Vec2fScale(direction, time, tmp1);
BH_Vec2fAdd(start, tmp1, out);
*t = time;
return BH_OK;
}
int BH_Ray2fIntersectTime(const float aStart[2],
const float aDirection[2],
const float bStart[2],
const float bDirection[2],
float *time1,
float *time2)
{
float tmp1[2], tmp2[2], tmp3[2];
float denom;
/* Rotate directions by 90 degrees and caluclate denom */
tmp1[0] = -aDirection[1]; tmp1[1] = aDirection[0];
tmp2[0] = -bDirection[1]; tmp2[1] = bDirection[0];
denom = BH_Vec2fDot(tmp1, bDirection);
if (fabsf(denom) < EPSILON)
return BH_ERROR;
/* Calculate segments offset and intersection times */
BH_Vec2fSub(aStart, bStart, tmp3);
*time1 = BH_Vec2fDot(tmp3, tmp2) / denom;
*time2 = BH_Vec2fDot(tmp3, tmp1) / denom;
return BH_OK;
}
int BH_Ray2fIntersectRay(const float aStart[2],
const float aDirection[2],
const float bStart[2],
const float bDirection[2],
float *t,
float out[2])
{
float tmp[2];
float time1, time2;
if (BH_Ray2fIntersectTime(aStart, aDirection, bStart, bDirection, &time1, &time2))
return BH_ERROR;
if (time1 < 0.0f || time2 < 0.0f)
return BH_ERROR;
BH_Vec2fScale(aDirection, time1, tmp);
BH_Vec2fAdd(aStart, tmp, out);
*t = time1;
return BH_OK;
}
int BH_Ray2fIntersectSegment(const float aStart[2],
const float aDirection[2],
const float bStart[2],
const float bEnd[2],
float *t,
float out[2])
{
float tmp[2];
float time1, time2;
BH_Vec2fSub(bEnd, bStart, tmp);
if (BH_Ray2fIntersectTime(aStart, aDirection, bStart, tmp, &time1, &time2))
return BH_ERROR;
if (time1 < 0.0f || time2 < 0.0f || time2 > 1.0f)
return BH_ERROR;
BH_Vec2fScale(aDirection, time1, tmp);
BH_Vec2fAdd(aStart, tmp, out);
*t = time1;
return BH_OK;
}
int BH_Segment2fIntersectLine(const float start[2],
const float end[2],
const float line[3],
float *t,
float out[2])
{
float tmp[2];
float denom, time;
/* Calculate intersection time */
BH_Vec2fSub(end, start, tmp);
denom = BH_Vec2fDot(tmp, line);
time = (line[2] - BH_Vec2fDot(line, start)) / denom;
/* Check for ray/plane parallel to each other or point is behing the ray. */
if (fabsf(denom) < EPSILON || time < 0.0f || time > 1.0f)
return BH_ERROR;
/* Compute intersection point */
BH_Vec2fScale(tmp, time, tmp);
BH_Vec2fAdd(start, tmp, out);
*t = time;
return BH_OK;
}
int BH_Segment2fIntersectSegment(const float aStart[2],
const float aEnd[2],
const float bStart[2],
const float bEnd[2],
float *t,
float out[2])
{
float tmp1[2], tmp2[2];
float time1, time2;
BH_Vec2fSub(aEnd, aStart, tmp1);
BH_Vec2fSub(bEnd, bStart, tmp2);
if (BH_Ray2fIntersectTime(aStart, tmp1, bStart, tmp2, &time1, &time2))
return BH_ERROR;
if (time1 < 0.0f || time1 > 1.0f || time2 < 0.0f || time2 > 1.0f)
return BH_ERROR;
BH_Vec2fLerp(aStart, aEnd, time1, out);
*t = time1;
return BH_OK;
}
int BH_Ray2fIntersectBox2f(const float aStart[2],
const float aDirection[2],
const float bMin[2],
const float bMax[2],
float *t,
float out[2])
{
float timeNear, timeFar, hitNear, hitFar, denom, tmp;
int i;
timeNear = -1.0f / 0.0f;
timeFar = 1.0f / 0.0f;
/* Check if origin inside box */
if (!BH_Box2fContains(bMin, bMax, aStart))
{
memcpy(out, aStart, sizeof(float) * 2);
*t = 0.0f;
return BH_OK;
}
/* Check each axis for the minimal and maximum intersection time */
for (i = 0; i < 2; i++)
{
if (fabsf(aDirection[i]) < EPSILON)
{
if (aStart[i] < bMin[i] || aStart[i] > bMax[i])
return BH_ERROR;
continue;
}
denom = 1.0f / aDirection[i];
hitNear = (bMin[i] - aStart[i]) * denom;
hitFar = (bMax[i] - aStart[i]) * denom;
if (hitNear > hitFar)
{
tmp = hitNear;
hitNear = hitFar;
hitFar = tmp;
}
if (hitNear > timeNear)
timeNear = hitNear;
if (hitFar < timeFar)
timeFar = hitFar;
if (timeNear > timeFar || timeFar < 0.0f)
return BH_ERROR;
}
out[0] = aStart[0] + aDirection[0] * timeNear;
out[1] = aStart[1] + aDirection[1] * timeNear;
*t = timeNear;
return BH_OK;
}
int BH_Segment2fIntersectBox2f(const float aStart[2],
const float aEnd[2],
const float bMin[2],
const float bMax[2],
float *t,
float out[2])
{
float tmp[3];
float time;
BH_Vec2fSub(aEnd, aStart, tmp);
if (BH_Ray2fIntersectBox2f(aStart, tmp, bMin, bMax, &time, out))
return BH_ERROR;
if (time > 1.0f)
return BH_ERROR;
*t = time;
return BH_OK;
}
|