VPTissue Reference Manual
CsvGzExporter.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 "CsvGzExporter.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 CsvGzExporter::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.gz";
68 }
69 
70 void CsvGzExporter::StreamCellData(const Mesh* mesh, QTextStream& csv_stream)
71 {
72  csv_stream << "\"Cell Index\",\"Center of mass (x)\",\"Center of mass (y)\",\"Cell area\",\"Cell length\"";
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() << ", " << get<0>(cell->GetGeoData().GetEllipseAxes());
84  for (unsigned int c = 0; c < num_chem; c++) {
85  csv_stream << ", " << cell->GetChemical(c);
86  }
87  csv_stream << endl;
88  }
89 }
90 
91 void CsvGzExporter::StreamMeshData(const Mesh* mesh, QTextStream& csv_stream)
92 {
93  csv_stream << "\"Morph area\",\"Number of cells\",\"Number of nodes\",\"Compactness\",\"Hull area\",\"Morph circumference\",\"Hull circumference\""
94  << endl;
95 
96  auto res_tuple = MeshGeometry::Compactness(mesh);
97  const double res_compactness = get<0>(res_tuple);
98  const double res_area = get<1>(res_tuple);
99  const double hull_circumference = get<2>(res_tuple);
100  const double morph_circumference = mesh->GetBoundaryPolygon()->GetCircumference();
101  const double mesh_area = mesh->GetBoundaryPolygon()->GetArea();
102 
103  csv_stream << mesh_area << ", " << mesh->GetCells().size() << ", " << mesh->GetNodes().size()
104  << ", " << res_compactness << ", " << res_area << ", "
105  << morph_circumference << ", " << hull_circumference << endl;
106 }
107 
108 void CsvGzExporter::StreamWallData(const Mesh* mesh, QTextStream& csv_stream)
109 {
110  csv_stream << "\"Wall Index\",\"Cell A\",\"Cell B\",\"Length\"";
111 
112  const unsigned int num_chem = mesh->GetNumChemicals();
113 
114  for (unsigned int c = 0; c < num_chem; c++) {
115  csv_stream << ",\"Transporter A:" << c << "\"";
116  }
117  for (unsigned int c = 0; c < num_chem; c++) {
118  csv_stream << ",\"Transporter B:" << c << "\"";
119  }
120  csv_stream << endl;
121  for (auto const& wall : mesh->GetWalls()) {
122  csv_stream << wall->GetIndex() << "," << wall->GetC1()->GetIndex() << ","
123  << wall->GetC2()->GetIndex() << "," << wall->GetLength();
124  for (unsigned int c = 0; c < num_chem; c++) {
125  csv_stream << "," << wall->GetTransporters1(c);
126  }
127  for (unsigned int c = 0; c < num_chem; c++) {
128  csv_stream << "," << wall->GetTransporters2(c);
129  }
130  csv_stream << endl;
131  }
132 }
133 
134 } // 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
const std::vector< Cell * > & GetCells() const
The cells of this mesh, EXCLUDING the boundary polygon.
Definition: Mesh.h:108
Namespace for the core simulator.
static bool Export(std::shared_ptr< SimPT_Sim::SimInterface > sim, std::string const &file_path, bool overwrite=true)
Export mesh state to csv format.
Interface for Cell.
static std::string GetFileExtension()
File extension associated with this export format.
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
const std::vector< Node * > & GetNodes() const
The nodes of the mesh.
Definition: Mesh.h:136
Interface for CsvGzExporter.
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
double GetArea() const
Return the area of the cell.
Definition: Cell.cpp:178
Interface for Wall.
Interface for Mesh.