Debian Codegraph @

lib/database/src/so/nodes/SoIndexedShape.c++

raw symbols SCIP
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 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.2 $
44 |
45 | Classes:
46 | SoIndexedShape
47 |
48 | Author(s) : Paul S. Strauss, Gavin Bell
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/actions/SoGetBoundingBoxAction.h>
55 #include <Inventor/elements/SoCoordinateElement.h>
56 #include <Inventor/elements/SoTextureCoordinateBindingElement.h>
57 #include <Inventor/errors/SoDebugError.h>
58 #include <Inventor/nodes/SoIndexedShape.h>
59
60 SO_NODE_ABSTRACT_SOURCE(SoIndexedShape);
61
62 int32_t *SoIndexedShape::consecutiveIndices = NULL;
63 int SoIndexedShape::numConsecutiveIndicesAllocated = 0;
64
65 ////////////////////////////////////////////////////////////////////////
66 //
67 // Description:
68 // Constructor
69 //
70 // Use: protected
71
72 SoIndexedShape::SoIndexedShape()
73 //
74 ////////////////////////////////////////////////////////////////////////
75 {
76 SO_NODE_CONSTRUCTOR(SoIndexedShape);
77
78 SO_NODE_ADD_FIELD(coordIndex, (0));
79 SO_NODE_ADD_FIELD(materialIndex, (-1));
80 SO_NODE_ADD_FIELD(normalIndex, (-1));
81 SO_NODE_ADD_FIELD(textureCoordIndex, (-1));
82 colorI = normalI = texCoordI = NULL;
83 // force reevaluation of binding:
84 materialBinding = normalBinding = texCoordBinding = 0;
85 }
86
87 ////////////////////////////////////////////////////////////////////////
88 //
89 // Description:
90 // Destructor
91 //
92 // Use: private
93
94 SoIndexedShape::~SoIndexedShape()
95 //
96 ////////////////////////////////////////////////////////////////////////
97 {
98 if (materialBinding == SoMaterialBindingElement::PER_VERTEX)
99 delete[] ((int32_t *)colorI);
100
101 if (normalBinding == SoNormalBindingElement::PER_VERTEX)
102 delete[] ((int32_t *)normalI);
103
104 if (texCoordBinding == SoTextureCoordinateBindingElement::PER_VERTEX)
105 delete[] ((int32_t *)texCoordI);
106
107 }
108
109 ////////////////////////////////////////////////////////////////////////
110 //
111 // Description:
112 // Keep things up to date when my fields change
113 //
114 // Use: protected
115
116 void
117 SoIndexedShape::notify(SoNotList *list)
118 //
119 ////////////////////////////////////////////////////////////////////////
120 {
121 if (list->getLastRec()->getType() == SoNotRec::CONTAINER) {
122 if (list->getLastField() == &coordIndex ||
123 list->getLastField() == &materialIndex ||
124 list->getLastField() == &normalIndex ||
125 list->getLastField() == &textureCoordIndex) {
126
127 // Force re-evaluation:
128 materialBinding = normalBinding = texCoordBinding = 0;
129 }
130 }
131
132 SoVertexShape::notify(list);
133 }
134
135 ////////////////////////////////////////////////////////////////////////
136 //
137 // Description:
138 // This handles bounding box computation for all shapes derived
139 // from this class. It sets the bounding box to contain all
140 // vertices of the shape, assuming that the shape uses the
141 // coordinates indexed by all non-negative values in the coordIndex
142 // field. It also sets the center to the average of the vertices'
143 // coordinates.
144 //
145 // Use: protected
146
147 void
148 SoIndexedShape::computeBBox(SoAction *action, SbBox3f &box, SbVec3f &center)
149 //
150 ////////////////////////////////////////////////////////////////////////
151 {
152 int32_t i, numIndices, numUsed;
153 const int32_t *indices;
154 const SoCoordinateElement *ce = NULL;
155 const SbVec3f *vpCoords = NULL;
156
157 SoVertexProperty *vp = (SoVertexProperty *)vertexProperty.getValue();
158 if (vp && vp->vertex.getNum() > 0) {
159 vpCoords = vp->vertex.getValues(0);
160 } else {
161 ce = SoCoordinateElement::getInstance(action->getState());
162 }
163
164 // Start with an empty box and zero sum
165 center.setValue(0, 0, 0);
166 box.makeEmpty();
167
168 // Loop through coordinates, keeping bounding box and sum of coords
169 numIndices = coordIndex.getNum();
170 indices = coordIndex.getValues(0);
171 numUsed = 0;
172 for (i = 0; i < numIndices; i++) {
173
174 // Look only at non-negative index values
175 if (indices[i] >= 0) {
176 const SbVec3f &v = (ce ? ce->get3((int) indices[i]) :
177 vpCoords[indices[i]]);
178 box.extendBy(v);
179 center += v;
180 numUsed++;
181 }
182 }
183
184 // Center is average of all coordinates
185 center /= (float) numUsed;
186 }
187
188 ////////////////////////////////////////////////////////////////////////
189 //
190 // Description:
191 // Returns TRUE if texture coordinates should be indexed.
192 //
193 // Use: protected, static
194
195 SbBool
196 SoIndexedShape::areTexCoordsIndexed(SoAction *action)
197 //
198 ////////////////////////////////////////////////////////////////////////
199 {
200 return SoTextureCoordinateBindingElement::get(action->getState())
201 == SoTextureCoordinateBindingElement::PER_VERTEX_INDEXED;
202 }
203
204 ////////////////////////////////////////////////////////////////////////
205 //
206 // Description:
207 // Starting at a given index in the coordIndex array, figure out
208 // how many vertices there are before either the end of the array
209 // or the next 'border' index:
210 //
211 // Use: protected
212
213 int
214 SoIndexedShape::getNumVerts(int startCoord)
215 //
216 ////////////////////////////////////////////////////////////////////////
217 {
218 int result = 0;
219 int numIndices = coordIndex.getNum();
220
221 while (startCoord+result < numIndices &&
222 coordIndex[startCoord+result] >= 0) {
223 result++;
224 }
225
226 return result;
227 }
228
229 ////////////////////////////////////////////////////////////////////////
230 //
231 // Description:
232 //
233 // Setup for fast rendering. This should be called by subclasses,
234 // which can then use the texCoordI/colorI/normalI arrays (which
235 // will either point to one of the coordIndex arrays, or to a
236 // consective array of integers. This must be called AFTER the
237 // vpCache has been filled in.
238 //
239 // Use: protected
240
241 void
242 SoIndexedShape::setupIndices(int numParts, int numFaces,
243 SbBool needNormals, SbBool needTexCoords)
244 //
245 ////////////////////////////////////////////////////////////////////////
246 {
247 #ifdef DEBUG
248 if (vpCache.vertexPtr == NULL) {
249 SoDebugError::post("SoIndexedShape::setupIndices",
250 "vpCache.fillInCache must be called first!\n");
251 }
252 //Check for valid indices:
253 int j;
254 for (j = 0; j < coordIndex.getNum(); j++){
255 if (coordIndex[j] != -1) {
256 if (coordIndex[j] >= vpCache.getNumVertices() ||
257 coordIndex[j] < 0 ){
258 SoDebugError::post("SoIndexedShape",
259 "vertex index %d is out of range 0 - %d",
260 coordIndex[j], vpCache.getNumVertices());
261 }
262 }
263 }
264 for (j = 0; j < materialIndex.getNum(); j++){
265 if (materialIndex[j] != -1) {
266 if (materialIndex[j] >= vpCache.getNumColors() ||
267 materialIndex[j] < 0 ){
268 SoDebugError::post("SoIndexedShape",
269 "material index %d is out of range 0 - %d",
270 materialIndex[j], vpCache.getNumColors());
271 }
272 }
273 }
274 if (needNormals) {
275 for (j = 0; j < normalIndex.getNum(); j++){
276 if (normalIndex[j] != -1) {
277 if (normalIndex[j] >= vpCache.getNumNormals() ||
278 normalIndex[j] < 0 ){
279 SoDebugError::post("SoIndexedShape",
280 "normal index %d is out of range 0 - %d",
281 normalIndex[j], vpCache.getNumNormals());
282 }
283 }
284 }
285 }
286 if (needTexCoords){
287 for (j = 0; j < textureCoordIndex.getNum(); j++){
288 if (textureCoordIndex[j] != -1) {
289 if (textureCoordIndex[j] >= vpCache.getNumTexCoords() ||
290 textureCoordIndex[j] < 0 ){
291 SoDebugError::post("SoIndexedShape",
292 "texture coordinate index %d is out of range 0 - %d",
293 textureCoordIndex[j], vpCache.getNumTexCoords());
294 }
295 }
296 }
297 }
298 #endif /*DEBUG*/
299
300 if (materialBinding != vpCache.getMaterialBinding()) {
301 // Free if old binding was PER_VERTEX:
302 if (materialBinding == SoMaterialBindingElement::PER_VERTEX) {
303 // Ok to cast const away here:
304 delete[] ((int32_t *)colorI);
305 colorI = NULL;
306 }
307 materialBinding = vpCache.getMaterialBinding();
308
309 #ifdef DEBUG
310 int numIndices = 0;
311 int numIndicesNeeded = 0;
312 SbBool useCoordIndexOK = TRUE;
313 int numColorsNeeded = 0;
314 const char *bindingString = NULL;
315 #endif
316
317 switch (vpCache.getMaterialBinding()) {
318 case SoMaterialBindingElement::OVERALL:
319 break;
320 case SoMaterialBindingElement::PER_PART:
321 allocateSequential(numParts);
322 colorI = NULL;
323 #ifdef DEBUG
324 numColorsNeeded = numParts;
325 bindingString = "PER_PART";
326 #endif
327 break;
328 case SoMaterialBindingElement::PER_FACE:
329 allocateSequential(numFaces);
330 colorI = NULL;
331 #ifdef DEBUG
332 numColorsNeeded = numFaces;
333 bindingString = "PER_FACE";
334 #endif
335 break;
336
337 case SoMaterialBindingElement::PER_VERTEX:
338 // The annoying case:
339 colorI = allocateSequentialWithHoles();
340 #ifdef DEBUG
341 for (int i = 0; i < coordIndex.getNum(); i++) {
342 if (coordIndex[i] >= 0) ++numColorsNeeded;
343 }
344 bindingString = "PER_VERTEX";
345 #endif
346 break;
347
348 case SoMaterialBindingElement::PER_PART_INDEXED:
349 if (materialIndex[0] < 0)
350 colorI = coordIndex.getValues(0);
351 else
352 colorI = materialIndex.getValues(0);
353 #ifdef DEBUG
354 useCoordIndexOK = FALSE;
355 numIndicesNeeded = numParts;
356 bindingString = "PER_PART_INDEXED";
357 if (colorI == coordIndex.getValues(0)) {
358 numIndices = coordIndex.getNum();
359 } else {
360 numIndices = materialIndex.getNum();
361 }
362 #endif
363 break;
364 case SoMaterialBindingElement::PER_FACE_INDEXED:
365 if (materialIndex[0] < 0)
366 colorI = coordIndex.getValues(0);
367 else
368 colorI = materialIndex.getValues(0);
369 #ifdef DEBUG
370 useCoordIndexOK = FALSE;
371 numIndicesNeeded = numFaces;
372 bindingString = "PER_FACE_INDEXED";
373 if (colorI == coordIndex.getValues(0)) {
374 numIndices = coordIndex.getNum();
375 } else {
376 numIndices = materialIndex.getNum();
377 }
378 #endif
379 break;
380 case SoMaterialBindingElement::PER_VERTEX_INDEXED:
381 if (materialIndex[0] < 0)
382 colorI = coordIndex.getValues(0);
383 else
384 colorI = materialIndex.getValues(0);
385 #ifdef DEBUG
386 useCoordIndexOK = TRUE;
387 numIndicesNeeded = coordIndex.getNum();
388 bindingString = "PER_VERTEX_INDEXED";
389 if (colorI == coordIndex.getValues(0)) {
390 numIndices = coordIndex.getNum();
391 } else {
392 numIndices = materialIndex.getNum();
393 }
394 #endif
395 break;
396 }
397
398 #ifdef DEBUG
399 // Check for mis-use of default materialIndex field
400 if (useCoordIndexOK == FALSE &&
401 colorI == coordIndex.getValues(0)) {
402 SoDebugError::post("SoIndexedShape",
403 "Material binding is %s"
404 " but materialIndex[0] < 0; coordIndex"
405 " will be used, which is probably not what"
406 " you want", bindingString);
407 }
408 // Check for enough indices:
409 if (numIndices < numIndicesNeeded) {
410 SoDebugError::post("SoIndexedShape",
411 "Need %d indices for %s "
412 " material binding, have only %d",
413 numIndicesNeeded, bindingString, numIndices);
414 }
415 else if (numIndices > 0) {
416 // Find greatest index:
417 for (int i = 0; i < numIndices; i++) {
418 if (colorI[i] > numColorsNeeded)
419 numColorsNeeded = colorI[i];
420 }
421 }
422 if (vpCache.getNumColors() < numColorsNeeded) {
423 SoDebugError::post("SoIndexedShape",
424 "Material binding is %s, but only %d"
425 " colors given (%d needed)",
426 bindingString, vpCache.getNumColors(), numColorsNeeded);
427 }
428 #endif
429 }
430 if (needNormals && (normalBinding != vpCache.getNormalBinding())) {
431 // Free if old binding was PER_VERTEX:
432 if (normalBinding == SoNormalBindingElement::PER_VERTEX) {
433 // Ok to cast const away here:
434 delete[] ((int32_t *)normalI);
435 normalI = NULL;
436 }
437 normalBinding = vpCache.getNormalBinding();
438
439 #ifdef DEBUG
440 int numIndices = 0;
441 int numIndicesNeeded = 0;
442 SbBool useCoordIndexOK = TRUE;
443 int numNormalsNeeded = 0;
444 const char *bindingString = NULL;
445 #endif
446
447 switch (vpCache.getNormalBinding()) {
448 case SoNormalBindingElement::OVERALL:
449 break;
450 case SoNormalBindingElement::PER_PART:
451 allocateSequential(numParts);
452 normalI = NULL;
453 #ifdef DEBUG
454 numNormalsNeeded = numParts;
455 bindingString = "PER_PART";
456 #endif
457 break;
458 case SoNormalBindingElement::PER_FACE:
459 allocateSequential(numFaces);
460 normalI = NULL;
461 #ifdef DEBUG
462 numNormalsNeeded = numFaces;
463 bindingString = "PER_FACE";
464 #endif
465 break;
466
467 case SoNormalBindingElement::PER_VERTEX:
468 // The annoying case:
469 normalI = allocateSequentialWithHoles();
470 #ifdef DEBUG
471 for (int i = 0; i < coordIndex.getNum(); i++) {
472 if (coordIndex[i] >= 0) ++numNormalsNeeded;
473 }
474 bindingString = "PER_VERTEX";
475 #endif
476 break;
477
478 case SoNormalBindingElement::PER_PART_INDEXED:
479 if (normalIndex[0] < 0)
480 normalI = coordIndex.getValues(0);
481 else
482 normalI = normalIndex.getValues(0);
483 #ifdef DEBUG
484 useCoordIndexOK = FALSE;
485 numIndicesNeeded = numParts;
486 bindingString = "PER_PART_INDEXED";
487 if (normalI == coordIndex.getValues(0)) {
488 numIndices = coordIndex.getNum();
489 } else {
490 numIndices = normalIndex.getNum();
491 }
492 #endif
493 break;
494 case SoNormalBindingElement::PER_FACE_INDEXED:
495 if (normalIndex[0] < 0)
496 normalI = coordIndex.getValues(0);
497 else
498 normalI = normalIndex.getValues(0);
499 #ifdef DEBUG
500 useCoordIndexOK = FALSE;
501 numIndicesNeeded = numFaces;
502 bindingString = "PER_FACE_INDEXED";
503 if (normalI == coordIndex.getValues(0)) {
504 numIndices = coordIndex.getNum();
505 } else {
506 numIndices = normalIndex.getNum();
507 }
508 #endif
509 break;
510 case SoNormalBindingElement::PER_VERTEX_INDEXED:
511 if (normalIndex[0] < 0)
512 normalI = coordIndex.getValues(0);
513 else
514 normalI = normalIndex.getValues(0);
515 #ifdef DEBUG
516 useCoordIndexOK = TRUE;
517 numIndicesNeeded = coordIndex.getNum();
518 bindingString = "PER_VERTEX_INDEXED";
519 if (normalI == coordIndex.getValues(0)) {
520 numIndices = coordIndex.getNum();
521 } else {
522 numIndices = normalIndex.getNum();
523 }
524 #endif
525 break;
526 }
527
528 #ifdef DEBUG
529 // Check for mis-use of default normalIndex field
530 if (useCoordIndexOK == FALSE &&
531 normalI == coordIndex.getValues(0)) {
532 SoDebugError::post("SoIndexedShape",
533 "Normal binding is %s"
534 " but normalIndex[0] < 0; coordIndex"
535 " will be used, which is probably not what"
536 " you want", bindingString);
537 }
538 // Check for enough indices:
539 if (numIndices < numIndicesNeeded) {
540 SoDebugError::post("SoIndexedShape",
541 "Need %d indices for %s "
542 " normal binding, have only %d",
543 numIndicesNeeded, bindingString, numIndices);
544 }
545 else if (numIndices > 0) {
546 // Find greatest index:
547 for (int i = 0; i < numIndices; i++) {
548 if (normalI[i] > numNormalsNeeded)
549 numNormalsNeeded = normalI[i];
550 }
551 }
552 if (vpCache.getNumNormals() < numNormalsNeeded) {
553 SoDebugError::post("SoIndexedShape",
554 "Normal binding is %s, but only %d"
555 " normals given (%d needed)",
556 bindingString, vpCache.getNumNormals(), numNormalsNeeded);
557 }
558 #endif
559 }
560
561 if (needTexCoords && (texCoordBinding != vpCache.getTexCoordBinding())) {
562 // Free if old binding was PER_VERTEX:
563 if (texCoordBinding == SoTextureCoordinateBindingElement::PER_VERTEX) {
564 // Ok to cast const away here:
565 delete[] ((int32_t *)texCoordI);
566 texCoordI = NULL;
567 }
568 texCoordBinding = vpCache.getTexCoordBinding();
569
570 #ifdef DEBUG
571 int numIndices = 0;
572 int numIndicesNeeded = 0;
573 int numTexCoordsNeeded = 0;
574 const char *bindingString = NULL;
575 #endif
576 switch (vpCache.getTexCoordBinding()) {
577 case SoTextureCoordinateBindingElement::PER_VERTEX:
578 // The annoying case:
579 texCoordI = allocateSequentialWithHoles();
580 #ifdef DEBUG
581 for (int i = 0; i < coordIndex.getNum(); i++) {
582 if (coordIndex[i] >= 0) ++numTexCoordsNeeded;
583 }
584 bindingString = "PER_VERTEX";
585 #endif
586 break;
587
588 case SoTextureCoordinateBindingElement::PER_VERTEX_INDEXED:
589 if (textureCoordIndex[0] < 0)
590 texCoordI = coordIndex.getValues(0);
591 else
592 texCoordI = textureCoordIndex.getValues(0);
593 #ifdef DEBUG
594 numIndicesNeeded = numFaces;
595 bindingString = "PER_VERTEX_INDEXED";
596 if (texCoordI == coordIndex.getValues(0)) {
597 numIndices = coordIndex.getNum();
598 } else {
599 numIndices = textureCoordIndex.getNum();
600 }
601 #endif
602 break;
603 }
604 #ifdef DEBUG
605 // Check for enough indices:
606 if (numIndices < numIndicesNeeded) {
607 SoDebugError::post("SoIndexedShape",
608 "Need %d indices for %s "
609 " texCoord binding, have only %d",
610 bindingString, numFaces, numIndices);
611 }
612 else if (numIndices > 0) {
613 // Find greatest index:
614 for (int i = 0; i < numIndices; i++) {
615 if (texCoordI[i] > numTexCoordsNeeded)
616 numTexCoordsNeeded = texCoordI[i];
617 }
618 }
619 if (vpCache.getNumTexCoords() < numTexCoordsNeeded) {
620 SoDebugError::post("SoIndexedShape",
621 "TexCoord binding is %s, but only %d"
622 " texCoords given (%d needed)",
623 bindingString, vpCache.getNumTexCoords(), numTexCoordsNeeded);
624 }
625 #endif
626 }
627 }
628
629 ////////////////////////////////////////////////////////////////////////
630 //
631 // Description:
632 // Re-allocate the consecutiveIndices array, if necessary
633 //
634 // Use: private
635
636 void
637 SoIndexedShape::allocateSequential(int howMany)
638 //
639 ////////////////////////////////////////////////////////////////////////
640 {
641 if (howMany > numConsecutiveIndicesAllocated) {
642 numConsecutiveIndicesAllocated = howMany;
643 if (consecutiveIndices != NULL) {
644 delete[] consecutiveIndices;
645 }
646 consecutiveIndices = new int32_t[howMany];
647 for (int i = 0; i < howMany; i++) {
648 consecutiveIndices[i] = i;
649 }
650 }
651 }
652
653 ////////////////////////////////////////////////////////////////////////
654 //
655 // Description:
656 // Return an array for PER_VERTEX bindings (with -1's in the right
657 // spots)
658 //
659 // Use: private
660
661 int32_t *
662 SoIndexedShape::allocateSequentialWithHoles()
663 //
664 ////////////////////////////////////////////////////////////////////////
665 {
666 int count = 0;
667 int num = coordIndex.getNum();
668 int32_t *result = new int32_t[num];
669 for (int i = 0; i < num; i++) {
670 if (coordIndex[i] >= 0) {
671 result[i] = count;
672 count++;
673 }
674 else
675 result[i] = coordIndex[i]; // Just copy-over negatives
676 }
677 return result;
678 }