ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/SHAPES/SHAPE.cpp
Revision: 1300
Committed: Thu Jun 24 19:06:47 2004 UTC (20 years ago) by gezelter
File size: 7839 byte(s)
Log Message:
Fixes for visualizer for amira

File Contents

# User Rev Content
1 gezelter 1290 #include "SHAPE.hpp"
2    
3     SHAPE::SHAPE(void) {
4     int i, j;
5    
6     mass = 0.0;
7     for (i=0; i<3; i++)
8     for (j=0; j<3; j++)
9     I[i][j] = 0.0;
10    
11     }
12    
13     SHAPE::~SHAPE(void) {
14     }
15    
16     void SHAPE::getI(double theI[3][3]) {
17     int i, j;
18    
19     for (i=0; i<3; i++)
20     for (j=0; j<3; j++)
21     theI[i][j] = I[i][j];
22     }
23    
24    
25     void SHAPE::readSHAPEfile(const char *fname) {
26    
27     int lineNum, nTokens;
28     char* eof_test;
29     char* foo;
30     char readLine[500];
31     SHFunc* shf;
32    
33     shapeFile = fopen(fname, "r");
34     if (shapeFile == NULL) {
35     printf("Could not open SHAPE file %s\n", fname);
36     exit(-1);
37     }
38 gezelter 1300
39 gezelter 1295 findBegin( "ShapeInfo" );
40    
41 gezelter 1290 eof_test = fgets( readLine, sizeof(readLine), shapeFile );
42 gezelter 1295
43 gezelter 1290 while( eof_test != NULL ){
44     // toss comment lines
45     if( readLine[0] != '!' && readLine[0] != '#' ){
46 gezelter 1295
47 gezelter 1300 if (isEndLine(readLine)) break;
48 gezelter 1290
49     nTokens = count_tokens(readLine, " ,;\t");
50     if (nTokens < 5) {
51 gezelter 1295 printf("Not enough data on shapeInfo line in SHAPE file.\n");
52 gezelter 1290 exit(-1);
53     }
54    
55     foo = strtok(readLine, " ,;\t");
56 gezelter 1300 setType(TrimSpaces(foo));
57 gezelter 1290 foo = strtok(NULL, " ,;\t");
58     mass = atof(foo);
59     foo = strtok(NULL, " ,;\t");
60     I[0][0] = atof(foo);
61     foo = strtok(NULL, " ,;\t");
62     I[1][1] = atof(foo);
63     foo = strtok(NULL, " ,;\t");
64     I[2][2] = atof(foo);
65     break;
66     }
67     eof_test = fgets( readLine, sizeof(readLine), shapeFile );
68     }
69    
70     findBegin( "ContactFunctions" );
71    
72 gezelter 1300 eof_test = fgets( readLine, sizeof(readLine), shapeFile );
73    
74 gezelter 1290 while( eof_test != NULL ){
75     // toss comment lines
76     if( readLine[0] != '!' && readLine[0] != '#' ){
77    
78 gezelter 1300 if (isEndLine(readLine)) break;
79 gezelter 1290
80 gezelter 1300 nTokens = count_tokens(readLine, " ,;\t");
81 gezelter 1290 if (nTokens != 4) {
82     printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n");
83     exit(-1);
84     }
85    
86     shf = new SHFunc();
87     foo = strtok(readLine, " ,;\t");
88     shf->setL(atoi(foo));
89     foo = strtok(NULL, " ,;\t");
90     shf->setM(atoi(foo));
91     foo = strtok(NULL, " ,;\t");
92     if (!strcasecmp(foo, "cos"))
93     shf->makeCosFunc();
94     else {
95     if (!strcasecmp(foo, "sin"))
96     shf->makeSinFunc();
97     else {
98     printf("unknown function type in shape file!");
99     exit(-1);
100     }
101     }
102     foo = strtok(NULL, " ,;\t");
103     shf->setCoefficient(atof(foo));
104     sigmaFuncs.push_back(shf);
105     }
106     eof_test = fgets( readLine, sizeof(readLine), shapeFile );
107     }
108    
109     findBegin( "RangeFunctions" );
110 gezelter 1300 eof_test = fgets( readLine, sizeof(readLine), shapeFile );
111 gezelter 1290
112     while( eof_test != NULL ){
113     // toss comment lines
114     if( readLine[0] != '!' && readLine[0] != '#' ){
115    
116 gezelter 1300 if (isEndLine(readLine)) break;
117 gezelter 1290
118     nTokens = count_tokens(readLine, " ,;\t");
119     if (nTokens != 4) {
120 gezelter 1300 printf("incorrect number of tokens on sFunc line in SHAPE file\n");
121 gezelter 1290 exit(-1);
122     }
123    
124     shf = new SHFunc();
125     foo = strtok(readLine, " ,;\t");
126     shf->setL(atoi(foo));
127     foo = strtok(NULL, " ,;\t");
128     shf->setM(atoi(foo));
129     foo = strtok(NULL, " ,;\t");
130     if (!strcasecmp(foo, "cos"))
131     shf->makeCosFunc();
132     else {
133     if (!strcasecmp(foo, "sin"))
134     shf->makeSinFunc();
135     else {
136     printf("unknown function type in shape file!");
137     exit(-1);
138     }
139     }
140     foo = strtok(NULL, " ,;\t");
141     shf->setCoefficient(atof(foo));
142     sFuncs.push_back(shf);
143     }
144     eof_test = fgets( readLine, sizeof(readLine), shapeFile );
145     }
146    
147     findBegin( "StrengthFunctions" );
148 gezelter 1300 eof_test = fgets( readLine, sizeof(readLine), shapeFile );
149 gezelter 1290
150     while( eof_test != NULL ){
151     // toss comment lines
152     if( readLine[0] != '!' && readLine[0] != '#' ){
153 gezelter 1300
154     if (isEndLine(readLine)) break;
155 gezelter 1290
156     nTokens = count_tokens(readLine, " ,;\t");
157     if (nTokens != 4) {
158 gezelter 1300 printf("incorrect number of tokens on epsFunc line in SHAPE file\n");
159 gezelter 1290 exit(-1);
160     }
161    
162     shf = new SHFunc();
163     foo = strtok(readLine, " ,;\t");
164     shf->setL(atoi(foo));
165     foo = strtok(NULL, " ,;\t");
166     shf->setM(atoi(foo));
167     foo = strtok(NULL, " ,;\t");
168     if (!strcasecmp(foo, "cos"))
169     shf->makeCosFunc();
170     else {
171     if (!strcasecmp(foo, "sin"))
172     shf->makeSinFunc();
173     else {
174     printf("unknown function type in shape file!");
175     exit(-1);
176     }
177     }
178     foo = strtok(NULL, " ,;\t");
179     shf->setCoefficient(atof(foo));
180     epsFuncs.push_back(shf);
181     }
182     eof_test = fgets( readLine, sizeof(readLine), shapeFile );
183     }
184     fclose(shapeFile);
185     }
186    
187     void SHAPE::findBegin( char* startText ){
188    
189     int foundText = 0;
190     int lineNum;
191     char* the_token;
192     char* eof_test;
193     char readLine[500];
194    
195     rewind( shapeFile );
196     lineNum = 0;
197    
198     eof_test = fgets( readLine, sizeof(readLine), shapeFile );
199     lineNum++;
200     if( eof_test == NULL ){
201     printf( "Error fast forwarding SHAPE file: file is empty.\n");
202     exit(-1);
203     }
204    
205     while( !foundText ){
206    
207     if( eof_test == NULL ){
208     printf("Error fast forwarding file at line %d: "
209     "file ended unexpectedly.\n", lineNum);
210     exit(-1);
211     }
212    
213     the_token = strtok( readLine, " ,;\t" );
214 gezelter 1300
215 gezelter 1290 if (!strcasecmp("begin", the_token)) {
216 gezelter 1300 the_token = TrimSpaces(strtok( NULL, " ,;\t" ));
217     foundText = !strcasecmp( startText, the_token );
218 gezelter 1290
219     }
220    
221     if( !foundText ){
222     eof_test = fgets( readLine, sizeof(readLine), shapeFile );
223     lineNum++;
224    
225    
226     if (eof_test == NULL) {
227     printf("Error fast forwarding file at line %d: "
228     "file ended unexpectedly.\n", lineNum);
229     exit(-1);
230     }
231     }
232     }
233     }
234    
235 gezelter 1300 int SHAPE::isEndLine(char *line) {
236     char *working_line;
237     char *foo;
238    
239     working_line = strdup(line);
240    
241     foo = strtok(working_line, " ,;\t");
242    
243     if (!strcasecmp(foo, "end")) return 1;
244    
245     return 0;
246     }
247    
248    
249 gezelter 1290 int SHAPE::count_tokens(char *line, char *delimiters) {
250 gezelter 1300 /* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */
251    
252 gezelter 1290 char *working_line; /* WORKING COPY OF LINE. */
253     int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */
254     char *strtok_ptr; /* POINTER FOR STRTOK. */
255 gezelter 1300
256 gezelter 1290 strtok_ptr= working_line= strdup(line);
257 gezelter 1300
258 gezelter 1290 ntokens=0;
259     while (strtok(strtok_ptr,delimiters)!=NULL)
260     {
261     ntokens++;
262     strtok_ptr=NULL;
263     }
264 gezelter 1300
265 gezelter 1290 free(working_line);
266     return(ntokens);
267     }
268 gezelter 1299
269    
270 gezelter 1300 /**
271     * Removes left and right spaces from a string
272     *
273     * @param str String to trim
274     *
275     * @return char* to the trimed string
276     */
277     char * SHAPE::TrimSpaces(char *str) {
278     size_t len;
279     char *right, *left;
280    
281     if (strlen(str) == 0) return(str);
282    
283     /* Trim whitespace from left side */
284     for (left = str; isspace(*left); left++);
285    
286     /* Trim whitespace from right side */
287     if ((len = strlen(left)))
288     {
289     right = left + (len - 1);
290    
291     while (isspace(*right))
292     {
293     *right = '\0';
294     right--;
295     }
296     }
297    
298     /* Only do the str copy if their was spaces to the left */
299     if (left != str)
300     strcpy(str, left);
301    
302     return (str);
303     }
304    
305    
306 gezelter 1299 double SHAPE::getSigmaAt(double costheta, double phi) {
307    
308     vector<SHFunc*>::iterator sigmaIter;
309     double sigma;
310    
311     sigma = 0.0;
312    
313     for(sigmaIter = sigmaFuncs.begin(); sigmaIter != sigmaFuncs.end();
314     ++sigmaIter)
315     sigma += (*sigmaIter)->getValueAt(costheta, phi);
316    
317     return sigma;
318     }
319    
320     double SHAPE::getSAt(double costheta, double phi) {
321    
322     vector<SHFunc*>::iterator sIter;
323     double s;
324    
325     s = 0.0;
326    
327     for(sIter = sFuncs.begin(); sIter != sFuncs.end(); ++sIter)
328     s += (*sIter)->getValueAt(costheta, phi);
329    
330     return s;
331     }
332    
333     double SHAPE::getEpsAt(double costheta, double phi) {
334    
335     vector<SHFunc*>::iterator epsIter;
336     double eps;
337    
338     eps = 0.0;
339    
340     for(epsIter = epsFuncs.begin(); epsIter != epsFuncs.end(); ++epsIter)
341     eps += (*epsIter)->getValueAt(costheta, phi);
342    
343     return eps;
344     }