File: frame.cpp

package info (click to toggle)
esys-particle 2.3.5%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,132 kB
  • sloc: cpp: 81,480; python: 5,872; makefile: 1,259; sh: 313; perl: 225
file content (574 lines) | stat: -rw-r--r-- 17,151 bytes parent folder | download | duplicates (5)
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
313
314
315
316
317
318
319
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
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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2017 by The University of Queensland //
// Centre for Geoscience Computing                         //
// http://earth.uq.edu.au/centre-geoscience-computing      //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.apache.org/licenses/LICENSE-2.0              //
//                                                         //
/////////////////////////////////////////////////////////////

#include "frame.h"

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>
#include <sstream>
#include <cstdlib>
#include <utility>
#include "vec3.h"

using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::string;
using std::vector;
using std::copy;
using std::istream_iterator;
using std::back_inserter;
using std::map;
using std::ostringstream;
using std::atoi;
using std::pair;
using std::make_pair;

int get_version(const string& infilename)
{
  string dummystring;
  int version;
  ifstream headerfile(infilename.c_str()); 
  // read token  
  headerfile >> dummystring;

  if(dummystring=="V"){ // if V -> new version 
    headerfile >> version ;
    cout << "version : " << version << endl;
  } else {
    cout << "pre- V.1 version" << endl;
    version=0;
  }
  headerfile.close();

  return version;
}


