PovrayWriter.cc 13.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


Thomas Witkowski's avatar
Thomas Witkowski committed
22
23
24
25
26
27
28
29
30
#include "PovrayWriter.h"
#include "DOFVector.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <algorithm>
#include "Traverse.h"
31
#include "DataCollector.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
32
33
34

using namespace std;

35
namespace AMDiS { namespace io {
Thomas Witkowski's avatar
Thomas Witkowski committed
36
37

  /* destructor */
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
38
39
  PovrayWriter::~PovrayWriter()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
40
    // free the memory for the bounding box
41
    if (bBox) {
Thomas Witkowski's avatar
Thomas Witkowski committed
42
      delete bBox;
43
      bBox = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    }
  }


  /* some forward declarations */
  double computeWeight(double&, double&, double&);
  string getColorString(double&);


  void PovrayWriter::tryMeshTraversal(ofstream &out)
  {
    /* prepare some data structures */
    DOFVector<double> *values = dataCollector->getValues();
    const FiniteElemSpace *feSpace = dataCollector->getFeSpace();
    const BasisFunction *basFcts = feSpace->getBasisFcts();
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
59
    std::vector<DegreeOfFreedom> dofs(basFcts->getNumber());
Thomas Witkowski's avatar
Thomas Witkowski committed
60
61
62
63
64
65
66
67
68
69
70
71
72

    /* create an 'iterator' */ 
    TraverseStack stack;
    ElInfo *elInfo =  stack.traverseFirst(dataCollector->getMesh(), -1, 
					  Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);

    /* find min and max values */
    double min_value = 0;
    double max_value = 0;
    if (values->getSize() > 0)
      min_value = max_value = (*values)[0];

    for (int index = 1; index < values->getSize(); index++) {
73
74
      min_value = std::min(min_value, (*values)[index]);
      max_value = std::max(max_value, (*values)[index]);
Thomas Witkowski's avatar
Thomas Witkowski committed
75
76
77
    }

    /* map DOFs to values */
78
    std::map <DegreeOfFreedom, double> value_map;
Thomas Witkowski's avatar
Thomas Witkowski committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    while (elInfo) {
      basFcts->getLocalIndices(elInfo->getElement(), feSpace->getAdmin(), dofs);
                        
      int max = Global::getGeo(WORLD)==2 ? 3 : 4;

      for (int i = 0; i < max; i++) {
	double value = (*values)[dofs[i]];
	value_map[dofs[i]] = value;
      }

      elInfo = stack.traverseNext(elInfo);
    }
                
    out << "\ttexture_list {" << endl;
    out << "\t" << value_map.size() << "," << endl;
94
    std::map<DegreeOfFreedom, double>::iterator map_iter;
Thomas Witkowski's avatar
Thomas Witkowski committed
95
96
97
98
    for (map_iter = value_map.begin(); map_iter != value_map.end(); ++map_iter) {
      double value = map_iter->second;
      double color_weight =  computeWeight(min_value,max_value,value);
      out << "\t\ttexture{ pigment{ " << getColorString(color_weight) << "}}";
99
      std::map<DegreeOfFreedom, double>::iterator final_iter = value_map.end();
Thomas Witkowski's avatar
Thomas Witkowski committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
      --final_iter;
      if (map_iter != final_iter)
	out << ",";      
      out << endl;
    }
    out << "\t}" << endl; // end of texture_list
  }

