ConfigFile.cpp 20.4 KB
Newer Older
1
/*
2
# Copyright 2016-2018 Ruben Jesus Garcia Hernandez
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 #
 #     http://www.apache.org/licenses/LICENSE-2.0
 #
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
*/


18
#include <algorithm>
19
#include <errno.h>
20
#include <string.h>
lrz's avatar
lrz committed
21
#include <stdlib.h>
22
23
24
25

#include "ConfigFile.h"
#include "atoms.hpp"
#include "eprintf.h"
26
#include "polyhedron.h"
27

28
29
char * PATH;
char * SCREENSHOT;
30
31
32
33
34
int ISOS;
float **isocolours; // [ISOS][4];
const char **plyfiles;
float **translations;
float userpos[3];
35
36
float scaling;
float markerscaling;
37
38

float BACKGROUND[3];
39

40
41
float atomScaling;
std::vector<float> *clonedAtoms;
42
43
std::vector<int> bonds;
int *numBonds;
44
bool displaybonds;
45
46
47
float bondscolours[4];
float atomtrajectorycolour[4];

48
49
50
51
52
53
54
int numClonedAtoms;
int *basisvectorreps;

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

55
bool displayunitcell;
56

57
58
float unitcellcolour[4];
float supercellcolour[4];
59

60
61
float infolinecolour[4];

62
int repetitions[3];
63
Solid *solid;
64

65
66
bool saveStereo;
int screenshotdownscaling;
67
bool hapticFeedback;
68
bool showcontrollers;
69
70
71
72
bool gazenavigation;
int transparencyquality;
float nearclip, farclip;

73
74
75
//markers such as hole positions and electron positions
float ** markers;
float ** markercolours;
76

77
78
float animationspeed;
float movementspeed;
79
int sidebuttontimestep; 
80

81
82
menubutton_t menubutton;

83
84
std::vector<information> info;

85
86
87
88
int secret;
const char * server;
int port;

89
90
91
const char * loadConfigFileErrors[] =
{
	"All Ok",//0
92
93
94
95
96
97
98
99
100
	"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
	"Isos present but no timestep value given",//-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
101
102
103
104
105
	"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
106
	"Error reading atomglyph", //-15
107
	"Error reading token", //-16
108
109
	"Markers with no previous correct timesteps parameter", //-17
	"Markercolours with no previous correct timesteps parameter", //-18
110
	"Error reading atomcolour", // -19
111
	"Error reading newatom", //-20
112
	"Error loading xyz file, add 100 to see the error",//<-100
113
114
115
	"Error loading cube file, add 200 to see the error",//<-200
	"Error loading encyclopedia json file, add 300 to see the error",//<-300
	"Error loading analytics json file, add 400 to see the error",//<-400
116
117
118
};

void updateTIMESTEPS (int timesteps)
119
{ 
120
121
122
123
124
125
126
127
if (TIMESTEPS==0)
	TIMESTEPS=timesteps;
else
	TIMESTEPS=std::min(TIMESTEPS, timesteps);
}

int readString(FILE *f, char *s)
{
128
	char s2[2048];
129
	int r, c;
130
	r = fscanf(f, "%2047s", s2);
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
	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++;
}
}

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
void cleanConfig()
{
	for (int i = 0; i < ISOS; i++) {
		delete[] isocolours[i];
		delete[] translations[i];
	}
	delete[] isocolours;
	isocolours=nullptr;
	delete[] translations;
	translations=nullptr;
	if (plyfiles) {
		for (int i=0;i<ISOS;i++)
			free ((void*)(plyfiles[i])); //strdup
		delete[] plyfiles;
	}
	plyfiles=nullptr;
	free(PATH);
	PATH=nullptr;
	atomtrajectoryrestarts.clear();
	free(SCREENSHOT);
	SCREENSHOT=nullptr;

	if (markers) {
		for (int i=0;i<TIMESTEPS;i++) {
			delete[] markers[i];
			delete[] markercolours[i];
		}
		delete[] markers;
		delete[] markercolours;
		markers=nullptr;
		markercolours=nullptr;
	}
	for (int i=0;i<info.size();i++) {
		free(info[i].filename);
	}
	info.clear();

	if (numAtoms) {
		for (int i=0;i<getAtomTimesteps();i++) {
			delete[] atoms[i];
		}
		delete[] atoms;
		delete[] numAtoms;
		numAtoms=nullptr;
		atoms=nullptr;
	}
}

