Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
X3DBuffer.c
Go to the documentation of this file.
1 /* @(#)root/g3d:$Id$ */
2 /* Author: Nenad Buncic 13/12/95*/
3 
4 #include "X3DBuffer.h"
5 #include "../../x3d/src/X3DDefs.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 
9 Size3D gVarSize3D;
10 Size3D* gFuncSize3D(){ return &gVarSize3D; }
11 
12 #if defined (WIN32) || defined (__MWERKS__)
13  void FillX3DBuffer (X3DBuffer *buff) { }
14  int AllocateX3DBuffer () { return 0;}
15 #else
16 
17 int currPoint = 0;
18 int currSeg = 0;
19 int currPoly = 0;
20 
21 Color *colors;
22 point *points;
23 segment *segs;
24 polygon *polys;
25 
26 
27 int AllocateX3DBuffer ()
28 {
29 /******************************************************************************
30  Allocate memory for points, colors, segments and polygons.
31  Returns 1 if OK, otherwise 0.
32 ******************************************************************************/
33 
34  int ret = 1;
35 
36  points = NULL;
37  colors = NULL;
38  segs = NULL;
39  polys = NULL;
40 
41  /*
42  * Allocate memory for points
43  */
44 
45  if (gSize3D.numPoints) {
46  points = (point *) calloc(gSize3D.numPoints, sizeof (point));
47  if (!points) {
48  puts ("Unable to allocate memory for points !");
49  ret = 0;
50  }
51  }
52  else return (0); /* if there are no points, return back */
53 
54 
55 
56  /*
57  * Allocate memory for colors
58  */
59 
60  colors = (Color *) calloc(28+4, sizeof (Color));
61  if(!colors) {
62  puts ("Unable to allocate memory for colors !");
63  ret = 0;
64  }
65  else {
66  colors[ 0].red = 92; colors[ 0].green = 92; colors[0].blue = 92;
67  colors[ 1].red = 122; colors[ 1].green = 122; colors[1].blue = 122;
68  colors[ 2].red = 184; colors[ 2].green = 184; colors[2].blue = 184;
69  colors[ 3].red = 215; colors[ 3].green = 215; colors[3].blue = 215;
70  colors[ 4].red = 138; colors[ 4].green = 15; colors[4].blue = 15;
71  colors[ 5].red = 184; colors[ 5].green = 20; colors[5].blue = 20;
72  colors[ 6].red = 235; colors[ 6].green = 71; colors[6].blue = 71;
73  colors[ 7].red = 240; colors[ 7].green = 117; colors[7].blue = 117;
74  colors[ 8].red = 15; colors[ 8].green = 138; colors[8].blue = 15;
75  colors[ 9].red = 20; colors[ 9].green = 184; colors[9].blue = 20;
76  colors[10].red = 71; colors[10].green = 235; colors[10].blue = 71;
77  colors[11].red = 117; colors[11].green = 240; colors[11].blue = 117;
78  colors[12].red = 15; colors[12].green = 15; colors[12].blue = 138;
79  colors[13].red = 20; colors[13].green = 20; colors[13].blue = 184;
80  colors[14].red = 71; colors[14].green = 71; colors[14].blue = 235;
81  colors[15].red = 117; colors[15].green = 117; colors[15].blue = 240;
82  colors[16].red = 138; colors[16].green = 138; colors[16].blue = 15;
83  colors[17].red = 184; colors[17].green = 184; colors[17].blue = 20;
84  colors[18].red = 235; colors[18].green = 235; colors[18].blue = 71;
85  colors[19].red = 240; colors[19].green = 240; colors[19].blue = 117;
86  colors[20].red = 138; colors[20].green = 15; colors[20].blue = 138;
87  colors[21].red = 184; colors[21].green = 20; colors[21].blue = 184;
88  colors[22].red = 235; colors[22].green = 71; colors[22].blue = 235;
89  colors[23].red = 240; colors[23].green = 117; colors[23].blue = 240;
90  colors[24].red = 15; colors[24].green = 138; colors[24].blue = 138;
91  colors[25].red = 20; colors[25].green = 184; colors[25].blue = 184;
92  colors[26].red = 71; colors[26].green = 235; colors[26].blue = 235;
93  colors[27].red = 117; colors[27].green = 240; colors[27].blue = 240;
94  }
95 
96 
97  /*
98  * Allocate memory for segments
99  */
100 
101  if (gSize3D.numSegs) {
102  segs = (segment *) calloc (gSize3D.numSegs, sizeof (segment));
103  if (!segs) {
104  puts ("Unable to allocate memory for segments !");
105  ret = 0;
106  }
107  }
108 
109 
110  /*
111  * Allocate memory for polygons
112  */
113 
114  if (gSize3D.numPolys) {
115  polys = (polygon *) calloc(gSize3D.numPolys, sizeof (polygon));
116  if (!polys) {
117  puts ("Unable to allocate memory for polygons !");
118  ret = 0;
119  }
120  }
121 
122  /*
123  * In case of error, free allocated memory
124  */
125 
126 
127  if (!ret) {
128  if (points) free (points);
129  if (colors) free (colors);
130  if (segs) free (segs);
131  if (polys) free (polys);
132 
133  points = NULL;
134  colors = NULL;
135  segs = NULL;
136  polys = NULL;
137  }
138 
139  return (ret);
140 }
141 
142 void FillX3DBuffer (X3DBuffer *buff)
143 {
144 /******************************************************************************
145  Read points, Read segments & Read polygons
146 ******************************************************************************/
147 
148 
149  int n, i, j, p, q, c;
150  int oldNumOfPoints, oldNumOfSegments;
151 
152  if (buff) {
153 
154  oldNumOfPoints = currPoint;
155  oldNumOfSegments = currSeg;
156 
157  /*
158  * Read points
159  */
160 
161  for (i = 0; i < buff->numPoints; i++, currPoint++) {
162  points[currPoint].x = buff->points[3*i ];
163  points[currPoint].y = buff->points[3*i+1];
164  points[currPoint].z = buff->points[3*i+2];
165  }
166 
167 
168  /*
169  * Read segments
170  */
171 
172  for (i = 0; i < buff->numSegs; i++, currSeg++) {
173  c = buff->segs[3*i];
174  p = oldNumOfPoints + buff->segs[3*i+1];
175  q = oldNumOfPoints + buff->segs[3*i+2];
176 
177  segs[currSeg].color = &(colors[c]);
178  segs[currSeg].P = &(points[p]);
179  segs[currSeg].Q = &(points[q]);
180 
181  /*
182  * Update points' segment lists
183  */
184 
185  if(points[p].numSegs == 0){
186  if((points[p].segs = (segment **)calloc(1, sizeof(segment *))) == NULL){
187  puts("Unable to allocate memory for point segments !");
188  return;
189  }
190  }else{
191  if((points[p].segs = (segment **)realloc(points[p].segs,
192  (points[p].numSegs + 1) * sizeof(segment *))) == NULL){
193  puts("Unable to allocate memory for point segments !");
194  return;
195  }
196  }
197 
198  if(points[q].numSegs == 0){
199  if((points[q].segs = (segment **)calloc(1, sizeof(segment *))) == NULL){
200  puts("Unable to allocate memory for point segments !");
201  return;
202  }
203  }else{
204  if((points[q].segs = (segment **)realloc(points[q].segs,
205  (points[q].numSegs + 1) * sizeof(segment *))) == NULL){
206  puts("Unable to allocate memory for point segments !");
207  return;
208  }
209  }
210  points[p].segs[points[p].numSegs] = &(segs[currSeg]);
211  points[q].segs[points[q].numSegs] = &(segs[currSeg]);
212  points[p].numSegs++;
213  points[q].numSegs++;
214 
215  }
216 
217  /*
218  * Read polygons
219  */
220 
221  n = 0;
222 
223  for (i = 0; i < buff->numPolys; i++, currPoly++) {
224  c = buff->polys[n++];
225  polys[currPoly].color = &(colors)[c];
226  polys[currPoly].numSegs = buff->polys[n++];
227 
228  polys[currPoly].segs = (segment **) calloc(polys[currPoly].numSegs, sizeof(segment *));
229  if (!polys[currPoly].segs) {
230  puts("Unable to allocate memory for polygon segments !");
231  return;
232  }
233  for (j = 0; j < polys[currPoly].numSegs; j++) {
234  int seg = oldNumOfSegments + buff->polys[n++];
235  polys[currPoly].segs[j] = &(segs[seg]);
236 
237  /*
238  * Update segments' polygon lists
239  */
240 
241  if(segs[seg].numPolys == 0) {
242  if((segs[seg].polys = (polygon **) calloc(1, sizeof(polygon *)))== NULL){
243  puts("Unable to allocate memory for segment polygons !");
244  return;
245  }
246  }
247  else{
248  if((segs[seg].polys = (polygon **) realloc(segs[seg].polys,
249  (segs[seg].numPolys + 1) * sizeof(polygon *))) == NULL){
250  puts("Unable to allocate memory for segment polygons !");
251  return;
252  }
253  }
254  segs[seg].polys[segs[seg].numPolys] = &(polys[currPoly]);
255  segs[seg].numPolys++;
256  }
257  }
258  }
259 }
260 
261 #endif
262