  // provides the bounding box of the mesh
  //         
  // note: computation is done during the first call other, this might cause
  //       problems in simulations with changing geometries
  BoundingBox* PovrayWriter::getBoundingBox(ofstream &out) 
  {
114
    if (bBox != NULL)
Thomas Witkowski's avatar
Thomas Witkowski committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
      return bBox;    

    DOFVector< std::list<VertexInfo> > *vertexInfos = dataCollector->getVertexInfos();
    DOFVector< std::list<VertexInfo> >::Iterator it(vertexInfos, USED_DOFS);
    int dow = Global::getGeo(WORLD);
    bBox = new BoundingBox();

    it.reset();
    std::list<VertexInfo>::iterator it2 = it->begin();
    bBox->minx = it2->coords[0];
    bBox->maxx = it2->coords[0];
    bBox->miny = it2->coords[1];
    bBox->maxy = it2->coords[1];
    if (dow == 3){
      bBox->minz = it2->coords[2];
      bBox->maxz = it2->coords[2];
    } else {
      bBox->minz = bBox->maxz = 0;
    }

    for (it.reset(); !it.end(); ++it) {
      // initialize mit and max values with coordinates of first vertex
      std::list<VertexInfo>::iterator it2 = it->begin();
138
139
140
141
      bBox->minx = std::min(bBox->minx, it2->coords[0]);
      bBox->maxx = std::max(bBox->maxx, it2->coords[0]);
      bBox->miny = std::min(bBox->miny, it2->coords[1]);
      bBox->maxy = std::max(bBox->maxy, it2->coords[1]);
Thomas Witkowski's avatar
Thomas Witkowski committed
142
      if (dow == 3) {
143
144
	bBox->minz = std::min(bBox->minz, it2->coords[2]);
	bBox->maxz = std::max(bBox->maxz, it2->coords[2]); 
Thomas Witkowski's avatar
Thomas Witkowski committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
      }
    }

    return bBox;
  }

  // computes the weight of a value for color computation
  double computeWeight(double &min, double &max, double &value)
  {
    double interval_length = (max - min); 
    return (value - min) / interval_length;
  }

  // determines the color for a given value/weight
  string getColorString(double &value) 
  {
161
162
    value = std::max(0.0, value);
    value = std::min(value, 1.0);
Thomas Witkowski's avatar
Thomas Witkowski committed
163
164
165
166
167
168
169
170
171
172
173

    // rot: 1,0,0
    // blau: 0,0,1

    // currently blue->red gradient
    stringstream res;
    res << "rgb<" << value << ",0," << (1 - value) << ">";
    return res.str();
  }

  // writes a povray script for the current time step to the specified file
174
  void PovrayWriter::writeFile(std::string filename)
Thomas Witkowski's avatar
Thomas Witkowski committed
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
  {
    ofstream out;
    out.open(filename.c_str(), ios::out);

    // delegate all the work to other subroutines
    writeHeader(out);
    writeIncludes(out);
    writeCamera(out);
    writeLight(out);
    writeTestStuff(out, *dataCollector);
    writeMesh2(out, *dataCollector);

    out.close();
  }

  
  // writes a simple info header (comments only) 
  void PovrayWriter::writeHeader(ofstream &out) 
  {
    out << "// povray file created by AMDiS" << endl;
    out << endl;
  }


  // writes all neccessary include file declarations
  void PovrayWriter::writeIncludes(ofstream &out) 
  {
    out << "#include \"colors.inc\"" << endl;
    out << "#include \"stones.inc\"" << endl;
    out << endl;
  }

  // computes an appropriate camera position and writes it to the povray file
  void PovrayWriter::writeCamera(ofstream &out) 
  {
    /* TODO: compute adequate camera position (computation needs to be performed
       only once, same camera position will be used for all iterations) */

    /* TODO: check whether camera position and location have been specified in the
       init file, use them if present*/

    BoundingBox *box = getBoundingBox(out);
    int dow = Global::getGeo(WORLD);
    double centerx = (box->minx+box->maxx)/2.;
    double centery = (box->miny+box->maxy)/2.;
    double centerz = (box->minz+box->maxz)/2.;

    if (dow == 2){
      out << "camera {" << endl;
      out << "\tlocation <" << centerx << ", " << centery << ", -3>" << endl;
      out << "\tlook_at  <" << centerx << ", " << centery << ",  0>" << endl;
      out << "}" << endl << endl; 
    } else {
      out << "camera {" << endl;
      out << "\tlocation <"<< centerx <<", "<<centery<<", -3>" << endl;
      out << "\tlook_at  <"<< centerx <<", "<<centery<<",  "<<centerz<<">" << endl;
      out << "}" << endl << endl; 
    }
  }


