File: vector.c

package info (click to toggle)
gltron 0.70final-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,752 kB
  • ctags: 4,793
  • sloc: ansic: 19,182; sh: 3,220; cpp: 973; makefile: 269
file content (322 lines) | stat: -rw-r--r-- 8,194 bytes parent folder | download | duplicates (10)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "base/nebu_vector.h"

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

vec4* vec4Add(vec4 *pOut, const vec4 *pV1, const vec4 *pV2) {
  pOut->v[0] = pV1->v[0] + pV2->v[0];
  pOut->v[1] = pV1->v[1] + pV2->v[1];
  pOut->v[2] = pV1->v[2] + pV2->v[2];
  pOut->v[3] = pV1->v[3] + pV2->v[3];
  return pOut;
}

vec4* vec4Sub(vec4 *pOut, const vec4 *pV1, const vec4 *pV2) {
  pOut->v[0] = pV1->v[0] - pV2->v[0];
  pOut->v[1] = pV1->v[1] - pV2->v[1];
  pOut->v[2] = pV1->v[2] - pV2->v[2];
  pOut->v[3] = pV1->v[3] - pV2->v[3];
  return pOut;
}

vec3* vec3Add(vec3 *pOut, const vec3 *pV1, const vec3 *pV2) {
  pOut->v[0] = pV1->v[0] + pV2->v[0];
  pOut->v[1] = pV1->v[1] + pV2->v[1];
  pOut->v[2] = pV1->v[2] + pV2->v[2];
  return pOut;
}

vec3* vec3Sub(vec3 *pOut, const vec3 *pV1, const vec3 *pV2) {
  pOut->v[0] = pV1->v[0] - pV2->v[0];
  pOut->v[1] = pV1->v[1] - pV2->v[1];
  pOut->v[2] = pV1->v[2] - pV2->v[2];
  return pOut;
}

vec3* vec3Cross(vec3 *pOut, const vec3 *pV1, const vec3 *pV2) {
  vec3 tmp;
  tmp.v[0] = pV1->v[1] * pV2->v[2] - pV1->v[2] * pV2->v[1];
  tmp.v[1] = pV1->v[2] * pV2->v[0] - pV1->v[0] * pV2->v[2];
  tmp.v[2] = pV1->v[0] * pV2->v[1] - pV1->v[1] * pV2->v[0];
  memcpy(pOut, &tmp, sizeof(vec3));
  return pOut;
}

float vec3Dot(const vec3 *pV1, const vec3 *pV2) {
  return pV1->v[0] * pV2->v[0] + 
    pV1->v[1] * pV2->v[1] + pV1->v[2] * pV2->v[2];
}

float vec4Dot(const vec4 *pV1, const vec4 *pV2) {
  return 
    pV1->v[0] * pV2->v[0] + pV1->v[1] * pV2->v[1] + 
    pV1->v[2] * pV2->v[2] + pV1->v[3] * pV2->v[3];
}

float vec3Length(const vec3 *pV) {
  return (float) sqrt( pV->v[0] * pV->v[0] + 
	       pV->v[1] * pV->v[1] + pV->v[2] * pV->v[2] );
}

float vec3LengthSqr(const vec3 *pV) {
  return pV->v[0] * pV->v[0] + 
    pV->v[1] * pV->v[1] + pV->v[2] * pV->v[2];
}
    
vec3* vec3Normalize(vec3 *pOut, const vec3 *pV) {
  float fLength = vec3Length(pV);
  if(fLength != 0) {
    pOut->v[0] = pV->v[0] / fLength;
    pOut->v[1] = pV->v[1] / fLength;
    pOut->v[2] = pV->v[2] / fLength;
  }
  return pOut;
}

void vec4Print(const vec4 *v) {
  int i;
  printf("[ ");
  for(i = 0; i < 4; i++) {
    printf("%.3f ", v->v[i]);
  }
  printf(" ]\n");
}

void vec3Print(const vec3 *v) {
  int i;
  printf("[ ");
  for(i = 0; i < 3; i++) {
    printf("%.3f ", v->v[i]);
  }
  printf(" ]\n");
}

vec4* vec4fromVec3(vec4 *pOut, const vec3 *pV) {
  memcpy(pOut, pV, sizeof(vec3));
  pOut->v[3] = 1;
  return pOut;
}

