source: GTP/trunk/App/Demos/Vis/CHC_revisited/DataTypes.c @ 2746

Revision 2746, 5.2 KB checked in by mattausch, 16 years ago (diff)
Line 
1//Copyright and Disclaimer:
2//This code is copyright Vienna University of Technology, 2004.
3
4#include <memory.h>
5#include <stdlib.h>
6#include <malloc.h>
7#include "DataTypes.h"
8#include "MathStuff.h"
9
10const struct Object OBJECT_NULL = { 0, 0 };
11const struct VecPoint VECPOINT_NULL = { 0, 0 };
12const struct VecPlane VECPLANE_NULL = { 0, 0 };
13
14void copyVector3(Vector3 result, const Vector3 input) {
15        result[0] = input[0];
16        result[1] = input[1];
17        result[2] = input[2];
18}
19
20void addVector3(Vector3 result, const Vector3 a, const Vector3 b)
21{
22        int i;
23       
24        for (i = 0; i < 3; ++ i)
25        {
26                result[i] = a[i] + b[i];
27        }
28}
29
30
31void diffVector3(Vector3 result, const Vector3 a, const Vector3 b)
32{
33        int i;
34        for (i = 0; i < 3; ++ i)
35        {
36                result[i] = a[i] - b[i];
37        }
38}
39
40
41void copyVector3Values(Vector3 result, const double x, const double y, const double z)
42{
43        result[0] = x;
44        result[1] = y;
45        result[2] = z;
46}
47
48
49void copyMatrix(Matrix4x4 result, const Matrix4x4 input)
50{
51        if (input != result)
52        {
53                memcpy(result,input,4*4*sizeof(double));
54        }
55}
56
57
58void planesSetSize(struct VecPlane* p, const int size)
59{
60        if (0 != p)
61        {
62                if(size == p->size)
63                {
64                        return;
65                }
66
67                p->plane = (struct Plane*)realloc(p->plane,size*sizeof(struct Plane));
68                p->size = size;
69        }
70}
71
72
73void emptyVecPlane(struct VecPlane* p)
74{
75        planesSetSize(p,0);
76}
77
78
79void vecPointSetSize(struct VecPoint* poly, const int size)
80{
81        if (0 != poly)
82        {
83                if (size == poly->size)
84                {
85                        return;
86                }
87               
88                poly->points = (Vector3*)realloc(poly->points,size*sizeof(Vector3));
89                poly->size = size;
90        }
91}
92
93
94void emptyVecPoint(struct VecPoint* poly)
95{
96        vecPointSetSize(poly,0);
97}
98
99
100void append2VecPoint(struct VecPoint* poly, const Vector3 p)
101{
102        if (0 != poly)
103        {
104                int size = poly->size;
105                vecPointSetSize(poly,size+1);
106                copyVector3(poly->points[size], p);
107        }
108}
109
110
111void copyVecPoint(struct VecPoint* poly, const struct VecPoint poly2)
112{
113        if (0 != poly)
114        {
115                int i;
116                vecPointSetSize(poly,poly2.size);
117                for (i = 0; i < poly2.size; ++ i)
118                {
119                        copyVector3(poly->points[i], poly2.points[i]);
120                }
121        }
122}
123
124
125void swapVecPoint(struct VecPoint* poly1, struct VecPoint* poly2)
126{
127        if(0 != poly1 && 0 != poly2 && poly1 != poly2)
128        {
129                Vector3* points = poly1->points;
130                poly1->points = poly2->points;
131                poly2->points = points;
132               
133                int size = poly1->size;
134                poly1->size = poly2->size;
135                poly2->size = size;
136        }
137}
138
139
140void objectSetSize(struct Object* obj, const int size)
141{
142        if (0 != obj)
143        {
144                int i;
145                if(size == obj->size)
146                {
147                        return;
148                }
149               
150                //dispose if shrinking
151                for(i = size; i < obj->size; i++)
152                {
153                        emptyVecPoint(&(obj->poly[i]));
154                }
155
156                //allocate new place
157                obj->poly = (struct VecPoint*)realloc(obj->poly,size*sizeof(struct VecPoint));
158               
159                //initialize new place
160                for(i = obj->size; i < size; i++)
161                {
162                        obj->poly[i] = VECPOINT_NULL;
163                }
164               
165                obj->size = size;
166        }
167}
168
169
170void emptyObject(struct Object* obj)
171{
172        objectSetSize(obj, 0);
173}
174
175
176void copyObject(struct Object* obj, const struct Object objIn)
177{
178        if(0 != obj)
179        {
180                int i;
181                objectSetSize(obj,objIn.size);
182
183                for(i = 0; i < objIn.size; i++)
184                {
185                        copyVecPoint( &(obj->poly[i]), objIn.poly[i]);
186                }
187        }
188}
189
190
191void append2Object(struct Object* obj, const struct VecPoint poly)
192{       
193        if (0 != obj)
194        {
195                int size = obj->size;
196                objectSetSize(obj, size + 1);
197                copyVecPoint(&(obj->poly[size]), poly);
198        }
199}
200
201
202void convObject2VecPoint(struct VecPoint* points,const struct Object obj)
203{
204        if(0 != points) {
205                int i, j;
206                emptyVecPoint(points);
207                for(i = 0; i < obj.size; i++) {
208                        struct VecPoint* p = &(obj.poly[i]);
209                        for(j = 0; j < p->size; j++) {
210                                append2VecPoint(points,p->points[j]);
211                        }
212                }
213        }
214}
215
216
217void calcAABoxPoints(Vector3x8 points, const struct AABox b) {
218    //generate 8 corners of the box
219        copyVector3Values(points[0],b.min[0],b.min[1],b.min[2]);//     7+------+6
220        copyVector3Values(points[1],b.max[0],b.min[1],b.min[2]);//     /|     /|
221        copyVector3Values(points[2],b.max[0],b.max[1],b.min[2]);//    / |    / |
222        copyVector3Values(points[3],b.min[0],b.max[1],b.min[2]);//   / 4+---/--+5
223        copyVector3Values(points[4],b.min[0],b.min[1],b.max[2]);// 3+------+2 /    y   z
224        copyVector3Values(points[5],b.max[0],b.min[1],b.max[2]);//  | /    | /     |  /
225        copyVector3Values(points[6],b.max[0],b.max[1],b.max[2]);//  |/     |/      |/
226        copyVector3Values(points[7],b.min[0],b.max[1],b.max[2]);// 0+------+1      *---x
227}
228
229
230void calcAABoxPlanes(struct VecPlane* planes, const struct AABox b) {
231        if(0 != planes) {
232                struct Plane *p;
233                planesSetSize(planes,6);
234               
235                //bottom plane
236                p = &(planes->plane[0]);
237                copyVector3Values(p->n,0,-1,0);
238                p->d = absDouble(b.min[1]);
239
240                //top plane
241                p = &(planes->plane[1]);
242                copyVector3Values(p->n,0,1,0);
243                p->d = absDouble(b.max[1]);
244
245                //left plane
246                p = &(planes->plane[2]);
247                copyVector3Values(p->n,-1,0,0);
248                p->d = absDouble(b.min[0]);
249
250                //right plane
251                p = &(planes->plane[3]);
252                copyVector3Values(p->n,1,0,0);
253                p->d = absDouble(b.max[0]);
254
255                //back plane
256                p = &(planes->plane[4]);
257                copyVector3Values(p->n,0,0,-1);
258                p->d = absDouble(b.min[2]);
259
260                //front plane
261                p = &(planes->plane[5]);
262                copyVector3Values(p->n,0,0,1);
263                p->d = absDouble(b.max[2]);
264
265        }
266}
267
268double getPlaneCoeff(struct Plane* plane, int i)
269{
270        if(i < 3) return plane->n[i];
271        return plane->d;
272}
273
274void setPlaneCoeff(double coeff, struct Plane* plane, int i)
275{
276        if(i < 3)
277                plane->n[i] = coeff;
278        else
279                plane->d = coeff;
280}
Note: See TracBrowser for help on using the repository browser.