Debian Codegraph @

apps/demos/qmorf/QuadThing.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 // Simple class that encapsulates some information about a quad-mesh.
39 // Used by the qmorf program and by the little programs that generate
40 // quad-mesh objects.
41 //
42
43 #include <cstdlib>
44 #include <assert.h>
45
46 #include <Inventor/SoDB.h>
47 #include <Inventor/SoInput.h>
48 #include <Inventor/nodes/SoSeparator.h>
49 #include <Inventor/nodes/SoCoordinate3.h>
50 #include <Inventor/nodes/SoNormal.h>
51 #include <Inventor/nodes/SoNormalBinding.h>
52 #include <Inventor/nodes/SoQuadMesh.h>
53 #include <Inventor/nodes/SoVertexProperty.h>
54 #include <Inventor/nodes/SoMaterial.h>
55 #include <Inventor/nodes/SoMaterialBinding.h>
56 #include <Inventor/nodes/SoTexture2.h>
57 #include <Inventor/nodes/SoTextureCoordinateBinding.h>
58 #include <Inventor/nodes/SoTextureCoordinate2.h>
59 #include <Inventor/actions/SoCallbackAction.h>
60 #include <Inventor/actions/SoGetBoundingBoxAction.h>
61 #include <Inventor/actions/SoSearchAction.h>
62
63 #include "QuadThing.h"
64
65 QuadThing::QuadThing(const QuadThing *from)
66 {
67 createSceneGraph();
68
69 interp(from, from, 0.0);
70 }
71
72 QuadThing::QuadThing(int nx, int ny, const SbVec3f *verts)
73 {
74 createSceneGraph();
75
76 coords->point.setValues(0, nx*ny, verts);
77 qmesh->verticesPerRow.setValue(nx);
78 qmesh->verticesPerColumn.setValue(ny);
79
80 figureNormals();
81 }
82
83 static SoNode *
84 searchLastType(SoPath *p, SoType t)
85 {
86 SoSearchAction sa;
87 sa.setSearchingAll(TRUE);
88 sa.setType(t);
89 sa.setInterest(SoSearchAction::LAST);
90 sa.apply(p);
91 SoPath *outPath = sa.getPath();
92
93 SoNode *result = NULL;
94 if (outPath != NULL && (outPath->getLength() > 0) )
95 result = outPath->getTail();
96
97 return result;
98 }
99
100
101 //
102 // Read in a scene graph and extract the information out of it needed
103 // to create a QuadThing. This also uniformly scales the object to
104 // reside in a -1 to 1 bounding box.
105 //
106 QuadThing::QuadThing(const char *filename)
107 {
108 //
109 // Need to extract:
110 // -- first QuadMesh; need its x,y size.
111 // -- coordinates
112 // -- materials
113 // -- material binding
114 // -- textures
115 // -- texture coordinates
116 // -- texture binding
117 //
118 // Note: these are only set to NULL in this constructor because
119 // this is the only constructor that may fail to create them (all
120 // the other constructors create them in the createSceneGraph
121 // routine)
122 //
123 sceneGraph = NULL;
124 coords = NULL;
125 norms = NULL;
126 qmesh = NULL;
127 materials = NULL;
128 matbind = NULL;
129 texture = NULL;
130 texcoords = NULL;
131 texbind = NULL;
132
133 SoInput in;
134 if (!in.openFile(filename)) {
135 fprintf(stderr, "Couldn't open %s\n", filename);
136 exit(1);
137 }
138 SoSeparator *graph = SoDB::readAll(&in);
139 in.closeFile();
140
141 if (graph == NULL) return;
142 graph->ref();
143
144 //
145 // Search for first mesh
146 //
147 SoSearchAction sa;
148 sa.setInterest(SoSearchAction::FIRST);
149 sa.setType(SoQuadMesh::getClassTypeId());
150 sa.apply(graph);
151
152 SoPath *pathToMesh = sa.getPath();
153 if (pathToMesh == NULL)
154 {
155 fprintf(stderr, "Error reading qmesh from %s\n", filename);
156 return;
157 }
158
159 pathToMesh->ref();
160 SoQuadMesh *q = (SoQuadMesh *)pathToMesh->getTail();
161
162 //
163 // If there is a VertexProperty node, make a new separator containing
164 // nodes with that information, and put it in the scene graph.
165 //
166 if (q->vertexProperty.getValue() != NULL)
167 {
168 SoVertexProperty *vp = (SoVertexProperty*)q->vertexProperty.getValue();
169 SoSeparator *quadsep = new SoSeparator;
170 int numVerts = vp->vertex.getNum();
171 if (numVerts > 0)
172 {
173 SoCoordinate3 *newcoord = new SoCoordinate3;
174 newcoord->point.setValues(0,numVerts,vp->vertex.getValues(0));
175 quadsep->addChild(newcoord);
176 }
177 int numNormals = vp->normal.getNum();
178 if (numNormals > 0)
179 {
180 SoNormal *newnormal = new SoNormal;
181 newnormal->vector.setValues(0,numNormals,vp->normal.getValues(0));
182 quadsep->addChild(newnormal);
183 }
184 int numTexCoords = vp->texCoord.getNum();
185 if (numTexCoords > 0)
186 {
187 SoTextureCoordinate2 *newtexcoord = new SoTextureCoordinate2;
188 newtexcoord->point.setValues(0,numTexCoords,vp->texCoord.getValues(0));
189 quadsep->addChild(newtexcoord);
190 }
191 int numColors = vp->orderedRGBA.getNum();
192 if (numColors > 0)
193 {
194 float transparency; SbColor color;
195 SoMaterial *newmat = new SoMaterial;
196 for (int i=0; i<numColors; i++)
197 {
198 color.setPackedValue(vp->orderedRGBA[i],transparency);
199 newmat->diffuseColor.set1Value(i,color);
200 newmat->transparency.set1Value(i,transparency);
201 }
202 quadsep->addChild(newmat);
203 }
204 q->vertexProperty.setValue(NULL);
205 quadsep->addChild(q);
206 ((SoGroup*)(pathToMesh->getNodeFromTail(1)))->replaceChild(q, quadsep);
207 pathToMesh->unref();
208
209 sa.apply(graph);
210 pathToMesh = sa.getPath();
211 if (pathToMesh == NULL)
212 {
213 fprintf(stderr, "Error reading qmesh from %s\n", filename);
214 return;
215 }
216 pathToMesh->ref();
217 q = (SoQuadMesh *)pathToMesh->getTail();
218 }
219
220
221 createSceneGraph();
222
223 sceneGraph->replaceChild(qmesh, q);
224 qmesh = q;
225
226 qmesh->verticesPerRow.setValue(q->verticesPerRow.getValue());
227 qmesh->verticesPerColumn.setValue(q->verticesPerColumn.getValue());
228
229 //
230 // Search for coordinates. We look for the last coordinates on
231 // the path to the mesh, and assume that the mesh will use those
232 // coordinates. This isn't quite correct; the ignore flag could
233 // be set on the coordinate's point field. We should check for
234 // that and search further up the path, but for simplicity I just
235 // assume that the ignore flag isn't set (certainly true for the
236 // data sets I'm likely to throw at this!).
237 //
238
239 SoCoordinate3 *c = (SoCoordinate3 *)
240 searchLastType(pathToMesh,
241 SoCoordinate3::getClassTypeId());
242 if (c == NULL)
243 {
244 fprintf(stderr, "Could not find coordinates in %s!\n",
245 filename);
246 exit(1);
247 }
248 sceneGraph->replaceChild(coords, c);
249 coords = c;
250
251 //
252 // And other stuff... Again, note that the results may not be
253 // correct if the ignore flag is set on any of this stuff. Hi ho.
254 //
255
256 materials = (SoMaterial *)
257 searchLastType(pathToMesh,
258 SoMaterial::getClassTypeId());
259 if (materials != NULL)
260 {
261 materials->ref();
262 }
263
264 matbind = (SoMaterialBinding *)
265 searchLastType(pathToMesh,
266 SoMaterialBinding::getClassTypeId());
267 if (matbind != NULL)
268 {
269 matbind->ref();
270 }
271 texture = (SoTexture2 *)
272 searchLastType(pathToMesh,
273 SoTexture2::getClassTypeId());
274 if (texture != NULL)
275 {
276 texture->ref();
277 }
278 texcoords = (SoTextureCoordinate2 *)
279 searchLastType(pathToMesh,
280 SoTextureCoordinate2::getClassTypeId());
281 if (texcoords != NULL)
282 {
283 texcoords->ref();
284 }
285 texbind = (SoTextureCoordinateBinding *)
286 searchLastType(pathToMesh,
287 SoTextureCoordinateBinding::getClassTypeId());
288 if (texbind != NULL)
289 {
290 texbind->ref();
291 }
292
293 pathToMesh->unref();
294 graph->unref(); // No longer needed
295
296 figureNormals();
297 }
298
299 QuadThing::~QuadThing()
300 {
301 //
302 // Delete any of the stuff we might have found
303 //
304 if (sceneGraph != NULL) sceneGraph->unref();
305 if (materials != NULL) materials->unref();
306 if (matbind != NULL) matbind->unref();
307 if (texture != NULL) texture->unref();
308 if (texcoords != NULL) texcoords->unref();
309 if (texbind != NULL) texbind->unref();
310 }
311
312 //
313 // Create the scene graph. Used by the constructors.
314 //
315 void
316 QuadThing::createSceneGraph()
317 {
318 sceneGraph = new SoSeparator;
319 sceneGraph->ref();
320
321 coords = new SoCoordinate3;
322 sceneGraph->addChild(coords);
323
324 norms = new SoNormal;
325 sceneGraph->addChild(norms);
326
327 SoNormalBinding *nb = new SoNormalBinding;
328 nb->value = SoNormalBinding::PER_VERTEX_INDEXED;
329 sceneGraph->addChild(nb);
330
331 qmesh = new SoQuadMesh;
332 sceneGraph->addChild(qmesh);
333 }
334
335 #define MAX(a,b) (a > b ? a : b)
336
337 //
338 // Modify the vertices so the bounding boxes of all of the objects are
339 // the same
340 //
341 void
342 QuadThing::scaleBBox()
343 {
344 SoGetBoundingBoxAction bba(SbVec2s(1,1));
345 bba.apply(sceneGraph);
346 SbBox3f bbox = bba.getBoundingBox();
347
348 SbVec3f center = bbox.getCenter();
349 float x, y, z, scale;
350 bbox.getSize(x, y, z);
351 scale = 2.0 / MAX( x, MAX(y,z) );
352
353 SbVec3f *vertices = coords->point.startEditing();
354
355 // Now modify vertices...
356 int nverts = (int) (qmesh->verticesPerColumn.getValue() *
357 qmesh->verticesPerRow.getValue());
358 for (int i = 0; i < nverts; i++)
359 {
360 vertices[i] = (vertices[i]-center)*scale;
361 }
362 coords->point.finishEditing();
363 }
364
365 //
366 // Newell's formula for the normal of a polygon-- this version is
367 // hardwired for quads. n will be close to zero if the quad is
368 // degenerate. This does NOT normalize the result.
369 //
370 void
371 QuadThing::newell4(SbVec3f &n, int v1, int v2, int v3, int v4)
372 {
373 const SbVec3f *v = coords->point.getValues(0);
374 n[0] = (v[v1][1] - v[v2][1])*(v[v1][2] + v[v2][2]);
375 n[1] = (v[v1][2] - v[v2][2])*(v[v1][0] + v[v2][0]);
376 n[2] = (v[v1][0] - v[v2][0])*(v[v1][1] + v[v2][1]);
377
378 n[0] += (v[v2][1] - v[v3][1])*(v[v2][2] + v[v3][2]);
379 n[1] += (v[v2][2] - v[v3][2])*(v[v2][0] + v[v3][0]);
380 n[2] += (v[v2][0] - v[v3][0])*(v[v2][1] + v[v3][1]);
381
382 n[0] += (v[v3][1] - v[v4][1])*(v[v3][2] + v[v4][2]);
383 n[1] += (v[v3][2] - v[v4][2])*(v[v3][0] + v[v4][0]);
384 n[2] += (v[v3][0] - v[v4][0])*(v[v3][1] + v[v4][1]);
385
386 n[0] += (v[v4][1] - v[v1][1])*(v[v4][2] + v[v1][2]);
387 n[1] += (v[v4][2] - v[v1][2])*(v[v4][0] + v[v1][0]);
388 n[2] += (v[v4][0] - v[v1][0])*(v[v4][1] + v[v1][1]);
389 }
390
391 //
392 // Average the normals of the faces around the vertex at the
393 // given row, column to figure out its normal. This actually weights
394 // the polygons' contributions by their area.
395 //
396 void
397 QuadThing::averageNormals(SbVec3f *normals, int r, int c)
398 {
399 int numx = (int)qmesh->verticesPerRow.getValue();
400
401 int i = r*numx+c;
402
403 SbVec3f n;
404
405 newell4(n, i-numx-1, i-1, i, i-numx);
406 normals[i] = n;
407 newell4(n, i-numx, i, i+1, i-numx+1);
408 normals[i] += n;
409 newell4(n, i, i+numx, i+numx+1, i+1);
410 normals[i] += n;
411 newell4(n, i-1, i+numx-1, i+numx, i);
412 normals[i] += n;
413
414 float d = normals[i].length();
415 if (d > 0.00001) normals[i] /= d;
416 else normals[i].setValue(0.0, 0.0, 1.0);
417 }
418
419 //
420 // Figure out the normals for this thing. This uses Newell's formula
421 // on the 4 vertices surrounding each vertex. This will be fast and
422 // fairly robust (edges are handled by using the vertex itself, so
423 // corners get calculated correctly). If the polygon is degenerate,
424 // an arbitrary normal is assigned to the vertex.
425 //
426 void
427 QuadThing::figureNormals()
428 {
429 int row, column;
430
431 int numx = (int)qmesh->verticesPerRow.getValue();
432 int numy = (int)qmesh->verticesPerColumn.getValue();
433
434 int nn = norms->vector.getNum();
435 if (nn < numx*numy)
436 {
437 norms->vector.insertSpace(0, numx*numy-nn);
438 }
439
440 SbVec3f *normals = norms->vector.startEditing();
441
442 //
443 // Do interior vertices. We take the normal of the plane through
444 // the neighboring vertices for speed.
445 //
446 for (row = 1; row < numy-1; row++)
447 {
448 for (column = 1; column < numx-1; column++)
449 {
450 int i = row*numx+column;
451 newell4(normals[i], i-numx, i-1, i+numx, i+1);
452
453 //
454 // Normalize; if using the neighboring points failed for
455 // any reason, average the normals of the surrounding
456 // faces:
457 //
458 float d = normals[i].length();
459 if (d > 0.00001) normals[i] /= d;
460 else averageNormals(normals, row, column);
461 }
462 }
463 //
464 // For the border quads, average the normals of surrounding faces.
465 // First, bottom and top:
466 //
467 SbVec3f lastBot(0.0, 0.0, 0.0);
468 SbVec3f lastTop(0.0, 0.0, 0.0);
469 int i;
470 for (i = 0; i < numx; i++)
471 {
472 int top = (numy-1)*numx+i;
473 normals[i] = lastBot;
474 normals[top] = lastTop;
475
476 if (i+1 < numx)
477 {
478 newell4(lastBot, i, i+numx, i+numx+1, i+1);
479 newell4(lastTop, top-numx, top, top+1, top-numx+1);
480
481 normals[i] += lastBot;
482 normals[top] += lastTop;
483 }
484 float d = normals[i].length();
485 if (d > 0.00001) normals[i] /= d;
486 else normals[i].setValue(0.0, 0.0, 1.0);
487 d = normals[top].length();
488 if (d > 0.00001) normals[top] /= d;
489 else normals[top].setValue(0.0, 0.0, 1.0);
490 }
491 //
492 // Next, left and right sides. There are no special cases
493 // because the corners were done in the previous loop.
494 //
495 SbVec3f lastLeft;
496 SbVec3f lastRight;
497 newell4(lastLeft, 0, numx, numx+1, 1);
498 newell4(lastRight, numx-2, 2*numx-2, 2*numx-1, numx-1);
499 for (i = 1; i < numy-1; i++)
500 {
501 int left = i*numx;
502 int right = left+numx-1;
503 normals[left] = lastLeft;
504 normals[right] = lastRight;
505
506 newell4(lastLeft, left, left+numx, left+numx+1, left+1);
507 newell4(lastRight, right-1, right+numx-1, right+numx, right);
508
509 normals[left] += lastLeft;
510 normals[right] += lastRight;
511
512 float d = normals[left].length();
513 if (d > 0.00001) normals[left] /= d;
514 else normals[left].setValue(0.0, 0.0, 1.0);
515 d = normals[right].length();
516 if (d > 0.00001) normals[right] /= d;
517 else normals[right].setValue(0.0, 0.0, 1.0);
518 }
519 norms->vector.finishEditing();
520 }
521
522 //
523 // Interpolate between two things. This does simple linear interpolation.
524 //
525 void
526 QuadThing::interp(const QuadThing *from, const QuadThing *to, float time)
527 {
528 int fnx = (int)from->qmesh->verticesPerRow.getValue();
529 int fny = (int)from->qmesh->verticesPerColumn.getValue();
530 int tnx = (int)to->qmesh->verticesPerRow.getValue();
531 int tny = (int)to->qmesh->verticesPerColumn.getValue();
532
533 int nx = fnx > tnx ? fnx : tnx;
534 int ny = fny > tny ? fny : tny;
535
536 int nv = coords->point.getNum();
537 if (nv < nx*ny)
538 {
539 coords->point.insertSpace(0, (int)(nx*ny-nv));
540 }
541
542 qmesh->verticesPerRow.setValue(nx);
543 qmesh->verticesPerColumn.setValue(ny);
544
545 SbVec3f *vertices = coords->point.startEditing();
546 const SbVec3f *f_vertices = from->coords->point.getValues(0);
547 const SbVec3f *t_vertices = to->coords->point.getValues(0);
548
549 // Use linear interpolation for now
550 for (int row = 0; row < ny; row++)
551 {
552 int from_row = (row * fny) / ny;
553 int to_row = (row * tny) / ny;
554 for (int col = 0; col < nx; col++)
555 {
556 int i = row*nx+col;
557 int from_i = from_row*fnx+col;
558 int to_i = to_row*tnx+col;
559 vertices[i] = f_vertices[from_i] * (1.0 - time) +
560 t_vertices[to_i] * time;
561 }
562 }
563 coords->point.finishEditing();
564 figureNormals();
565 }
566
567 //
568 // Some access routines.
569 //
570 SoSeparator *
571 QuadThing::getSceneGraph()
572 {
573 return sceneGraph;
574 }
575
576 SoMaterial *
577 QuadThing::getMaterial()
578 {
579 return materials;
580 }
581
582 SoMaterialBinding *
583 QuadThing::getMatBinding()
584 {
585 return matbind;
586 }
587
588 SoTexture2 *
589 QuadThing::getTexture2()
590 {
591 return texture;
592 }
593
594 SoTextureCoordinate2 *
595 QuadThing::getTexCoord()
596 {
597 return texcoords;
598 }
599
600 SoTextureCoordinateBinding *
601 QuadThing::getTexBinding()
602 {
603 return texbind;
604 }