void do_single_frame(const string& infilename,const string& outfilename,int iframe, const vector<string>& incfile,int camdir,const ColorMap *cm, int field,Vec3 clook,Vec3 cpos,bool use_cam,bool bonds_only)
{
  map<int,Vec3> posmap;
  map<int,float> radmap;
  map<int,Vec3> colormap;

  vector<pair<int,int> > bonds;

  int version=get_version(infilename.c_str());
  ifstream headerfile(infilename.c_str()); 
  float dummy,xmax,ymax,zmax,xmin,ymin,zmin;
  string dummystring;
  vector<string> filenames;

  if(version==0){
    headerfile >> dummy >> dummy >> dummy;
    headerfile >> dummystring >> dummy;
  } else if ((version==1) || (version==2) || (version==3)) {
    headerfile >> dummystring >> dummy;
    headerfile >> dummy >> dummy >> dummy;
    headerfile >> dummystring >> dummy;
  } else {
    cerr << "unknown checkpoint version " << version << endl;
  }

  // get bounding box
  headerfile >> dummystring;
  headerfile >> xmin >> ymin >> zmin >> xmax >> ymax >> zmax ;

  // ignore periodic bdry
  headerfile >> dummystring >> dummy >> dummy >> dummy;

  // ignore dimension
  headerfile >> dummystring >> dummystring;

  // get file names
  copy(istream_iterator<string>(headerfile),istream_iterator<string>(),back_inserter(filenames));
  
  headerfile.close();
  // test 
  for(vector<string>::iterator iter=filenames.begin();
      iter!=filenames.end();
      iter++){
    ifstream datafile(iter->c_str());
    std::cerr << iter->c_str() << std::endl; 
    vector<float> pdata;

    // get particles
    int npart;
    Vec3 pos;
    Vec3 oldpos;
    Vec3 initpos;
    Vec3 vel;
    Vec3 force;
    Vec3 circshift;
    double rad;
    double mass;
    int id;
    int tag;
    datafile >> npart;
    for(int i=0;i<npart;i++){
      datafile >> pos  >> rad >> id >> tag >> mass >> initpos >> oldpos >> vel >> force;
      if(version>1) datafile >> circshift ;
      posmap[id]=pos;
      radmap[id]=rad;
      double c_temp;
      switch(field){
      case 1: c_temp=pos.X(); break;
      case 2: c_temp=pos.Y(); break;
      case 3: c_temp=pos.Z(); break;
      case 4: c_temp=rad; break;
      case 5: c_temp=double(id); break;
      case 6: c_temp=double(tag); break;
      case 7: c_temp=mass; break;
      case 8: c_temp=initpos.X(); break;
      case 9: c_temp=initpos.Y(); break;
      case 10: c_temp=initpos.Z(); break;
      case 11: c_temp=vel.X(); break;
      case 12: c_temp=vel.Y(); break;
      case 13: c_temp=vel.Z(); break;
      case 14: c_temp=(pos-initpos).norm();; break;
      default: c_temp=0.0;
      }
      colormap[id]=cm->getColor(c_temp);
    }
      
    // get bonds
    int ngrp;
    int nbond;
    string type;

    datafile >> ngrp;
    if(version>0){
      datafile >> type;
      if(type=="Bonded"){
	datafile >> nbond;
	for(int i=0;i<nbond;i++){
	  int b1,b2;
	  
	  datafile >> b1 >> b2 >> dummy;
	  bonds.push_back(make_pair(b1,b2));
	}
      }
    } else { // pre - V1 snapshot -> assume bondend pair IG
      datafile >> nbond;
      for(int i=0;i<nbond;i++){
	int b1,b2;
	
	datafile >> b1 >> b2 >> dummy;
	bonds.push_back(make_pair(b1,b2));
      }
    }



    datafile.close();
  }
  
  // calculate camera position
  Vec3 lookat; 
  Vec3 loc;
  Vec3 light1_loc,light2_loc;

  if(use_cam){
    lookat=clook;
    loc=cpos;
    light1_loc=clook+Vec3(0.0,50.0,0.0);
    light1_loc=cpos+Vec3(0.0,10.0,0.0);
  } else {
    float cdist=0.0;
    switch(camdir){
    case 1: {// camera x
      loc=lookat+Vec3(xmax+cdist,0.0,0.0); 
      light1_loc=lookat+Vec3(xmax+2*cdist,0.0,cdist);
      light2_loc=lookat+Vec3(xmax+2*cdist,0.0,-1.0*cdist);
    } break; 
    case 2: {// camera y
      loc=lookat+Vec3(0.0,ymax+cdist,0.0); 
      light1_loc=lookat+Vec3(0.0,ymax+2*cdist,cdist);
      light2_loc=lookat+Vec3(0.0,ymax+2*cdist,-1.0*cdist);
    } break; 
    default: {// camera z
      loc=lookat+Vec3(0.0,0.0,zmax+cdist); 
      light1_loc=lookat+Vec3(cdist,0.0,zmax+2*cdist);
      light2_loc=lookat+Vec3(-1.0*cdist,0.0,zmax+2*cdist);
    } break; 
    }
  }
  // open output file
  ostringstream povfilename;
  povfilename << outfilename << iframe << ".pov";
  ofstream povfile(povfilename.str().c_str());
  // comment
  povfile << "//generated by dump2pov\n\n";
  // write camera, lights
  povfile << "#include \"colors.inc\" \n";
  for(vector<string>::const_iterator iter=incfile.begin();
      iter!=incfile.end();
      iter++){
    povfile << "#include \"" << *iter <<"\" \n";
  }
  povfile << "background { color White } \n";
  povfile << "camera { \n location <" << loc.X() << "," << loc.Y() << "," << loc.Z() << "> \n look_at  <" << lookat.X() << "," << lookat.Y() << "," << lookat.Z() << "> \n}\n";
  povfile << "light_source { <" << light1_loc.X() << "," << light1_loc.Y() << "," << light1_loc.Z() << "> color White}\n";
  povfile << "light_source { <" << light2_loc.X() << "," << light2_loc.Y() << "," << light2_loc.Z() << "> color White}\n\n";
  
  // write particles
  povfile << "union{\n";
  for(map<int,Vec3>::iterator iter=posmap.begin();
      iter!=posmap.end();
      iter++){
    Vec3 pos=iter->second;
    float rad=radmap[iter->first];
    Vec3 color=colormap[iter->first];
    povfile << "sphere { <" << pos.X() << " , " << pos.Y() << " , " << pos.Z() << ">, " << rad << "  " ;
    povfile << "pigment { rgb < " << color.X() << " , " << color.Y() << " , " << color.Z() << "> } }\n"; 
  }
  // write bonds
  for(vector<pair<int,int> >::iterator iter=bonds.begin();
      iter!=bonds.end();
      iter++){
    // get endpoints
    Vec3 p1=posmap[iter->first];
    Vec3 p2=posmap[iter->second];
    // get radii
    float r1=radmap[iter->first];
    float r2=radmap[iter->second];
    // sanity check
    if((p1-p2).norm()<2.0*(r1+r2)){ // cull circular bonds
      // calc color
      Vec3 color=0.5*(colormap[iter->first]+colormap[iter->second]);
      // write
      povfile << "cone { < " << p1.X() << "," << p1.Y() << "," << p1.Z() << "> " << r1*0.75 << " < " << p2.X() << "," << p2.Y() << "," << p2.Z() << "> " << r2*0.75 << " ";
      povfile << "pigment { rgb < " << color.X() << " , " << color.Y() << " , " << color.Z() << "> } }\n"; 
    }
  }
  povfile << "}\n";
  povfile.close();
}