218
void initState()
219
{
220
	BACKGROUND[0] = 0.95f;
221
222
	BACKGROUND[1] = 0.95f;
	BACKGROUND[2] = 0.95f;
223
	SCREENSHOT=strdup("C:\\temp\\frame");
224
225
226
	ISOS = 0;
	TIMESTEPS=0;
	PATH=strdup("");
227
	numAtoms=nullptr;
228
229
	atomScaling=1;
	clonedAtoms=0;
230
	fixedAtoms=false;
231
	bonds.clear();
232
	displaybonds=false;
233
234
235
	showTrajectories = false;
	basisvectorreps=0;
	numClonedAtoms=0;
236
	has_abc=false;
237
238
239
240
	for (int i=0;i<3;i++)
		repetitions[i]=1;
	for (int i=0;i<3;i++)
		userpos[i] = 0;
241
242
	for (int i=0;i<3;i++)
		supercell[i] = 1;
243
	solid=0;
244

245
246
247
248
249
250
251
252
253
254
	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;
255
256
	saveStereo=false;
	screenshotdownscaling=1;
257
	hapticFeedback=false;
258
	showcontrollers=false;
259
	gazenavigation=false;
260
	inv_abc_init=false;
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282

	transparencyquality=12;
	nearclip=0.2f;
	farclip=200.f;

	for (int i=0;i<4;i++)
		unitcellcolour[i]=1.0f;
	
	supercellcolour[0]=0.0f;
	supercellcolour[1]=1.0f;
	supercellcolour[2]=1.0f;
	supercellcolour[3]=1.0f;

	bondscolours[0]=0.5f;
	bondscolours[1]=0.5f;
	bondscolours[2]=1.0f;
	bondscolours[3]=1.0f;

	atomtrajectorycolour[0]=1.0f;
	atomtrajectorycolour[1]=0.0f;
	atomtrajectorycolour[2]=0.0f;
	atomtrajectorycolour[3]=1.0f;
283
284
285

	animationspeed=1.0f;
	movementspeed=1.0f;
286
	sidebuttontimestep=-1;
287
288
289
290
291

	infolinecolour[0] = 1.0f;
	infolinecolour[1] = 1.0f;
	infolinecolour[2] = 0.0f;
	infolinecolour[3] = 1.0f;
292
293
294
295

	menubutton = Record;

	bondscaling = 0.7f;
296
	bondThickness = 1.0f;
297
298
299
300

	secret=0;
	server=nullptr;
	port=-1;
301
302
303
304
305
306
307
308
309
310
311
312
}

int loadConfigFile(const char * f)
{
	//default values
	//eprintf ("load config file start");
	bool nonperiodic=false;
	char base_url[1024]="http://enc-testing-nomad.esc.rzg.mpg.de/v1.0/materials/";
	char material[1024]="";
	initState();
	char *token=0;

313
314
315
316
	FILE *F = fopen(f, "r");
	if (F == 0)
	{
		eprintf( "Could not open config file %s\n", f);
317
		eprintf("Error: %d", errno);
318
319
320
321
322
323
324
325
		return -1;
	}
	char s[100];
	int r;
	while (!feof(F)) {
		r=fscanf(F, "%99s", s);
		if (r <= 0)
			continue;
326
327
		if ((unsigned char)s[0]==0xef && (unsigned char)s[1]==0xbb && (unsigned char)s[2]==0xbf) //utf bom
			strcpy (s, s+3);
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
		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;
415
			free (SCREENSHOT);
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
			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);
431
			//eprintf ("After read of xyzfile, numatoms 0 =%d", numAtoms[0]);
432
433
434
435
436
		}
		else if (!strcmp(s, "cubefile")) {
			r=readString(F, s);
			if (r!=0)
				return -14;
437
			int timesteps=TIMESTEPS;
438
439
440
441
442
443
444
445
446
447
448
449
450
			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);
		}
451
452
453
		else if (!strcmp(s, "scaling")) {
			r = fscanf(F, "%f", &scaling);
		}
454
		else if (!strcmp(s, "abc") || !strcmp (s, "unitcell") ) {
455
456
457
458
459
460
461
462
			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;
		}
463
		else if (!strcmp(s, "json") || !strcmp (s, "encyclopediajson")) {
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
			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);
479
480
481
482
483
484
485
486
487
488
489
490
491
492
		}
		else if (!strcmp(s, "analyticsjson")) {
			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 = readAtomsAnalyticsJson(file, &numAtoms, &timesteps, &atoms, abc, &clonedAtoms);
			if (e<0)
				return e - 400;
493
494
495
496
497
498

			if (has_abc)
				numClonedAtoms = clonedAtoms[0].size() / 4;
			else
				numClonedAtoms=0;

499
500
			updateTIMESTEPS(timesteps);
		}
501
502
503
504
505
506
507
508
509
510
511
512
513
514
		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
