VPTissue Reference Manual
CBMBuilder.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 "CBMBuilder.h"
21 
22 #include "Cell.h"
23 #include "Mesh.h"
24 #include "Node.h"
25 
26 #include <iostream>
27 
28 namespace SimPT_Sim {
29 
30 using namespace std;
31 using namespace SimPT_Sim;
32 
33 CBMBuilder::CBMBuilder()
34 {
35 }
36 
37 shared_ptr<Mesh> CBMBuilder::Build(const Mesh& src)
38 {
39  //Initialize.
40  auto target = make_shared<Mesh>(src.m_num_chemicals);
41 
42  vector<Wall*> newWalls; //Will be filled with the copied walls later for efficiency reasons.
43 
44 
45  //Lambdas.
46  auto find_cell = [&target](Cell* c) {
47  return (c->GetIndex() == -1 ?
48  target->m_boundary_polygon : target->m_cells[c->GetIndex()]);
49  };
50  auto find_node = [&target](Node* n) {
51  return target->m_nodes[n->GetIndex()];
52  };
53  auto find_wall = [&newWalls](Wall* w) {
54  return newWalls[w->GetIndex()];
55  };
56 
57  //Copy nodes.
58  for (const auto& n : src.m_nodes) {
59  target->BuildNode(n->GetIndex(), *n, n->IsAtBoundary())->NodeAttributes::operator=(*n);
60  }
61 
62  //Copy cells (without the walls set).
63  for (auto c : src.m_cells) {
64  vector<unsigned int> node_ids;
65  transform(
66  c->GetNodes().begin(),
67  c->GetNodes().end(),
68  back_inserter(node_ids),
69  [](Node* n){return n->GetIndex();});
70  target->BuildCell(c->GetIndex(), node_ids)->CellAttributes::operator=(*c);
71  }
72 
73  //Copy boundary polygon (without the walls set).
74  if (src.m_boundary_polygon != nullptr) {
75  vector<unsigned int> node_ids;
76  transform(
77  src.m_boundary_polygon->GetNodes().begin(),
78  src.m_boundary_polygon->GetNodes().end(),
79  back_inserter(node_ids),
80  [](Node* n){return n->GetIndex();});
81  target->BuildBoundaryPolygon(node_ids)->CellAttributes::operator=(*src.m_boundary_polygon);
82  }
83 
84  //Copy walls.
85  for (const auto& w : src.m_walls) {
86  Node* n1 = find_node(w->GetN1());
87  Node* n2 = find_node(w->GetN2());
88  Cell* c1 = find_cell(w->GetC1());
89  Cell* c2 = find_cell(w->GetC2());
90  target->BuildWall(w->GetIndex(), n1, n2, c1, c2)->WallAttributes::operator=(*w);
91  }
92 
93  //Copy walls in a local vector (for an efficient copy).
94  copy(target->m_walls.begin(), target->m_walls.end(), back_inserter(newWalls));
95 
96  //Set the walls in the cells and boundary polygon.
97  for (auto c : src.m_cells) {
98  transform(
99  c->GetWalls().begin(),
100  c->GetWalls().end(),
101  back_inserter(find_cell(c)->GetWalls()),
102  find_wall);
103  }
104  if (src.m_boundary_polygon != nullptr) {
105  transform(
106  src.m_boundary_polygon->GetWalls().begin(),
107  src.m_boundary_polygon->GetWalls().end(),
108  back_inserter(target->m_boundary_polygon->GetWalls()),
109  find_wall);
110  }
111 
112  //Node owning neighbors are already set (see Mesh::BuildCell).
113 
114  //Set node owning walls.
115  for (const auto& node_owning_wall : src.m_node_owning_walls) {
116  auto pair = target->m_node_owning_walls.insert(
117  make_pair(find_node(node_owning_wall.first), list<Wall*>()));
118  assert(pair.second && "node_owning_wall");
119  transform(
120  node_owning_wall.second.begin(),
121  node_owning_wall.second.end(),
122  back_inserter((pair.first)->second),
123  find_wall);
124  }
125 
126  //Set wall neighbors.
127  for (const auto& wall_neighbor : src.m_wall_neighbors) {
128  auto pair = target->m_wall_neighbors.insert(
129  make_pair(find_cell(wall_neighbor.first), list<Cell*>()));
130  assert(pair.second && "m_wall_neighbors");
131  transform(
132  wall_neighbor.second.begin(),
133  wall_neighbor.second.end(),
134  back_inserter((pair.first)->second),
135  find_cell);
136  }
137 
138  return target;
139 }
140 
141 }
STL namespace.
A cell contains walls and nodes.
Definition: Cell.h:48
Node in cell wall.
Definition: Node.h:39
const std::list< Wall * > & GetWalls() const
Access the cell's walls.
Definition: Cell.h:88
Namespace for the core simulator.
Interface for Cell.
std::shared_ptr< Mesh > Build(const Mesh &mesh)
Build a mesh exactly like the given mesh.
Definition: CBMBuilder.cpp:37
const std::vector< Node * > & GetNodes() const
Access the nodes of cell's polygon.
Definition: Cell.h:79
Interface for Node.
Structure of cells; key data structure.
Definition: Mesh.h:62
Interface of CBMBuilder.
A cell wall, runs between cell corner points and consists of wall elements.
Definition: Wall.h:48
Interface for Mesh.