VPTissue Reference Manual
CsvExporter.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 "CsvExporter.h"
21 
22 #include "bio/Cell.h"
23 #include "bio/Mesh.h"
24 #include "bio/Wall.h"
25 #include "math/MeshGeometry.h"
26 #include "sim/CoreData.h"
27 #include "sim/SimInterface.h"
28 
29 #include <QFile>
30 #include <QString>
31 #include <QTextStream>
32 #include <fstream>
33 #include <tuple>
34 
35 namespace SimPT_Shell {
36 
37 using namespace std;
38 using namespace SimPT_Sim;
39 
40 bool CsvExporter::Export(shared_ptr<SimPT_Sim::SimInterface> sim, string const& file_path, bool overwrite)
41 {
42  if (ifstream(file_path).good()) {
43  if (overwrite) {
44  // Qt doesn't overwrite by default, must delete file first
45  QFile::remove(QString::fromStdString(file_path));
46  } else {
47  return false; // Don't overwrite
48  }
49  }
50 
51  bool status = false;
52  QFile file(QString::fromStdString(file_path));
53  if (file.open(QIODevice::WriteOnly)) {
54  const auto mesh = sim->GetCoreData().m_mesh.get();
55  QTextStream stream(&file);
56  StreamCellData(mesh, stream);
57  StreamWallData(mesh, stream);
58  StreamMeshData(mesh, stream);
59  file.close();
60  status = true;
61  }
62  return status;
63 }
64 
66 {
67  return "csv";
68 }
69 
70 void CsvExporter::StreamCellData(const Mesh* mesh, QTextStream& csv_stream)
71 {
72  csv_stream << "\"Cell Index\",\"Center of mass (x)\",\"Center of mass (y)\",\"Cell length\",\"CK/AUX\"";
73 
74  const unsigned int num_chem = mesh->GetNumChemicals();
75 
76  for (unsigned int c = 0; c < num_chem; c++) {
77  csv_stream << ",\"[Chemical " << c << "]" << "\"";
78  }
79  csv_stream << endl;
80  for (auto const& cell : mesh->GetCells()) {
81  auto centroid = cell->GetCentroid();
82  csv_stream << cell->GetIndex() << ", " << centroid[0] << ", " << centroid[1] << ", "
83  << cell->GetArea() / 16 /*<< ", "
84  << (cell->GetChemical(0)) / (cell->GetChemical(1)) << get<0>(cell->GetGeoData().GetEllipseAxes())*/;
85  for (unsigned int c = 0; c < num_chem; c++) {
86  csv_stream << ", " << (cell->GetChemical(c)) / (cell->GetArea());
87  }
88  csv_stream << endl;
89  }
90 }
91 
92 void CsvExporter::StreamMeshData(const Mesh* mesh, QTextStream& csv_stream)
93 {
94  csv_stream << "\"Morph area\",\"Number of cells\",\"Number of nodes\",\"Compactness\",\"Hull area\",\"Morph circumference\",\"Hull circumference\""
95  << endl;
96 
97  auto res_tuple = MeshGeometry::Compactness(mesh);
98  const double res_compactness = get<0>(res_tuple);
99  const double res_area = get<1>(res_tuple);
100  const double hull_circumference = get<2>(res_tuple);
101  const double morph_circumference = mesh->GetBoundaryPolygon()->GetCircumference();
102  const double mesh_area = mesh->GetBoundaryPolygon()->GetArea();
103 
104  csv_stream << mesh_area << ", " << mesh->GetCells().size() << ", " << mesh->GetNodes().size()
105  << ", " << res_compactness << ", " << res_area << ", "
106  << morph_circumference << ", " << hull_circumference << endl;
107 }
108 
109 void CsvExporter::StreamWallData(const Mesh* mesh, QTextStream& csv_stream)
110 {
111  csv_stream << "\"Wall Index\",\"Cell A\",\"Cell B\",\"Length\"";
112 
113  const unsigned int num_chem = mesh->GetNumChemicals();
114 
115  for (unsigned int c = 0; c < num_chem; c++) {
116  csv_stream << ",\"Transporter A:" << c << "\"";
117  }
118  for (unsigned int c = 0; c < num_chem; c++) {
119  csv_stream << ",\"Transporter B:" << c << "\"";
120  }
121  csv_stream << endl;
122  for (auto const& wall : mesh->GetWalls()) {
123  csv_stream << wall->GetIndex() << "," << wall->GetC1()->GetIndex() << ","
124  << wall->GetC2()->GetIndex() << "," << wall->GetLength();
125  for (unsigned int c = 0; c < num_chem; c++) {
126  csv_stream << "," << wall->GetTransporters1(c);
127  }
128  for (unsigned int c = 0; c < num_chem; c++) {
129  csv_stream << "," << wall->GetTransporters2(c);
130  }
131  csv_stream << endl;
132  }
133 }
134 
135 } // namespace
Simulator interface.
STL namespace.
Core data used during model execution.
Interface for MeshGeometry.
static std::tuple< double, double, double > Compactness(const Mesh *mesh)
Calculate the convex hull of the cells in the mesh and returns respectively the ratio of the areas of...
Namespace for SimPT shell package.
Definition: Client.cpp:50
static bool Export(std::shared_ptr< SimPT_Sim::SimInterface > sim, std::string const &file_path, bool overwrite=true)
Export mesh state to csv format.
Definition: CsvExporter.cpp:40
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.
double GetCircumference() const
Return the circumference along the edges.
Definition: Cell.cpp:213
Cell * GetBoundaryPolygon() const
Get the boundary polygon of the mesh.
Definition: Mesh.h:105
Interface for Csv Exporter.
const std::vector< Node * > & GetNodes() const
The nodes of the mesh.
Definition: Mesh.h:136
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 std::string GetFileExtension()
File extension associated with this export format.
Definition: CsvExporter.cpp:65
double GetArea() const
Return the area of the cell.
Definition: Cell.cpp:178
Interface for Wall.
Interface for Mesh.