File: geometric.h

package info (click to toggle)
sipp 3.1-2
  • links: PTS
  • area: main
  • in suites: potato, slink
  • size: 1,256 kB
  • ctags: 743
  • sloc: ansic: 8,534; makefile: 301; lex: 69; sh: 2
file content (195 lines) | stat: -rw-r--r-- 5,507 bytes parent folder | download | duplicates (4)
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
/**
 ** sipp - SImple Polygon Processor
 **
 **  A general 3d graphic package
 **
 **  Copyright Equivalent Software HB  1992
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation; either version 1, or any later version.
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 ** You can receive a copy of the GNU General Public License from the
 ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 **/

/**
 ** geometric.h - All kinds of stuff with matrixes, transformations, 
 **               coordinates
 **/

/* Make sure no multiple including */
#ifndef _GEOMETRIC_H_
#define _GEOMETRIC_H_

#include <math.h>
#ifndef NOMEMCPY
#include <memory.h>
#endif


/* #define PI    3.1415926535897932384626 */


typedef struct {
    double x, y, z;
} Vector;


/*
 * NOTE:
 * Capitalized types denote Vectors and other aggregates.
 * Lower case denote scalars.
 */

/* V = Vec(x, y, z) */
#define MakeVector(V, xx, yy, zz)  { (V).x=(xx); \
                                     (V).y=(yy); \
                                     (V).z=(zz); }

/* A = -A */
#define VecNegate(A)	         { (A).x=0-(A).x; \
			           (A).y=0-(A).y; \
			           (A).z=0-(A).z; }

/* return A . B */
#define VecDot(A, B)	((A).x*(B).x+(A).y*(B).y+(A).z*(B).z)

/* return length(A) */
#define VecLen(A)	(sqrt((double)VecDot(A, A)))

/* B = A */
#define VecCopy(B, A)	((B) = (A))

/* C = A + B */
#define VecAdd(C, A, B)	 { (C).x=(A).x+(B).x; \
			   (C).y=(A).y+(B).y; \
			   (C).z=(A).z+(B).z; }

/* C = A - B */
#define VecSub(C, A, B)	 { (C).x=(A).x-(B).x; \
			   (C).y=(A).y-(B).y; \
			   (C).z=(A).z-(B).z; }

/* C = a*A */
#define VecScalMul(C, a, A)	 { (C).x=(a)*(A).x; \
				   (C).y=(a)*(A).y; \
				   (C).z=(a)*(A).z; }

/* C = a*A + B */
#define VecAddS(C, a, A, B)	 { (C).x=(a)*(A).x+(B).x; \
				   (C).y=(a)*(A).y+(B).y; \
				   (C).z=(a)*(A).z+(B).z; }

/* C = a*A + b*B */
#define VecComb(C, a, A, b, B)	 { (C).x=(a)*(A).x+(b)*(B).x; \
				   (C).y=(a)*(A).y+(b)*(B).y; \
			 	   (C).z=(a)*(A).z+(b)*(B).z; }

/* C = A X B */
#define VecCross(C, A, B)   	 { (C).x=(A).y*(B).z-(A).z*(B).y; \
                                   (C).y=(A).z*(B).x-(A).x*(B).z; \
			           (C).z=(A).x*(B).y-(A).y*(B).x; }


#define VecMax(C, A, B)        	 { (C).x=(((A).x>(B).x)?(A).x:(B).x); \
                                   (C).y=(((A).y>(B).y)?(A).y:(B).y); \
                                   (C).z=(((A).z>(B).z)?(A).z:(B).z); }


#define VecMin(C, A, B)        	 { (C).x=(((A).x<(B).x)?(A).x:(B).x); \
                                   (C).y=(((A).y<(B).y)?(A).y:(B).y); \
                                   (C).z=(((A).z<(B).z)?(A).z:(B).z); }

/* ================================================================ */
/*                         Matrix operations                        */


/*
 * Define a homogenous transformation matrix. The first row (vector) 
 * is the new X axis, i.e. the X axis in the transformed coordinate 
 * system. The second row is the new Y axis, and so on. The last row
 * is the translation, for a transformed point.
 *
 * The reason we make surround the rows with a struct is that we
 * don't want to say (Transf_mat *) &foo[0] instead of &foo when
 * sending an address to a matrix as a parameter to a function.
 * Alas, arrays are not first class objects in C.
 */

typedef struct {
    double   mat[4][3];
} Transf_mat;


extern Transf_mat   ident_matrix;


/* *A = *B    N.b. A and B are pointers! */
#define MatCopy(A, B)		 (*A) = (*B)


/*----------------------------------------------------------------------*/


/* Function declarations for the functions in geometric.c */

EXTERN void
vecnorm _ANSI_ARGS_((Vector  *vec));

EXTERN Transf_mat *
transf_mat_create _ANSI_ARGS_((Transf_mat  *initmat));

EXTERN void
transf_mat_destruct _ANSI_ARGS_((Transf_mat  *mat));

EXTERN void
mat_translate _ANSI_ARGS_((Transf_mat  *mat,
                           double       dx,
                           double       dy, 
                           double       dz));

EXTERN void
mat_rotate_x _ANSI_ARGS_((Transf_mat  *mat,
                          double       ang));

EXTERN void
mat_rotate_y _ANSI_ARGS_((Transf_mat  *mat,
                          double       ang));

EXTERN void
mat_rotate_z _ANSI_ARGS_((Transf_mat  *mat,
                          double       ang));

EXTERN void
mat_rotate _ANSI_ARGS_((Transf_mat  *mat,
                        Vector      *point,
                        Vector      *vector,
                        double       ang));

EXTERN void
mat_scale _ANSI_ARGS_((Transf_mat  *mat,
                       double       xscale,
                       double       yscale,
                       double       zscale));

EXTERN void
mat_mirror_plane _ANSI_ARGS_((Transf_mat  *mat,
                              Vector      *point,
                              Vector      *norm));

EXTERN void
mat_mul _ANSI_ARGS_((Transf_mat  *res,
                     Transf_mat  *a,
                     Transf_mat  *b));

EXTERN void
point_transform _ANSI_ARGS_((Vector      *res,
                             Vector      *vec,
                             Transf_mat  *mat));


#endif  /* _GEOMETRIC_H_ */