Debian Codegraph @

apps/nodes/GeneralizedCylinder/GeneralizedCylinder.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 | Classes:
39 | GeneralizedCylinder
40 |
41 | Author(s) : Paul Isaacs
42 |
43 */
44
45 #include <cstdio>
46 #include <Inventor/SbLinear.h>
47 #include <Inventor/SoDB.h>
48 #include <Inventor/nodekits/SoAppearanceKit.h>
49 #include <Inventor/nodes/SoGroup.h>
50 #include <Inventor/nodes/SoSeparator.h>
51 #include <Inventor/nodes/SoShapeHints.h>
52 #include <Inventor/nodes/SoCoordinate3.h>
53 #include <Inventor/nodes/SoFaceSet.h>
54 #include <Inventor/nodes/SoIndexedFaceSet.h>
55 #include <Inventor/nodes/SoMaterial.h>
56 #include <Inventor/nodes/SoQuadMesh.h>
57 #include <Inventor/nodes/SoSwitch.h>
58 #include <Inventor/nodes/SoTextureCoordinateBinding.h>
59 #include <Inventor/nodes/SoTextureCoordinate2.h>
60 #include <Inventor/nodes/SoTexture2Transform.h>
61 #include <Inventor/nodes/SoTexture2.h>
62 #include <Inventor/nodes/SoTransform.h>
63 #include <Inventor/nodes/SoTriangleStripSet.h>
64
65 #include <Inventor/manips/SoTransformManip.h>
66 #include <Inventor/draggers/SoDragger.h>
67 #include <Inventor/nodes/SoSurroundScale.h>
68
69 #include <Inventor/actions/SoWriteAction.h>
70
71 #include <Inventor/sensors/SoNodeSensor.h>
72
73 #include "GeneralizedCylinder.h"
74 #include "Triangulator.h"
75 #include "NurbMaker.h"
76 #include <Inventor/nodes/SoVertexShape.h>
77
78
79 SO_KIT_SOURCE(GeneralizedCylinder);
80
81
82 ////////////////////////////////////////////////////////////////////////
83 //
84 // Description:
85 // This initializes the GeneralizedCylinder class.
86 //
87 // Use: internal
88
89 void
90 GeneralizedCylinder::initClass()
91 //
92 ////////////////////////////////////////////////////////////////////////
93 {
94 SO__KIT_INIT_CLASS(GeneralizedCylinder, "GeneralizedCylinder",
95 SoSeparatorKit);
96 }
97
98 ////////////////////////////////////////////////////////////////////////
99 //
100 // Description:
101 // Constructor
102 //
103 // Use: public
104
105 GeneralizedCylinder::GeneralizedCylinder()
106 //
107 ////////////////////////////////////////////////////////////////////////
108 {
109 SO_KIT_CONSTRUCTOR(GeneralizedCylinder);
110
111 SO_KIT_ADD_FIELD(renderShapeType, (FACE_SET));
112 SO_KIT_ADD_FIELD(normsFlipped, (FALSE));
113 SO_KIT_ADD_FIELD(profileClosed, (FALSE));
114 SO_KIT_ADD_FIELD(crossSectionClosed, (FALSE));
115 SO_KIT_ADD_FIELD(spineClosed, (FALSE));
116 SO_KIT_ADD_FIELD(twistClosed, (FALSE));
117 SO_KIT_ADD_FIELD(minNumRows, (1));
118 SO_KIT_ADD_FIELD(minNumCols, (1));
119 SO_KIT_ADD_FIELD(withSides, (TRUE));
120 SO_KIT_ADD_FIELD(withTopCap, (TRUE));
121 SO_KIT_ADD_FIELD(withBottomCap, (TRUE));
122 SO_KIT_ADD_FIELD(withTextureCoords, (FALSE));
123
124 SO_KIT_DEFINE_ENUM_VALUE(RenderShapeType, FACE_SET);
125 SO_KIT_DEFINE_ENUM_VALUE(RenderShapeType, TRIANGLE_STRIP_SET);
126 SO_KIT_DEFINE_ENUM_VALUE(RenderShapeType, QUAD_MESH);
127 SO_KIT_DEFINE_ENUM_VALUE(RenderShapeType, BEZIER_SURFACE);
128 SO_KIT_DEFINE_ENUM_VALUE(RenderShapeType, CUBIC_SPLINE_SURFACE);
129 SO_KIT_DEFINE_ENUM_VALUE(RenderShapeType, CUBIC_TO_EDGE_SURFACE);
130 SO_KIT_SET_SF_ENUM_TYPE(renderShapeType, RenderShapeType);
131
132 // The input half of the scene graph
133 SO_KIT_ADD_CATALOG_ENTRY(inputSwitch,SoSwitch,TRUE,topSeparator,childList,FALSE );
134 SO_KIT_ADD_CATALOG_ENTRY(profileCoords,SoCoordinate3,TRUE,inputSwitch,,TRUE );
135 SO_KIT_ADD_CATALOG_ENTRY(crossSectionCoords,SoCoordinate3,TRUE,inputSwitch,,TRUE );
136 SO_KIT_ADD_CATALOG_ENTRY(spineCoords,SoCoordinate3,TRUE,inputSwitch,,TRUE );
137 SO_KIT_ADD_CATALOG_ENTRY(twistCoords,SoCoordinate3,TRUE,inputSwitch,,TRUE );
138
139 // The output half of the scene graph
140 // ( the shape produced during 'updateSurface' based on the contents
141 // of the input half of the scene graph
142 SO_KIT_ADD_CATALOG_ENTRY(outputGrp,SoGroup,TRUE,topSeparator,childList,FALSE );
143 SO_KIT_ADD_CATALOG_ENTRY(shapeHints,SoShapeHints,TRUE,outputGrp,,TRUE );
144 SO_KIT_ADD_CATALOG_ENTRY(textureBinding,SoTextureCoordinateBinding,TRUE,outputGrp,,TRUE );
145 SO_KIT_ADD_CATALOG_ENTRY(textureCoords,SoTextureCoordinate2,TRUE,outputGrp,,TRUE );
146 SO_KIT_ADD_CATALOG_ENTRY(texture2,SoTexture2,TRUE,outputGrp,,TRUE );
147 SO_KIT_ADD_CATALOG_ENTRY(coords,SoCoordinate3,TRUE,outputGrp,,TRUE);
148 SO_KIT_ADD_CATALOG_ENTRY(faceSet,SoFaceSet,TRUE,outputGrp,,TRUE);
149 SO_KIT_ADD_CATALOG_ENTRY(quadMesh,SoQuadMesh,TRUE,outputGrp,,TRUE);
150 SO_KIT_ADD_CATALOG_ENTRY(nurbsSurfaceGroup,SoGroup,TRUE,outputGrp,,TRUE);
151 SO_KIT_ADD_CATALOG_ENTRY(triangleStripSet,SoTriangleStripSet,TRUE,outputGrp,,TRUE);
152 SO_KIT_ADD_CATALOG_ENTRY(capTextureBinding,SoTextureCoordinateBinding,TRUE,outputGrp,,TRUE );
153 SO_KIT_ADD_CATALOG_ENTRY(capTextureCoords,SoTextureCoordinate2,TRUE,outputGrp,,TRUE );
154 SO_KIT_ADD_CATALOG_ENTRY(topCapCoords,SoCoordinate3,TRUE,outputGrp,,TRUE );
155 SO_KIT_ADD_CATALOG_ENTRY(topCapFaces,SoIndexedFaceSet,TRUE,outputGrp,,TRUE );
156 SO_KIT_ADD_CATALOG_ENTRY(bottomCapCoords,SoCoordinate3,TRUE,outputGrp,,TRUE );
157 SO_KIT_ADD_CATALOG_ENTRY(bottomCapFaces,SoIndexedFaceSet,TRUE,outputGrp,,TRUE );
158
159 SO_KIT_INIT_INSTANCE();
160
161
162 //////////////////////////////////////////
163 // Set up the input half of the subgraph
164 //////////////////////////////////////////
165 SoSwitch *crvSwitch = new SoSwitch;
166 crvSwitch->whichChild = SO_SWITCH_NONE;
167 setAnyPart( "inputSwitch",crvSwitch,TRUE );
168
169 setPart( "profileCoords",new SoCoordinate3 );
170 setPart( "crossSectionCoords",new SoCoordinate3 );
171 setPart( "spineCoords", new SoCoordinate3 );
172 setPart( "twistCoords", new SoCoordinate3 );
173
174 // Set the default values for these curves...
175 set("profileCoords { point [ 1 -1 0, 1 1 0 ] }" );
176 set("crossSectionCoords { point [ 1 0 1, 1 0 -1, -1 0 -1, -1 0 1 ] }" );
177 set("spineCoords { point [ 0 -1 0, 0 1 0 ] }" );
178 set("twistCoords { point [ 0 -1 0, 0 1 0 ] }" );
179
180
181
182 //////////////////////////////////////////
183 // Set up the flags and parameters
184 //////////////////////////////////////////
185
186 fullProfile = new SoCoordinate3;
187 fullCrossSection = new SoCoordinate3;
188 fullProfile->ref();
189 fullCrossSection->ref();
190
191 profileAlreadyClosed = FALSE;
192 crossSectionAlreadyClosed = FALSE;
193 spineAlreadyClosed = FALSE;
194 twistAlreadyClosed = FALSE;
195 alreadyWithTextureCoords = FALSE;
196
197 //////////////////////////////////////////
198 // Set up the output half of the subgraph
199 // that needs to be ready ahead of time.
200 //////////////////////////////////////////
201
202 //
203 // This combination of shape hints will turn on two-sided lighting
204 // for us (if it is available)
205 //
206 SoShapeHints *hints = new SoShapeHints;
207 hints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
208 hints->shapeType = SoShapeHints::UNKNOWN_SHAPE_TYPE;
209 hints->faceType = SoShapeHints::CONVEX;
210
211 hints->creaseAngle = M_PI/6.0;
212 setPart( "shapeHints", hints );
213
214 topCapScratchFace = new SoIndexedFaceSet;
215 topCapScratchFace->ref();
216
217 bottomCapScratchFace = new SoIndexedFaceSet;
218 bottomCapScratchFace->ref();
219
220 numAllocedSpineKeyRots = 0;
221 spineKeyPointRotations = NULL;
222 spineParamDistances = NULL;
223
224 // Initialize our nurbMaker
225 myNurbMaker = NULL;
226
227 selfSensor = new SoNodeSensor( GeneralizedCylinder::fieldsChangedCB, this);
228 selfSensor->setPriority(0);
229
230 inputSensor = new SoNodeSensor( GeneralizedCylinder::inputChangedCB, this);
231 inputSensor->setPriority(0);
232
233 setUpConnections(TRUE,TRUE);
234 }
235
236 // detach/attach any sensors, callbacks, and/or field connections.
237 // Called by: start/end of SoBaseKit::readInstance
238 // and on new copy by: start/end of SoBaseKit::copy.
239 // Classes that redefine must call setUpConnections(TRUE,TRUE)
240 // at end of constructor.
241 // Returns the state of the node when this was called.
242 SbBool
243 GeneralizedCylinder::setUpConnections( SbBool onOff, SbBool doItAlways )
244 {
245 if ( !doItAlways && connectionsSetUp == onOff)
246 return onOff;
247
248 if ( onOff ) {
249 // We connect AFTER base class.
250 SoSeparatorKit::setUpConnections( onOff, doItAlways );
251
252 // Turn sensors on...
253 SoNode *iSw = inputSwitch.getValue();
254 if (iSw && inputSensor->getAttachedNode() != iSw)
255 inputSensor->attach(iSw);
256 if (selfSensor->getAttachedNode() != this)
257 selfSensor->attach(this);
258 updateSurface();
259 }
260 else {
261
262 // We disconnect BEFORE base class.
263 if (selfSensor->getAttachedNode())
264 selfSensor->detach();
265 if (inputSensor->getAttachedNode())
266 inputSensor->detach();
267
268 SoSeparatorKit::setUpConnections( onOff, doItAlways );
269 }
270 return !(connectionsSetUp = onOff);
271 }
272
273 void
274 GeneralizedCylinder::setDefaultOnNonWritingFields()
275 {
276 // Before writing, call setDefault(TRUE) on all parts
277 // under the output group.
278 outputGrp.setDefault(TRUE);
279 shapeHints.setDefault(TRUE);
280 textureBinding.setDefault(TRUE);
281 textureCoords.setDefault(TRUE);
282 texture2Transform.setDefault(TRUE);
283 coords.setDefault(TRUE);
284 faceSet.setDefault(TRUE);
285 quadMesh.setDefault(TRUE);
286 nurbsSurfaceGroup.setDefault(TRUE);
287 triangleStripSet.setDefault(TRUE);
288 capTextureBinding.setDefault(TRUE);
289 capTextureCoords.setDefault(TRUE);
290 topCapCoords.setDefault(TRUE);
291 topCapFaces.setDefault(TRUE);
292 bottomCapCoords.setDefault(TRUE);
293 bottomCapFaces.setDefault(TRUE);
294
295 // Call base class...
296 SoSeparatorKit::setDefaultOnNonWritingFields();
297 }
298
299 //
300 // Destructor. Must unref everything reffed in the constructor.
301 //
302 GeneralizedCylinder::~GeneralizedCylinder()
303 {
304 if ( topCapScratchFace != NULL) {
305 topCapScratchFace->unref();
306 topCapScratchFace = NULL;
307 }
308 if ( bottomCapScratchFace != NULL) {
309 bottomCapScratchFace->unref();
310 bottomCapScratchFace = NULL;
311 }
312
313 if ( fullProfile != NULL ) {
314 fullProfile->unref();
315 fullProfile = NULL;
316 }
317 if ( fullCrossSection != NULL ) {
318 fullCrossSection->unref();
319 fullProfile = NULL;
320 }
321
322 if ( selfSensor != NULL ) {
323 delete selfSensor;
324 selfSensor = NULL;
325 }
326
327 if ( inputSensor != NULL ) {
328 delete inputSensor;
329 inputSensor = NULL;
330 }
331 if (myNurbMaker != NULL)
332 delete myNurbMaker;
333 }
334
335
336 void
337 GeneralizedCylinder::loadRow( int rowNum, SbVec3f *newCoords )
338 {
339 float scale, paramDist;
340 SbVec3f center;
341 SbRotation rot;
342 SbRotation twistRot;
343
344 scale = fullProfile->point[rowNum][0];
345 paramDist = (fullProfile->point[rowNum][1] - profileMinY)
346 * profileOverHeight;
347
348 getSpineInfo( paramDist, center, rot );
349
350 getTwistInfo( paramDist, twistRot );
351
352 SbMatrix changer, temp;
353 changer.setRotate( twistRot );
354 temp.setScale( scale );
355 changer.multRight( temp );
356
357 temp.setRotate( rot );
358
359 changer.multRight( temp );
360 temp.setTranslate( center );
361 changer.multRight( temp );
362
363 int numCrossPts = fullCrossSection->point.getNum();
364 for ( int i = 0; i < numCrossPts; i++ )
365 changer.multVecMatrix( fullCrossSection->point[i], newCoords[i] );
366 }
367
368 void
369 GeneralizedCylinder::loadTextureRow( int rowNum, SbVec2f *newCoords )
370 {
371 float uVal, vVal;
372 SoMFVec3f *cSC = &fullCrossSection->point;
373
374 if ( rowNum == -1 ) {
375 // special case. This is for the caps of the object.
376 for ( int i = 0; i < cSC->getNum(); i++ ) {
377 uVal = ( (*cSC)[i][0] - crossSectionMinX ) / crossSectionWidth;
378 vVal = 1 - (( (*cSC)[i][2] - crossSectionMinZ) / crossSectionDepth);
379 newCoords[i].setValue( uVal, vVal );
380 }
381 return;
382 }
383
384 // vVal is distance parametrically along the spine...
385 vVal = (fullProfile->point[rowNum][1] - profileMinY)
386 * profileOverHeight;
387
388 SbVec3f diff;
389 int numCrossPts = cSC->getNum();
390 float thisLength = 0.0;
391 for ( int i = 0; i < numCrossPts; i++ ) {
392 if ( i > 0 ) {
393 diff = (*cSC)[i] - (*cSC)[i-1];
394 thisLength += diff.length();
395 }
396
397 uVal = thisLength / crossSectionLength;
398
399 newCoords[i].setValue( uVal, vVal );
400 }
401 }
402
403 void
404 GeneralizedCylinder::getSpineInfo( float paramDist, SbVec3f &center,
405 SbRotation &rot )
406 {
407 if ( spineLength == 0.0 ) {
408 center = SbVec3f( 0,paramDist * profileHeight,0);
409 rot = SbRotation::identity();
410 return;
411 }
412
413 float desiredDist = paramDist * spineLength;
414
415 float lengthSoFar = 0.0;
416 float nextLength = 0.0;
417 SbVec3f p1, p2, diff, norm1, norm2, norm;
418 SbVec3f zAxis( 0, 0, 1 );
419
420 SoCoordinate3 *spinC = SO_GET_PART( this, "spineCoords", SoCoordinate3 );
421
422 if ( paramDist > 1.0 ) {
423 int last = spinC->point.getNum() - 1;
424 center = spinC->point[ last ];
425 rot = getSpinePointRotation( last, last, 0.0 );
426 return;
427 }
428 else if ( paramDist < 0.0 ) {
429 center = spinC->point[ 0 ];
430 rot = getSpinePointRotation( 0, 0, 0.0 );
431 return;
432 }
433
434 for ( int i = 0; i < spinC->point.getNum() - 1; i++ ) {
435
436 p1 = spinC->point[i];
437 p2 = spinC->point[i+1];
438 diff = p2 - p1;
439 nextLength = lengthSoFar + diff.length();
440
441 if ( desiredDist >= lengthSoFar && desiredDist <= nextLength ) {
442 float fraction = (desiredDist - lengthSoFar)
443 / (nextLength - lengthSoFar);
444 center = p1 + fraction * ( p2 - p1 );
445 rot = getSpinePointRotation( i, i+1, fraction );
446 return;
447 }
448
449 lengthSoFar = nextLength;
450 }
451 }
452
453 SbRotation
454 GeneralizedCylinder::getSpinePointRotation(int ind0, int ind1, float paramDist )
455 {
456 SbRotation rot0, rot1, interpedRot;
457
458 if ( ind0 == ind1 )
459 return( spineKeyPointRotations[ind0]);
460 else {
461 rot0 = spineKeyPointRotations[ind0];
462 rot1 = spineKeyPointRotations[ind1];
463
464 interpedRot = SbRotation::slerp( rot0, rot1, paramDist );
465
466 return interpedRot;
467 }
468 }
469
470 void
471 GeneralizedCylinder::initSpineKeyPointRotations()
472 {
473 SoCoordinate3 *spinC = SO_GET_PART( this, "spineCoords", SoCoordinate3 );
474
475 if ( spinC->point.getNum() > numAllocedSpineKeyRots ) {
476 if ( spineKeyPointRotations != NULL )
477 delete [] spineKeyPointRotations;
478 spineKeyPointRotations = new SbRotation[spinC->point.getNum()];
479 numAllocedSpineKeyRots = spinC->point.getNum();
480 }
481
482 if (spinC->point.getNum() <= 0)
483 return;
484
485 if (spinC->point.getNum() == 1) {
486 spineKeyPointRotations[0] = SbRotation::identity();
487 return;
488 }
489
490 SbVec3f xAxis( 1, 0, 0 );
491 SbVec3f yAxis( 0, 1, 0 );
492 SbVec3f zAxis( 0, 0, 1 );
493 SbVec3f zeroVec( 0, 0, 0 );
494 SbVec3f diff1, diff2;
495
496 if (spinC->point.getNum() == 2) {
497 diff1 = spinC->point[1] - spinC->point[0];
498 diff1.normalize();
499 spineKeyPointRotations[0].setValue( yAxis, diff1 );
500 spineKeyPointRotations[1] = spineKeyPointRotations[0];
501 return;
502 }
503
504 // More than two points...
505 SbVec3f rotatedX, rotatedY, rotatedZ, prevRotatedZ;
506 SbVec3f p0, p1, p2;
507 int lastInd = spinC->point.getNum() - 1;
508
509 // Find rotation for First point...
510
511 // First, treat case where spine is a closed polygon
512 if ( spineClosed.getValue() ) {
513 // Z axis is perp to plane formed by three points around p[0]
514 diff1 = spinC->point[lastInd-1] - spinC->point[0];
515 diff2 = spinC->point[1] - spinC->point[0];
516 rotatedZ = diff2.cross( diff1 );
517 // If 3 pts colinear, don't do this, but go on to other algorithm...
518 if ( rotatedZ != zeroVec ) {
519 rotatedZ.normalize();
520 // But make it lean towards the Z axis of the previous point.
521 if ( rotatedZ.dot( zAxis ) < 0 )
522 rotatedZ *= -1.0;
523
524 // X axis is cross product of (nextPt-prevPt) with Z axis.
525 diff1 = spinC->point[1] - spinC->point[lastInd-1];
526 rotatedX = diff1.cross( rotatedZ );
527 rotatedX.normalize();
528
529 rotatedY = rotatedZ.cross( rotatedX );
530 }
531 }
532
533 // Spine not closed, or it was closed but couldn't get rotation...
534 if ( rotatedZ == zeroVec || !spineClosed.getValue() ) {
535 // Y axis just points from point 0 to first point that differs.
536 int i = 0;
537 p0 = p1 = spinC->point[i++];
538 while (p0 == p1 && i <= lastInd )
539 p1 = spinC->point[i++];
540 if ( p0 == p1 ) {
541 // all points were the same. Make everyone have no rotation.
542 for ( int j = 0; j <= lastInd; j++ ) {
543 spineKeyPointRotations[j] = SbRotation::identity();
544 }
545 return;
546 }
547 rotatedY = p1 - p0;
548 rotatedY.normalize();
549
550 // Z axis is perpendicular to plane formed by 1st three points.
551 // Repeat as long as the two vectors are colinear.
552 diff1 = p0 - p1;
553 diff1.normalize();
554 rotatedZ = zeroVec;
555 while (rotatedZ == zeroVec && i <= lastInd ) {
556 p2 = spinC->point[i++];
557 diff2 = p2 - p1;
558 diff2.normalize();
559 rotatedZ = diff2.cross( diff1 );
560 }
561 if ( rotatedZ == zeroVec ) {
562 // all points coplanar. Make everyone have the same rotation.
563 for ( int j = 0; j <= lastInd; j++ ) {
564 spineKeyPointRotations[j].setValue( yAxis, rotatedY );
565 }
566 return;
567 }
568 rotatedZ.normalize();
569 // But make it lean towards positive Z each time.
570 if ( rotatedZ.dot( zAxis ) < 0 )
571 rotatedZ *= -1.0;
572
573 // X axis is calculated based on Y and Z
574 rotatedX = rotatedY.cross( rotatedZ );
575 }
576
577 SbMatrix firstAnswerMat(rotatedX[0],rotatedX[1],rotatedX[2],0,
578 rotatedY[0],rotatedY[1],rotatedY[2],0,
579 rotatedZ[0],rotatedZ[1],rotatedZ[2],0,
580 0 , 0 , 0 ,1);
581 spineKeyPointRotations[0].setValue(firstAnswerMat);
582
583
584 prevRotatedZ = rotatedZ;
585
586 // Find rotation for all but first and last points...
587 for ( int ind = 1; ind < lastInd; ind++ ) {
588 // Z axis is perpendicular to plane formed by three points surrounding
589 // This one.
590 diff1 = spinC->point[ind-1] - spinC->point[ind];
591 diff2 = spinC->point[ind+1] - spinC->point[ind];
592 diff1.normalize();
593 diff2.normalize();
594 rotatedZ = diff2.cross( diff1 );
595 if (rotatedZ == zeroVec) // if colinear, use previous rotatedZ
596 rotatedZ = prevRotatedZ;
597 rotatedZ.normalize();
598 // But make it lean towards the Z axis of the previous point.
599 if ( rotatedZ.dot( prevRotatedZ ) < 0.0 )
600 rotatedZ *= -1.0;
601
602 // X axis is cross product of (nextPt-prevPt) with Z axis.
603 diff1 = spinC->point[ind+1] - spinC->point[ind-1];
604 if ( diff1 != zeroVec ) {
605 diff1.normalize();
606 rotatedX = diff1.cross( rotatedZ );
607 rotatedX.normalize();
608 }
609 // if diff1 = 0, just use previous value for rotatedX
610
611 // Y axis is calculated based on X and Z
612 rotatedY = rotatedZ.cross( rotatedX );
613
614 SbMatrix answerMat(rotatedX[0],rotatedX[1],rotatedX[2],0,
615 rotatedY[0],rotatedY[1],rotatedY[2],0,
616 rotatedZ[0],rotatedZ[1],rotatedZ[2],0,
617 0 , 0 , 0 ,1);
618 spineKeyPointRotations[ind].setValue(answerMat);
619
620 prevRotatedZ = rotatedZ;
621 }
622
623 // Find rotation for Last point...
624 if ( spineClosed.getValue() ) {
625 spineKeyPointRotations[lastInd] = spineKeyPointRotations[0];
626 }
627 else {
628 // Y axis just points from next-to-last point to last point
629 diff1 = spinC->point[lastInd] - spinC->point[lastInd - 1];
630 if (diff1 != zeroVec )
631 rotatedY = diff1;
632 // otherwise use previous value...
633 rotatedY.normalize();
634
635 // Z axis is same as previous value.
636 rotatedZ = prevRotatedZ;
637
638 // X axis is calculated based on Y and Z
639 rotatedX = rotatedY.cross( rotatedZ );
640
641 SbMatrix lastAnswerMat(rotatedX[0],rotatedX[1],rotatedX[2],0,
642 rotatedY[0],rotatedY[1],rotatedY[2],0,
643 rotatedZ[0],rotatedZ[1],rotatedZ[2],0,
644 0 , 0 , 0 ,1);
645 spineKeyPointRotations[lastInd].setValue(lastAnswerMat);
646 }
647 }
648
649 void
650 GeneralizedCylinder::getTwistInfo( float paramDist, SbRotation &twistRot )
651 {
652 // X coordinate determines amount of twist
653
654 float desiredHeight;
655
656 desiredHeight = paramDist * (twistMaxY - twistMinY) + twistMinY;
657
658 if (desiredHeight < twistMinY)
659 desiredHeight = twistMinY;
660 else if (desiredHeight > twistMaxY)
661 desiredHeight = twistMaxY;
662
663 float thisHeight, nextHeight, thisAngle, nextAngle;
664 float angle, fraction;
665 SbVec3f yAxis( 0, 1, 0 );
666
667 SoCoordinate3 *twisC = SO_GET_PART( this, "twistCoords", SoCoordinate3 );
668
669 for ( int i = 0; i < twisC->point.getNum() - 1; i++ ) {
670
671 thisHeight = twisC->point[i][1];
672 nextHeight = twisC->point[i+1][1];
673
674 if ( desiredHeight >= thisHeight && desiredHeight <= nextHeight ) {
675 fraction = (desiredHeight - thisHeight) / (nextHeight - thisHeight);
676 thisAngle = twisC->point[i][0];
677 nextAngle = twisC->point[i+1][0];
678 angle = thisAngle + fraction * ( nextAngle - thisAngle );
679
680 twistRot = SbRotation( yAxis, angle );
681 return;
682 }
683 if ( desiredHeight >= nextHeight && desiredHeight <= thisHeight ) {
684 fraction = (desiredHeight - nextHeight) / (thisHeight - nextHeight);
685 thisAngle = twisC->point[i][0];
686 nextAngle = twisC->point[i+1][0];
687 angle = nextAngle + fraction * ( thisAngle - nextAngle );
688
689 twistRot = SbRotation( yAxis, angle );
690 return;
691 }
692 }
693
694 twistRot = SbRotation::identity();
695 return;
696 }
697
698 void
699 GeneralizedCylinder::initUpdateInfo()
700 {
701 profileMinY = 100000000;
702 float maxY = -100000000;
703
704 float val;
705
706 SoCoordinate3 *profC = SO_GET_PART( this, "profileCoords", SoCoordinate3 );
707 SoCoordinate3 *crosC = SO_GET_PART( this, "crossSectionCoords", SoCoordinate3 );
708 SoCoordinate3 *spinC = SO_GET_PART( this, "spineCoords", SoCoordinate3 );
709 SoCoordinate3 *twisC = SO_GET_PART( this, "twistCoords", SoCoordinate3 );
710
711 int i;
712 for ( i = 0; i < profC->point.getNum(); i++ ) {
713 val = profC->point[i][1];
714 if ( profileMinY > val )
715 profileMinY = val;
716 if ( maxY < val )
717 maxY = val;
718 }
719
720 if ( maxY == profileMinY )
721 maxY += 1.0;
722 profileHeight = maxY - profileMinY;
723 profileOverHeight = 1.0 / profileHeight;
724
725 //Spine length, and parametric spine distance of each point on spine.
726 if ( spineParamDistances != NULL )
727 delete [] spineParamDistances;
728 spineParamDistances = new float[spinC->point.getNum()];
729 spineLength = 0.0;
730 spineParamDistances[0] = 0;
731 SbVec3f diff;
732 for ( i = 0; i < spinC->point.getNum() - 1; i++ ) {
733 diff = spinC->point[i+1] - spinC->point[i];
734 spineLength += diff.length();
735 spineParamDistances[i+1] = spineLength; // absolute distance
736 }
737 if ( spineLength != 0.0 ) {
738 // convert absolute distance into parametric distance.
739 for ( i = 0; i < spinC->point.getNum(); i++ )
740 spineParamDistances[i] = spineParamDistances[i] / spineLength;
741 }
742
743 // Rotations at key points of the spine...
744 initSpineKeyPointRotations();
745
746 //Profile length
747 profileLength = 0.0;
748 for ( i = 0; i < profC->point.getNum() - 1; i++ ) {
749 diff = profC->point[i+1] - profC->point[i];
750 profileLength += diff.length();
751 }
752
753 //CrossSection length
754 crossSectionLength = 0.0;
755 for ( i = 0; i < crosC->point.getNum() - 1; i++ ) {
756 diff = crosC->point[i+1] - crosC->point[i];
757 crossSectionLength += diff.length();
758 }
759
760 float xVal, zVal;
761 float maxX = -100000000;
762 float maxZ = -100000000;
763 crossSectionMinX = crossSectionMinZ = 100000000;
764
765 for ( i = 0; i < crosC->point.getNum(); i++ ) {
766 xVal = crosC->point[i][0];
767 if ( crossSectionMinX > xVal )
768 crossSectionMinX = xVal;
769 if ( maxX < xVal )
770 maxX = xVal;
771
772 zVal = crosC->point[i][2];
773 if ( maxZ < zVal )
774 maxZ = zVal;
775 if ( crossSectionMinZ > zVal )
776 crossSectionMinZ = zVal;
777 }
778
779 if ( maxX == crossSectionMinX )
780 maxX += 1.0;
781 if ( maxZ == crossSectionMinZ )
782 maxZ += 1.0;
783 crossSectionWidth = maxX - crossSectionMinX;
784 crossSectionDepth = maxZ - crossSectionMinZ;
785
786
787
788 twistMinY = 100000000;
789 twistMaxY = -100000000;
790 for ( i = 0; i < twisC->point.getNum(); i++ ) {
791 val = twisC->point[i][1];
792 if ( twistMinY > val )
793 twistMinY = val;
794 if ( twistMaxY < val )
795 twistMaxY = val;
796 val = twisC->point[i][0];
797 }
798
799 calculateFullProfile();
800 calculateFullCrossSection();
801 }
802
803 //
804 // Given a set of 2D profile coordinates (the z-coordinate of this
805 // Vec3f field is ignored), this creates a generalized cylinder
806 // If passed NULL, this routine will use the last coordinates passed
807 // (this is used when the number of sides in the generalized cylinder is
808 // changed).
809 //
810
811 void
812 GeneralizedCylinder::updateSurface()
813 {
814 initUpdateInfo();
815
816 const SoMFVec3f *prof = &fullProfile->point;
817 const SoMFVec3f *cross = &fullCrossSection->point;
818
819 int numRows = prof->getNum();
820 int numCols = cross->getNum();
821
822 SoFaceSet *myFaceSet = NULL;
823 SoTriangleStripSet *myTStripSet = NULL;
824 SoQuadMesh *myQMesh = NULL;
825 SoGroup *myNurbsSurfs = NULL;
826
827 GeneralizedCylinder::RenderShapeType sType
828 = (GeneralizedCylinder::RenderShapeType)renderShapeType.getValue();
829
830 // DEAL WITH SHAPE HINTS
831 SoShapeHints *hints = (SoShapeHints *) getPart("shapeHints", FALSE);
832 if (hints) {
833 switch(sType) {
834 case BEZIER_SURFACE:
835 case CUBIC_SPLINE_SURFACE:
836 case CUBIC_TO_EDGE_SURFACE:
837 hints->vertexOrdering = SoShapeHints::UNKNOWN_ORDERING;
838 break;
839 case FACE_SET:
840 case TRIANGLE_STRIP_SET:
841 case QUAD_MESH:
842 hints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
843 break;
844 }
845 }
846
847 SoCoordinate3 *coords = NULL;
848
849 // DELETE AND GET THE CORRECT NODES.
850 if (withSides.getValue() == FALSE ) {
851 setPart( "faceSet", NULL );
852 setPart( "triangleStripSet", NULL );
853 setPart( "quadMesh", NULL );
854 setPart( "nurbsSurfaceGroup", NULL );
855 setPart( "coords", NULL );
856 }
857 else {
858 switch(sType) {
859 case FACE_SET:
860 myFaceSet = SO_GET_PART( this, "faceSet", SoFaceSet );
861 setPart( "triangleStripSet", NULL );
862 setPart( "quadMesh", NULL );
863 setPart( "nurbsSurfaceGroup", NULL );
864 break;
865 case TRIANGLE_STRIP_SET:
866 myTStripSet
867 = SO_GET_PART(this,"triangleStripSet", SoTriangleStripSet );
868 setPart( "faceSet", NULL );
869 setPart( "quadMesh", NULL );
870 setPart( "nurbsSurfaceGroup", NULL );
871 break;
872 case QUAD_MESH:
873 myQMesh = SO_GET_PART( this, "quadMesh", SoQuadMesh );
874 setPart( "faceSet", NULL );
875 setPart( "triangleStripSet", NULL );
876 setPart( "nurbsSurfaceGroup", NULL );
877 break;
878 case BEZIER_SURFACE:
879 case CUBIC_SPLINE_SURFACE:
880 case CUBIC_TO_EDGE_SURFACE:
881 myNurbsSurfs = SO_GET_PART(this, "nurbsSurfaceGroup", SoGroup );
882 setPart( "faceSet", NULL );
883 setPart( "triangleStripSet", NULL );
884 setPart( "quadMesh", NULL );
885 break;
886 }
887 coords = SO_GET_PART( this, "coords", SoCoordinate3 );
888 }
889
890
891 // FORGET IT IF TOO FEW POINTS
892 if ( numRows < 2 || numCols < 2 ) {
893 setPart("faceSet", NULL);
894 setPart("triangleStripSet", NULL );
895 setPart("quadMesh", NULL );
896 setPart("nurbsSurfaceGroup", NULL );
897
898 setPart("topCapFaces", NULL);
899 setPart("bottomCapFaces", NULL);
900
901 if (withSides.getValue())
902 coords->point.deleteValues( 0, -1 );
903 updateSurroundingManip();
904 return;
905 }
906
907 // SET UP TOPOLOGICAL INFORMATION
908 int numCoordsTotal;
909 SoMFInt32 *nV;
910 if ( withSides.getValue() ) {
911 switch (sType ) {
912 case QUAD_MESH:
913 // There will be (numRows * numCols) coordinates
914 numCoordsTotal = numRows * numCols;
915 myQMesh->verticesPerColumn = numRows;
916 myQMesh->verticesPerRow = numCols;
917 if (myQMesh->startIndex.getValue() != 0)
918 myQMesh->startIndex = 0;
919 break;
920 case TRIANGLE_STRIP_SET:
921 // Each strip (there are numRows-1 in all) has 2*numCols coordinates
922 numCoordsTotal = (numRows - 1) * (numCols * 2);
923
924 nV = &myTStripSet->numVertices;
925
926 if (nV->getNum() != (numRows-1)) {
927 nV->setNum(numRows-1);
928 for (int k = 0; k < (numRows-1); k++ )
929 nV->set1Value( k, (numCols*2) );
930 }
931 if (myTStripSet->startIndex.getValue() != 0)
932 myTStripSet->startIndex = 0;
933 break;
934 case FACE_SET:
935 // Each square is made of 2 tris, drawn with 3 coords.
936 {
937 int numTris = 2 * (numRows - 1) * (numCols - 1);
938 numCoordsTotal = numTris * 3;
939
940 nV = &myFaceSet->numVertices;
941 if (nV->getNum() != numTris) {
942 nV->setNum(numTris);
943 for (int k = 0; k < numTris; k++ )
944 nV->set1Value( k, 3 );
945 }
946 if (myFaceSet->startIndex.getValue() != 0)
947 myFaceSet->startIndex = 0;
948 }
949 break;
950 case BEZIER_SURFACE:
951 case CUBIC_SPLINE_SURFACE:
952 case CUBIC_TO_EDGE_SURFACE:
953 {
954 // There will be (numRows * numCols) coordinates
955 numCoordsTotal = numRows * numCols;
956
957 // Enlist a NurbMaker to create the surface
958 // patch nodes.
959 if (myNurbMaker == NULL)
960 myNurbMaker = new NurbMaker;
961 if (sType == BEZIER_SURFACE)
962 myNurbMaker->setPatchType( NurbMaker::BEZIER );
963 else if (sType == CUBIC_SPLINE_SURFACE)
964 myNurbMaker->setPatchType( NurbMaker::CUBIC );
965 else
966 myNurbMaker->setPatchType( NurbMaker::CUBIC_TO_EDGE );
967 myNurbMaker->setFlipNormals( normsFlipped.getValue() );
968
969 SbVec2s closure( spineClosed.getValue(),
970 crossSectionClosed.getValue());
971 myNurbMaker->setWraparound(closure);
972
973 SbVec2s numMeshDivs(numCols, numRows);
974
975 SoGroup *surfGroup;
976 surfGroup = myNurbMaker->createNurbsGroup( numMeshDivs, closure );
977 setPart("nurbsSurfaceGroup", surfGroup );
978 }
979 break;
980 }
981
982 if (coords->point.getNum() < numCoordsTotal)
983 coords->point.insertSpace(0, numCoordsTotal - coords->point.getNum());
984 else if ( coords->point.getNum() > numCoordsTotal )
985 coords->point.deleteValues(0, coords->point.getNum() - numCoordsTotal );
986 }
987
988 int topRow, botRow;
989
990 static SbVec3f *topPoints = NULL;
991 static SbVec3f *botPoints = NULL;
992 static SbVec2f *topTex = NULL;
993 static SbVec2f *botTex = NULL;
994
995 static int p_alloc = 0;
996 if ( p_alloc != numCols ) {
997 if ( topPoints != NULL ) {
998 delete [] topPoints;
999 topPoints = NULL;
1000 }
1001 if ( botPoints != NULL ) {
1002 delete [] botPoints;
1003 botPoints = NULL;
1004 }
1005 if ( topTex != NULL ) {
1006 delete [] topTex;
1007 topTex = NULL;
1008 }
1009 if ( botTex != NULL ) {
1010 delete [] botTex;
1011 botTex = NULL;
1012 }
1013 p_alloc = 0;
1014 }
1015 if ( p_alloc == 0 ) {
1016 p_alloc = numCols;
1017 topPoints = new SbVec3f[p_alloc];
1018 botPoints = new SbVec3f[p_alloc];
1019 topTex = new SbVec2f[p_alloc];
1020 botTex = new SbVec2f[p_alloc];
1021 }
1022
1023
1024 // If either cap is being drawn, and texture is on...
1025 if ( withTextureCoords.getValue()
1026 && ( withTopCap.getValue() || withBottomCap.getValue() )
1027 && crossSectionClosed.getValue() && numCols >= 4 ) {
1028 loadTextureRow( -1, topTex );
1029 SoTextureCoordinate2 *cTC
1030 = SO_GET_PART( this, "capTextureCoords", SoTextureCoordinate2 );
1031 cTC->point.setValues( 0, numCols, topTex );
1032 }
1033 else {
1034 setPart("capTextureCoords", NULL);
1035 }
1036
1037 SoCoordinate3 *topCapC = SO_GET_PART(this,"topCapCoords",SoCoordinate3);
1038 SoIndexedFaceSet *topCapF = SO_GET_PART(this,"topCapFaces", SoIndexedFaceSet);
1039 SoCoordinate3 *botCapC = SO_GET_PART(this,"bottomCapCoords",SoCoordinate3);
1040 SoIndexedFaceSet *botCapF =SO_GET_PART(this,"bottomCapFaces", SoIndexedFaceSet);
1041
1042 if ( withTopCap.getValue() && crossSectionClosed.getValue()
1043 && numCols >= 4 ) {
1044
1045
1046 SoMFVec3f *topC = &topCapC->point;
1047 SoMFInt32 *topF = &topCapF->coordIndex;
1048 SoMFInt32 *topS = &topCapScratchFace->coordIndex;
1049
1050 // Load the values of the section points into topCapC.
1051 // Leave out the last point, since it is a repeat of the first
1052 // (remember, crossSectionClosed == TRUE !)
1053 if ( numCols - 1 < topC->getNum() )
1054 topC->deleteValues( 0, topC->getNum() - (numCols - 1) );
1055 topC->setValues( 0, numCols - 1, cross->getValues(0) );
1056
1057 // The triangulator likes the y values to be 0.0. So flatten the
1058 // sucker out.
1059 SbVec3f *bunchaTopVecs = topC->startEditing();
1060 SbBool topVecsChanged = FALSE;
1061 for (int topCount = 0; topCount < topC->getNum(); topCount++ ) {
1062 SbVec3f theVec = (*topC)[topCount];
1063 if (bunchaTopVecs[topCount][1] != 0.0) {
1064 bunchaTopVecs[topCount][1] = 0.0;
1065 topVecsChanged = TRUE;
1066 }
1067 }
1068 bunchaTopVecs = NULL;
1069 if (topVecsChanged)
1070 topC->finishEditing();
1071
1072 // Load the indices to draw the section as a single polygon into
1073 // topCapScratchFace
1074 if ( (topC->getNum() + 1) < topS->getNum() )
1075 topS->deleteValues( 0, topS->getNum() - (topC->getNum() + 1) );
1076 else if ( (topC->getNum() + 1) > topS->getNum() )
1077 topS->insertSpace( 0, (topC->getNum() + 1) - topS->getNum() );
1078 int32_t *scratchVals = topS->startEditing();
1079 for ( int i = 0; i < topC->getNum(); i++ ) {
1080 scratchVals[i] = i;
1081 }
1082 scratchVals[ topC->getNum() ] = -1; // to close the polygon
1083 topS->finishEditing();
1084
1085 // Before calling loadRow (i.e., while the points still lay in the
1086 // y=0 plane), and before triangulating (i.e., while we still have
1087 // a record of a polygon which follows the entire cross section)
1088 // determine if the cross section is clockwise ordered. This will
1089 // affect the normal vector we select.
1090 SbBool isSectionClockWise;
1091 isSectionClockWise = Triangulator::clockWiseTest(
1092 (*topC), (*topS), 0, topC->getNum());
1093
1094 // Triangulate, putting result into topCapF
1095 Triangulator::triangulate( *topC, *topS, *topF );
1096
1097 // Triangulation makes everything clockwise ordered.
1098 // Assure that normals are in the right direction...
1099 if ( normsFlipped.getValue() != isSectionClockWise ) {
1100 // reverse the order of the points in topF.
1101 // Each polygon in topF should be a triangle at this point,
1102 // so reversing the order is simple.
1103 int32_t *vals = topF->startEditing();
1104 int32_t swapTemp;
1105 for ( int ind = 0; ind < topF->getNum(); ind += 4 ) {
1106 // Switch ind and ind+2 (ind+3 is a -1 to signal end of poly)
1107 swapTemp = vals[ind];
1108 vals[ind] = vals[ind+2];
1109 vals[ind+2] = swapTemp;
1110 }
1111 topF->finishEditing();
1112 }
1113
1114 // Check that, after triangulation, there are still polygons left...
1115 if ( topF->getNum() > 0 ) {
1116
1117 // Transform the values for the points in the section as they
1118 // appear in the final row
1119 topC->insertSpace( 0, 1 ); // Add room for an extra point at the
1120 // end since loadRow will put it there...
1121 SbVec3f *topCapPoints = topC->startEditing();
1122 loadRow( 0, topCapPoints );
1123 topC->finishEditing();
1124 }
1125 }
1126 else {
1127 // Clear out the coord and face set for top cap
1128 topCapC->point.deleteValues( 0, -1 );
1129 topCapF->coordIndex.deleteValues( 0, -1 );
1130 }
1131
1132 if ( withBottomCap.getValue() && crossSectionClosed.getValue()
1133 && numCols >= 4 ) {
1134
1135 SoMFVec3f *botC = &botCapC->point;
1136 SoMFInt32 *botF = &botCapF->coordIndex;
1137 SoMFInt32 *botS = &bottomCapScratchFace->coordIndex;
1138
1139 // Load the values of the section points into botCapC.
1140 // Leave out the last point, since it is a repeat of the first
1141 // (remember, crossSectionClosed == TRUE !)
1142 if ( numCols - 1 < botC->getNum() )
1143 botC->deleteValues( 0, botC->getNum() - (numCols - 1) );
1144 botC->setValues( 0, numCols - 1, cross->getValues(0) );
1145
1146 // The triangulator likes the y values to be 0.0. So flatten the
1147 // sucker out.
1148 SbVec3f *bunchaBotVecs = botC->startEditing();
1149 SbBool botVecsChanged = FALSE;
1150 for (int botCount = 0; botCount < botC->getNum(); botCount++ ) {
1151 SbVec3f theVec = (*botC)[botCount];
1152 if (bunchaBotVecs[botCount][1] != 0.0) {
1153 bunchaBotVecs[botCount][1] = 0.0;
1154 botVecsChanged = TRUE;
1155 }
1156 }
1157 bunchaBotVecs = NULL;
1158 if (botVecsChanged)
1159 botC->finishEditing();
1160
1161 // Load the indices to draw the section as a single polygon into
1162 // bottomCapScratchFace
1163 if ( (botC->getNum() + 1) < botS->getNum() )
1164 botS->deleteValues( 0, botS->getNum() - (botC->getNum() + 1) );
1165 else if ( (botC->getNum() + 1) > botS->getNum() )
1166 botS->insertSpace( 0, (botC->getNum() + 1) - botS->getNum() );
1167 int32_t *scratchVals = botS->startEditing();
1168 for ( int i = 0; i < botC->getNum(); i++ ) {
1169 scratchVals[i] = i;
1170 }
1171 scratchVals[ botC->getNum() ] = -1; // to close the polygon
1172 botS->finishEditing();
1173
1174 // Before calling loadRow (i.e., while the points still lay in the
1175 // y=0 plane), and before triangulating (i.e., while we still have
1176 // a record of a polygon which follows the entire cross section)
1177 // determine if the cross section is clockwise ordered. This will
1178 // affect the normal vector we select.
1179 SbBool isSectionClockWise;
1180 isSectionClockWise = Triangulator::clockWiseTest(
1181 (*botC), (*botS), 0, botC->getNum());
1182
1183 // Triangulate, putting result into bottomCapFaces
1184 Triangulator::triangulate( *botC, *botS, *botF );
1185
1186 // Triangulation makes everything clockwise ordered.
1187 // Assure that normals are in the right direction...
1188 if ( normsFlipped.getValue() == isSectionClockWise ) {
1189 // reverse the order of the points in botF.
1190 // Each polygon in topF should be a triangle at this point,
1191 // so reversing the order is simple.
1192 int32_t *vals = botF->startEditing();
1193 int32_t swapTemp;
1194 for ( int ind = 0; ind < botF->getNum(); ind += 4 ) {
1195 // Switch ind and ind+2 (ind+3 is a -1 to signal end of poly)
1196 swapTemp = vals[ind];
1197 vals[ind] = vals[ind+2];
1198 vals[ind+2] = swapTemp;
1199 }
1200 botF->finishEditing();
1201 }
1202
1203 // Check that, after triangulation, there are still polygons left...
1204 if ( botF->getNum() > 0 ) {
1205
1206 // Transform the values for the points in the section as they
1207 // appear in the final row
1208 botC->insertSpace( 0, 1 ); // Add room for an extra point at the
1209 // end, since loadRow will put it there.
1210 SbVec3f *bottomCapPoints = botC->startEditing();
1211 loadRow( numRows - 1, bottomCapPoints );
1212 botC->finishEditing();
1213 }
1214 }
1215
1216 else {
1217 // Clear out the coord and face set for bottom cap
1218 botCapC->point.deleteValues( 0, -1 );
1219 botCapF->coordIndex.deleteValues( 0, -1 );
1220 }
1221
1222
1223
1224 if ( withSides.getValue() ) {
1225
1226 SbVec2f *tCEdit;
1227 SoTextureCoordinate2 *tCN;
1228 if ( withTextureCoords.getValue() ) {
1229 tCN = SO_GET_PART(this, "textureCoords", SoTextureCoordinate2 );
1230 if (tCN->point.getNum() < numCoordsTotal)
1231 tCN->point.insertSpace(0, numCoordsTotal - tCN->point.getNum());
1232 else if ( tCN->point.getNum() > numCoordsTotal )
1233 tCN->point.deleteValues(0, tCN->point.getNum() - numCoordsTotal );
1234 tCEdit = tCN->point.startEditing();
1235 }
1236 else {
1237 setPart("textureCoords", NULL);
1238 }
1239
1240 SbVec3f *coordsEdit = coords->point.startEditing();
1241 int curCoord = 0;
1242 int curTexCoord = 0;
1243
1244 for (topRow = 0, botRow = 1; topRow < numRows - 1; topRow++ , botRow++ ){
1245 if ( topRow == 0 ) {
1246 // calculate topRow
1247 loadRow( topRow, topPoints );
1248 if ( withTextureCoords.getValue() )
1249 loadTextureRow( topRow, topTex );
1250 }
1251 else {
1252 // switch rows, so old botRow becomes new topRow
1253 SbVec3f *temp = topPoints;
1254 topPoints = botPoints;
1255 botPoints = temp;
1256 if ( withTextureCoords.getValue() ) {
1257 SbVec2f *temp2 = topTex;
1258 topTex = botTex;
1259 botTex = temp2;
1260 }
1261 }
1262
1263 // calculate botRow
1264 loadRow( botRow, botPoints );
1265 if ( withTextureCoords.getValue() )
1266 loadTextureRow( botRow, botTex );
1267
1268
1269 // put the coordinates for this row triangles into the node...
1270 SbBool flipped = normsFlipped.getValue();
1271
1272 switch(sType) {
1273 case QUAD_MESH:
1274 case BEZIER_SURFACE:
1275 case CUBIC_SPLINE_SURFACE:
1276 case CUBIC_TO_EDGE_SURFACE:
1277 // For these two, load only coords for the bottom row.
1278 // However load the top row also the first time through
1279 {
1280 int curCol;
1281 if (topRow == 0) {
1282 if ( flipped )
1283 for (curCol = 0; curCol < numCols; curCol++ )
1284 coordsEdit[curCoord++] = topPoints[curCol];
1285 else
1286 for (curCol = numCols-1; curCol >= 0; curCol-- )
1287 coordsEdit[curCoord++] = topPoints[curCol];
1288 }
1289
1290 // load bottom row
1291 if ( flipped )
1292 for (curCol = 0; curCol < numCols; curCol++ )
1293 coordsEdit[curCoord++] = botPoints[curCol];
1294 else
1295 for (curCol = numCols-1; curCol >= 0; curCol-- )
1296 coordsEdit[curCoord++] = botPoints[curCol];
1297 }
1298 break;
1299 case TRIANGLE_STRIP_SET:
1300 {
1301 for( int curCol = 0; curCol < numCols; curCol++ ) {
1302 if ( ! flipped ) {
1303 coordsEdit[curCoord++] = topPoints[curCol];
1304 coordsEdit[curCoord++] = botPoints[curCol];
1305 }
1306 else {
1307 coordsEdit[curCoord++] = botPoints[curCol];
1308 coordsEdit[curCoord++] = topPoints[curCol];
1309 }
1310 }
1311 }
1312 break;
1313 case FACE_SET:
1314 int leftCol, rightCol;
1315 for ( leftCol = 0, rightCol = 1; leftCol < numCols - 1;
1316 leftCol++, rightCol++ ) {
1317
1318 if ( ! flipped ) {
1319 // first draw the top left triangle
1320 coordsEdit[curCoord++] = topPoints[leftCol];
1321 coordsEdit[curCoord++] = topPoints[rightCol];
1322 coordsEdit[curCoord++] = botPoints[leftCol];
1323
1324 // then draw the bottom right triangle
1325 coordsEdit[curCoord++] = botPoints[leftCol];
1326 coordsEdit[curCoord++] = topPoints[rightCol];
1327 coordsEdit[curCoord++] = botPoints[rightCol];
1328 }
1329 else {
1330 // first draw the top left triangle
1331 coordsEdit[curCoord++] = botPoints[leftCol];
1332 coordsEdit[curCoord++] = topPoints[rightCol];
1333 coordsEdit[curCoord++] = topPoints[leftCol];
1334
1335 // then draw the bottom right triangle
1336 coordsEdit[curCoord++] = botPoints[rightCol];
1337 coordsEdit[curCoord++] = topPoints[rightCol];
1338 coordsEdit[curCoord++] = botPoints[leftCol];
1339 }
1340 }
1341 break;
1342 }
1343
1344 if ( withTextureCoords.getValue() ) {
1345 switch(sType) {
1346 case QUAD_MESH:
1347 case BEZIER_SURFACE:
1348 case CUBIC_SPLINE_SURFACE:
1349 case CUBIC_TO_EDGE_SURFACE:
1350 // For these two, load only coords for the bottom row.
1351 // However load the top row also the first time through
1352 {
1353 int curCol;
1354 if (topRow == 0) {
1355 if ( flipped )
1356 for (curCol = 0; curCol < numCols; curCol++ )
1357 tCEdit[curTexCoord++] = topTex[curCol];
1358 else
1359 for (curCol = numCols-1; curCol >= 0; curCol-- )
1360 tCEdit[curTexCoord++] = topTex[curCol];
1361 }
1362
1363 // load bottom row
1364 if ( flipped )
1365 for (curCol = 0; curCol < numCols; curCol++ )
1366 tCEdit[curTexCoord++] = botTex[curCol];
1367 else
1368 for (curCol = numCols-1; curCol >= 0; curCol-- )
1369 tCEdit[curTexCoord++] = botTex[curCol];
1370 }
1371 break;
1372 case TRIANGLE_STRIP_SET:
1373 {
1374 for (int curCol=0; curCol < numCols;curCol++){
1375 if ( ! flipped ) {
1376 tCEdit[ curTexCoord++] = topTex[curCol];
1377 tCEdit[ curTexCoord++] = botTex[curCol];
1378 }
1379 else {
1380 tCEdit[ curTexCoord++] = botTex[curCol];
1381 tCEdit[ curTexCoord++] = topTex[curCol];
1382 }
1383 }
1384 }
1385 break;
1386 case FACE_SET:
1387 int leftCol, rightCol;
1388
1389 // set the texture coordinates for these triangles...
1390 for ( leftCol = 0, rightCol = 1; leftCol < numCols - 1;
1391 leftCol++, rightCol++ ) {
1392
1393 if ( ! flipped ) {
1394 // first draw the top left triangle
1395 tCEdit[ curTexCoord++] = topTex[leftCol];
1396 tCEdit[ curTexCoord++] = topTex[rightCol];
1397 tCEdit[ curTexCoord++] = botTex[leftCol];
1398
1399 // then draw the bottom right triangle
1400 tCEdit[ curTexCoord++] = botTex[leftCol];
1401 tCEdit[ curTexCoord++] = topTex[rightCol];
1402 tCEdit[ curTexCoord++] = botTex[rightCol];
1403 }
1404 else {
1405 // first draw the top left triangle
1406 tCEdit[ curTexCoord++] = botTex[leftCol];
1407 tCEdit[ curTexCoord++] = topTex[rightCol];
1408 tCEdit[ curTexCoord++] = topTex[leftCol];
1409
1410 // then draw the bottom right triangle
1411 tCEdit[ curTexCoord++] = botTex[rightCol];
1412 tCEdit[ curTexCoord++] = topTex[rightCol];
1413 tCEdit[ curTexCoord++] = botTex[leftCol];
1414 }
1415 }
1416 break;
1417 }
1418 }
1419 }
1420 coords->point.finishEditing();
1421 if ( withTextureCoords.getValue() ) {
1422 tCN->point.finishEditing();
1423 }
1424 }
1425
1426 // If there's a manip as our transform, tell it to change size.
1427 updateSurroundingManip();
1428 }
1429
1430 SoSeparator *
1431 GeneralizedCylinder::makeVanillaVersion()
1432 {
1433 // Start with a nice update!
1434 updateSurface();
1435
1436 SoSeparator *answer = new SoSeparator;
1437 answer->ref();
1438
1439 SoNode *newKid;
1440
1441 int numCols = fullCrossSection->point.getNum();
1442
1443 // Go through all the relevant parts. If you find them, add them as
1444 // a child to the answer.
1445
1446 if ( newKid = getPart( "callbackList", FALSE ))
1447 answer->addChild( newKid );
1448 if ( newKid = getPart( "pickStyle", FALSE ))
1449 answer->addChild( newKid );
1450
1451 if ( newKid = getPart( "appearance.lightModel", FALSE ))
1452 answer->addChild( newKid );
1453 if ( newKid = getPart( "appearance.environment", FALSE ))
1454 answer->addChild( newKid );
1455 if ( newKid = getPart( "appearance.drawStyle", FALSE ))
1456 answer->addChild( newKid );
1457 if ( newKid = getPart( "appearance.material", FALSE ))
1458 answer->addChild( newKid );
1459 if ( newKid = getPart( "appearance.complexity", FALSE ))
1460 answer->addChild( newKid );
1461 if ( withTextureCoords.getValue() ) {
1462 if ( newKid = getPart( "appearance.texture2", FALSE ))
1463 answer->addChild( newKid );
1464 }
1465 if ( newKid = getPart( "appearance.font", FALSE ))
1466 answer->addChild( newKid );
1467
1468 if ( newKid = getPart( "units", FALSE ))
1469 answer->addChild( newKid );
1470 if ( newKid = getPart( "transform", FALSE ))
1471 answer->addChild( newKid );
1472 if ( withTextureCoords.getValue() ) {
1473 if ( newKid = getPart( "texture2Transform", FALSE ))
1474 answer->addChild( newKid );
1475 }
1476
1477 if ( newKid = getPart( "shapeHints", FALSE ))
1478 answer->addChild( newKid );
1479 if ( withTextureCoords.getValue() ) {
1480 if ( newKid = getPart( "textureBinding", FALSE ))
1481 answer->addChild( newKid );
1482 if ( newKid = getPart( "textureCoords", FALSE ))
1483 answer->addChild( newKid );
1484 if ( newKid = getPart( "texture2", FALSE ))
1485 answer->addChild( newKid );
1486 }
1487 if ( newKid = getPart( "coords", FALSE ))
1488 answer->addChild( newKid );
1489 if ( newKid = getPart( "faceSet", FALSE ))
1490 answer->addChild( newKid );
1491 if ( newKid = getPart( "quadMesh", FALSE ))
1492 answer->addChild( newKid );
1493 if ( newKid = getPart( "nurbsSurfaceGroup", FALSE ))
1494 answer->addChild( newKid );
1495 if ( newKid = getPart( "triangleStripSet", FALSE ))
1496 answer->addChild( newKid );
1497
1498 if ( withTextureCoords.getValue()
1499 && ( withTopCap.getValue() || withBottomCap.getValue() )
1500 && crossSectionClosed.getValue() && numCols >= 4 ) {
1501 if ( newKid = getPart( "capTextureBinding", FALSE ))
1502 answer->addChild( newKid );
1503 if ( newKid = getPart( "capTextureCoords", FALSE ))
1504 answer->addChild( newKid );
1505 }
1506
1507 if ( withTopCap.getValue()
1508 && crossSectionClosed.getValue() && numCols >= 4 ) {
1509 if ( newKid = getPart( "topCapCoords", FALSE ))
1510 answer->addChild( newKid );
1511 if ( newKid = getPart( "topCapFaces", FALSE ))
1512 answer->addChild( newKid );
1513 }
1514
1515 if ( withBottomCap.getValue()
1516 && crossSectionClosed.getValue() && numCols >= 4 ) {
1517 if ( newKid = getPart( "bottomCapCoords", FALSE ))
1518 answer->addChild( newKid );
1519 if ( newKid = getPart( "bottomCapFaces", FALSE ))
1520 answer->addChild( newKid );
1521 }
1522
1523 if ( newKid = getPart( "childList", FALSE ))
1524 answer->addChild( newKid );
1525
1526 answer->unrefNoDelete();
1527 return answer;
1528 }
1529
1530 void
1531 GeneralizedCylinder::changeCurveClosure( char *curveName, SbBool newClosed )
1532 {
1533 SoCoordinate3 *coords = SO_GET_PART( this, curveName, SoCoordinate3 );
1534
1535 SoMFVec3f *cField = &coords->point;
1536 int numC = cField->getNum();
1537 if ( newClosed == TRUE ) {
1538 if ( numC > 0 ) {
1539 // make a new final point that is a copy of the zero'th
1540 if ( numC > 1 && ((*cField)[0] == (*cField)[numC-1] ))
1541 // First make sure that the first and last points aren't already
1542 // already the same.
1543 return;
1544 else
1545 cField->set1Value( numC, (*cField)[0] );
1546 }
1547 }
1548 else if ( numC > 1 ) {
1549 // remove the last point
1550 if ( (*cField)[0] != (*cField)[numC-1] )
1551 // First make sure that the first and last points are the same.
1552 return;
1553 else
1554 cField->deleteValues( numC - 1, 1 );
1555 }
1556 }
1557
1558 void
1559 GeneralizedCylinder::changeWithTextureCoords( SbBool newWith )
1560 {
1561 if ( withTextureCoords.getValue() != newWith )
1562 withTextureCoords = newWith;
1563
1564 if ( newWith ) {
1565 set( "textureBinding { value PER_VERTEX }");
1566 set( "capTextureBinding { value PER_VERTEX_INDEXED }");
1567 set( "texture2 { filename \"defaultTexture.rgb\" }" );
1568 }
1569 else {
1570
1571 // The texture2 and texture2Transform parts may have some useful
1572 // information, so we check them before deleting.
1573 // The others can just go bye-bye
1574
1575 SoTexture2 *tex2 = (SoTexture2 *) getPart("texture2",FALSE);
1576 if ( tex2 != NULL ) {
1577 tex2->filename = "";
1578 if (tex2->hasDefaultValues())
1579 setPart("texture2", NULL );
1580 }
1581 SoTexture2Transform *txXf
1582 = (SoTexture2Transform *) getPart("texture2Transform",FALSE);
1583 if ( txXf != NULL ) {
1584 if (txXf->hasDefaultValues())
1585 setPart("texture2Transform", NULL );
1586 }
1587
1588 setPart("textureBinding", NULL );
1589 setPart("capTextureBinding", NULL );
1590
1591 setPart("textureCoords", NULL );
1592 setPart("capTextureCoords", NULL );
1593 }
1594 }
1595
1596 void
1597 GeneralizedCylinder::calculateFullProfile()
1598 {
1599 SoCoordinate3 *profC = SO_GET_PART( this, "profileCoords", SoCoordinate3 );
1600 SoCoordinate3 *spinC = SO_GET_PART( this, "spineCoords", SoCoordinate3 );
1601 SoCoordinate3 *twisC = SO_GET_PART( this, "twistCoords", SoCoordinate3 );
1602
1603 int numInProf = profC->point.getNum();
1604
1605 // Start by just copying profC into fullProfile...
1606 int i = numInProf - fullProfile->point.getNum();
1607 if ( i > 0 )
1608 fullProfile->point.insertSpace(0, i);
1609 else if ( i < 0 )
1610 fullProfile->point.deleteValues(0, -i);
1611 fullProfile->point.setValues(0,numInProf,profC->point.getValues(0));
1612
1613 // Next, add in any points in the spine curve that are not demarcated
1614 // in the profile curve.
1615 for ( i = 0; i < spinC->point.getNum(); i++ ) {
1616 // Get parametric distance of point along length of spine.
1617 float pDist = spineParamDistances[i];
1618 float pHeight = profileMinY + pDist * profileHeight;
1619
1620 // Translate this parametric distance into a point or points in
1621 // the profile curve.
1622 SbVec3f p0, p1, profPoint;
1623 float amt;
1624 for ( int j = 0; j < fullProfile->point.getNum() - 1; j++ ) {
1625 p0 = fullProfile->point[j];
1626 p1 = fullProfile->point[j+1];
1627 // If the spine point falls at a point along the profile that
1628 // lies between p0 and p1, then interpolate and add it in.
1629 if ( pHeight > p0[1] && pHeight < p1[1] ) {
1630 amt = (pHeight - p0[1]) / (p1[1] - p0[1]);
1631 profPoint = p0 + amt * ( p1 - p0);
1632 // Add the point
1633 fullProfile->point.insertSpace(j+1,1);
1634 fullProfile->point.set1Value(j+1,profPoint);
1635 j++;
1636 }
1637 else if ( pHeight < p0[1] && pHeight > p1[1] ) {
1638 amt = (pHeight - p1[1]) / (p0[1] - p1[1]);
1639 profPoint = p1 + amt * ( p0 - p1);
1640 // Add the point
1641 fullProfile->point.insertSpace(j+1,1);
1642 fullProfile->point.set1Value(j+1,profPoint);
1643 j++;
1644 }
1645 }
1646 }
1647
1648 // Next, add in any points in the twist curve that are not demarcated
1649 // in the profile curve.
1650 for ( i = 0; i < twisC->point.getNum(); i++ ) {
1651 }
1652
1653
1654 if ( numInProf < 2 )
1655 return;
1656
1657 if ( minNumRows.getValue() > numInProf ) {
1658
1659 float overDist = profileLength / minNumRows.getValue();
1660 SbVec3f p0, p1, newPt, diff;
1661 float length;
1662 for ( int i = 0; i < fullProfile->point.getNum() - 1; i++ ) {
1663 p0 = fullProfile->point[i];
1664 p1 = fullProfile->point[i+1];
1665 diff = p1 - p0;
1666 length = diff.length();
1667 diff.normalize();
1668 if ( length > overDist ) {
1669 newPt = p0 + diff * overDist;
1670 fullProfile->point.insertSpace(i+1,1);
1671 fullProfile->point.set1Value(i+1,newPt);
1672 }
1673 }
1674 }
1675 }
1676 void
1677 GeneralizedCylinder::calculateFullCrossSection()
1678 {
1679 SoCoordinate3 *crosC = SO_GET_PART( this, "crossSectionCoords", SoCoordinate3 );
1680
1681 int numInCross = crosC->point.getNum();
1682
1683 // Start by just copying crosC into fullCrossSection...
1684 int i = numInCross - fullCrossSection->point.getNum();
1685 if ( i > 0 )
1686 fullCrossSection->point.insertSpace(0, i);
1687 else if ( i < 0 )
1688 fullCrossSection->point.deleteValues(0, -i);
1689 fullCrossSection->point.setValues( 0, numInCross, crosC->point.getValues(0));
1690
1691 if ( numInCross < 2 )
1692 return;
1693
1694 if ( minNumCols.getValue() > numInCross ) {
1695
1696 float overDist = crossSectionLength / minNumCols.getValue();
1697 SbVec3f p0, p1, newPt, diff;
1698 float length;
1699 for ( int i = 0; i < fullCrossSection->point.getNum() - 1; i++ ) {
1700 p0 = fullCrossSection->point[i];
1701 p1 = fullCrossSection->point[i+1];
1702 diff = p1 - p0;
1703 length = diff.length();
1704 diff.normalize();
1705 if ( length > overDist ) {
1706 newPt = p0 + diff * overDist;
1707 fullCrossSection->point.insertSpace(i+1,1);
1708 fullCrossSection->point.set1Value(i+1,newPt);
1709 }
1710 }
1711 }
1712 }
1713
1714 void
1715 GeneralizedCylinder::fieldsChangedCB(void *data, SoSensor *sens )
1716 {
1717 if ( ((SoDataSensor *)sens)->getTriggerNode() != data )
1718 return;
1719
1720 GeneralizedCylinder *s = (GeneralizedCylinder *)data;
1721
1722 SoField *triggerField = ((SoDataSensor *)sens)->getTriggerField();
1723 SbBool flagFieldChanged = FALSE;
1724
1725 if (triggerField == &s->renderShapeType)
1726 flagFieldChanged = TRUE;
1727 else if (triggerField == &s->normsFlipped)
1728 flagFieldChanged = TRUE;
1729 else if (triggerField == &s->profileClosed)
1730 flagFieldChanged = TRUE;
1731 else if (triggerField == &s->crossSectionClosed)
1732 flagFieldChanged = TRUE;
1733 else if (triggerField == &s->spineClosed)
1734 flagFieldChanged = TRUE;
1735 else if (triggerField == &s->twistClosed)
1736 flagFieldChanged = TRUE;
1737 else if (triggerField == &s->minNumRows)
1738 flagFieldChanged = TRUE;
1739 else if (triggerField == &s->minNumCols)
1740 flagFieldChanged = TRUE;
1741 else if (triggerField == &s->withSides)
1742 flagFieldChanged = TRUE;
1743 else if (triggerField == &s->withTopCap)
1744 flagFieldChanged = TRUE;
1745 else if (triggerField == &s->withBottomCap)
1746 flagFieldChanged = TRUE;
1747 else if (triggerField == &s->withTextureCoords)
1748 flagFieldChanged = TRUE;
1749
1750 if (flagFieldChanged == FALSE )
1751 return;
1752
1753 // Special stuff to do if a curve's closurehas been changed
1754 if ( s->profileClosed.getValue() != s->profileAlreadyClosed ) {
1755 s->changeCurveClosure( "profileCoords", s->profileClosed.getValue() );
1756 s->profileAlreadyClosed = s->profileClosed.getValue();
1757 }
1758 if ( s->crossSectionClosed.getValue() != s->crossSectionAlreadyClosed ) {
1759 s->changeCurveClosure( "crossSectionCoords",
1760 s->crossSectionClosed.getValue() );
1761 s->crossSectionAlreadyClosed = s->crossSectionClosed.getValue();
1762 }
1763 if ( s->spineClosed.getValue() != s->spineAlreadyClosed ) {
1764 s->changeCurveClosure( "spineCoords", s->spineClosed.getValue() );
1765 s->spineAlreadyClosed = s->spineClosed.getValue();
1766 }
1767 if ( s->twistClosed.getValue() != s->twistAlreadyClosed ) {
1768 s->changeCurveClosure( "twistCoords", s->twistClosed.getValue() );
1769 s->twistAlreadyClosed = s->twistClosed.getValue();
1770 }
1771
1772 // Special stuff to do if a curve's texture status has been changed
1773 if ( s->withTextureCoords.getValue() != s->alreadyWithTextureCoords) {
1774 s->changeWithTextureCoords( s->withTextureCoords.getValue() );
1775 s->alreadyWithTextureCoords = s->withTextureCoords.getValue();
1776 }
1777
1778 s->updateSurface();
1779 }
1780
1781 void
1782 GeneralizedCylinder::inputChangedCB(void *data, SoSensor *sens )
1783 {
1784 GeneralizedCylinder *s = (GeneralizedCylinder *)data;
1785 SoNode *notifier = ((SoNodeSensor *)sens)->getTriggerNode();
1786
1787 SoCoordinate3 *coordPart = NULL;
1788 SbBool isClosed;
1789
1790 coordPart = SO_CHECK_PART( s, "profileCoords", SoCoordinate3 );
1791 if ( notifier == coordPart )
1792 isClosed = s->profileClosed.getValue();
1793 else {
1794 coordPart = SO_CHECK_PART( s, "crossSectionCoords", SoCoordinate3 );
1795 if ( notifier == coordPart )
1796 isClosed = s->crossSectionClosed.getValue();
1797 else {
1798 coordPart = SO_CHECK_PART( s, "spineCoords", SoCoordinate3 );
1799 if ( notifier == coordPart )
1800 isClosed = s->spineClosed.getValue();
1801 else {
1802 coordPart = SO_CHECK_PART( s, "twistCoords", SoCoordinate3 );
1803 if ( notifier == coordPart )
1804 isClosed = s->twistClosed.getValue();
1805 else
1806 coordPart = NULL;
1807 }
1808 }
1809 }
1810
1811 if (coordPart == NULL)
1812 return;
1813
1814 if (isClosed) {
1815 SoMFVec3f *cField = &(coordPart->point);
1816 int numC = cField->getNum();
1817 // make the last point follow the first point.
1818 if ( numC == 3 )
1819 cField->set1Value( numC, (*cField)[0] );
1820 else if ( numC > 3 && (*cField)[0] != (*cField)[numC - 1] )
1821 cField->set1Value( numC - 1, (*cField)[0] );
1822 }
1823
1824 // Clamp the values of the profile coordinates so that
1825 // no values are below zero:
1826 if (coordPart == s->profileCoords.getValue()) {
1827 SbBool changed = FALSE;
1828 coordPart->point.enableNotify(FALSE);
1829 SbVec3f *pCoords = coordPart->point.startEditing();
1830 for ( int i = 0; i < coordPart->point.getNum(); i++ ) {
1831 if ( pCoords[i][0] < 0.0 ) {
1832 pCoords[i].setValue( 0.0, pCoords[i][1], pCoords[i][2] );
1833 changed = TRUE;
1834 }
1835 }
1836 coordPart->point.finishEditing();
1837 coordPart->point.enableNotify(TRUE);
1838 if (changed) {
1839 // return now -- changing the coords will trigger notification
1840 // and bring us back here.
1841 coordPart->point.touch();
1842 }
1843 }
1844
1845 s->updateSurface();
1846 }
1847
1848
1849 void
1850 GeneralizedCylinder::updateSurroundingManip()
1851 {
1852 SoNode *n;
1853
1854 // Is there a transform?
1855 n = getPart( "transform", FALSE);
1856 if (n==NULL) return;
1857
1858 // If it's not a transform manip, then return...
1859 if ( !n->isOfType( SoTransformManip::getClassTypeId() ) )
1860 return;
1861
1862 SoTransformManip *xfm = (SoTransformManip *) n;
1863
1864 // Try to get the dragger from the manip.
1865 SoDragger *d = xfm->getDragger();
1866 if (d==NULL)
1867 return;
1868
1869 SoSurroundScale *ss
1870 = (SoSurroundScale *) d->getPart( "surroundScale", FALSE );
1871
1872 if ( ss != NULL)
1873 ss->invalidate();
1874 }
1875
1876 void
1877 GeneralizedCylinder::changeTransformType( SoType newType )
1878 {
1879 // Return if manip is already of correct type.
1880 SoTransform *curXf = (SoTransform *) getPart("transform",FALSE);
1881 if ( curXf && (curXf->getTypeId() == newType ))
1882 return;
1883
1884
1885 if ( newType.isBad() ||
1886 ! newType.isDerivedFrom(SoTransformManip::getClassTypeId() ) ||
1887 ! newType.canCreateInstance() ) {
1888
1889 // If no transform part, just return:
1890 SoFullPath *xfPath = (SoFullPath *) createPathToPart("transform",FALSE);
1891 if ( ! xfPath )
1892 return;
1893
1894 // If tail is not an SoTransformManip, just return.
1895 SoNode *tail = xfPath->getTail();
1896 if ( ! tail->isOfType( SoTransformManip::getClassTypeId() ) )
1897 return;
1898
1899 // Tail is an SoTransformManip. Tell it to replace itself with
1900 // a regular transform
1901 xfPath->ref();
1902 tail->ref();
1903 SoTransformManip *m = (SoTransformManip *) tail;
1904 m->replaceManip(xfPath, new SoTransform );
1905 tail->unref();
1906 xfPath->unref();
1907 }
1908 else {
1909
1910 // Make a manip of given type, tell it to insert itself in the path
1911 SoPath *xfPath = createPathToPart("transform",TRUE);
1912 SoTransformManip *m = (SoTransformManip *) newType.createInstance();
1913 xfPath->ref();
1914 m->replaceNode(xfPath );
1915 xfPath->unref();
1916 }
1917 }