vec3* vec3fromVec4(vec3 *pOut, const vec4 *pV) {
  if(pV->v[3] != 0) {
    pOut->v[0] = pV->v[0] / pV->v[3];
    pOut->v[1] = pV->v[1] / pV->v[3];
    pOut->v[2] = pV->v[2] / pV->v[3];
    return pOut;
  } else {
    memset(pOut, 0, sizeof(vec3));
    return NULL;
  }
}

vec3* vec3Copy(vec3 *pOut, const vec3 *pV) {
	memcpy(pOut, pV, sizeof(vec3));
	return pOut;
}

vec3* vec3Scale(vec3 *pOut, const vec3 *pV, float f) {
	pOut->v[0] = f * pV->v[0];
	pOut->v[1] = f * pV->v[1];
	pOut->v[2] = f * pV->v[2];
	return pOut;
}

unsigned int uintFromVec3(vec3 *pV) {
	return
		( ( (unsigned int)(pV->v[0] * 127.0f + 128.0f) ) << 16 ) +
		( ( (unsigned int)(pV->v[1] * 127.0f + 128.0f) ) << 8 ) +
		( ( (unsigned int)(pV->v[2] * 127.0f + 128.0f) ) << 0 );
}
		
vec3* vec3Zero(vec3 *pV) {
	memset(pV, 0, sizeof(vec3));
	return pV;
}

vec3* vec3TriNormalDirection(vec3* pOut, 
														 const vec3* pV1, 
														 const vec3* pV2, 
														 const vec3 *pV3) {
	vec3 v1, v2;
	vec3Sub(&v1, pV2, pV1);
	vec3Sub(&v2, pV3, pV1);
	vec3Cross(pOut, &v1, &v2);
	return pOut;
}

vec2* vec2Copy(vec2 *pOut, const vec2 *pV) {
	memcpy(pOut, pV, sizeof(vec2));
	return pOut;
}

vec2* vec2_Orthogonal(vec2 *pOut, const vec2 *pV) {
	pOut->v[0] = pV->v[1];
	pOut->v[1] = - pV->v[0];
	return pOut;
}

vec2* vec2Add(vec2 *pOut, const vec2 *pV1, const vec2 *pV2) {
	pOut->v[0] = pV1->v[0] + pV2->v[0];
	pOut->v[1] = pV1->v[1] + pV2->v[1];
	return pOut; 
}

vec2* vec2Sub(vec2 *pOut, const vec2 *pV1, const vec2 *pV2) { 
	pOut->v[0] = pV1->v[0] - pV2->v[0];
	pOut->v[1] = pV1->v[1] - pV2->v[1];
	return pOut; 
}

vec2* vec2Scale(vec2 *pOut, const vec2 *pV, float fScale) { 
	pOut->v[0] = pV->v[0] * fScale;
	pOut->v[1] = pV->v[1] * fScale;
	return pOut; 
}

vec2* vec2Normalize(vec2 *pOut, const vec2 *pV) {
	return vec2Scale(pOut, pV, 1 / vec2Length(pV));
}

float vec2Dot(const vec2 *pV1, const vec2 *pV2) {
	return pV1->v[0] * pV2->v[0] + pV1->v[1] * pV2->v[1];
}

float vec2Length(const vec2 *pV) {
	float fLength2 = (pV->v[0] * pV->v[0] + pV->v[1] * pV->v[1]);
	float l = sqrt(fLength2);
	return l;
}

int segment2_findT(float *t, const segment2 *s, const vec2 *v) {
	float epsilon = 0.001;
	if( fabs(s->vDirection.v[0]) > fabs(s->vDirection.v[1]) ) {
		*t = (v->v[0] - s->vStart.v[0]) / s->vDirection.v[0];
		if( fabs(v->v[1] - (s->vStart.v[1] + *t * s->vDirection.v[1])) >
				epsilon ) {
			return 1;
		}
	} else {
		*t = (v->v[1] - s->vStart.v[1]) / s->vDirection.v[1];
		if( fabs(v->v[0] - (s->vStart.v[0] + *t * s->vDirection.v[0])) >
				epsilon ) {
			return 1;
		}
	}
	return 0;
}