515
			//eprintf ("load config file start, before readAtomsJsonURL");
516
			e = readAtomsJsonURL (url, &numAtoms, &timesteps, &atoms, abc, &clonedAtoms, token);
517
			//eprintf ("load config file start, after readAtomsJsonURL");
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
			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;
			}
540
		} else if (!strcmp(s, "atomglyph")) {
541
			r=fscanf (F, "%99s", s);
542
543
544
545
546
547
548
549
			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);
550
551
552
553
554
			else if(!strcmp(s, "sphere")) {
				int subd;
				r=fscanf (F, "%d", &subd);
				solid=new Solid(Solid::Type::Sphere, subd);
			}
555
556
			else
				return -15;
557
558
559
560
561
562
563
		} else if (!strcmp (s, "token")) {
			if (token)
				delete (token);
			token=new char [2048];
			r=readString (F, token);
			if (r!=0)
				return -16;
564
565
		} else if (!strcmp (s, "fixedatoms")) {
			fixedAtoms=true;
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
		} 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
584
585
586
587
588
					if (translations) {
						for (int s=0;s<3;s++) {
							while (tmp[s]<-translations[0][s])
								tmp[s]+=supercell[2-s];
						}
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
					}
					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;
614
615
616
		} else if (!strcmp (s, "atomcolour")) {
			char atom [100];
			float rgb[3];
617
			r = fscanf(F, "%99s %f %f %f", atom, rgb, rgb + 1, rgb + 2);
618
619
620
621
622
623
624
625
626
627
628
			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];
629
630
631
632
		} else if (!strcmp (s, "newatom")) {
			char atom [100];
			float rgb[3];
			float size;
633
			r = fscanf(F, "%99s %f %f %f %f", atom, rgb, rgb + 1, rgb + 2, &size);
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
			if (r!=5) {
				eprintf ("Error loading newatom");
				return -20;
			}
			int a=findAtom(atom);
			if (a!=-1) {
				if (a<atomsInPeriodicTable) {
					for (int i=0;i<3;i++)
						atomColours[a][i]=rgb[i];
					atomColours[a][3]=size;
				} else {
					for (int i=0;i<3;i++)
						extraAtomData[a-atomsInPeriodicTable][i]=rgb[i];
					extraAtomData[a-atomsInPeriodicTable][3]=size;
				}
			} else { //new
				extraAtomNames.push_back(strdup(atom));
				extraAtomData.push_back(new float[4]);
				float *e=extraAtomData.back();
				for (int i=0;i<3;i++)
					e[i]=rgb[i];
				e[3]=size;
			}
657
658
659
660
		} else if (!strcmp (s, "markerscaling")) {
			r = fscanf(F, "%f", &markerscaling);
		} else if (!strcmp (s, "displayunitcell")) {
			displayunitcell=true;
661
662
663
664
665
666
		} 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");
667
668
		} else if (!strcmp (s, "hapticfeedback")) {
			hapticFeedback=true;
669
670
		} else if (!strcmp (s, "supercell")) {
			r=fscanf (F, "%f %f %f", supercell, supercell+1, supercell+2);
671
672
			if (r<3)
				eprintf ("Error reading supercell value");
673
674
		} else if (!strcmp (s, "showcontrollers")) { 
			showcontrollers=true;
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
		} else if (!strcmp (s, "gazenavigation")) { 
			gazenavigation=true;
		} else if (!strcmp (s, "transparencyquality")) {
			r=fscanf (F, "%d", &transparencyquality);
			if (r<1)
				eprintf ("Error reading transparencyquality value");
		} else if (!strcmp (s, "clippingplanes")) {
			r=fscanf (F, "%f %f", &nearclip, &farclip);
			if (r<2)
				eprintf ("Error reading clippingplanes values");
		}else if (!strcmp (s, "bondscolour")) {
			r=fscanf (F, "%f %f %f", bondscolours, bondscolours+1, bondscolours+2);
			if (r<3)
				eprintf ("Error reading bondscolour value");
		}else if (!strcmp (s, "unitcellcolour")) {
			r=fscanf (F, "%f %f %f", unitcellcolour, unitcellcolour+1, unitcellcolour+2);
			if (r<3)
				eprintf ("Error reading unitcellcolour value");
		}else if (!strcmp (s, "supercellcolour")) {
			r=fscanf (F, "%f %f %f", supercellcolour, supercellcolour+1, supercellcolour+2);
			if (r<3)
				eprintf ("Error reading supercellcolour value");
		}else if (!strcmp (s, "atomtrajectorycolour")) {
			r=fscanf (F, "%f %f %f", atomtrajectorycolour, atomtrajectorycolour+1, atomtrajectorycolour+2);
			if (r<3)
				eprintf ("Error reading atomtrajectorycolour value");
701
702
703
704
		}else if (!strcmp (s, "infolinecolour")) {
			r=fscanf (F, "%f %f %f", infolinecolour, infolinecolour+1, infolinecolour+2);
			if (r<3)
				eprintf ("Error reading atomtrajectorycolour value");
705
706
707
708
709
710
		}
		else if (!strcmp(s, "animationspeed")) {
			r = fscanf(F, "%f", &animationspeed);
			if (r < 1)
				eprintf("Error reading animationspeed");
		}
