Source

Coin / src / nodes / SoArray.cpp

Full commit
Morten Eriksen 08431c9 
Marius Kintel 3ba6b41 




























Morten Eriksen 08431c9 



Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 







Peder Blekken 604c3ed 
Morten Eriksen d80f6b1 
Morten Eriksen b60274e 

Morten Eriksen 37c5269 








Morten Eriksen b60274e 
Morten Eriksen 37c5269 
Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 



Peder Blekken 604c3ed 
Morten Eriksen d80f6b1 
Peder Blekken 604c3ed 
Morten Eriksen 08431c9 

Peder Blekken 7bab509 
Morten Eriksen 08431c9 
Morten Eriksen d80f6b1 
Morten Eriksen f2522a7 
Lars J. Aas 92263e4 

Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 

Morten Eriksen 08431c9 




Morten Eriksen d80f6b1 




Morten Eriksen 08431c9 
Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 


Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 


Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 
Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 


Morten Eriksen 08431c9 


Morten Eriksen d80f6b1 


Morten Eriksen 08431c9 


Morten Eriksen d80f6b1 


Morten Eriksen 08431c9 



Morten Eriksen f9a2eff 
Morten Eriksen 08431c9 



Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 
Morten Eriksen c36f67e 
Morten Eriksen 08431c9 







Morten Eriksen b66cdde 
Morten Eriksen 08431c9 













Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 


Peder Blekken 6c32f39 
Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 


Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 




Morten Eriksen b66cdde 
Morten Eriksen 08431c9 











Morten Eriksen b66cdde 


Morten Eriksen 08431c9 
Morten Eriksen b66cdde 












Morten Eriksen 08431c9 
Morten Eriksen b66cdde 

Morten Eriksen 08431c9 
Morten Eriksen b66cdde 



Morten Eriksen 08431c9 

Morten Eriksen b66cdde 




Morten Eriksen 08431c9 

Morten Eriksen b66cdde 

Morten Eriksen 08431c9 
Morten Eriksen b66cdde 
Morten Eriksen 08431c9 
Morten Eriksen b66cdde 



Morten Eriksen 08431c9 
Morten Eriksen b66cdde 


Morten Eriksen 08431c9 
Morten Eriksen b66cdde 
Peder Blekken 7bab509 
Morten Eriksen 08431c9 
Morten Eriksen b66cdde 














Morten Eriksen 08431c9 























Morten Eriksen b66cdde 
Morten Eriksen 08431c9 











Morten Eriksen b66cdde 
Morten Eriksen 08431c9 












Morten Eriksen b66cdde 



Morten Eriksen 08431c9 
Morten Eriksen b66cdde 


Peder Blekken 7bab509 
Morten Eriksen b66cdde 
Frode Øijord 99965ff 
Morten Eriksen b66cdde 











Morten Eriksen 08431c9 




Morten Eriksen b66cdde 
Morten Eriksen 08431c9 
Morten Eriksen c71c9bd 
Morten Eriksen 08431c9 


Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 


Morten Eriksen f2522a7 
Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 


Morten Eriksen 3523436 
Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 
Morten Eriksen 310dd16 
Morten Eriksen 08431c9 
Morten Eriksen 310dd16 




Morten Eriksen b66cdde 


Morten Eriksen 310dd16 
Morten Eriksen b66cdde 












Morten Eriksen 310dd16 
Morten Eriksen b66cdde 










Frode Øijord 99965ff 
Morten Eriksen b66cdde 





Morten Eriksen 310dd16 


Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 
Morten Eriksen 310dd16 
Morten Eriksen 08431c9 
Morten Eriksen 3dce2bc 
Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 
Morten Eriksen 310dd16 
Morten Eriksen 08431c9 
Morten Eriksen d80f6b1 











Morten Eriksen 310dd16 
Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 
Morten Eriksen 310dd16 
Morten Eriksen 08431c9 
Morten Eriksen 310dd16 

Morten Eriksen 08431c9 

Peder Blekken d024875 






Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 
Morten Eriksen 310dd16 
Morten Eriksen c71c18d 


Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 


Peder Blekken 604c3ed 






Morten Eriksen 08431c9 
Peder Blekken 604c3ed 
Morten Eriksen 08431c9 

