vert.c
1.82 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
#include "vert.h"
#include <math.h>
double
vlen( Vertex vert )
{
double len;
double x;
x = vert[0];
len = x*x;
x = vert[1];
len += x*x;
x = vert[2];
len += x*x;
len = sqrt(len);
return( len );
}
double
vdot( Vertex vert1, Vertex vert2 )
{
double x;
x = vert1[0] * vert2[0] +
vert1[1] * vert2[1] +
vert1[2] * vert2[2];
return( x );
}
double
vdist( Vertex vert1, Vertex vert2 )
{
double len;
double x;
x = vert1[0] - vert2[0];
len = x*x;
x = vert1[1] - vert2[1];
len += x*x;
x = vert1[2] - vert2[2];
len += x*x;
len = sqrt(len);
return( len );
}
void
normalize( Vertex vert )
{
double len;
len = vlen( vert );
vert[0] /= len;
vert[1] /= len;
vert[2] /= len;
}
void
calc_cent( int face[], int num_ps, Vertex verts[], Vertex cent )
{
int i;
double *vert;
cent[0] = cent[1] = cent[2] = 0.0;
for(i=0; i<num_ps; i++ ) {
vert = &(verts[face[i]-1][0]);
cent[0] += vert[0];
cent[1] += vert[1];
cent[2] += vert[2];
};
cent[0] /= num_ps;
cent[1] /= num_ps;
cent[2] /= num_ps;
}
double
area( Vertex pa, Vertex pb, Vertex pc )
{
double a,b,c,s;
a = vdist( pa, pb );
b = vdist( pb, pc );
c = vdist( pc, pa );
s = (a+b+c)/2;
a = sqrt( s*(s-a)*(s-b)*(s-c) );
return( a );
}
void
vadd( Vertex va, Vertex vb, Vertex vres )
{
vres[0] = va[0] + vb[0];
vres[1] = va[1] + vb[1];
vres[2] = va[2] + vb[2];
}
void
vsub( Vertex va, Vertex vb, Vertex vres )
{
vres[0] = va[0] - vb[0];
vres[1] = va[1] - vb[1];
vres[2] = va[2] - vb[2];
}
void
vinterp( Vertex va, Vertex vb, double fmix, Vertex vres )
{
vres[0] = va[0]*fmix + vb[0]*(1-fmix);
vres[1] = va[1]*fmix + vb[1]*(1-fmix);
vres[2] = va[2]*fmix + vb[2]*(1-fmix);
normalize( vres );
}