-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
315 lines (253 loc) · 6.9 KB
/
Copy pathfunctions.cpp
File metadata and controls
315 lines (253 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "functions.h"
unsigned char isFile =0x8;
const char COMMENT_CHAR = '#';
const char FIELD_BEGIN_CHAR = '[';
const char FIELD_END_CHAR = ']';
void scanWrlFiles(vector<string> &files, char* target_directory)
{
int len;
struct dirent *pDirEntry;
DIR *pDir;
regex_t re;
int reti;
// Compile regular expression
reti = regcomp(&re, ".*[.]wrl$", 0);
if( reti )
{
fprintf(stderr, "Could not compile regex\n");
exit(1);
}
// pDir = opendir ("./");
pDir = opendir (target_directory);
if (pDir == NULL)
{
printf ("Cannot open directory '%s'\n", target_directory);
exit(1);
}
while ((pDirEntry = readdir(pDir)) != NULL)
{
if ( pDirEntry->d_type == isFile)
{
string filename = string(pDirEntry->d_name);
// Execute regular expression
reti = regexec(&re, filename.c_str(), 0, NULL, 0);
//printf ("filename = %s, reti = %d\n", filename.c_str(),reti);
if( !reti )
{
//printf ("[%s]\n", pDirEntry->d_name);
files.push_back( filename);
}
}
}
//printf ("------------------\n");
// Free compiled regular expression if you want to use the regex_t again
regfree(&re);
closedir (pDir);
}
void readLabel(ifstream &cfg_ptr, const char *label)
{
register int i;
register unsigned char c = '\0';
char line[100];
bool stop = false;
// Strip off blank lines and comment lines.
// int get() -> Extracts a character from the stream and returns its value (casted to an integer).
while (!stop && ((i = cfg_ptr.get()) != EOF))
{
c = (unsigned char) i;
if ((c == '\n') || (c == COMMENT_CHAR))
{
while ((c != '\n') && ((i = cfg_ptr.get()) != EOF))
{
c = (unsigned char) i;
}
}
else
{
stop = true;
}
}
if (!stop)
{
cerr << "Error: file EOF encountered before " << label <<
" found. - 1 \n";
exit(4);
}
cfg_ptr.putback(c); //rewind (put character back)
// Decrements the internal get pointer by one,
// and c becomes the character to be read
// at that position by the next input operation.
// Read in the strings until label is found or EOF encountered.
// The skipws flag (skip whitespaces) is set in standard streams on initialization
while ((cfg_ptr >> line)) // will automatically skip all the spaces and enters
{
cout<<"[unprocessed line] "<<line<<endl;
if ((line[0] != COMMENT_CHAR) && (line[0] != '\n'))
{
if (strncmp(line, label, strlen(label)) == 0)
{
printf("found %s \n", label);
return;
}
}
c = '\0';
// if '\n' or '#' are not the first character of a line
while ((c != '\n') && ((i = cfg_ptr.get()) != EOF))
{
c = (unsigned char) i;
}
}
cerr << "Error: file EOF encountered before " << label
<< " found. - 2\n";
exit(4);
}
void convertWrlToXan(string filename_wrl, Matrix3f Rot)
{
// open a .wrl file and grab model information
filename_wrl = "./huboplus/huboplus/models/"+filename_wrl;
ifstream fptr( filename_wrl.c_str() );
if (!fptr)
{
cerr << "Unable to open wrl file: "<<filename_wrl << endl;
exit(7);
}
//typedef vector<float> Point;
typedef vector<int> Index;
vector<Vector3f> points;
vector<Index> indexVec;
float ambIntensity;
float shine;
float scolor[3] = {0.0, 0.0, 0.0};
float dcolor[3] = {0.0, 0.0, 0.0};
readLabel(fptr, "ambientIntensity");
fptr >> ambIntensity;
readLabel(fptr, "diffuseColor");
fptr >> dcolor[0] >> dcolor[1] >> dcolor[2];
readLabel(fptr, "specularColor");
fptr >> scolor[0] >> scolor[1] >> scolor[2];
readLabel(fptr, "shininess");
fptr >> shine;
readLabel(fptr, "point");
readLabel(fptr, "[");
Vector3f p(0,0,0);
char str[100];
char cc;
int i = 0;
int numPoints, numIdx;
bool KeepGoing = 1;
while (KeepGoing)
{
fptr >> str;
if (strncmp( str, "]", strlen("]")) == 0 )
{
printf("reach the end of point data block\n");
KeepGoing = false;
}
else
{
sscanf (str,"%f", &p[0]);
fptr >> p[1] >> p[2];
cc = fptr.get();
Vector3f p1 = Rot*p; // convert to DH compliant coordinates
points.push_back (p1);
//printf("p[%d] = [%f %f %f ]\n",i, p[0], p[1], p[2]);
i++;
}
}
numPoints = i;
readLabel(fptr, "coordIndex");
readLabel(fptr, "[");
KeepGoing = 1;
i = 0;
char str1[256];
char * str2, *str3;
fptr.getline(str1, 256); // finish last line (once)
//printf("-[%s]-\n",str1);
while (KeepGoing)
{
fptr.getline(str1, 256); // get a whole line, stop at '\n'
// Trim str2 leading space
str2 = str1;
int j = 0;
while (str1[j]==' '|| str1[j]=='\t')
{
str2++;
j++;
}
//printf("-[%s]-\n",str2);
if (strncmp( str2, "]", strlen("]")) == 0 )
{
printf("reach the end of coordinate index data block\n");
KeepGoing = false;
}
if (KeepGoing == 1)
{
Index idx;
int temp;
str3 = strtok(str2, ",");
sscanf (str3,"%d", &temp); //printf("%d\n",temp);
idx.push_back(temp);
while (str3 != NULL)
{
str3 = strtok (NULL, ",");
if (str3 != NULL)
{
sscanf (str3,"%d", &temp); //printf ("%d\n",temp);
idx.push_back(temp);
}
}
indexVec.push_back (idx);
}
}
numIdx = indexVec.size();
fptr.close();
// reformat model information, write to .xan file
// Note:
// A good set of settings for a light source would be to set the Diffuse and Specular
// components to the colour of the light source, and the Ambient to the same colour
// - but at MUCH reduced intensity, 10% to 40% seems reasonable in most cases.
ofstream ofs;
size_t found;
string fn = filename_wrl;
found = fn.find(".wrl");
fn.erase(found, 4 );
fn += ".xan";
string filename_xan = fn;
ofs.open(filename_xan.c_str(),ios::out|ios::trunc);
if( !ofs.is_open())
{
cerr<<"xan File not open! - Error: "<< filename_xan <<endl<<endl;
}
else
{
ofs.setf(ios::fixed,ios::floatfield);
ofs<<setw(12)<< double(0.000) <<setw(12)<< 0.000 <<setw(12)<< 0.000<<endl; // emission
ofs<<setw(12)<< ambIntensity*dcolor[0]<<setw(12)<<ambIntensity*dcolor[1]<<setw(12)<<ambIntensity*dcolor[2]<<endl; // ambient
ofs<<setw(12)<<dcolor[0]<<setw(12)<<dcolor[1]<<setw(12)<<dcolor[2]<<endl; // diffuse
ofs<<setw(12)<<scolor[0]<<setw(12)<<scolor[1]<<setw(12)<<scolor[2]<<endl; // specular
ofs<<setw(12)<<shine<<endl; // shininess
ofs<<setw(12)<<1.0<<endl; // alpha
ofs<<setw(12)<<1.0<<setw(12)<<1.0<<setw(12)<<1.0<<endl<<endl; // scale
ofs<<setw(12)<< points.size() <<setw(12)<<indexVec.size()<<endl<<endl; // #_of_verteces #_of_faces
for( int u = 0; u<points.size();u++) // verteces
{
ofs<<setw(15)<<points[u][0];
ofs<<setw(15)<<points[u][1];
ofs<<setw(15)<<points[u][2]<<endl;
}
ofs<<endl;
for( int u = 0; u<indexVec.size();u++) // verteces
{
ofs<<setw(2)<<indexVec[u].size() - 1;
}
ofs<<endl<<endl;
for( int u = 0; u<indexVec.size();u++) // verteces
{
ofs<<setw(15)<<indexVec[u][0];
ofs<<setw(15)<<indexVec[u][1];
ofs<<setw(15)<<indexVec[u][2]<<endl;
}
ofs.close();
}
cout<<"-converted "<<filename_wrl<<" to "<<filename_xan<<endl;
}