  // computes an appropriate light source and writes it to the povray file
  void PovrayWriter::writeLight(ofstream &out) 
  {
    // TODO: compute adequate light position
    out << "light_source { <0, 0, -15> color White}" << endl;
  }


  // writes the data in the 'mesh2' format
245
  void PovrayWriter::writeMesh2(ofstream &out, DataCollector<> &dataCollector)
Thomas Witkowski's avatar
Thomas Witkowski committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  {
    // initialization and preparations
    //Mesh *mesh = dataCollector.getMesh();
	
    // begin of mesh2 block
    out << "mesh2 {" << endl;
		
    // delegate work to other methods
    writeVertexVectors(out, dataCollector);
    //writeTextureList(out, dataCollector);
    tryMeshTraversal(out);		
    writeFaceIndices(out, dataCollector);
    out << "\t" << "pigment {rgb 1}" << endl;
    // end of mesh2 block
    out << "}" << endl;
  } 


  // writes all vertices of the model
265
  void PovrayWriter::writeVertexVectors(ofstream &out, DataCollector<> &dataCollector)
Thomas Witkowski's avatar
Thomas Witkowski committed
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
  {
    // initialization and preparations
    Mesh *mesh = dataCollector.getMesh();
    DOFVector< std::list<VertexInfo> > *vertexInfos = dataCollector.getVertexInfos();
    DOFVector< std::list<VertexInfo> >::Iterator it(vertexInfos, USED_DOFS);

    // begin of vertex_vectors block
    out << "\tvertex_vectors {" << endl;
    int nVertices = mesh->getNumberOfVertices();
    out << "\t\t" << nVertices << "," << endl;

    // For all DOFs of vertices, write the coordinates.
    for (it.reset(); !it.end(); ++it) {
      // for all vertex infos of this DOF
      for (std::list<VertexInfo>::iterator it2 = it->begin(); it2 != it->end(); ++it2) {
	out << endl;		    
	// TODO: right implementation using STL vector/iterator
	out  << "\t\t<";
	WorldVector<double> coords;
	coords = it2->coords;
	for (int i = 0; i < Global::getGeo(WORLD); i++){
	  if (i > 0)
	    out << ", ";

	  out << std::scientific << coords[i];
	}	
	for (int i = Global::getGeo(WORLD); i < 3; i++)
	  out << ", 0.0";
	out << ">,";
      }     
    }
    // undo last comma
    long pos=out.tellp(); //tells pos the actual stream position
    out.seekp(pos - 1);     //sets stream position -1
    out <<  endl;         //overwrites the las comma with space
    // end of vertex_vectors block
    out << "\t}" << endl;		
  }
  

  // writes the list of textures (basically one color for each vertex, used to draw a color gradient on each triangle)
307
  void PovrayWriter::writeTextureList(ofstream &out, DataCollector<> &dataCollector)
Thomas Witkowski's avatar
Thomas Witkowski committed
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  {
    // prepare some data structures		
    Mesh *mesh = dataCollector.getMesh();		
    DOFVector< std::list<VertexInfo> > *vertexInfos = dataCollector.getVertexInfos();
    DOFVector< std::list<VertexInfo> >::Iterator it(vertexInfos, USED_DOFS);


    // begin of texture_list block
    out << "\ttexture_list {" << endl;

    // compute and write the number of textures/colors (one for each vertex)
    int nTextures = mesh->getNumberOfVertices();
    out << "\t\t" << nTextures << "," << endl;

    // For all DOFs of vertices, write the coordinates.
    for (it.reset(); !it.end(); ++it) {
      // for all vertex infos of this DOF
      for (std::list<VertexInfo>::iterator it2 = it->begin(); it2 != it->end(); ++it2) {
	// test: use y-coordinate to compute color
	double redValue = it2->coords[1];
328
329
	redValue = std::max(0., redValue);
	redValue = std::min(redValue, 1.);
Thomas Witkowski's avatar
Thomas Witkowski committed
330
331
332
333
334
335
336
337
338
339
340
341
342
		    
	out << "\n\t\ttexture{ pigment{ rgb"<< getColorString(redValue) <<" } }" << ",";
      }     
    }
    long pos = out.tellp();
    out.seekp(pos - 1);
    out << endl;
    // end of texture_list block
    out << "\t}" << endl;
  }


