source: GTP/trunk/App/Demos/Vis/Teapots/DataTypes.c @ 643

Revision 643, 5.1 KB checked in by mattausch, 18 years ago (diff)

made demo running

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        int i;
22        for(i = 0; i < 3; i++) {
23                result[i] = a[i]+b[i];
24        }
25}
26
27void diffVector3(Vector3 result, const Vector3 a, const Vector3 b) {
28        int i;
29        for(i = 0; i < 3; i++) {
30                result[i] = a[i]-b[i];
31        }
32}
33
34void copyVector3Values(Vector3 result, const double x, const double y, const double z) {
35        result[0] = x;
36        result[1] = y;
37        result[2] = z;
38}
39
40void copyMatrix(Matrix4x4 result, const Matrix4x4 input) {
41        if(input != result) {
42                memcpy(result,input,4*4*sizeof(double));
43        }
44}
45
46void planesSetSize(struct VecPlane* p, const int size) {
47        if(0 != p) {
48                if(size == p->size) {
49                        return;
50                }
51                p->plane = (struct Plane*)realloc(p->plane,size*sizeof(struct Plane));
52                p->size = size;
53        }
54}
55
56void emptyVecPlane(struct VecPlane* p) {
57        planesSetSize(p,0);
58}
59
60void vecPointSetSize(struct VecPoint* poly, const int size) {
61        if(0 != poly) {
62                if(size == poly->size) {
63                        return;
64                }
65                poly->points = (Vector3*)realloc(poly->points,size*sizeof(Vector3));
66                poly->size = size;
67        }
68}
69
70void emptyVecPoint(struct VecPoint* poly) {
71        vecPointSetSize(poly,0);
72}
73
74void append2VecPoint(struct VecPoint* poly, const Vector3 p) {
75        if(0 != poly) {
76                int size = poly->size;
77                vecPointSetSize(poly,size+1);
78                copyVector3(poly->points[size],p);
79        }
80}
81
82void copyVecPoint(struct VecPoint* poly, const struct VecPoint poly2) {
83        if(0 != poly) {
84                int i;
85                vecPointSetSize(poly,poly2.size);
86                for(i= 0; i < poly2.size; i++) {
87                        copyVector3(poly->points[i],poly2.points[i]);
88                }
89        }
90}
91
92void swapVecPoint(struct VecPoint* poly1, struct VecPoint* poly2) {
93        if(0 != poly1 && 0 != poly2 && poly1 != poly2) {
94                {
95                        Vector3* points = poly1->points;
96                        poly1->points = poly2->points;
97                        poly2->points = points;
98                }
99                {
100                        int size = poly1->size;
101                        poly1->size = poly2->size;
102                        poly2->size = size;
103                }
104        }
105}
106
107void objectSetSize(struct Object* obj, const int size) {
108        if(0 != obj) {
109                int i;
110                if(size == obj->size) {
111                        return;
112                }
113                //dispose if shrinking
114                for(i = size; i < obj->size; i++) {
115                        emptyVecPoint( &(obj->poly[i]) );
116                }
117                //allocate new place
118                obj->poly = (struct VecPoint*)realloc(obj->poly,size*sizeof(struct VecPoint));
119                //initialize new place
120                for(i = obj->size; i < size; i++) {
121                        obj->poly[i] = VECPOINT_NULL;
122                }
123                obj->size = size;
124        }
125}
126
127void emptyObject(struct Object* obj) {
128        objectSetSize(obj,0);
129}
130
131void copyObject(struct Object* obj, const struct Object objIn) {
132        if(0 != obj) {
133                int i;
134                objectSetSize(obj,objIn.size);
135                for(i = 0; i < objIn.size; i++) {
136                        copyVecPoint( &(obj->poly[i]), objIn.poly[i]);
137                }
138        }
139}
140
141void append2Object(struct Object* obj, const struct VecPoint poly) {
142        if(0 != obj) {
143                int size = obj->size;
144                objectSetSize(obj,size+1);
145                copyVecPoint( &(obj->poly[size]) ,poly);
146        }
147}
148
149
150void convObject2VecPoint(struct VecPoint* points,const struct Object obj) {
151        if(0 != points) {
152                int i, j;
153                emptyVecPoint(points);
154                for(i = 0; i < obj.size; i++) {
155                        struct VecPoint* p = &(obj.poly[i]);
156                        for(j = 0; j < p->size; j++) {
157                                append2VecPoint(points,p->points[j]);
158                        }
159                }
160        }
161}
162
163void calcAABoxPoints(Vector3x8 points, const struct AABox b) {
164    //generate 8 corners of the box
165        copyVector3Values(points[0],b.min[0],b.min[1],b.min[2]);//     7+------+6
166        copyVector3Values(points[1],b.max[0],b.min[1],b.min[2]);//     /|     /|
167        copyVector3Values(points[2],b.max[0],b.max[1],b.min[2]);//    / |    / |
168        copyVector3Values(points[3],b.min[0],b.max[1],b.min[2]);//   / 4+---/--+5
169        copyVector3Values(points[4],b.min[0],b.min[1],b.max[2]);// 3+------+2 /    y   z
170        copyVector3Values(points[5],b.max[0],b.min[1],b.max[2]);//  | /    | /     |  /
171        copyVector3Values(points[6],b.max[0],b.max[1],b.max[2]);//  |/     |/      |/
172        copyVector3Values(points[7],b.min[0],b.max[1],b.max[2]);// 0+------+1      *---x
173}
174
175void calcAABoxPlanes(struct VecPlane* planes, const struct AABox b) {
176        if(0 != planes) {
177                struct Plane *p;
178                planesSetSize(planes,6);
179               
180                //bottom plane
181                p = &(planes->plane[0]);
182                copyVector3Values(p->n,0,-1,0);
183                p->d = absDouble(b.min[1]);
184
185                //top plane
186                p = &(planes->plane[1]);
187                copyVector3Values(p->n,0,1,0);
188                p->d = absDouble(b.max[1]);
189
190                //left plane
191                p = &(planes->plane[2]);
192                copyVector3Values(p->n,-1,0,0);
193                p->d = absDouble(b.min[0]);
194
195                //right plane
196                p = &(planes->plane[3]);
197                copyVector3Values(p->n,1,0,0);
198                p->d = absDouble(b.max[0]);
199
200                //back plane
201                p = &(planes->plane[4]);
202                copyVector3Values(p->n,0,0,-1);
203                p->d = absDouble(b.min[2]);
204
205                //front plane
206                p = &(planes->plane[5]);
207                copyVector3Values(p->n,0,0,1);
208                p->d = absDouble(b.max[2]);
209
210        }
211}
212
213double getPlaneCoeff(struct Plane* plane, int i)
214{
215        if(i < 3) return plane->n[i];
216        return plane->d;
217}
218
219void setPlaneCoeff(double coeff, struct Plane* plane, int i)
220{
221        if(i < 3)
222                plane->n[i] = coeff;
223        else
224                plane->d = coeff;
225}
Note: See TracBrowser for help on using the repository browser.