Morten Eriksen d80f6b1 
Morten Eriksen 08431c9 
Morten Eriksen e0fb354 
Morten Eriksen 08431c9 
Morten Eriksen b66cdde 
Morten Eriksen 08431c9 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

/*!
  \class SoArray SoArray.h Inventor/nodes/SoArray.h
  \brief The SoArray class is a group node for setting up regular arrays of subgraphs.
  \ingroup nodes

  SoArray presents a convenient way of duplicating a node (typically a
  shape node) or a complete subgraph in 1 to 3 dimensions.

  The child node or subgraph can only be translated by regular offsets
  for all dimensions.

  For more flexible functionality for duplication of geometry, see the
  SoMultipleCopy group node, which can do general transformations
  (including rotation and scaling) for its child.

  <b>FILE FORMAT/DEFAULTS:</b>
  \code
    Array {
        origin FIRST
        numElements1 1
        numElements2 1
        numElements3 1
        separation1 1 0 0
        separation2 0 1 0
        separation3 0 0 1
    }
  \endcode

  \sa SoMultipleCopy
*/

#include <Inventor/nodes/SoArray.h>

#include <Inventor/nodes/SoSwitch.h> // SO_SWITCH_ALL
#include <Inventor/actions/SoCallbackAction.h>
#include <Inventor/actions/SoSearchAction.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/actions/SoGetBoundingBoxAction.h>
#include <Inventor/elements/SoBBoxModelMatrixElement.h>
#include <Inventor/elements/SoSwitchElement.h>
#include <Inventor/misc/SoState.h>

#include "nodes/SoSubNodeP.h"

/*!
  \enum SoArray::Origin

  The possible settings for the SoArray::origin field.
*/


/*!
  \var SoSFEnum SoArray::origin

  Where the origin of the array should be set, ie how the array
  elements will be distributed from the local origo.

  Default value is SoArray::FIRST.
*/

/*!
  \var SoSFShort SoArray::numElements1
  Number of duplicates for each X axis row. Default 1.
*/
/*!
  \var SoSFShort SoArray::numElements2
  Number of duplicates for each Y axis row. Default 1.
*/
/*!
  \var SoSFShort SoArray::numElements3
  Number of duplicates for each Z axis row. Default 1.
*/

/*!
  \var SoSFVec3f SoArray::separation1

  Distance in current units between the center point of each element
  along the X axis. Default [1.0, 0.0, 0.0].
*/
/*!
  \var SoSFVec3f SoArray::separation2

  Distance in current units between the center point of each element
  along the Y axis. Default [0.0, 1.0, 0.0].
*/
/*!
  \var SoSFVec3f SoArray::separation3

  Distance in current units between the center point of each element
  along the Z axis. Default [0.0, 0.0, 1.0].
*/

// *************************************************************************

SO_NODE_SOURCE(SoArray);

/*!
  Constructor.
*/
SoArray::SoArray(void)
{
  SO_NODE_INTERNAL_CONSTRUCTOR(SoArray);

  SO_NODE_ADD_FIELD(origin, (SoArray::FIRST));
  SO_NODE_ADD_FIELD(numElements1, (1));
  SO_NODE_ADD_FIELD(numElements2, (1));
  SO_NODE_ADD_FIELD(numElements3, (1));
  SO_NODE_ADD_FIELD(separation1, (SbVec3f(1, 0, 0)));
  SO_NODE_ADD_FIELD(separation2, (SbVec3f(0, 1, 0)));
  SO_NODE_ADD_FIELD(separation3, (SbVec3f(0, 0, 1)));


  SO_NODE_DEFINE_ENUM_VALUE(Origin, FIRST);
  SO_NODE_DEFINE_ENUM_VALUE(Origin, CENTER);
  SO_NODE_DEFINE_ENUM_VALUE(Origin, LAST);
  SO_NODE_SET_SF_ENUM_TYPE(origin, Origin);
}

/*!
  Destructor.
*/
SoArray::~SoArray()
{
}

// Doc in superclass.
void
SoArray::initClass(void)
{
  SO_NODE_INTERNAL_INIT_CLASS(SoArray, SO_FROM_INVENTOR_1);
}

