VPTissue Reference Manual
PlyExporter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "PlyExporter.h"
21 
22 #include "bio/Cell.h"
23 #include "bio/Edge.h"
24 #include "bio/Mesh.h"
25 #include "sim/CoreData.h"
26 #include "sim/SimInterface.h"
27 
28 #include <fstream>
29 #include <QFile>
30 #include <QString>
31 #include <QTextStream>
32 
33 namespace SimPT_Shell {
34 
35 using namespace std;
36 using namespace SimPT_Sim;
37 
38 bool PlyExporter::Export(shared_ptr<SimInterface> sim, string const& file_path, bool overwrite)
39 {
40  if (ifstream(file_path).good()) {
41  if (overwrite) {
42  // Qt doesn't overwrite by default, must delete file first
43  QFile::remove(QString::fromStdString(file_path));
44  } else {
45  return false; // Don't overwrite
46  }
47  }
48 
49  bool status = false;
50  QFile file(QString::fromStdString(file_path));
51  if (file.open(QIODevice::WriteOnly)) {
52  const auto mesh = sim->GetCoreData().m_mesh;
53  QTextStream stream(&file);
54  StreamHeader(*mesh, stream);
55  StreamBody(*mesh, stream);
56  file.close();
57  status = true;
58  }
59  return status;
60 }
61 
63 {
64  return "ply";
65 }
66 
67 void PlyExporter::StreamHeader(const Mesh& mesh, QTextStream& stream)
68 {
69  stream << "ply" << endl;
70  stream << "format ascii 1.0" << endl;
71  stream << "element vertex " << mesh.GetNodes().size() << endl;
72  stream << "property double x" << endl;
73  stream << "property double y" << endl;
74  stream << "property double z" << endl;
75  stream << "property uint attributes.fixed" << endl; //use uint for boolean
76  stream << "element face " << mesh.GetCells().size() << endl;
77  stream << "property list uchar int vertex_indices" << endl;
78  stream << "property list uchar int wall_indices" << endl;
79  stream << "property list uchar double attributes.chemical_array" << endl;
80  stream << "element boundary 1" << endl;
81  stream << "property list uchar int vertex_indices" << endl;
82  stream << "property list uchar int wall_indices" << endl;
83  stream << "element wall " << mesh.GetWalls().size() << endl;
84  stream << "property int node1" << endl;
85  stream << "property int node2" << endl;
86  stream << "property int cell1" << endl;
87  stream << "property int cell2" << endl;
88  stream << "property double attributes.rest_length" << endl;
89  stream << "property double attributes.rest_length_init" << endl;
90  stream << "element edge " << mesh.GetCells().size() + mesh.GetNodes().size() - 1 << endl;
91  stream << "property int vertex1" << endl;
92  stream << "property int vertex2" << endl;
93  stream << "end_header" << endl;
94 }
95 
96 void PlyExporter::StreamBody(const Mesh& mesh, QTextStream& stream)
97 {
98  //Stream nodes (order matters for indexing when streaming cells).
99  for (const auto& n : mesh.GetNodes()) {
100  stream << (*n)[0] << " " << (*n)[1] << " " << (*n)[2] << " " << (unsigned int)n->IsFixed() << endl;
101  }
102 
103  //Stream cells.
104  for (auto c : mesh.GetCells()) {
105  stream << c->GetNodes().size();
106  for (Node* n : c->GetNodes()) {
107  stream << " " << n->GetIndex();
108  }
109  stream << " " << c->GetWalls().size();
110  for (Wall* w : c->GetWalls()) {
111  stream << " " << w->GetIndex();
112  }
113  auto chemicals = c->GetChemicals();
114  stream << " " << chemicals.size();
115  for (double chemical : chemicals) {
116  stream << " " << chemical;
117  }
118  stream << endl;
119  }
120 
121  //Stream boundary polygon.
122  stream << mesh.GetBoundaryPolygon()->GetNodes().size();
123  for (Node* n : mesh.GetBoundaryPolygon()->GetNodes()) {
124  stream << " " << n->GetIndex();
125  }
126  stream << " " << mesh.GetBoundaryPolygon()->GetWalls().size();
127  for (Wall* w : mesh.GetBoundaryPolygon()->GetWalls()) {
128  stream << " " << w->GetIndex();
129  }
130  stream << endl;
131 
132  //Stream walls and collect edges.
133  std::list<Edge> edges;
134  for (const auto& w : mesh.GetWalls()) {
135  stream << w->GetN1()->GetIndex() << " " << w->GetN2()->GetIndex() << " ";
136  stream << w->GetC1()->GetIndex() << " " << w->GetC2()->GetIndex() << " ";
137  stream << w->GetRestLength() << endl;
138 
139  auto newEdges = w->GetEdges();
140  edges.insert(edges.end(), newEdges.begin(), newEdges.end());
141  }
142  edges.unique();
143 
144  //Stream edges.
145  for (const auto& e : edges) {
146  stream << e.GetFirst()->GetIndex() << " " << e.GetSecond()->GetIndex() << endl;
147  }
148 }
149 
150 } // namespace
Simulator interface.
STL namespace.
Node in cell wall.
Definition: Node.h:39
Core data used during model execution.
const std::list< Wall * > & GetWalls() const
Access the cell's walls.
Definition: Cell.h:88
Namespace for SimPT shell package.
Definition: Client.cpp:50
const std::vector< Cell * > & GetCells() const
The cells of this mesh, EXCLUDING the boundary polygon.
Definition: Mesh.h:108
Namespace for the core simulator.
Interface for Cell.
const std::vector< Node * > & GetNodes() const
Access the nodes of cell's polygon.
Definition: Cell.h:79
Cell * GetBoundaryPolygon() const
Get the boundary polygon of the mesh.
Definition: Mesh.h:105
static std::string GetFileExtension()
File extension associated with this export format.
Definition: PlyExporter.cpp:62
Interface for PlyExporter.
const std::vector< Node * > & GetNodes() const
The nodes of the mesh.
Definition: Mesh.h:136
Interface for Edge.
Structure of cells; key data structure.
Definition: Mesh.h:62
const std::vector< Wall * > & GetWalls() const
The walls of this mesh.
Definition: Mesh.h:148
static bool Export(std::shared_ptr< SimPT_Sim::SimInterface > sim, std::string const &file_path, bool overwrite=true)
Export sim state to ply format.
Definition: PlyExporter.cpp:38
A cell wall, runs between cell corner points and consists of wall elements.
Definition: Wall.h:48
Interface for Mesh.