Debian Codegraph @

apps/tools/ivnorm/FindNormals.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 // Takes a scene graph and generates normals for it
39 //
40
41 #include <assert.h>
42 #include <cmath>
43
44 #include <Inventor/SoDB.h>
45 #include <Inventor/nodes/SoGroup.h>
46 #include <Inventor/nodes/SoIndexedFaceSet.h>
47 #include <Inventor/nodes/SoNormal.h>
48 #include <Inventor/nodes/SoNormalBinding.h>
49 #include <Inventor/nodes/SoCoordinate3.h>
50 #include <Inventor/nodes/SoShapeHints.h>
51 #include <Inventor/actions/SoSearchAction.h>
52
53 #include "FindNormals.h"
54 #include "Edges.h"
55
56 FindNormals::FindNormals()
57 {
58 defaultOrientation = Face::UNKNOWN;
59 creaseAngle = M_PI/6;
60 verbose = FALSE;
61 }
62
63 FindNormals::~FindNormals()
64 {
65 }
66
67 void
68 FindNormals::AssumeOrientation(FaceOrientation o)
69 {
70 defaultOrientation = o;
71 }
72
73 void
74 FindNormals::apply(SoNode *root, int doVertexNormals)
75 {
76 //
77 // IndexedFaceSets:
78 // First, search for them:
79 //
80 if (verbose)
81 fprintf(stderr, "FindNormals: searching for IndexedFaceSets.\n");
82 SoSearchAction sa;
83 sa.setType(SoIndexedFaceSet::getClassTypeId());
84 sa.setInterest(SoSearchAction::ALL);
85 sa.apply(root);
86 SoPathList &paths = sa.getPaths();
87 if (verbose) fprintf(stderr, "FindNormals: searching finished.\n");
88 for (int i=0; i < paths.getLength(); i++) {
89 if (verbose && !(i%50))
90 fprintf(stderr, "FindNormals: faceSet %d of %d\n",
91 i+1, paths.getLength());
92
93 SoGroup *parent = (SoGroup*)paths[i]->getNodeFromTail(1);
94
95 // disable notification, or we'll have n^2 time spent notifying
96 // the entire path list for every change.
97 #ifndef INVENTOR1
98 SbBool pNot = parent->enableNotify(FALSE);
99 #else
100 parent->setNotification(FALSE);
101 #endif
102
103 doIndexedFaceSet(paths[i], doVertexNormals);
104
105 // restore notification
106 #ifndef INVENTOR1
107 parent->enableNotify(pNot);
108 #else
109 parent->setNotification(TRUE);
110 #endif
111 }
112 }
113
114 SoNode *
115 FindNormals::searchLastType(SoPath *p, SoType t)
116 {
117 SoSearchAction sa;
118 sa.setType(t);
119 sa.setInterest(SoSearchAction::LAST);
120 sa.apply(p);
121 SoPath *foundPath = sa.getPath();
122
123 if (foundPath != NULL)
124 return foundPath->getTail();
125
126 return NULL;
127 }
128
129 void
130 FindNormals::doIndexedFaceSet(SoPath *p, int doVertexNormals)
131 {
132 // If there is already a normal node, bail
133
134 if (searchLastType(p, SoNormal::getClassTypeId()) != NULL)
135 return;
136
137 //
138 // First, find Coordinates in path:
139 //
140 SoCoordinate3 *coords = (SoCoordinate3 *)
141 searchLastType(p, SoCoordinate3::getClassTypeId());
142
143 SoIndexedFaceSet *ifs = (SoIndexedFaceSet *)p->getTail();
144
145 assert(coords != NULL);
146
147 FaceList faces(coords->point.getValues(0), ifs);
148
149 if (defaultOrientation == Face::UNKNOWN)
150 {
151 faces.findOrientation();
152 }
153 else
154 {
155 for (int i = 0; i < faces.getLength(); i++)
156 {
157 faces[i]->orientation = defaultOrientation;
158 }
159 faces.correctOrientation();
160 }
161
162 SoNormal *n = new SoNormal;
163 SoNormalBinding *normb = new SoNormalBinding;
164
165 SoNode *tn = p->getNode(p->getLength()-2);
166 assert(tn->isOfType(SoGroup::getClassTypeId()));
167
168 SoGroup *g = (SoGroup *)tn;
169
170 int index = g->findChild(ifs);
171
172 assert(index != -1);
173
174 g->insertChild(normb, index);
175 g->insertChild(n, index);
176
177 faces.findShapeInfo();
178 if (faces.isSolid()) {
179 SoShapeHints *hints = new SoShapeHints;
180 hints->vertexOrdering = SoShapeHints::COUNTERCLOCKWISE;
181 hints->shapeType = SoShapeHints::SOLID;
182 g->insertChild(hints, index);
183 }
184
185 if (doVertexNormals)
186 {
187 faces.findVertexNormals(n, ifs, creaseAngle);
188 normb->value.setValue(SoNormalBinding::PER_VERTEX_INDEXED);
189 }
190 else
191 {
192 faces.findFacetNormals(n);
193 normb->value.setValue(SoNormalBinding::PER_FACE);
194 }
195 }