.
1 /*
2 * GraphicsGems.h
3 * Version 1.0 - Andrew Glassner
4 * from "Graphics Gems", Academic Press, 1990
5 */
6
7 /*
8 * EULA: The Graphics Gems code is copyright-protected. In other words,
9 * you cannot claim the text of the code as your own and resell it.
10 * Using the code is permitted in any program, product, or library,
11 * non-commercial or commercial. Giving credit is not required, though
12 * is a nice gesture. The code comes as-is, and if there are any flaws
13 * or problems with any Gems code, nobody involved with Gems - authors,
14 * editors, publishers, or webmasters - are to be held responsible.
15 * Basically, don't be a jerk, and remember that anything free comes
16 * with no guarantee.
17 */
18
19 #ifndef GG_H
20
21 #define GG_H 1
22
23 /*********************/
24 /* 2d geometry types */
25 /*********************/
26
27 typedef struct Point2Struct { /* 2d point */
28 double x, y;
29 } Point2;
30 typedef Point2 Vector2;
31
32 typedef struct IntPoint2Struct { /* 2d integer point */
33 int x, y;
34 } IntPoint2;
35
36 typedef struct Matrix3Struct { /* 3-by-3 matrix */
37 double element[3][3];
38 } Matrix3;
39
40 typedef struct Box2dStruct { /* 2d box */
41 Point2 min, max;
42 } Box2;
43
44
45 /*********************/
46 /* 3d geometry types */
47 /*********************/
48
49 typedef struct Point3Struct { /* 3d point */
50 double x, y, z;
51 } Point3;
52 typedef Point3 Vector3;
53
54 typedef struct IntPoint3Struct { /* 3d integer point */
55 int x, y, z;
56 } IntPoint3;
57
58
59 typedef struct Matrix4Struct { /* 4-by-4 matrix */
60 double element[4][4];
61 } Matrix4;
62
63 typedef struct Box3dStruct { /* 3d box */
64 Point3 min, max;
65 } Box3;
66
67
68
69 /***********************/
70 /* one-argument macros */
71 /***********************/
72
73 /* absolute value of a */
74 #define ABS(a) (((a)<0) ? -(a) : (a))
75
76 /* round a to nearest int */
77 #define ROUND(a) ((a)>0 ? (int)((a)+0.5) : -(int)(0.5-(a)))
78
79 /* take sign of a, either -1, 0, or 1 */
80 #define ZSGN(a) (((a)<0) ? -1 : (a)>0 ? 1 : 0)
81
82 /* take binary sign of a, either -1, or 1 if >= 0 */
83 #define SGN(a) (((a)<0) ? -1 : 1)
84
85 /* shout if something that should be true isn't */
86 #define ASSERT(x) \
87 if (!(x)) fprintf(stderr," Assert failed: x\n");
88
89 /* square a */
90 #define SQR(a) ((a)*(a))
91
92
93 /***********************/
94 /* two-argument macros */
95 /***********************/
96
97 /* find minimum of a and b */
98 #define MIN(a,b) (((a)<(b))?(a):(b))
99
100 /* find maximum of a and b */
101 #define MAX(a,b) (((a)>(b))?(a):(b))
102
103 /* swap a and b (see Gem by Wyvill) */
104 #define SWAP(a,b) { a^=b; b^=a; a^=b; }
105
106 /* linear interpolation from l (when a=0) to h (when a=1)*/
107 /* (equal to (a*h)+((1-a)*l) */
108 #define LERP(a,l,h) ((l)+(((h)-(l))*(a)))
109
110 /* clamp the input to the specified range */
111 #define CLAMP(v,l,h) ((v)<(l) ? (l) : (v) > (h) ? (h) : v)
112
113
114 /****************************/
115 /* memory allocation macros */
116 /****************************/
117
118 /* create a new instance of a structure (see Gem by Hultquist) */
119 #define NEWSTRUCT(x) (struct x *)(malloc((unsigned)sizeof(struct x)))
120
121 /* create a new instance of a type */
122 #define NEWTYPE(x) (x *)(malloc((unsigned)sizeof(x)))
123
124
125 /********************/
126 /* useful constants */
127 /********************/
128
129 #define PI 3.141592 /* the venerable pi */
130 #define PITIMES2 6.283185 /* 2 * pi */
131 #define PIOVER2 1.570796 /* pi / 2 */
132 #define E 2.718282 /* the venerable e */
133 #define SQRT2 1.414214 /* sqrt(2) */
134 #define SQRT3 1.732051 /* sqrt(3) */
135 #define GOLDEN 1.618034 /* the golden ratio */
136 #define DTOR 0.017453 /* convert degrees to radians */
137 #define RTOD 57.29578 /* convert radians to degrees */
138
139
140 /************/
141 /* booleans */
142 /************/
143
144 #define TRUE 1
145 #define FALSE 0
146 #define ON 1
147 #define OFF 0
148 typedef int boolean; /* boolean data type */
149 typedef boolean flag; /* flag data type */
150
151 extern double V2SquaredLength(), V2Length();
152 extern double V2Dot(), V2DistanceBetween2Points();
153 extern Vector2 *V2Negate(), *V2Normalize(), *V2Scale(), *V2Add(), *V2Sub();
154 extern Vector2 *V2Lerp(), *V2Combine(), *V2Mul(), *V2MakePerpendicular();
155 extern Vector2 *V2New(), *V2Duplicate();
156 extern Point2 *V2MulPointByMatrix();
157 extern Matrix3 *V2MatMul();
158
159 extern double V3SquaredLength(), V3Length();
160 extern double V3Dot(), V3DistanceBetween2Points();
161 extern Vector3 *V3Normalize(), *V3Scale(), *V3Add(), *V3Sub();
162 extern Vector3 *V3Lerp(), *V3Combine(), *V3Mul(), *V3Cross();
163 extern Vector3 *V3New(), *V3Duplicate();
164 extern Point3 *V3MulPointByMatrix();
165 extern Matrix4 *V3MatMul();
166
167 extern double RegulaFalsi(), NewtonRaphson(), findroot();
168
169 #endif
170
171