LBIBCell
 All Classes Functions Variables Friends Pages
vtkCellPNGReporter.cpp
1 /* Copyright (c) 2013 David Sichau <mail"at"sichau"dot"eu>
2  * 2013-2015 Simon Tanaka <tanakas"at"gmx"dot"ch>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 #include <LbmLib/include/reportHandler/vtkCellPNGReporter.hpp>
23 #include <UtilLib/include/Exception.hpp>
24 
25 #include <vtkSmartPointer.h>
26 #include <vtkPolygon.h>
27 #include <vtkCellArray.h>
28 #include <vtkPolyData.h>
29 #include <vtkDoubleArray.h>
30 #include <vtkStringArray.h>
31 #include <vtkPointData.h>
32 #include <vtkMultiBlockDataSet.h>
33 #include <vtkXMLMultiBlockDataWriter.h>
34 #include <vtkXMLMultiBlockDataReader.h>
35 
36 #include <vtkPolyDataMapper.h>
37 #include <vtkActor.h>
38 #include <vtkRenderWindow.h>
39 #include <vtkRenderer.h>
40 #include <vtkRenderWindowInteractor.h>
41 #include <vtkTriangleFilter.h>
42 #include <vtkWindowToImageFilter.h>
43 #include <vtkPNGWriter.h>
44 #include <vtkImageCanvasSource2D.h>
45 #include <vtkImageCast.h>
46 
47 #include <sys/stat.h>
48 #include <sstream>
49 #include <fstream>
50 #include <iomanip>
51 
52 namespace LbmLib {
53 namespace reportHandler {
54 void vtkCellPNGReporter::operator()(unsigned int time) const {
55  std::stringstream filename;
56  filename << filename_ << "_" << time << ".png";
57 
58  vtkSmartPointer<vtkPoints> polygonpoints =
59  vtkSmartPointer<vtkPoints>::New();
60  vtkSmartPointer<vtkCellArray> polygons =
61  vtkSmartPointer<vtkCellArray>::New();
62  vtkSmartPointer<vtkPolyData> polygonPolyData =
63  vtkSmartPointer<vtkPolyData>::New();
64  vtkTriangleFilter *tri=
65  vtkTriangleFilter::New();
66  vtkPolyDataMapper *map =
67  vtkPolyDataMapper::New();
68  vtkSmartPointer<vtkActor> actor =
69  vtkSmartPointer<vtkActor>::New();
70  vtkSmartPointer<vtkRenderer> renderer =
71  vtkSmartPointer<vtkRenderer>::New();
72  vtkSmartPointer<vtkRenderWindow> renderWindow =
73  vtkSmartPointer<vtkRenderWindow>::New();
74  std::vector<vtkSmartPointer<vtkPolygon> > polygonvector;
75  vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
76  vtkSmartPointer<vtkWindowToImageFilter>::New();
77  vtkSmartPointer<vtkPNGWriter> writer =
78  vtkSmartPointer<vtkPNGWriter>::New();
79  std::map<unsigned int,std::vector<std::shared_ptr<LbmLib::geometry::Connection> > > celldefinition;
80  std::shared_ptr<LbmLib::geometry::Connection> startC = nullptr;
81  std::shared_ptr<LbmLib::geometry::Connection> tempC = nullptr;
82 
83  int globalpointcounter = 0;
84  int polygonpointcounter;
85  int polygoncounter = 0;
86 
87  for (auto it : this->connections_) { // create a cell definition map:
88  celldefinition[(*it).getDomainIdentifier()].push_back(it);
89  }
90 
91  for (auto it : celldefinition) { // loop the cells
92  startC = it.second[0];
93  tempC = startC;
94  polygonpointcounter = 0;
95  polygonvector.push_back(vtkSmartPointer<vtkPolygon>::New());
96 
97  do {
98  polygonpoints->InsertNextPoint(tempC->getGeometryNodes().second->getXPos(),
99  tempC->getGeometryNodes().second->getYPos(),
100  0.0); // add the points
101 
102  polygonvector.back()->GetPointIds()->InsertId(polygonpointcounter, globalpointcounter);
103 
104  tempC = tempC->getGeometryNodes().second->getConnection<1>(); // go to next connection
105  globalpointcounter++;
106  polygonpointcounter++;
107  } while(tempC != startC); // do until reaching beginning of the polygon
108  polygoncounter++;
109  }
110 
111  for (auto k : polygonvector) { // add the polygons to a list of polygons
112  polygons->InsertNextCell(k);
113  }
114 
115  polygonPolyData->SetPoints(polygonpoints); // add the points to the polydata container
116  polygonPolyData->SetPolys(polygons);
117 
118 #if VTK_MAJOR_VERSION <= 5
119  tri->SetInput(polygonPolyData); //The output of the triangle filter will be a triangulation of the inputPolyData
120  map->SetInput(tri->GetOutput());
121 #else
122  tri->SetInputData(polygonPolyData); //The output of the triangle filter will be a triangulation of the inputPolyData
123  map->SetInputData(tri->GetOutput());
124 #endif
125 
126  actor->SetMapper(map);
127 
128  renderWindow->SetOffScreenRendering( 1 );
129  renderWindow->AddRenderer(renderer);
130 
131 
132  renderer->AddActor(actor);
133  renderer->SetBackground(0,0,0);
134  renderWindow->Render();
135 
136  windowToImageFilter->SetInput(renderWindow);
137  windowToImageFilter->SetMagnification(3); //set the resolution of the output image (3 times the current resolution of vtk render window)
138  //windowToImageFilter->SetInputBufferTypeToRGBA(); //also record the alpha (transparency) channel
139  windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
140  windowToImageFilter->Update();
141 
142  writer->SetFileName(filename.str().c_str());
143  writer->SetInputConnection(windowToImageFilter->GetOutputPort());
144  writer->Write();
145 }
146 }
147 } // end namespace
virtual void operator()(unsigned int time) const
operator() Writes the report
const std::string filename_
filename_ Stores the filename of this functor