// Doc in superclass.
void
SoArray::getBoundingBox(SoGetBoundingBoxAction * action)
{
#if 0 // OBSOLETED: mortene's old (buggy ?) code (removed 19990423, pederb)
  // store incoming modelmatrix
  SbMatrix mat = SoModelMatrixElement::get(action->getState());

  // get reference to the box
  SbXfBox3f & box = action->getXfBoundingBox();

  // store current bbox
  SbXfBox3f incomingbox = box;

  // accumulation variables
  SbVec3f acccenter(0.0f, 0.0f, 0.0f);
  int numCenters = 0;
  SbXfBox3f totalbox;

  for (int i=0; i < numElements3.getValue(); i++) {
    for (int j=0; j < numElements2.getValue(); j++) {
      for (int k=0; k < numElements1.getValue(); k++) {

        float multfactor_i = float(i);
        float multfactor_j = float(j);
        float multfactor_k = float(k);

        switch (origin.getValue()) {
        case SoArray::FIRST:
          break;
        case SoArray::CENTER:
          multfactor_i = -float(numElements3.getValue()-1.0f)/2.0f + float(i);
          multfactor_j = -float(numElements2.getValue()-1.0f)/2.0f + float(j);
          multfactor_k = -float(numElements1.getValue()-1.0f)/2.0f + float(k);
          break;
        case SoArray::LAST:
          multfactor_i = -multfactor_i;
          multfactor_j = -multfactor_j;
          multfactor_k = -multfactor_k;
          break;

        default: assert(0); break;
        }

        SbVec3f instance_pos =
          separation3.getValue() * multfactor_i +
          separation2.getValue() * multfactor_j +
          separation1.getValue() * multfactor_k;

#if 0 // debug
        SoDebugError::postInfo("SoArray::getBoundingBox",
                               "instance_pos: <%f, %f, %f>",
                               instance_pos[0],
                               instance_pos[1],
                               instance_pos[2]);
#endif // debug

        SbMatrix mat;
        mat.setTranslate(instance_pos);

        action->getState()->push();

        SoSwitchElement::set(action->getState(),
                             i * numElements2.getValue() *
                             numElements1.getValue() +
                             j * numElements1.getValue() + k);

        // make current box empty to calculate bbox of this separator
        box.makeEmpty();
        box.setTransform(SbMatrix::identity());

        // set local matrix to identity
        SoBBoxModelMatrixElement::set(action->getState(), this, mat);

        // traverse all children, calculate the local bbox
        inherited::getBoundingBox(action);

        // If center point is set, accumulate center.
        if (action->isCenterSet()) {
          acccenter += action->getCenter();
          numCenters++;
          action->resetCenter();
        }

        // expand box by stored bbox
        if (!totalbox.isEmpty()) box.extendBy(totalbox);
        totalbox = box;

        action->getState()->pop();
      }
    }
  }

  // transform the local bbox by stored model matrix
  if (!box.isEmpty()) box.transform(mat);
  if (!incomingbox.isEmpty()) box.extendBy(incomingbox);

  if (numCenters != 0)
    action->setCenter(acccenter / numCenters, FALSE);

#else // "new" code, 19990423, pederb

  float curri = 0.0f;
  float currj = 0.0f;
  float currk = 0.0f;

  float inci = 1.0f;
  float incj = 1.0f;
  float inck = 1.0f;

  // accumulation variables
  SbVec3f acccenter(0.0f, 0.0f, 0.0f);
  int numCenters = 0;

  switch (origin.getValue()) {
  case SoArray::FIRST:
    break;
  case SoArray::CENTER:
    curri = -(numElements3.getValue()-1.0f)/2.0f;
    currj = -(numElements2.getValue()-1.0f)/2.0f;
    currk = -(numElements1.getValue()-1.0f)/2.0f;
    break;
  case SoArray::LAST:
    inci = -1.0f;
    incj = -1.0f;
    inck = -1.0f;
    break;
  default: assert(0); break;
  }

  float initj = currj;
  float initk = currk;

  int N = 0;
  for (int i=0; i < numElements3.getValue(); i++) {
    currj = initj;
    for (int j=0; j < numElements2.getValue(); j++) {
      currk = initk;
      for (int k=0; k < numElements1.getValue(); k++) {

        SbVec3f instance_pos =
          separation3.getValue() * curri +
          separation2.getValue() * currj +
          separation1.getValue() * currk;

        action->getState()->push();

        // translate bbox matrix
        SoBBoxModelMatrixElement::translateBy(action->getState(),
                                              this, instance_pos);
        SoSwitchElement::set(action->getState(),N++);

        inherited::getBoundingBox(action);

        // If center point is set, accumulate center.
        if (action->isCenterSet()) {
          acccenter += action->getCenter();
          numCenters++;
          action->resetCenter();
        }
        // pop back to the original bboxmatrix
        action->getState()->pop();
        currk += inck;
      }
      currj += incj;
    }
    curri += inci;
  }

  if (numCenters != 0)
    action->setCenter(acccenter / float(numCenters), FALSE);
#endif // end of new code by pederb
}

