ConfigFile.cpp 12.6 KB
Newer Older
1
#include <algorithm>
2
#include <errno.h>
3
#include <string.h>
4
5
6
7

#include "ConfigFile.h"
#include "atoms.hpp"
#include "eprintf.h"
8
#include "polyhedron.h"
9
10
11
12
13
14
15
16
17

const char * PATH;
const char * SCREENSHOT;
int ISOS;
int TIMESTEPS;
float **isocolours; // [ISOS][4];
const char **plyfiles;
float **translations;
float userpos[3];
18
19
float scaling;
float markerscaling;
20
21
22
23

float BACKGROUND[3];
int* numAtoms; //[timesteps]
float **atoms; //[timesteps][numAtoms[i]*4] //xyzu, u=atom number
24
25
bool fixedAtoms; 

26
27
float atomScaling;
std::vector<float> *clonedAtoms;
28
29
std::vector<int> bonds;
int *numBonds;
30
bool displaybonds;
31
32
33
34
35
36
37
38
39
int numClonedAtoms;
int *basisvectorreps;

bool showTrajectories;
std::vector<int> atomtrajectories;
std::vector<std::vector<int>> atomtrajectoryrestarts;

float abc[3][3]; //basis vectors
bool has_abc = false;
40
41
42
bool displayunitcell;
float supercell[3];
int voxelSize[3];
43
44

int repetitions[3];
45
Solid *solid;
46

47
48
bool saveStereo;
int screenshotdownscaling;
49
bool hapticFeedback;
50

51
52
53
54
//markers such as hole positions and electron positions
float ** markers;
float ** markercolours;
float cubetrans[3];
55
56


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const char * loadConfigFileErrors[] =
{
	"All Ok",//0
	"file could not be opened", //-1
	"unrecognized parameter",//-2
	"values with no previous iso", //-3
	"colours with no previous iso",//-4
	"translations with no previous correct iso",//-5
	"Missing isos and xyzfile",//-6
	"missing values",//-7
	"timesteps from config file and from atom file do not match",//-8, unused, now minimum is used
	"isos parameter specified twice",//-9
	"Error reading unit cell parameters", //-10
	"Error reading repetitions",//-11
	"Non-periodic, but repetitions requested", //-12
	"No basis vectors, but repetitions requested", //-13
	"Error loading config file",// -14
74
	"Error reading atomglyph", //-15
75
	"Error reading token", //-16
76
77
	"markers with no previous correct timesteps parameter", //-17
	"markercolours with no previous correct timesteps parameter", //-18
78
	"Error reading atomcolour", // -19
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	"Error loading xyz file, add 100 to see the error",//<-100
	"Error loading cube file, add 100 to see the error",//<-200
	"Error loading json file, add 200 to see the error",//<-300
};

void updateTIMESTEPS (int timesteps)
{
if (TIMESTEPS==0)
	TIMESTEPS=timesteps;
else
	TIMESTEPS=std::min(TIMESTEPS, timesteps);
}

int readString(FILE *f, char *s)
{
94
	char s2[2048];
95
	int r, c;
96
	r = fscanf(f, "%2047s", s2);
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
	if (r!=1)
		return -1;
	if (s2[0]!='"') {
		strcpy(s, s2);
		return 0;
	} else {
		strcpy(s, s2+1); //skip "
		size_t l = strlen(s);
		if (s[l-1] == '"') {
			s[l-1] = '\0';
			return 0;
		}
		else {
			char *p = s2;
			do {
				c = fgetc(f);
				*p++ = c;
			} while (c != EOF && c != '"');
			*(p-1) = '\0';
			strcat(s, s2);
		}
	}
	return 0;
}

void fixFile(char * file)
{
#ifdef WIN32
	const char c='\\';
#else
	const char c='/';
#endif
while (*file!='\0') {
	if (*file=='/' || *file=='\\')
		*file=c;
	file++;
}
}

