Debian Codegraph @

apps/nodes/BarChart/BarChart.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 //
38 // Source file for "BarChart" shape node.
39 //
40
41 #include <assert.h>
42
43 #include <GL/gl.h>
44 #include <Inventor/SoDB.h>
45
46 #include <Inventor/SbBox.h>
47
48 #include <Inventor/actions/SoGLRenderAction.h>
49 #include <Inventor/actions/SoGetBoundingBoxAction.h>
50
51 #include <Inventor/nodes/SoDrawStyle.h>
52 #include <Inventor/nodes/SoLightModel.h>
53 #include <Inventor/nodes/SoSeparator.h>
54 #include <Inventor/nodes/SoTranslation.h>
55 #include <Inventor/nodes/SoScale.h>
56 #include <Inventor/nodes/SoRotationXYZ.h>
57 #include <Inventor/nodes/SoTransform.h>
58 #include <Inventor/nodes/SoCube.h>
59 #include <Inventor/nodes/SoText3.h>
60 #include <Inventor/nodes/SoCylinder.h>
61 #include <Inventor/nodes/SoSphere.h>
62 #include <Inventor/nodes/SoMaterial.h>
63
64 #include "BarChart.h"
65
66 SO_NODE_SOURCE(BarChart);
67
68 #define MAX(x,y) ((x) > (y) ? (x) : (y))
69 #define MIN(x,y) ((x) < (y) ? (x) : (y))
70 #define TWOPI (2.0*M_PI)
71
72 //
73 // This initializes the BarChart class.
74 //
75
76 void
77 BarChart::initClass()
78 {
79 SO_NODE_INIT_CLASS(BarChart, SoShape, "Shape");
80 }
81
82 //
83 // Constructor
84 //
85
86 BarChart::BarChart()
87 {
88 children = new SoChildList(this);
89
90 SO_NODE_CONSTRUCTOR(BarChart);
91 SO_NODE_ADD_FIELD(plateColor, (0.3, 0.3, 0.3, 0));
92 SO_NODE_ADD_FIELD(poleColor, (0, 0.3, 0.5, 0));
93 SO_NODE_ADD_FIELD(values, (1.0));
94 SO_NODE_ADD_FIELD(valueColors, (0.8, 0, 0, 0));
95 SO_NODE_ADD_FIELD(xLabels, ("X Label"));
96 SO_NODE_ADD_FIELD(xLabelColors, (0.8, 0, 0, 0));
97 SO_NODE_ADD_FIELD(xLabelScale, (1, 1, 1));
98 SO_NODE_ADD_FIELD(yLabels, ("Y Label"));
99 SO_NODE_ADD_FIELD(yLabelColors, (0.8, 0.8, 0, 0));
100 SO_NODE_ADD_FIELD(yLabelScale, (1, 1, 1));
101 SO_NODE_ADD_FIELD(xDimension, (1));
102 SO_NODE_ADD_FIELD(yDimension, (1));
103 SO_NODE_ADD_FIELD(xBarProportion, (0.7));
104 SO_NODE_ADD_FIELD(yBarProportion, (0.7));
105 SO_NODE_ADD_FIELD(minValue, (0));
106 SO_NODE_ADD_FIELD(maxValue, (1.0));
107 SO_NODE_ADD_FIELD(zLabelColor, (0.8, 0.8, 0, 0));
108 SO_NODE_ADD_FIELD(zMarkerColor, (0.8, 0, 0, 0));
109 SO_NODE_ADD_FIELD(zLabelScale, (1, 1, 1));
110 SO_NODE_ADD_FIELD(zLabelIncrement, (0.2));
111 SO_NODE_ADD_FIELD(barStyle, (BAR));
112 }
113
114 //
115 // Destructor
116 //
117
118 BarChart::~BarChart()
119 {
120 }
121
122 //
123 // Implements the SoGetBoundingBoxAction for the BarChart node.
124 //
125
126 SbBox3f
127 BarChart::getBbox() const
128 {
129 // everything inside BarChart normalizes to a unit volume
130 SbVec3f max( 1, 1, 1 );
131 return SbBox3f(-max, max);
132 }
133
134 void
135 BarChart::computeBBox(SoAction *, SbBox3f &box, SbVec3f &center)
136 {
137 box = getBbox();
138 center.setValue(0.0, 0.0, 0.0);
139 }
140
141 //
142 // Implements the SoGLRenderAction for the BarChart node.
143 //
144
145 void
146 BarChart::GLRender(SoGLRenderAction *action)
147 {
148 if (! shouldGLRender(action))
149 return;
150
151 if (children->getLength() == 0) generateChildren();
152 children->traverse(action);
153 }
154
155
156 void
157 BarChart::generatePrimitives(SoAction *action)
158 {
159 if (children->getLength() == 0) generateChildren();
160 children->traverse(action);
161 }
162
163 SoChildList
164 *BarChart::getChildren() const
165 {
166 return children;
167 }
168
169 void
170 BarChart::generateChildren()
171 {
172 SoTranslation *tr;
173 SoScale *sc;
174 SoRotationXYZ *rot;
175 SoMaterial *mtl;
176 SoSeparator *sep;
177
178 // this should get called only once so there should be no children yet.
179 assert(children->getLength() == 0);
180
181 // for testing, a wireframe cube surrounding the volume
182 /*
183 sep = new SoSeparator();
184 children->append(sep);
185 SoDrawStyle *ds = new SoDrawStyle();
186 ds->style = SoDrawStyle::LINES;
187 SoLightModel *lm = new SoLightModel();
188 lm->model = SoLightModel::BASE_COLOR;
189 sep->addChild(ds);
190 sep->addChild(lm);
191 sep->addChild(new SoCube);
192 */
193
194 // base plate
195 sep = new SoSeparator();
196 children->append(sep);
197 tr = new SoTranslation();
198 tr->translation.setValue(0, -0.95, 0);
199 sep->addChild(tr);
200 mtl = new SoMaterial();
201 mtl->diffuseColor.setValue( plateColor.getValue()[0],
202 plateColor.getValue()[1],
203 plateColor.getValue()[2] );
204 mtl->transparency.setValue( plateColor.getValue()[3] );
205 sep->addChild(mtl);
206 SoCube *plate = new SoCube();
207 plate->height = 0.1;
208 plate->width = 1.9;
209 plate->depth = 1.9;
210 sep->addChild(plate);
211
212 // boundary poles
213 sep = new SoSeparator();
214 children->append(sep);
215 mtl = new SoMaterial();
216 mtl->diffuseColor.setValue( poleColor.getValue()[0],
217 poleColor.getValue()[1],
218 poleColor.getValue()[2] );
219 mtl->transparency.setValue( poleColor.getValue()[3] );
220 sep->addChild(mtl);
221 SoCylinder *pole = new SoCylinder();
222 pole->radius = 0.03;
223 tr = new SoTranslation();
224 tr->translation.setValue(-0.95, 0, -0.95);
225 sep->addChild(tr);
226 sep->addChild(pole);
227 tr = new SoTranslation();
228 tr->translation.setValue(0, 0, 1.9);
229 sep->addChild(tr);
230 sep->addChild(pole);
231 tr = new SoTranslation();
232 tr->translation.setValue(1.9, 0, 0);
233 sep->addChild(tr);
234 sep->addChild(pole);
235 tr = new SoTranslation();
236 tr->translation.setValue(0, 0, -1.9);
237 sep->addChild(tr);
238 sep->addChild(pole);
239
240 // generate appropriate bars
241 float barXSpace = 1.8/MAX(xDimension.getValue(), 1);
242 float barYSpace = 1.8/MAX(yDimension.getValue(), 1);
243 float barWidth = barXSpace*MIN(xBarProportion.getValue(), 1);
244 float barDepth = barYSpace*MIN(yBarProportion.getValue(), 1);
245
246 switch (barStyle.getValue())
247 {
248 case BAR: generateBars(barXSpace, barYSpace, barWidth, barDepth);
249 break;
250 }
251
252 // add X labels
253 sep = new SoSeparator();
254 children->append(sep);
255 tr = new SoTranslation();
256 tr->translation.setValue(-0.9+barXSpace/2, -0.95, 0.98);
257 sep->addChild(tr);
258 sc = new SoScale();
259 sc->scaleFactor.setValue( xLabelScale.getValue()[0]*0.01,
260 xLabelScale.getValue()[1]*0.01,
261 xLabelScale.getValue()[2]*0.01 );
262 sep->addChild(sc);
263
264 int i;
265
266 for (i = 0; i < xLabels.getNum() && i < xDimension.getValue(); ++i)
267 {
268 SoMaterial *mtl = new SoMaterial();
269 int c = i % xLabelColors.getNum();
270 mtl->diffuseColor.setValue( xLabelColors[c][0],
271 xLabelColors[c][1],
272 xLabelColors[c][2] );
273 mtl->transparency.setValue( xLabelColors[c][3] );
274 sep->addChild(mtl);
275
276 SoText3 *text = new SoText3();
277 text->string = xLabels[i];
278 text->justification = SoText3::CENTER;
279 text->parts = SoText3::ALL;
280 sep->addChild(text);
281
282 tr = new SoTranslation();
283 tr->translation.setValue(barXSpace/(xLabelScale.getValue()[0]*0.01),
284 0, 0);
285 sep->addChild(tr);
286 }
287
288 // add Y labels (just rotate -90 degrees about XZ and do same as xlabels
289 sep = new SoSeparator();
290 children->append(sep);
291 rot = new SoRotationXYZ();
292 rot->axis = SoRotationXYZ::Y;
293 rot->angle = TWOPI/4;
294 sep->addChild(rot);
295 tr = new SoTranslation();
296 tr->translation.setValue(-0.9+barYSpace/2, -0.95, 0.98);
297 sep->addChild(tr);
298 sc = new SoScale();
299 sc->scaleFactor.setValue( yLabelScale.getValue()[0]*0.01,
300 yLabelScale.getValue()[1]*0.01,
301 yLabelScale.getValue()[2]*0.01 );
302 sep->addChild(sc);
303
304 for (i = 0; i < yLabels.getNum() && i < yDimension.getValue(); ++i)
305 {
306 SoMaterial *mtl = new SoMaterial();
307 int c = i % yLabelColors.getNum();
308 mtl->diffuseColor.setValue( yLabelColors[c][0],
309 yLabelColors[c][1],
310 yLabelColors[c][2] );
311 mtl->transparency.setValue( yLabelColors[c][3] );
312 sep->addChild(mtl);
313
314 SoText3 *text = new SoText3();
315 text->string = yLabels[i];
316 text->justification = SoText3::CENTER;
317 text->parts = SoText3::ALL;
318 sep->addChild(text);
319
320 tr = new SoTranslation();
321 tr->translation.setValue(barYSpace/(yLabelScale.getValue()[0]*0.01),
322 0, 0);
323 sep->addChild(tr);
324 }
325
326 // add Z markings
327 SoSeparator *zsep = new SoSeparator();
328 children->append(zsep);
329 tr = new SoTranslation();
330 tr->translation.setValue(-0.95, -0.95, 0.95);
331 zsep->addChild(tr);
332 sep = new SoSeparator();
333 zsep->addChild(sep);
334
335 float nzLabels = (maxValue.getValue() - minValue.getValue()) /
336 zLabelIncrement.getValue();
337
338 // first add the spheres for markers
339 mtl = new SoMaterial();
340 mtl->diffuseColor.setValue( zMarkerColor.getValue()[0],
341 zMarkerColor.getValue()[1],
342 zMarkerColor.getValue()[2] );
343 mtl->transparency.setValue( zMarkerColor.getValue()[3] );
344 sep->addChild(mtl);
345
346 float val;
347 SoSphere *sph = new SoSphere(); // sphere for instancing
348 sph->radius = 0.04;
349
350 for (val = minValue.getValue() + zLabelIncrement.getValue();
351 val <= maxValue.getValue();
352 val += zLabelIncrement.getValue())
353 {
354 tr = new SoTranslation();
355 tr->translation.setValue(0, (1.8/nzLabels), 0);
356 sep->addChild(tr);
357 sep->addChild(sph);
358 }
359
360 // now do the labels
361 sep = new SoSeparator();
362 zsep->addChild(sep);
363 SoTransform *xform = new SoTransform();
364 xform->scaleFactor.setValue(zLabelScale.getValue()[0]*0.01,
365 zLabelScale.getValue()[1]*0.01,
366 zLabelScale.getValue()[2]*0.01 );
367 xform->center.setValue(0, 0, 0.05);
368 xform->rotation.setValue(SbVec3f(0, 1, 0), TWOPI/10);
369 sep->addChild(xform);
370
371 mtl = new SoMaterial();
372 mtl->diffuseColor.setValue( zLabelColor.getValue()[0],
373 zLabelColor.getValue()[1],
374 zLabelColor.getValue()[2] );
375 mtl->transparency.setValue( zLabelColor.getValue()[3] );
376 sep->addChild(mtl);
377
378 for (val = minValue.getValue();
379 val <= maxValue.getValue();
380 val += zLabelIncrement.getValue())
381 {
382 SoText3 *text = new SoText3();
383 char s[40];
384 sprintf(s, "%g", val);
385 text->string = s;
386 text->justification = SoText3::CENTER;
387 text->parts = SoText3::ALL;
388 sep->addChild(text);
389
390 tr = new SoTranslation();
391 tr->translation.setValue(0, (1.8/nzLabels) /
392 (zLabelScale.getValue()[0]*0.01), 0);
393 sep->addChild(tr);
394 }
395 }
396
397 void BarChart::generateBars(float barXSpace, float barYSpace,
398 float barWidth, float barDepth)
399 {
400 // first, figure out the tallest bar for scaling
401 float maxbar = values[0];
402 float minbar = minValue.getValue();
403 int i = values.getNum();
404 while (--i > 0)
405 if (values[i] > maxbar) maxbar = values[i];
406
407 if (maxbar > maxValue.getValue()) maxbar = maxValue.getValue();
408
409 // bars
410 SoSeparator *sep = new SoSeparator();
411 children->append(sep);
412 SoTranslation *tr = new SoTranslation();
413 tr->translation.setValue(-0.9+barXSpace/2, 0.09, 0.9-barYSpace/2);
414 sep->addChild(tr);
415
416 int n = 0;
417 int j = 0;
418
419 // traversal in both directions will go 1.8 units, so
420 // each movement will be 1.8 / ({x,y}Dim - 1)
421
422 for ( ; ; )
423 {
424 SoSeparator *ysep = new SoSeparator();
425 sep->addChild(ysep);
426
427 if (n >= values.getNum()) break;
428 float curPlant = 0;
429 int i = 0;
430
431 for ( ; ; )
432 {
433 if (n >= values.getNum()) break;
434
435 // translate in Y to plant on base plate
436 float curZ = (values[n]-minbar) / maxbar * 1.8;
437
438 tr = new SoTranslation();
439 tr->translation.setValue(0, curPlant-(1-curZ/2), 0);
440 curPlant = (1-curZ/2);
441 ysep->addChild(tr);
442 SoMaterial *mtl = new SoMaterial();
443 int c = n % valueColors.getNum();
444 mtl->diffuseColor.setValue( valueColors[c][0],
445 valueColors[c][1],
446 valueColors[c][2] );
447 mtl->transparency.setValue( valueColors[c][3] );
448 ysep->addChild(mtl);
449
450 SoCube *bar = new SoCube();
451 bar->width = barWidth;
452 bar->depth = barDepth;
453 bar->height = curZ;
454 ysep->addChild(bar);
455 n++;
456
457 if (++i >= xDimension.getValue()) break;
458 tr = new SoTranslation();
459 tr->translation.setValue(barXSpace, 0, 0);
460 ysep->addChild(tr);
461 }
462 if (++j >= yDimension.getValue()) break;
463 tr = new SoTranslation();
464 tr->translation.setValue(0, 0, -barYSpace);
465 sep->addChild(tr);
466 }
467 }