1 |
#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 |
rewind(shapeFile); |
39 |
lineNum = 0; |
40 |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
41 |
lineNum++; |
42 |
if( eof_test == NULL ){ |
43 |
printf("Error in reading SHAPE from SHAPE file at line %d.\n", |
44 |
lineNum ); |
45 |
exit(-1); |
46 |
} |
47 |
// first find the shape name: |
48 |
while( eof_test != NULL ){ |
49 |
// toss comment lines |
50 |
if( readLine[0] != '!' && readLine[0] != '#' ){ |
51 |
|
52 |
nTokens = count_tokens(readLine, " ,;\t"); |
53 |
if (nTokens < 5) { |
54 |
printf("Not enough data on information line in SHAPE file.\n"); |
55 |
exit(-1); |
56 |
} |
57 |
|
58 |
foo = strtok(readLine, " ,;\t"); |
59 |
setType(foo); |
60 |
foo = strtok(NULL, " ,;\t"); |
61 |
mass = atof(foo); |
62 |
foo = strtok(NULL, " ,;\t"); |
63 |
I[0][0] = atof(foo); |
64 |
foo = strtok(NULL, " ,;\t"); |
65 |
I[1][1] = atof(foo); |
66 |
foo = strtok(NULL, " ,;\t"); |
67 |
I[2][2] = atof(foo); |
68 |
break; |
69 |
} |
70 |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
71 |
lineNum++; |
72 |
} |
73 |
|
74 |
findBegin( "ContactFunctions" ); |
75 |
|
76 |
while( eof_test != NULL ){ |
77 |
// toss comment lines |
78 |
if( readLine[0] != '!' && readLine[0] != '#' ){ |
79 |
|
80 |
foo = strtok(readLine, " ,;\t"); |
81 |
if (!strcasecmp(foo, "end")) break; |
82 |
|
83 |
nTokens = count_tokens(readLine, " ,;\t"); |
84 |
if (nTokens != 4) { |
85 |
printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n"); |
86 |
exit(-1); |
87 |
} |
88 |
|
89 |
shf = new SHFunc(); |
90 |
foo = strtok(readLine, " ,;\t"); |
91 |
shf->setL(atoi(foo)); |
92 |
foo = strtok(NULL, " ,;\t"); |
93 |
shf->setM(atoi(foo)); |
94 |
foo = strtok(NULL, " ,;\t"); |
95 |
if (!strcasecmp(foo, "cos")) |
96 |
shf->makeCosFunc(); |
97 |
else { |
98 |
if (!strcasecmp(foo, "sin")) |
99 |
shf->makeSinFunc(); |
100 |
else { |
101 |
printf("unknown function type in shape file!"); |
102 |
exit(-1); |
103 |
} |
104 |
} |
105 |
foo = strtok(NULL, " ,;\t"); |
106 |
shf->setCoefficient(atof(foo)); |
107 |
sigmaFuncs.push_back(shf); |
108 |
} |
109 |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
110 |
} |
111 |
|
112 |
findBegin( "RangeFunctions" ); |
113 |
|
114 |
while( eof_test != NULL ){ |
115 |
// toss comment lines |
116 |
if( readLine[0] != '!' && readLine[0] != '#' ){ |
117 |
|
118 |
foo = strtok(readLine, " ,;\t"); |
119 |
if (!strcasecmp(foo, "end")) break; |
120 |
|
121 |
nTokens = count_tokens(readLine, " ,;\t"); |
122 |
if (nTokens != 4) { |
123 |
printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n"); |
124 |
exit(-1); |
125 |
} |
126 |
|
127 |
shf = new SHFunc(); |
128 |
foo = strtok(readLine, " ,;\t"); |
129 |
shf->setL(atoi(foo)); |
130 |
foo = strtok(NULL, " ,;\t"); |
131 |
shf->setM(atoi(foo)); |
132 |
foo = strtok(NULL, " ,;\t"); |
133 |
if (!strcasecmp(foo, "cos")) |
134 |
shf->makeCosFunc(); |
135 |
else { |
136 |
if (!strcasecmp(foo, "sin")) |
137 |
shf->makeSinFunc(); |
138 |
else { |
139 |
printf("unknown function type in shape file!"); |
140 |
exit(-1); |
141 |
} |
142 |
} |
143 |
foo = strtok(NULL, " ,;\t"); |
144 |
shf->setCoefficient(atof(foo)); |
145 |
sFuncs.push_back(shf); |
146 |
} |
147 |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
148 |
} |
149 |
|
150 |
findBegin( "StrengthFunctions" ); |
151 |
|
152 |
while( eof_test != NULL ){ |
153 |
// toss comment lines |
154 |
if( readLine[0] != '!' && readLine[0] != '#' ){ |
155 |
|
156 |
foo = strtok(readLine, " ,;\t"); |
157 |
if (!strcasecmp(foo, "end")) break; |
158 |
|
159 |
nTokens = count_tokens(readLine, " ,;\t"); |
160 |
if (nTokens != 4) { |
161 |
printf("incorrect number of tokens on sigmaFunc line in SHAPE file\n"); |
162 |
exit(-1); |
163 |
} |
164 |
|
165 |
shf = new SHFunc(); |
166 |
foo = strtok(readLine, " ,;\t"); |
167 |
shf->setL(atoi(foo)); |
168 |
foo = strtok(NULL, " ,;\t"); |
169 |
shf->setM(atoi(foo)); |
170 |
foo = strtok(NULL, " ,;\t"); |
171 |
if (!strcasecmp(foo, "cos")) |
172 |
shf->makeCosFunc(); |
173 |
else { |
174 |
if (!strcasecmp(foo, "sin")) |
175 |
shf->makeSinFunc(); |
176 |
else { |
177 |
printf("unknown function type in shape file!"); |
178 |
exit(-1); |
179 |
} |
180 |
} |
181 |
foo = strtok(NULL, " ,;\t"); |
182 |
shf->setCoefficient(atof(foo)); |
183 |
epsFuncs.push_back(shf); |
184 |
} |
185 |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
186 |
} |
187 |
fclose(shapeFile); |
188 |
} |
189 |
|
190 |
void SHAPE::findBegin( char* startText ){ |
191 |
|
192 |
int foundText = 0; |
193 |
int lineNum; |
194 |
char* the_token; |
195 |
char* eof_test; |
196 |
char readLine[500]; |
197 |
|
198 |
rewind( shapeFile ); |
199 |
lineNum = 0; |
200 |
|
201 |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
202 |
lineNum++; |
203 |
if( eof_test == NULL ){ |
204 |
printf( "Error fast forwarding SHAPE file: file is empty.\n"); |
205 |
exit(-1); |
206 |
} |
207 |
|
208 |
while( !foundText ){ |
209 |
while( eof_test != NULL) { |
210 |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
211 |
lineNum++; |
212 |
} |
213 |
|
214 |
if( eof_test == NULL ){ |
215 |
printf("Error fast forwarding file at line %d: " |
216 |
"file ended unexpectedly.\n", lineNum); |
217 |
exit(-1); |
218 |
} |
219 |
|
220 |
the_token = strtok( readLine, " ,;\t" ); |
221 |
if (!strcasecmp("begin", the_token)) { |
222 |
|
223 |
the_token = strtok( NULL, " ,;\t" ); |
224 |
foundText = !strcmp( startText, the_token ); |
225 |
|
226 |
} |
227 |
|
228 |
if( !foundText ){ |
229 |
eof_test = fgets( readLine, sizeof(readLine), shapeFile ); |
230 |
lineNum++; |
231 |
|
232 |
|
233 |
if (eof_test == NULL) { |
234 |
printf("Error fast forwarding file at line %d: " |
235 |
"file ended unexpectedly.\n", lineNum); |
236 |
exit(-1); |
237 |
} |
238 |
} |
239 |
} |
240 |
} |
241 |
|
242 |
int SHAPE::count_tokens(char *line, char *delimiters) { |
243 |
/* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ |
244 |
|
245 |
char *working_line; /* WORKING COPY OF LINE. */ |
246 |
int ntokens; /* NUMBER OF TOKENS FOUND IN LINE. */ |
247 |
char *strtok_ptr; /* POINTER FOR STRTOK. */ |
248 |
|
249 |
strtok_ptr= working_line= strdup(line); |
250 |
|
251 |
ntokens=0; |
252 |
while (strtok(strtok_ptr,delimiters)!=NULL) |
253 |
{ |
254 |
ntokens++; |
255 |
strtok_ptr=NULL; |
256 |
} |
257 |
|
258 |
free(working_line); |
259 |
return(ntokens); |
260 |
} |