vec2* segment2_IntersectParallel(vec2 *pOut, float *t1, float *t2,
												 const segment2 *s1, const segment2 *s2) {
	// if the lines don't overlap, return NULL
	// else find t2 for t1 == 0
	// if t2 in [0,1] return t2, t1 = 0
	// else find t1 for t2 == 0 and t2 == 1
	// if t1 < 0 return NULL (no intersection)
	// else return the smaller t1 and the corresponding t2

	vec2 v; float t;

	// if the lines don't overlap, return NULL
	// else find t2 for t1 == 0
	vec2Copy(&v, &s1->vStart);
	if(segment2_findT(t2, s2, &v)) {
		// printf("[vector] are not collinear\n");
		return NULL;
	}
	
	// if t2 in [0,1] return t2, t1 = 0
	if(*t2 >= 0 && *t2 <= 1) {
		vec2Copy(pOut, &s1->vStart);
		*t1 = 0;
		return pOut;
	}
	// else find t1 for t2 == 0 and t2 == 1
	vec2Copy(&v, &s2->vStart);
	if(segment2_findT(t1, s1, &v))
		return NULL;
	// if t1 < 0 return NULL (no intersection)
	if(*t1 < 0) return NULL;
	vec2Add(&v, &s2->vStart, &s2->vDirection);
	if(segment2_findT(&t, s1, &v))
		return NULL;
	assert(t >= 0);
	
	if(*t1 > 1 && t > 1)
		return NULL;
	// else return the smaller t1 and the corresponding t2
	if(t < *t1) {
		*t1 = t;
		*t2 = 1;
		vec2Copy(pOut, &v);
	} else {
		*t2 = 0;
		vec2Copy(pOut, &s2->vStart);
	}
	return pOut;
}

vec2* segment2_IntersectNonParallel(vec2 *pOut, float *t1, float *t2,
												 const segment2 *s1, const segment2 *s2) {
	vec3 v1, v2;
	vec3 tmp1, tmp2;
	vec3 vIntersection;
	
	// compute the homogenous line coordinates
	tmp1.v[0] = s1->vStart.v[0];
	tmp1.v[1] = s1->vStart.v[1];
	tmp1.v[2] = 1;
	tmp2.v[0] = s1->vStart.v[0] + s1->vDirection.v[0];
	tmp2.v[1] = s1->vStart.v[1] + s1->vDirection.v[1];
	tmp2.v[2] = 1;
	vec3Cross(&v1, &tmp1, &tmp2);

	tmp1.v[0] = s2->vStart.v[0];
	tmp1.v[1] = s2->vStart.v[1];
	tmp1.v[2] = 1;
	tmp2.v[0] = s2->vStart.v[0] + s2->vDirection.v[0];
	tmp2.v[1] = s2->vStart.v[1] + s2->vDirection.v[1];
	tmp2.v[2] = 1;
	vec3Cross(&v2, &tmp1, &tmp2);

	// compute the intersection in homogenous coordinates and
	// project back to 2d
	vec3Cross(&vIntersection, &v1, &v2);
	pOut->v[0] = vIntersection.v[0] / vIntersection.v[2];
	pOut->v[1] = vIntersection.v[1] / vIntersection.v[2];
	
	// compute t1, t2
	if(fabs(s1->vDirection.v[0]) > fabs(s1->vDirection.v[1]))
		*t1 = (pOut->v[0] - s1->vStart.v[0]) / s1->vDirection.v[0];
	else
		*t1 = (pOut->v[1] - s1->vStart.v[1]) / s1->vDirection.v[1];
	if(fabs(s2->vDirection.v[0]) > fabs(s2->vDirection.v[1]))
		*t2 = (pOut->v[0] - s2->vStart.v[0]) / s2->vDirection.v[0];
	else
		*t2 = (pOut->v[1] - s2->vStart.v[1]) / s2->vDirection.v[1];
	
	return pOut;
}

vec2* segment2_Intersect(vec2 *pOut, float *t1, float *t2,
												 const segment2 *s1, const segment2 *s2) {
	// check if s1, s2 are parallel
	vec2 tmp;
	if( fabs(vec2Dot(&s1->vDirection, 
							vec2_Orthogonal(&tmp, & s2->vDirection) )) < 0.1) {
		// printf("[vector] lines are parallel\n");
		pOut = segment2_IntersectParallel(pOut, t1, t2, s1, s2);
		if(!pOut) {
			*t1 = 0;
			*t2 = 0;
		}
	}	else {
		pOut = segment2_IntersectNonParallel(pOut, t1, t2, s1, s2);
	}
	return pOut;
}

float segment2_Length(const segment2 *s) {
	return vec2Length(&s->vDirection);
}