int loadConfigFile(const char * f)
{
	//default values
139
	//eprintf ("load config file start");
140
141
142
143
144
145
146
147
148
149
150
151
152
	bool nonperiodic=false;
	char base_url[1024]="http://enc-testing-nomad.esc.rzg.mpg.de/v1.0/materials/";
	char material[1024]="";
	BACKGROUND[0] = 0.95f;
	BACKGROUND[1] = 0.95f;
	BACKGROUND[2] = 0.95f;
	SCREENSHOT="C:\\temp\\frame";
	ISOS = 0;
	TIMESTEPS=0;
	PATH=strdup("");
	numAtoms=0;
	atomScaling=1;
	clonedAtoms=0;
153
	fixedAtoms=false;
154
	bonds.clear();
155
	displaybonds=false;
156
157
158
	showTrajectories = false;
	basisvectorreps=0;
	numClonedAtoms=0;
159
	has_abc=false;
160
161
162
163
	for (int i=0;i<3;i++)
		repetitions[i]=1;
	for (int i=0;i<3;i++)
		userpos[i] = 0;
164
165
	for (int i=0;i<3;i++)
		supercell[i] = 1;
166
	solid=0;
167
	char *token=0;
168
169
170
171
172
173
174
175
176
177
	markers=nullptr;
	markercolours=nullptr;
	displayunitcell=false;
	scaling =1;
	markerscaling=0.8;
	for (int i=0;i<3;i++)
		cubetrans[i]=0;
	translations=nullptr;
	for (int i=0;i<3;i++)
		voxelSize[i]=-1;
178
179
	saveStereo=false;
	screenshotdownscaling=1;
180
	hapticFeedback=false;
181
182
183
184
185
	//
	FILE *F = fopen(f, "r");
	if (F == 0)
	{
		eprintf( "Could not open config file %s\n", f);
186
		eprintf("Error: %d", errno);
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
		return -1;
	}
	char s[100];
	int r;
	while (!feof(F)) {
		r=fscanf(F, "%99s", s);
		if (r <= 0)
			continue;
		if (s[0] == '#') {//comment
			discardline(F);
			continue;
		}
		if (!strcmp(s, "timesteps")) {
			int timesteps;
			r = fscanf(F, "%d", &timesteps);
			if (TIMESTEPS==0)
				TIMESTEPS=timesteps;
			else
				TIMESTEPS=std::min(TIMESTEPS, timesteps);
		}
		else if (!strcmp(s, "isos")) {
			if (ISOS!=0) 
				return -9;
			r = fscanf(F, "%d", &ISOS);
			plyfiles = new const char*[ISOS];
			isocolours = new float*[ISOS];
			translations = new float*[ISOS];
			for (int i = 0; i < ISOS; i++) {
				plyfiles[i] = strdup("");
				isocolours[i] = new float[4];
				translations[i] = new float[3];
				//default values
				for (int j = 0; j < 4; j++)
					isocolours[i][j] = 1.f;
				for (int j = 0; j < 3; j++)
					translations[i][j] = 0.f;
			}
		}
		else if (!strcmp(s, "values")) {
			if (ISOS == 0) {
				eprintf( "values with no previous correct isos parameter\n");
				fclose(F);
				return -3;
			}
			for (int i = 0; i < ISOS; i++) {
				r=readString(F, s);
				if (r!=0)
					return -14;
				free ((void*)(plyfiles[i]));
				plyfiles[i] = strdup(s);
			}

		}
		else if (!strcmp(s, "colours")) {
			if (ISOS == 0) {
				eprintf( "colours with no previous correct isos parameter\n");
				return -4;
			}
			for (int i = 0; i < ISOS; i++) {
				for (int j = 0; j < 4; j++)
					r = fscanf(F, "%f", &(isocolours[i][j]));
				//if (r==0)
			}
		}
		else if (!strcmp(s, "path")) {
			r=readString(F, s);
			if (r!=0)
				return -14;			
			free((void*)PATH);
			PATH = strdup(s);
		}
		else if (!strcmp(s, "background")) {
			for (int i = 0; i < 3; i++)
				r = fscanf(F, "%f", BACKGROUND + i);
		}
		else if (!strcmp(s, "translations")) {
			if (ISOS == 0) {
				eprintf( "translations with no previous correct isos parameter\n");
				fclose(F);
				return -5;
			}
			for (int i=0;i<ISOS;i++)
				for (int j = 0; j < 3; j++) {
					r = fscanf(F, "%f", &(translations[i][j]));
				}
		}
		else if (!strcmp(s, "userpos")) {
			for (int j = 0; j < 3; j++) {
				r = fscanf(F, "%f", &(userpos[j]));
			}
		}
		else if (!strcmp(s, "screenshot")) {
			r=readString(F, s);
			if (r!=0)
				return -14;
			SCREENSHOT = strdup(s);
		}
		else if (!strcmp(s, "xyzfile")||!strcmp(s, "atomfile")) {
			r=readString(F, s);
			if (r!=0)
				return -14;
			int timesteps;
			char file[256];
			sprintf (file, "%s%s", PATH, s);
			fixFile(file);
			int e;
			e=readAtomsXYZ(file, &numAtoms, &timesteps, &atoms);
			if (e<0)
				return e-100;
			updateTIMESTEPS (timesteps);
297
			//eprintf ("After read of xyzfile, numatoms 0 =%d", numAtoms[0]);
298
299
300
301
302
		}
		else if (!strcmp(s, "cubefile")) {
			r=readString(F, s);
			if (r!=0)
				return -14;
303
			int timesteps=TIMESTEPS;
304
305
306
307
308
309
310
311
312
313
314
315
316
			char file[256];
			sprintf(file, "%s%s", PATH, s);
			fixFile(file);
			int e;
			e = readAtomsCube(file, &numAtoms, &timesteps, &atoms);
			if (e<0)
				return e - 200;
			updateTIMESTEPS (timesteps);
			//	return -8;
		}
		else if (!strcmp(s, "atomscaling")) {
			r = fscanf(F, "%f", &atomScaling);
		}
317
318
319
		else if (!strcmp(s, "scaling")) {
			r = fscanf(F, "%f", &scaling);
		}
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
		else if (!strcmp(s, "abc")) {
			for (int i=0;i<3;i++)
				for (int j=0;j<3;j++) {
					r = fscanf(F, "%f", &(abc[i][j]));
					if (r!=1)
						return -10;
				}
			has_abc = true;
		}
		else if (!strcmp(s, "json")) {
			r=readString(F, s);
			if (r!=0)
				return -14;
			char file[256];
			sprintf(file, "%s%s", PATH, s);
			fixFile(file);
			int e;
			int timesteps;
			//rgh fixme, we know only one
			e = readAtomsJson (file, &numAtoms, &timesteps, &atoms, abc, &clonedAtoms);
			if (e<0)
				return e-300;
			numClonedAtoms=clonedAtoms[0].size()/4;
			has_abc=true;
			updateTIMESTEPS (timesteps);
		} 
		else if (!strcmp(s, "baseurl")) {
			r=readString (F, base_url);
			if (r!=0)
				return -14;
		} 
		else if (!strcmp(s, "jsonurl")) {
			int e;
			int timesteps;
			r=readString (F, material);
			if (r!=0)
				return -14;
			char url[2048];
			sprintf (url, "%s%s", base_url, material);
			//rgh fixme, we know only one
360
			//eprintf ("load config file start, before readAtomsJsonURL");
361
			e = readAtomsJsonURL (url, &numAtoms, &timesteps, &atoms, abc, &clonedAtoms, token);
362
			//eprintf ("load config file start, after readAtomsJsonURL");
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
			if (e<0)
				return e-300;
			numClonedAtoms=clonedAtoms[0].size()/4;
			has_abc=true;
			updateTIMESTEPS (timesteps);
		}
		else if (!strcmp(s, "showtrajectory")) {
			showTrajectories = true;
			int atom;
			while (1 == (r = fscanf(F, "%d", &atom))) {
				atomtrajectories.push_back(atom - 1);
			}
			if (atomtrajectories.size() == 0) //empty showtrajectory
				showTrajectories = false;
		} else if (!strcmp(s, "nonperiodic")) {
			nonperiodic=true;
		} else if (!strcmp(s, "repetitions")) {
			for (int j=0;j<3;j++) {
					r = fscanf(F, "%d", repetitions+j);
					if (r!=1)
						return -11;
			}
385
386
387
388
389
390
391
392
393
394
395
396
		} else if (!strcmp(s, "atomglyph")) {
			r=fscanf (F, "%s", s);
			if (r==0)
				return -15;
			if (!strcmp(s, "icosahedron"))
				solid=new Solid(Solid::Type::Icosahedron);
			else if(!strcmp(s, "octahedron"))
				solid=new Solid(Solid::Type::Octahedron);
			else if(!strcmp(s, "tetrahedron"))
				solid=new Solid(Solid::Type::Tetrahedron);
			else
				return -15;
397
398
399
400
401
402
403
		} else if (!strcmp (s, "token")) {
			if (token)
				delete (token);
			token=new char [2048];
			r=readString (F, token);
			if (r!=0)
				return -16;
404
405
		} else if (!strcmp (s, "fixedatoms")) {
			fixedAtoms=true;
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
		} else if (!strcmp (s, "markers")) {
			if (TIMESTEPS == 0) {
				eprintf( "markers with no previous correct timesteps parameter\n");
				fclose(F);
				return -17;
			}
			markers=new float* [TIMESTEPS];
			for (int i=0;i<TIMESTEPS;i++) {
				markers[i]=new float[3];
				//in abc coordinates if they exist
				float tmp[3];
				for (int j = 0; j < 3; j++) {
					r = fscanf(F, "%f", &(tmp[j]));
				}
				if (has_abc) {
					//for (int s=0;s<3;s++) 
					//	tmp[s]-=translations[0][s]; //using translation of iso 0
					//check if coordinates are outside of cell
					for (int s=0;s<3;s++) {
						while (tmp[s]<-translations[0][s])
							tmp[s]+=supercell[2-s];
					}
					for (int s=0;s<3;s++) {
						markers[i][s]=tmp[2]*abc[0][s]+tmp[1]*abc[1][s]+tmp[0]*abc[2][s]; //hole positions seem to be in zyx
					}
				}
				else
					for (int s=0;s<3;s++)
						markers[i][s]=tmp[s];

			}
		} else if (!strcmp (s, "markercolours")) {
		if (TIMESTEPS == 0) {
			eprintf( "markecolours with no previous correct timesteps parameter\n");
			fclose(F);
			return -18;
		}
		markercolours=new float* [TIMESTEPS];
		for (int i=0;i<TIMESTEPS;i++) {
			markercolours[i]=new float[4];
			for (int j = 0; j < 4; j++) {
				r = fscanf(F, "%f", &(markercolours[i][j]));
			}
		}
		} else if (!strcmp (s, "displaybonds")) {
			displaybonds=true;
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
		} else if (!strcmp (s, "atomcolour")) {
			char atom [100];
			float rgb[3];
			r = fscanf(F, "%s %f", atom, rgb, rgb + 1, rgb + 2);
			if (r!=4) {
				eprintf ("Error loading atom colour");
				return -19;
			}
			int a=findAtom(atom);
			if (a==-1) {
				eprintf ("atomcolour, unknown atom type %s", atom);
				return -19;
			}
			for (int i=0;i<3;i++)
				atomColours[a][i]=rgb[i];
467
468
469
470
		} else if (!strcmp (s, "markerscaling")) {
			r = fscanf(F, "%f", &markerscaling);
		} else if (!strcmp (s, "displayunitcell")) {
			displayunitcell=true;
471
472
473
474
475
476
		} else if (!strcmp (s, "stereoscreenshot")) {
			saveStereo=true;
		} else if (!strcmp (s, "screenshotdownscaling")) {
			r= fscanf(F, "%d", &screenshotdownscaling);
			if (r<1)
				eprintf ("Error reading screenshotdownscaling value");
477
478
		} else if (!strcmp (s, "hapticfeedback")) {
			hapticFeedback=true;
479
480
		} else if (!strcmp (s, "supercell")) {
			r=fscanf (F, "%f %f %f", supercell, supercell+1, supercell+2);
481
482
483
		} else if (!strcmp (s, "\x0d")) { //discard windows newline (problem in Sebastian Kokott's phone (?!)
			continue;
		} else {
484
			eprintf( "Unrecognized parameter %s\n", s);
485
486
			for (int i=0;i<strlen(s);i++)
				eprintf ("<%d>", s[i]);
487
488
489
490
491
492
493
494
			fclose(F);
			return -2;
		}
	}

//verification and additional processing
	fclose(F);

495
	/*if (ISOS == 0 && numAtoms==0) {
496
497
		eprintf( "Missing isos and atomfile parameter\n");
		return -6;
498
	} */
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
	if (ISOS !=0 && plyfiles[0] == 0) {
		eprintf( "Missing values parameter\n");
		fclose(F);
		return -7;
	}
	if (nonperiodic) {
		numClonedAtoms=0;
		if (repetitions[0]!=1 ||repetitions[1]!=1 ||repetitions[2]!=1)
			return -12;
	}
	if (repetitions[0]!=1 ||repetitions[1]!=1 ||repetitions[2]!=1) {
		if (!has_abc)
			return (-13);
		numClonedAtoms=0;
	}

	if (showTrajectories && atomtrajectories[0]<0) {
		atomtrajectories.clear();
		for (int i=0;i<*numAtoms;i++)
			atomtrajectories.push_back(i);
	}
520
521
522
523
524

	//chemical bonds
	//if (numAtoms) {
	//}

525
	//eprintf ("Before returning, numatoms 0 =%d", numAtoms[0]);
526
	return 0;
527
}