void do_single_frame_r(const string& infilename,const string& outfilename,int iframe, const vector<string>& incfile,int camdir,const ColorMap *cm, int field,Vec3 clook,Vec3 cpos,bool use_cam,bool bonds_only,bool no_bonds,map<int,Vec3>& p_orig)
{
  map<int,Vec3> posmap;
  map<int,float> radmap;
  map<int,Vec3> colormap;

  vector<pair<int,int> > bonds;

  int version=get_version(infilename.c_str());
  ifstream headerfile(infilename.c_str()); 
  float dummy,xmax,ymax,zmax,xmin,ymin,zmin;
  string dummystring;
  vector<string> filenames;

  if(version==0){
    headerfile >> dummy >> dummy >> dummy;
    headerfile >> dummystring >> dummy; 
  } else if ((version==1) || (version==2) || (version==3)) {
    headerfile >> dummystring >> dummy;
    headerfile >> dummy >> dummy >> dummy;
    headerfile >> dummystring >> dummy;
  } else {
    cerr << "unknown checkpoint version " << version << endl;
  }

  // get bounding box
  headerfile >> dummystring;
  headerfile >> xmin >> ymin >> zmin >> xmax >> ymax >> zmax ;
  
  // ignore periodic bdry
  headerfile >> dummystring >> dummy >> dummy >> dummy;
  
  // ignore dimension
  headerfile >> dummystring >> dummystring;
  
  // get file names
  copy(istream_iterator<string>(headerfile),istream_iterator<string>(),back_inserter(filenames));
  
  headerfile.close();
  // test 
  double ct_min=1000;
  double ct_max=-1000;
  for(vector<string>::iterator iter=filenames.begin();
      iter!=filenames.end();
      iter++){
    ifstream datafile(iter->c_str());
    vector<float> pdata;
    
    // get particles
    int npart;
    Vec3 pos;
    Vec3 oldpos;
    Vec3 initpos;
    Vec3 vel;
    Vec3 force;
    Vec3 angvel;
    double rad;
    double mass;
    double q1,q2,q3,q4;
    int id;
    int tag;
    datafile >> npart;
    if(version<2){
      for(int i=0;i<npart;i++){
	datafile >> pos  >> rad >> id >> tag >> mass >> initpos >> oldpos >> vel >> force >> q1 >> q2 >> q3 >> q4 >> angvel;
	posmap[id]=pos;
	radmap[id]=rad;
	double c_temp;
	switch(field){
	case 1: c_temp=pos.X(); break;
	case 2: c_temp=pos.Y(); break;
	case 3: c_temp=pos.Z(); break;
	case 4: c_temp=rad; break;
	case 5: c_temp=double(id); break;
	case 6: c_temp=double(tag); break;
	case 7: c_temp=mass; break;
	case 8: c_temp=initpos.X(); break;
	case 9: c_temp=initpos.Y(); break;
	case 10: c_temp=initpos.Z(); break;
	case 11: c_temp=vel.X(); break;
	case 12: c_temp=vel.Y(); break;
	case 13: c_temp=vel.Z(); break;
	case 14: c_temp=(pos-initpos).norm(); break;
	case 15: c_temp=(pos-p_orig[id]).norm(); break;
	case 16: c_temp=p_orig[id].Z(); break;
	default: c_temp=0.0;
	}
	ct_max=c_temp > ct_max ? c_temp : ct_max;
	ct_min=c_temp < ct_min ? c_temp : ct_min;
	colormap[id]=cm->getColor(c_temp);
      }
    } else {
      Vec3 circ_shift;
      for(int i=0;i<npart;i++){
	datafile >> pos  >> rad >> id >> tag >> mass >> initpos >> oldpos >> vel >> force >> circ_shift >> q1 >> q2 >> q3 >> q4 >> angvel;
	posmap[id]=pos;
	radmap[id]=rad;
	double c_temp;
	switch(field){
	case 1: c_temp=pos.X(); break;
	case 2: c_temp=pos.Y(); break;
	case 3: c_temp=pos.Z(); break;
	case 4: c_temp=rad; break;
	case 5: c_temp=double(id); break;
	case 6: c_temp=double(tag); break;
	case 7: c_temp=mass; break;
	case 8: c_temp=initpos.X(); break;
	case 9: c_temp=initpos.Y(); break;
	case 10: c_temp=initpos.Z(); break;
	case 11: c_temp=vel.X(); break;
	case 12: c_temp=vel.Y(); break;
	case 13: c_temp=vel.Z(); break;
	case 14: c_temp=(pos-initpos).norm(); break;
	case 15: c_temp=(pos-p_orig[id]).norm(); break;
	case 16: c_temp=p_orig[id].Z(); break;
	default: c_temp=0.0;
	}
	ct_max=c_temp > ct_max ? c_temp : ct_max;
	ct_min=c_temp < ct_min ? c_temp : ct_min;
	colormap[id]=cm->getColor(c_temp);
      }
    }
    std::cout << "min/max : " << ct_min << " / " << ct_max << std::endl;

    // get bonds
    int ngrp;
    int nbond;
    string type;

    datafile >> ngrp;
    if(version>0){
      datafile >> type;
      if((type=="Bonded") || (type=="RotBonded")){
	datafile >> nbond;
	for(int i=0;i<nbond;i++){
	  int b1,b2;
	  
	  datafile >> b1 >> b2 >> dummy;
	  bonds.push_back(make_pair(b1,b2));
	}
      }
    } else { // pre - V1 snapshot -> assume bondend pair IG
      datafile >> nbond;
      for(int i=0;i<nbond;i++){
	int b1,b2;
	
	datafile >> b1 >> b2 >> dummy;
	bonds.push_back(make_pair(b1,b2));
      }
    }

    datafile.close();
  }
  
  // calculate camera position
  Vec3 lookat; 
  Vec3 loc;
  Vec3 light1_loc,light2_loc;

  if(use_cam){
    lookat=clook;
    loc=cpos;
    light1_loc=clook+Vec3(0.0,50.0,0.0);
    light1_loc=cpos+Vec3(0.0,10.0,0.0);
  } else {
    float cdist=0.0;
    switch(camdir){
    case 1: {// camera x
      loc=lookat+Vec3(xmax+cdist,0.0,0.0); 
      light1_loc=lookat+Vec3(xmax+2*cdist,0.0,cdist);
      light2_loc=lookat+Vec3(xmax+2*cdist,0.0,-1.0*cdist);
    } break; 
    case 2: {// camera y
      loc=lookat+Vec3(0.0,ymax+cdist,0.0); 
      light1_loc=lookat+Vec3(0.0,ymax+2*cdist,cdist);
      light2_loc=lookat+Vec3(0.0,ymax+2*cdist,-1.0*cdist);
    } break; 
    default: {// camera z
      loc=lookat+Vec3(0.0,0.0,zmax+cdist); 
      light1_loc=lookat+Vec3(cdist,0.0,zmax+2*cdist);
      light2_loc=lookat+Vec3(-1.0*cdist,0.0,zmax+2*cdist);
    } break; 
    }
  }
  // open output file
  ostringstream povfilename;
  povfilename << outfilename << iframe << ".pov";
  ofstream povfile(povfilename.str().c_str());
  // comment
  povfile << "//generated by dump2pov\n\n";
  // write camera, lights
  povfile << "#include \"colors.inc\" \n";
  for(vector<string>::const_iterator iter=incfile.begin();
      iter!=incfile.end();
      iter++){
    povfile << "#include \"" << *iter <<"\" \n";
  }
  povfile << "background { color White } \n";
 //  povfile << "camera { \n location <" << loc.X() << "," << loc.Y() << "," << loc.Z() << "> \n look_at  <" << lookat.X() << "," << lookat.Y() << "," << lookat.Z() << "> \n}\n";
//   povfile << "light_source { <" << light1_loc.X() << "," << light1_loc.Y() << "," << light1_loc.Z() << "> color White}\n";
//   povfile << "light_source { <" << light2_loc.X() << "," << light2_loc.Y() << "," << light2_loc.Z() << "> color White}\n\n";
  
  // write particles
  povfile << "union{\n";
  if(!bonds_only){
    for(map<int,Vec3>::iterator iter=posmap.begin();
	iter!=posmap.end();
	iter++){
      Vec3 pos=iter->second;
      float rad=radmap[iter->first];
      Vec3 color=colormap[iter->first];
      povfile << "sphere { <" << pos.X() << " , " << pos.Y() << " , " << pos.Z() << ">, " << rad << "  " ;
      povfile << "pigment { rgb < " << color.X() << " , " << color.Y() << " , " << color.Z() << "> } }\n"; 
    }
  }
  // write bonds
  if(!no_bonds){
    for(vector<pair<int,int> >::iterator iter=bonds.begin();
	iter!=bonds.end();
	iter++){
      // get endpoints
      Vec3 p1=posmap[iter->first];
      Vec3 p2=posmap[iter->second];
      // get radii
      float r1=radmap[iter->first];
      float r2=radmap[iter->second];
      // sanity check
      if((p1-p2).norm()<2.0*(r1+r2)){ // cull circular bonds
	// calc color
	Vec3 color=0.5*(colormap[iter->first]+colormap[iter->second]);
	// write
	povfile << "cone { < " << p1.X() << "," << p1.Y() << "," << p1.Z() << "> " << r1*0.75 << " < " << p2.X() << "," << p2.Y() << "," << p2.Z() << "> " << r2*0.75 << " ";
	povfile << "pigment { rgb < " << color.X() << " , " << color.Y() << " , " << color.Z() << "> } }\n"; 
      }
    }
  }
  povfile << "}\n";
  povfile.close();
}