711
		else if (!strcmp(s, "bondscaling")) {
712
			r = fscanf(F, "%f", &bondscaling);
713
			if (r<1)
714
715
				eprintf("Error reading bondscaling");
			bondscaling = sqrt(bondscaling);
716
717
718
719
720
721
		} else if (!strcmp(s, "bondthickness")) {
			r = fscanf(F, "%f", &bondThickness);
			if (r<1)
				eprintf("Error reading bondthickness");
		}
		else if (!strcmp(s, "menubutton")) {
722
			r = fscanf(F, "%99s", s);
723
724
725
726
727
728
			if (!strcmp(s, "Record"))
				menubutton = Record;
			else if (!strcmp(s, "Infobox"))
				menubutton = Infobox;
			else if (!strcmp (s, "Nothing"))
				menubutton = Nothing;
729
			else eprintf ("Unknown menubutton parameter %s\n", s);
730
731
732
733
		} else if (!strcmp (s, "movementspeed")) {
			r=fscanf (F, "%f", &movementspeed);
			if (r<1)
				eprintf ("Error reading movementspeed");
734
735
736
737
		} else if (!strcmp (s, "sidebuttontimestep")) {
			r=fscanf (F, "%d", &sidebuttontimestep);
			if (r<1)
				eprintf ("Error reading sidebuttontimestep");		
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
#ifdef WIN32
		} else if (!strcmp (s, "info")) {
			information i;
			r=fscanf (F, "%f %f %f %f %d", i.pos, i.pos+1, i.pos+2, &(i.size), &(i.atom));
			if (r<5)
				eprintf ("Error reading info");
			r=readString(F, s);
			if (r!=0)
				eprintf ("Error reading info");
			char file[256];
			sprintf (file, "%s%s", PATH, s);
			i.filename=strdup(file);
			//i.tex=LoadPNG(i.filename); //opengl not initialized yet
			info.push_back(i);
#endif
753
754
		} else if (!strcmp (s, "\x0d")) { //discard windows newline (problem in Sebastian Kokott's phone (?!)
			continue;
755
756
757
758
759
760
761
762
763
		} else if (!strcmp (s, "server")) { //multiuser support
			int r;
			if (server)
				delete(server);
			server=new char[100];
			r=fscanf (F, "%99s %d %d", server, &port, &secret);
			if (r<3) {
				eprintf ("Error reading server paramters");
			}
764
		} else {
765
			eprintf( "Unrecognized parameter %s\n", s);
766
767
			for (int i=0;i<strlen(s);i++)
				eprintf ("<%d>", s[i]);
768
769
770
771
772
773
774
775
			fclose(F);
			return -2;
		}
	}

//verification and additional processing
	fclose(F);

776
777
	if (ISOS != 0 && TIMESTEPS == 0) {
		eprintf("Isos requested, but no timesteps indicated\n");
778
		return -6;
779
	}
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
	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);
	}
801

802
803
	if (markers && !markercolours) {
		markercolours=new float* [TIMESTEPS];
804
805
806
807
808
809
810
811
812
813
		float mc [4];
		if (fabs (BACKGROUND[0]-0.5) < 0.1 && fabs (BACKGROUND[1]-0.5) < 0.1 && fabs (BACKGROUND[2]-0.5) < 0.1) //almost grey
			for (int j = 0; j < 3; j++) {
				mc[j]=0.0f;
			}
		else
			for (int j = 0; j < 3; j++) {
				mc[j]=1-BACKGROUND[j];
			}
		mc[3]=0.5;
814
815
		for (int i=0;i<TIMESTEPS;i++) {
			markercolours[i]=new float[4];
816
817
			for (int j = 0; j < 4; j++) {
				markercolours [i][j]=mc[j];
818
819
820
821
			}
		}
	}

822
823
824
825
	//chemical bonds
	//if (numAtoms) {
	//}

826
	//eprintf ("Before returning, numatoms 0 =%d", numAtoms[0]);
827
	return 0;
828
}