  // writes the connectivity
343
  void PovrayWriter::writeFaceIndices(ofstream &out, DataCollector<> &dataCollector)
Thomas Witkowski's avatar
Thomas Witkowski committed
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
  {
    // begin of face_indices block
    out << "\tface_indices {" << endl;

    std::list<ElementInfo> *elementInfos = dataCollector.getElementInfos();
    int vertices = dataCollector.getMesh()->getGeo(VERTEX);
    int meshDim = dataCollector.getMesh()->getDim();		
    int counter = 0;

    if (meshDim == 2) {
      int nElements = dataCollector.getElementInfos()->size();
      out << "\t\t" << nElements << "," << endl;		   	
      for (std::list<ElementInfo>::iterator iter = elementInfos->begin(); 
	   iter != elementInfos->end(); iter++) {
	out << endl;				
	out << "\t\t<";    			
	for (int i = 0; i < vertices; i++) {
	  if (i > 0)
	    out << ", ";
	  
	  out << iter->vertexInfo[i]->outputIndex;
	}
	out << ">";

	// write indices again for texture/color mapping
	for (int i = 0; i < vertices; i++) {
	  out << ", ";
	  out << iter->vertexInfo[i]->outputIndex;
	}

	out << ",";
      }
      //Delete last comma 
      long pos = out.tellp();
      out.seekp(pos - 1);
      out << endl;
    } else if (meshDim == 3) {
      int nElements = dataCollector.getElementInfos()->size() * 4;
      out << "\t\t" << nElements << "," << endl;
      for (std::list<ElementInfo>::iterator iter = elementInfos->begin(); 
	   iter != elementInfos->end(); iter++) {
	for (int j = 0; j < vertices; j++) {
	  out << "/* " << counter++ << " */";
	  out << "\t\t<"; 
	  for (int i = 0; i < vertices; i++) {
	    if (i==j) 
	      continue;					

	    if (!((j > 0 && i == 0) || (j == 0 && i == 1)))
	      out << ", ";
	    
	    out << iter->vertexInfo[i]->outputIndex;
	  }
	  out << ">";
	  // write indices again for texture/color mapping
	  for (int i = 0; i < vertices; i++) {
	    if (i == j) 
	      continue;
	    out << ", ";
	    out << iter->vertexInfo[i]->outputIndex;
	  }
	  out << "," << endl;
	}
      }
    }

    // end of face_indices block
    out << "\n\t}" << endl; //
  }
	


  /// writes some additional information for debugging purposes
417
  void PovrayWriter::writeTestStuff(ofstream &out, DataCollector<> &dataCollector)
Thomas Witkowski's avatar
Thomas Witkowski committed
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
  {
    Mesh *mesh = dataCollector.getMesh();

    out << "/*" << endl;
    out << "TestData:" << endl;

    int dow = Global::getGeo(WORLD);
    out << " world dimension:\t" << dow << endl;

    out << " dimension:\t" << mesh->getDim() << endl;
    out << " numberOfNodes:\t" << mesh->getNumberOfNodes() << endl;
    out << " numberOfEdges:\t" << mesh->getNumberOfEdges() << endl;
    out << " numberOfVertices:\t" << mesh->getNumberOfVertices() << endl;
    out << " numberOfFaces:\t" << mesh->getNumberOfFaces() << endl;
    out << " numberOfElements:\t" << mesh->getNumberOfElements() << endl;
    out << "*/" << endl << endl;
  }

436
} } // end of namespace io, AMDiS