File: geom.c

package info (click to toggle)
gltron 0.61-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,948 kB
  • ctags: 1,175
  • sloc: ansic: 7,580; perl: 168; makefile: 149; sh: 15
file content (118 lines) | stat: -rw-r--r-- 2,216 bytes parent folder | download
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
/* some geometric routines always needed */

#include <math.h>

float length(float *v) {
	return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}

float length2(float *v) {
  return sqrt(v[0] * v[0] + v[1] * v[1]);
}

float length3(float *v) {
  return length(v);
}

void normalize(float *v) {
	float d = length(v);
	if (d == 0) return;
	v[0] /= d;
	v[1] /= d;
	v[2] /= d;
}

void crossprod(float *v1, float *v2, float *out) {
	out[0] = v1[1] * v2[2] - v1[2] * v2[1];
	out[1] = v1[2] * v2[0] - v1[0] * v2[2];
	out[2] = v1[0] * v2[1] - v1[1] * v2[0];
}

void normcrossprod(float *v1, float *v2, float *out) {
	crossprod(v1, v2, out);
	normalize(out);
}

float scalarprod2(float *v1, float *v2) {
	return v1[0] * v2[0] + v1[1] * v2[1];
}

float scalarprod(float *v1, float *v2) {
	return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
}

void vsub2(float *v1, float *v2, float *out) {
	out[0] = v1[0] - v2[0];
	out[1] = v1[1] - v2[1];
}

void vsub(float *v1, float *v2, float *out) {
	out[0] = v1[0] - v2[0];
	out[1] = v1[1] - v2[1];
	out[2] = v1[2] - v2[2];
}

void vadd2(float *v1, float *v2, float *out) {
	out[0] = v1[0] + v2[0];
	out[1] = v1[1] + v2[1];
}

void vadd(float *v1, float *v2, float *out) {
	out[0] = v1[0] + v2[0];
	out[1] = v1[1] + v2[1];
	out[2] = v1[2] + v2[2];
}

void vcopy(float *v1, float *out) {
	out[0] = v1[0];
	out[1] = v1[1];
	out[2] = v1[2];
}

void vmul(float *v, float f) {
	v[0] *= f;
	v[1] *= f;
	v[2] *= f;
}

/* 4 entries... */

float length4(float *v) {
	return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] +
		    v[3] * v[3]);
}

void normalize4(float *v) {
	float d = length(v);
	if (d == 0) return;
	v[0] /= d;
	v[1] /= d;
	v[2] /= d;
	v[3] /= d;
}

float scalarprod4(float *v1, float *v2) {
	return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] +
	  v1[3] * v2[3];
}

void vsub4(float *v1, float *v2, float *out) {
	out[0] = v1[0] - v2[0];
	out[1] = v1[1] - v2[1];
	out[2] = v1[2] - v2[2];
	out[3] = v1[3] - v2[3];
}

void vadd4(float *v1, float *v2, float *out) {
	out[0] = v1[0] + v2[0];
	out[1] = v1[1] + v2[1];
	out[2] = v1[2] + v2[2];
	out[3] = v1[3] + v2[3];
}

void vcopy4(float *v1, float *out) {
	out[0] = v1[0];
	out[1] = v1[1];
	out[2] = v1[2];
	out[3] = v1[3];
}