Debian Codegraph @

apps/nodes/GeneralizedCylinder/NurbMaker.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 | Description:
39 | Defines the NurbMaker class. Given a quadMesh,
40 | a bunch of parameters, it creates a group node with a bunch
41 | of SoIndexedNurbsSurface nodes. These nodes will create a nurbs
42 | surface if placed after an SoCoordinate3 node with the number of
43 | points specified by the QuadMesh node.
44 |
45 | Note that you only need to look at the SoCoordinate3 node if
46 | you need to figure out whether edges match for wraparound.
47 | Otherwise this is all strictly 'topological'
48 |
49 | Author(s) : Paul Isaacs
50 */
51
52
53
54 #include <Inventor/nodes/SoCoordinate3.h>
55 #include <Inventor/nodes/SoGroup.h>
56 #include <Inventor/nodes/SoQuadMesh.h>
57 #include <Inventor/nodes/SoIndexedNurbsSurface.h>
58
59 #include "NurbMaker.h"
60
61 // These knot vectors are used to make CUBIC_TO_EDGE splines
62 // meet up with the mesh edge.
63
64 static float cubic2FromLateEdgeKnots[8] = { 0, 1, 2, 3, 4, 5, 6, 6 };
65 static float cubic1FromLateEdgeKnots[8] = { 0, 1, 2, 3, 4, 5, 5, 5 };
66 static float cubic0FromLateEdgeKnots[8] = { 0, 1, 2, 3, 4, 4, 4, 4 };
67
68 static float cubic2FromEarlyEdgeKnots[8] = { 0, 0, 1, 2, 3, 4, 5, 6 };
69 static float cubic1FromEarlyEdgeKnots[8] = { 0, 0, 0, 1, 2, 3, 4, 5 };
70 static float cubic0FromEarlyEdgeKnots[8] = { 0, 0, 0, 0, 1, 2, 3, 4 };
71
72 // Creates group with new SoIndexedNurbsSurface nodes underneath.
73 SoGroup *
74 NurbMaker::createNurbsGroup( SoQuadMesh *quadNode, SoCoordinate3 *coordNode )
75 {
76 SbBool rowsMatch = FALSE;
77 SbBool colsMatch = FALSE;
78
79 int vPerCol = (int) quadNode->verticesPerColumn.getValue();
80 int vPerRow = (int) quadNode->verticesPerRow.getValue();
81
82 SbVec2s quadMeshDivs( vPerRow, vPerCol );
83
84 // Only bother checking if we're wrapping.
85 if (coordNode != NULL && myWrap[0] == TRUE) {
86 rowsMatch = TRUE;
87 // Test if first and last row are the same.
88 for (int topInd = 0, botInd = vPerRow * (vPerCol - 1);
89 topInd < vPerRow; topInd++, botInd++ ) {
90 if ( coordNode->point[topInd] != coordNode->point[botInd] ) {
91 rowsMatch = FALSE;
92 }
93 }
94 }
95 if (coordNode != NULL && myWrap[1] == TRUE) {
96 SbBool colsMatch = TRUE;
97 // Test if first and last col are the same.
98 for (int leftInd = 0, rightInd = vPerRow - 1;
99 leftInd < vPerRow * (vPerCol - 1);
100 leftInd += vPerRow, rightInd += vPerRow) {
101 if ( coordNode->point[leftInd] != coordNode->point[rightInd] ) {
102 colsMatch = FALSE;
103 }
104 }
105 }
106
107 return ( createNurbsGroup( quadMeshDivs, SbVec2s(rowsMatch,colsMatch )));
108 }
109
110 // Creates group with new SoIndexedNurbsSurface nodes underneath.
111 SoGroup *
112 NurbMaker::createNurbsGroup( SbVec2s numQuadMeshDivisions,
113 SbVec2s doEdgesMatch )
114 {
115 establishMyKnotParams();
116 // The patch size is the number of control points on the side
117 // of each patch. It's determined by the number of knots and order
118 SbVec2s patchSize = myNumKnots - myOrder;
119
120 // Make a group to put the new NURBS under
121 if (nurbsGroup != NULL)
122 nurbsGroup->unref();
123
124 nurbsGroup = new SoGroup;
125 nurbsGroup->ref();
126
127 // What are the mesh dimensions?
128 int vPerRow = (int) numQuadMeshDivisions[0];
129 int vPerCol = (int) numQuadMeshDivisions[1];
130
131 // Determine last row and last column that we can use for our
132 // upper left corner of our little patch.
133 // Initially, figure that the last patch's right/bottom edge
134 // need to line up with the mesh's right/bottom edge.
135 int lastRowToDo = vPerCol - patchSize[1];
136 int lastColToDo = vPerRow - patchSize[0];
137
138 // If we don't wrap, all rows and columns are used.
139 // If we are wrapping and the first&last row/column are the same,
140 // then we will never use the last row/column.
141 int numUseableRows = vPerCol;
142 int numUseableCols = vPerRow;
143
144 // If we're wrapping rows, the last patch left edge is the same as the
145 // mesh's right edge.
146 // But if the left and right edges of the mesh are identical, we don't
147 // need to repeat the set of patches that uses the mesh's right edge as
148 // their own (the patches' own) left edge.
149 if (myWrap[0] == TRUE) {
150 if (doEdgesMatch[0]) {
151 lastRowToDo = vPerCol - 2;
152 numUseableRows = vPerCol - 1;
153 }
154 else {
155 lastRowToDo = vPerCol - 1;
156 numUseableRows = vPerCol;
157 }
158 }
159
160 // If we're wrapping cols, the last patch top edge is the same as the
161 // mesh's bottom edge.
162 // But if the top and bottom edges of the mesh are identical, we don't
163 // need to repeat the set of patches that uses the mesh's bottom edge as
164 // their own (the patches' own) top edge.
165 if (myWrap[1] == TRUE) {
166 if (doEdgesMatch[1]) {
167 lastColToDo = vPerRow - 2;
168 numUseableCols = vPerRow - 1;
169 }
170 else {
171 lastColToDo = vPerRow - 1;
172 numUseableCols = vPerRow;
173 }
174 }
175
176 // In each patch, we will lay down the coordinate indices as either
177 // increasing from left to right , as in:
178 // 0 1 2 3
179 // 4 5 6 7
180 // 8 9 10 11
181 // 12 13 14 15
182 //
183 // or increasing from right to left, depending on 'flipNormals'
184 // 3 2 1 0
185 // 7 6 5 4
186 // 11 10 9 8
187 // 15 14 13 12
188 int numPatchInds = patchSize[0] * patchSize[1];
189 int *patchInds = new int [ numPatchInds ];
190 if ( flipNormals == FALSE ) {
191 for (int i = 0, count=0; i < patchSize[1]; i++ ){
192 for(int j = 0; j < patchSize[0]; j++ ) {
193 patchInds[count] = (i * patchSize[0]) + j;
194 count++;
195 }
196 }
197 }
198 else for (int i = 0, count=0; i < patchSize[1]; i++ ){
199 for(int j = patchSize[0]-1; j >=0; j--) {
200 patchInds[count] = (i * patchSize[0]) + j;
201 count++;
202 }
203 }
204
205 // Now, create the patches. Each is a nurb with
206 // a patchSize[0]by patchSize[1] control point patch.
207 // The knots are as already established.
208 for ( int row = 0; row <= lastRowToDo; row+= myShift[0] ) {
209 for ( int col = 0; col <= lastColToDo; col+= myShift[1] ) {
210
211 // Create the NURB and set the easy fields...
212 SoIndexedNurbsSurface *myNurb = new SoIndexedNurbsSurface;
213 nurbsGroup->addChild(myNurb);
214 myNurb->numUControlPoints = patchSize[0];
215 myNurb->numVControlPoints = patchSize[1];
216 if ( patchType != CUBIC_TO_EDGE) {
217 myNurb->uKnotVector.setValues(0,myNumKnots[0],myUKnots);
218 myNurb->vKnotVector.setValues(0,myNumKnots[1],myVKnots);
219 }
220 else {
221 applyCubicToEdgeKnotVectors(row, col, myNurb,
222 lastRowToDo, lastColToDo);
223 }
224
225 for (int pRow = 0,count = 0; pRow < patchSize[1]; pRow++ ) {
226
227 // Now we've got to set up the indices. Determine the row
228 // number in the quadmesh (0<=value<numUseableRows)
229 // to use as the row number in the patch( 0<=pRow<patchSize[1])
230 int rowInQuadMesh = row + pRow;
231
232 // Now check to see if we need to wraparound this row.
233 if (rowInQuadMesh >= numUseableRows)
234 rowInQuadMesh -= numUseableRows;
235
236 for (int pCol = 0; pCol < patchSize[0]; pCol++ ) {
237
238 // Determine the col number in the mesh
239 // (0<=value<numUseableCols) to use as the col number
240 // in the patch( 0<=pRow<patchSize[0])
241 int colInQuadMesh = col + pCol;
242
243 // Now check to see if we need to wraparound this column.
244 if (colInQuadMesh >= numUseableCols)
245 colInQuadMesh -= numUseableCols;
246
247
248 myNurb->coordIndex.set1Value( patchInds[count],
249 rowInQuadMesh * vPerRow + colInQuadMesh);
250
251 count++;
252 }
253 }
254 }
255 }
256
257 return nurbsGroup;
258 }
259
260 NurbMaker::NurbMaker()
261 {
262 nurbsGroup = NULL;
263
264 patchType = CUBIC_TO_EDGE;
265
266 myNumKnots.setValue(0,0);
267 myUKnots = myVKnots = NULL;
268 myOrder.setValue(4,4);
269 myShift.setValue(1,1);
270 myWrap.setValue(0,0);
271
272 userNumKnots.setValue(0,0);
273 userUKnots = userVKnots = NULL;
274 userOrder.setValue(4,4);
275
276 flipNormals = FALSE;
277 }
278
279 NurbMaker::~NurbMaker()
280 {
281 if (nurbsGroup) {
282 nurbsGroup->unref();
283 nurbsGroup = NULL;
284 }
285 }
286
287 void
288 NurbMaker::establishMyKnotParams()
289 {
290 int i;
291
292 // Take care of knot array allocation.
293 if ( patchType == BEZIER || patchType == CUBIC
294 || patchType == CUBIC_TO_EDGE ) {
295 if ( myNumKnots[0] != 8 ) {
296 if (myUKnots != NULL) delete []myUKnots;
297 myNumKnots[0] = 8;
298 myUKnots = new float[8];
299 }
300 if ( myNumKnots[1] != 8 ) {
301 if (myVKnots != NULL) delete []myVKnots;
302 myNumKnots[1] = 8;
303 myVKnots = new float[8];
304 }
305 }
306 else {
307 if (myUKnots != NULL) delete []myUKnots;
308 if (myVKnots != NULL) delete []myVKnots;
309
310 myNumKnots = userNumKnots;
311
312 myUKnots = new float[myNumKnots[0]];
313 myVKnots = new float[myNumKnots[1]];
314 }
315
316 // Take care of knot values and order values.
317 switch (patchType) {
318 case BEZIER:
319 myOrder.setValue(4,4);
320 for (i = 0; i < 4; i++)
321 myUKnots[i] = myVKnots[i] = 0;
322 for (i = 4; i < 8; i++)
323 myUKnots[i] = myVKnots[i] = 1;
324 break;
325 case CUBIC:
326 case CUBIC_TO_EDGE:
327 myOrder.setValue(4,4);
328 for (i = 0; i < 8; i++)
329 myUKnots[i] = myVKnots[i] = i;
330 break;
331 case USER_KNOTS:
332 myOrder = userOrder;
333 for (i = 0; i < myNumKnots[0]; i++)
334 myUKnots[i] = userUKnots[i];
335 for (i = 0; i < myNumKnots[1]; i++) {
336 myVKnots[i] = userVKnots[i];
337 }
338 break;
339 }
340 }
341
342 void
343 NurbMaker::setUserKnots( SbVec2s newNumKnots,
344 float *newUKnots, float *newVKnots)
345 {
346 userNumKnots = newNumKnots;
347 if (userUKnots == NULL)
348 delete [] userUKnots;
349 if (userVKnots == NULL)
350 delete [] userVKnots;
351 userUKnots = new float [userNumKnots[0]];
352 userVKnots = new float [userNumKnots[1]];
353 for (int i = 0; i < userNumKnots[0]; i++ )
354 userUKnots[i] = newUKnots[i];
355 for (int j = 0; j < userNumKnots[1]; j++ )
356 userVKnots[j] = newVKnots[j];
357 }
358
359 void
360 NurbMaker::getUserKnots( SbVec2s &numKnots,
361 float * UKnots, float * VKnots)
362 {
363 numKnots = userNumKnots;
364 if (UKnots == NULL)
365 delete [] UKnots;
366 if (VKnots == NULL)
367 delete [] VKnots;
368 UKnots = new float [userNumKnots[0]];
369 VKnots = new float [userNumKnots[1]];
370 for (int i = 0; i < userNumKnots[0]; i++ )
371 UKnots[i] = userUKnots[i];
372 for (int j = 0; j < userNumKnots[1]; j++ )
373 VKnots[j] = userVKnots[j];
374 }
375
376 void
377 NurbMaker::setPatchType(PatchType newPatchType)
378 {
379 patchType = newPatchType;
380 if (patchType == BEZIER) {
381 myShift.setValue(3,3);
382 }
383 else if (patchType == CUBIC || patchType == CUBIC_TO_EDGE) {
384 myShift.setValue(1,1);
385 }
386 }
387
388 void
389 NurbMaker::applyCubicToEdgeKnotVectors(int row, int col,
390 SoIndexedNurbsSurface *myNurb, int lastRowToDo, int lastColToDo)
391 {
392 float *uKs, *vKs;
393
394 if ( myWrap[1] ) uKs = myUKnots;
395 else if (col == 0 ) uKs = cubic0FromEarlyEdgeKnots;
396 else if (col == lastColToDo) uKs = cubic0FromLateEdgeKnots;
397 else if (col == 1 ) uKs = cubic1FromEarlyEdgeKnots;
398 else if (col == lastColToDo-1) uKs = cubic1FromLateEdgeKnots;
399 else if (col == 2 ) uKs = cubic2FromEarlyEdgeKnots;
400 else if (col == lastColToDo-2) uKs = cubic2FromLateEdgeKnots;
401 else uKs = myUKnots;
402
403 if ( myWrap[0] ) vKs = myVKnots;
404 else if (row == 0 ) vKs = cubic0FromEarlyEdgeKnots;
405 else if (row == lastRowToDo) vKs = cubic0FromLateEdgeKnots;
406 else if (row == 1 ) vKs = cubic1FromEarlyEdgeKnots;
407 else if (row == lastRowToDo-1) vKs = cubic1FromLateEdgeKnots;
408 else if (row == 2 ) vKs = cubic2FromEarlyEdgeKnots;
409 else if (row == lastRowToDo-2) vKs = cubic2FromLateEdgeKnots;
410 else vKs = myVKnots;
411
412 myNurb->uKnotVector.setValues(0,8,uKs);
413 myNurb->vKnotVector.setValues(0,8,vKs);
414
415 };