// Doc in superclass.
void
SoArray::GLRender(SoGLRenderAction * action)
{
  SoArray::doAction(action);
}

// Doc in superclass.
SbBool
SoArray::affectsState(void) const
{
  return FALSE; // state is pushed/popped for each traversal
}

// Doc in superclass.
void
SoArray::doAction(SoAction *action)
{
  int N = 0;
  for (int i=0; i < numElements3.getValue(); i++) {
    for (int j=0; j < numElements2.getValue(); j++) {
      for (int k=0; k < numElements1.getValue(); k++) {

        float multfactor_i = float(i);
        float multfactor_j = float(j);
        float multfactor_k = float(k);

        switch (origin.getValue()) {
        case SoArray::FIRST:
          break;
        case SoArray::CENTER:
          multfactor_i = -float(numElements3.getValue()-1.0f)/2.0f + float(i);
          multfactor_j = -float(numElements2.getValue()-1.0f)/2.0f + float(j);
          multfactor_k = -float(numElements1.getValue()-1.0f)/2.0f + float(k);
          break;
        case SoArray::LAST:
          multfactor_i = -multfactor_i;
          multfactor_j = -multfactor_j;
          multfactor_k = -multfactor_k;
          break;

        default: assert(0); break;
        }

        SbVec3f instance_pos =
          separation3.getValue() * multfactor_i +
          separation2.getValue() * multfactor_j +
          separation1.getValue() * multfactor_k;

        action->getState()->push();

        SoSwitchElement::set(action->getState(),
                             N++);

        SoModelMatrixElement::translateBy(action->getState(), this,
                                          instance_pos);

        inherited::doAction(action);
        action->getState()->pop();
      }
    }
  }
}

// Doc in superclass.
void
SoArray::callback(SoCallbackAction *action)
{
  SoArray::doAction((SoAction*)action);
}

// Doc in superclass.
void
SoArray::pick(SoPickAction *action)
{
  // We came across what we think is a bug in TGS/SGI OIV when
  // implementing picking for this node and testing against the
  // original Inventor library. The SoPickedPoint class can return the
  // object space point, normal and texture coordinates. TGS/SGI OIV
  // do not consider the translation inside this node before returning
  // the object space data from SoPickedPoint, since the path in
  // SoPickedPoint does not say anything about on which copy the pick
  // occured.
  //
  // We solved this simply by extending SoPickedPoint for storing both
  // world space and object space data.

  SoArray::doAction((SoAction*)action);
}

// Doc in superclass.
void
SoArray::handleEvent(SoHandleEventAction *action)
{
  SoNode::handleEvent(action);
  inherited::handleEvent(action);
}

// Doc in superclass
void
SoArray::audioRender(SoAudioRenderAction * action)
{
  SoArray::doAction((SoAction*)action);
}

// Doc in superclass.
void
SoArray::getMatrix(SoGetMatrixAction *action)
{
  // path does not specify which copy to traverse
  inherited::getMatrix(action);
}

// Doc in superclass.
void
SoArray::search(SoSearchAction * action)
{
  SoState * state = action->getState();
  state->push();
  // set Switch element so that subgraphs depending on this element
  // will traverse all children (it's set during normal traversal in
  // doAction()).
  SoSwitchElement::set(action->getState(), SO_SWITCH_ALL);
  // just use SoGroup::search() to traverse all children.
  inherited::search(action);
  state->pop();  
}

// Doc in superclass.
void
SoArray::getPrimitiveCount(SoGetPrimitiveCountAction *action)
{
  SoArray::doAction((SoAction*)action);
}