map<int,Vec3> get_frame_disp_r(const string& infilename)
{
  map<int,Vec3> posmap;

  int version=get_version(infilename.c_str());
  ifstream headerfile(infilename.c_str()); 
  float dummy,xmax,ymax,zmax,xmin,ymin,zmin;
  string dummystring;
  vector<string> filenames;

  if(version==0){
    headerfile >> dummy >> dummy >> dummy;
    headerfile >> dummystring >> dummy; 
  } else if ((version==1) || (version==2)|| (version==3)) {
    headerfile >> dummystring >> dummy;
    headerfile >> dummy >> dummy >> dummy;
    headerfile >> dummystring >> dummy;
  } else {
    cerr << "unknown checkpoint version " << version << endl;
  }

  // get bounding box
  headerfile >> dummystring;
  headerfile >> xmin >> ymin >> zmin >> xmax >> ymax >> zmax ;
  
  // ignore periodic bdry
  headerfile >> dummystring >> dummy >> dummy >> dummy;
  
  // ignore dimension
  headerfile >> dummystring >> dummystring;
  
  // get file names
  copy(istream_iterator<string>(headerfile),istream_iterator<string>(),back_inserter(filenames));
  
  headerfile.close();
  for(vector<string>::iterator iter=filenames.begin();
      iter!=filenames.end();
      iter++){
    ifstream datafile(iter->c_str());
    vector<float> pdata;
    
    // get particles
    int npart;
    Vec3 pos;
    Vec3 oldpos;
    Vec3 initpos;
    Vec3 vel;
    Vec3 force;
    Vec3 angvel;
    double rad;
    double mass;
    double q1,q2,q3,q4;
    int id;
    int tag;
    datafile >> npart;
    if(version<2){
      for(int i=0;i<npart;i++){
	datafile >> pos  >> rad >> id >> tag >> mass >> initpos >> oldpos >> vel >> force >> q1 >> q2 >> q3 >> q4 >> angvel;
	posmap[id]=pos;
      }
    } else {
      Vec3 circ_shift;
      for(int i=0;i<npart;i++){
	datafile >> pos  >> rad >> id >> tag >> mass >> initpos >> oldpos >> vel >> force >> circ_shift >> q1 >> q2 >> q3 >> q4 >> angvel;
	posmap[id]=pos;
      }
    }
    datafile.close();
  }
  
  return posmap;
}