Debian Codegraph @

apps/tools/ivnorm/ivnorm.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 //______________________________________________________________________
39 //_____________ S I L I C O N G R A P H I C S I N C . ____________
40 //
41 // $Source: /oss/CVS/cvs/inventor/apps/tools/ivnorm/ivnorm.c++,v $
42 // $Revision: 1.3 $
43 // $Date: 2001/09/25 00:45:30 $
44 //
45 // ivnorm
46 //
47 // This is the mainline for the IRIS Inventor `ivnorm' program.
48 // This program reads an Inventor data file, finds all of the
49 // indexed face set nodes, generates normals for each, and writes the
50 // result in tact (it does not remove any properties or hierarchy)
51 // to a new file.
52 //
53 // Author(s) : Gavin Bell
54 //
55 // Notes:
56 //
57 //_____________ S I L I C O N G R A P H I C S I N C . ____________
58 //______________________________________________________________________
59
60 #include <cstdio>
61 #include <unistd.h>
62 #include <cmath>
63 #include <assert.h>
64 #include <cstdlib>
65
66 #include <Inventor/SoInteraction.h>
67 #include <Inventor/SoDB.h>
68 #include <Inventor/SoInput.h>
69 #include <Inventor/actions/SoWriteAction.h>
70
71 #include "FindNormals.h"
72
73 Face::Orientation orientation = Face::UNKNOWN;
74 float creaseAngle = M_PI/6.0; // 30 degrees
75 int findVNorms = 0;
76 int verbose = 0;
77 char *inFileName = "stdin";
78 char *outFileName = "stdout";
79
80 //-----------------------------------------------------------------------------
81 //
82 // My standard command-line parser for tools that can take two
83 // filenames or act as a filter.
84 //
85 //-----------------------------------------------------------------------------
86 static void
87 ParseCommandLine(int argc, char **argv, FILE **in, FILE **out)
88 {
89 int err; /* Flag: error in options? */
90 int c;
91 *in = stdin;
92 *out = stdout;
93 err = 0;
94
95 while ((c = getopt(argc, argv, "cCva:bVh")) != -1)
96 {
97 switch(c)
98 {
99 case 'c':
100 orientation = Face::CCW;
101 break;
102 case 'C':
103 orientation = Face::CW;
104 break;
105 case 'v':
106 findVNorms = TRUE;
107 break;
108 case 'a':
109 findVNorms = TRUE;
110 // convert from given arg (in degrees) to radians
111 creaseAngle = atof(optarg) * M_PI / 180.0;
112 break;
113 case 'V':
114 verbose = TRUE;
115 break;
116 case 'h': /* Help */
117 default:
118 err = 1;
119 break;
120 }
121 }
122 /* Handle optional filenames */
123 for (; optind < argc; optind++)
124 {
125 if (*in == stdin)
126 {
127 inFileName = argv[optind];
128 *in = fopen(inFileName, "r");
129 if (*in == NULL)
130 {
131 char buf[500];
132 sprintf(buf, "%s: %s", argv[0], inFileName);
133 perror(buf);
134 exit(99);
135 }
136 }
137 else if (*out == stdout)
138 {
139 outFileName = argv[optind];
140 *out = fopen(outFileName, "w");
141 if (*out == NULL)
142 {
143 char buf[500];
144 sprintf(buf, "%s: %s", argv[0], outFileName);
145 perror(buf);
146 exit(99);
147 }
148 }
149 else err = 1; /* Too many arguments */
150 }
151
152 /* If stdin is a terminal, spit out usage */
153 if (*in == stdin && isatty(fileno(stdin))) err = 1;
154
155 if (err)
156 {
157 (void)fprintf(stderr, "Usage: %s [options]", argv[0]);
158 (void)fprintf(stderr, " [infile] [outfile]\n");
159 (void)fprintf(stderr, "-c Assume counter-clockwise faces\n");
160 (void)fprintf(stderr, "-C Assume clockwise faces\n");
161 (void)fprintf(stderr, "-v Find vertex normals\n");
162 (void)fprintf(stderr, "-a angle Use angle (in degrees)"
163 " as crease angle\n");
164 (void)fprintf(stderr, "-V verbose trace\n");
165 (void)fprintf(stderr, "-h This message (help)\n");
166 exit(99);
167 }
168 }
169
170
171 int
172 main(int argc, char **argv)
173 {
174 FILE *fin;
175 FILE *fout;
176
177 SoInteraction::init();
178
179 ParseCommandLine(argc, argv, &fin, &fout);
180
181 SoNode *node = NULL;
182 SbBool ok;
183
184 SoInput in;
185 in.setFilePointer(fin);
186
187 SoOutput out;
188 out.setFilePointer(fout);
189 SoWriteAction wa(&out);
190
191 FindNormals normalFinder;
192 normalFinder.AssumeOrientation(orientation);
193 normalFinder.setCreaseAngle(creaseAngle);
194 normalFinder.setVerbose(verbose);
195
196 for (;;) {
197 if (verbose) fprintf(stderr, "%s: reading graph from '%s'.\n", argv[0], inFileName);
198 ok = SoDB::read(&in, node);
199 if (verbose) fprintf(stderr, "%s: finished reading graph from '%s'.\n", argv[0], inFileName);
200 if (!ok || !node)
201 break;
202
203 node->ref();
204 // Generate normals
205 normalFinder.apply(node, findVNorms);
206
207 // Write out the (modified) scene graph
208 if (verbose) fprintf(stderr, "%s: writing graph to '%s'.\n", argv[0], outFileName);
209 out.setBinary(in.isBinary());
210 wa.apply(node);
211 if (verbose) fprintf(stderr, "%s: finished writing graph to '%s'.\n", argv[0], outFileName);
212 }
213
214 if (!ok)
215 {
216 fprintf(stderr, "Bad data.\n");
217 return 1;
218 }
219
220 return 0;
221 }