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