Chaste  Build::
MappableGridGenerator.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4  All rights reserved.
5 
6  University of Oxford means the Chancellor, Masters and Scholars of the
7  University of Oxford, having an administrative office at Wellington
8  Square, Oxford OX1 2JD, UK.
9 
10  This file is part of Chaste.
11 
12  Redistribution and use in source and binary forms, with or without
13  modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34  */
35 
36 #include <math.h>
37 #include <vector>
38 #include "UblasIncludes.hpp"
39 #include "Polygon.hpp"
40 #include "DimensionalChastePoint.hpp"
41 #include "MappableGridGenerator.hpp"
42 #include "BaseUnits.hpp"
43 
45  mReferenceLength(BaseUnits::Instance()->GetReferenceLengthScale())
46 {
47 }
48 
50 {
51 
52 }
53 
54 boost::shared_ptr<Part<3> > MappableGridGenerator::GeneratePlane(unsigned numX, unsigned numY, bool withEndCaps)
55 {
56  // Make a regular grid of polygons
57  // Front vertices
58  std::vector<boost::shared_ptr<DimensionalChastePoint<3> > > vertices;
59  for(unsigned jdx=0; jdx< numY; jdx++)
60  {
61  for(unsigned idx=0; idx<numX; idx++)
62  {
63  double x_location = double(idx);
64  double y_location = double(jdx);
65  double z_location = 0.0;
66 
67  // Create the vertices
68  vertices.push_back(DimensionalChastePoint<3>::Create(x_location, y_location, z_location, mReferenceLength));
69  }
70  }
71 
72  // Back vertices
73  for(unsigned jdx=0; jdx< numY; jdx++)
74  {
75  for(unsigned idx=0; idx<numX; idx++)
76  {
77  double x_location = double(idx);
78  double y_location = double(jdx);
79  double z_location = 1.0;
80 
81  // Create the vertices
82  vertices.push_back(DimensionalChastePoint<3>::Create(x_location, y_location, z_location, mReferenceLength));
83 
84  }
85  }
86 
87  // Make the polygons
88  // Front face
89  std::vector<boost::shared_ptr<Polygon<3> > > polygons;
90  for(unsigned jdx=0; jdx< numY - 1; jdx++)
91  {
92  for(unsigned idx=0; idx<numX - 1; idx++)
93  {
94  unsigned front_left_index = idx + numX * jdx;
95  unsigned front_right_index = idx + 1 + numX * jdx;
96  unsigned front_left_top_index = idx + numX * (jdx+1);
97  unsigned front_right_top_index = idx + 1 + numX * (jdx+1);
98 
99  std::vector<boost::shared_ptr<DimensionalChastePoint<3> > > poly_vertices;
100  poly_vertices.push_back(vertices[front_left_index]);
101  poly_vertices.push_back(vertices[front_right_index]);
102  poly_vertices.push_back(vertices[front_right_top_index]);
103  poly_vertices.push_back(vertices[front_left_top_index]);
104  polygons.push_back(Polygon<3>::Create(poly_vertices));
105  }
106  }
107 
108  // Back face
109  for(unsigned jdx=0; jdx< numY - 1; jdx++)
110  {
111  for(unsigned idx=0; idx<numX - 1; idx++)
112  {
113  unsigned front_left_index = idx + numX * jdx + numX*numY;
114  unsigned front_right_index = idx + 1 + numX * jdx + numX*numY;
115  unsigned front_left_top_index = idx + numX * (jdx+1) + numX*numY;
116  unsigned front_right_top_index = idx + 1 + numX * (jdx+1) + numX*numY;
117 
118  std::vector<boost::shared_ptr<DimensionalChastePoint<3> > > poly_vertices;
119  poly_vertices.push_back(vertices[front_left_index]);
120  poly_vertices.push_back(vertices[front_right_index]);
121  poly_vertices.push_back(vertices[front_right_top_index]);
122  poly_vertices.push_back(vertices[front_left_top_index]);
123  polygons.push_back(Polygon<3>::Create(poly_vertices));
124  }
125  }
126 
127  if(withEndCaps)
128  {
129  // Left face
130  for(unsigned jdx=0; jdx< numY - 1; jdx++)
131  {
132  unsigned front_index = numX * jdx;
133  unsigned top_front_index = numX * (jdx+1);
134  unsigned back_index = numX * jdx + numX*numY;
135  unsigned top_back_index = numX * (jdx+1) + numX*numY;
136 
137  std::vector<boost::shared_ptr<DimensionalChastePoint<3> > > poly_vertices;
138  poly_vertices.push_back(vertices[front_index]);
139  poly_vertices.push_back(vertices[top_front_index]);
140  poly_vertices.push_back(vertices[top_back_index]);
141  poly_vertices.push_back(vertices[back_index]);
142  polygons.push_back(Polygon<3>::Create(poly_vertices));
143  }
144 
145  // Right face
146  for(unsigned jdx=0; jdx< numY - 1; jdx++)
147  {
148  unsigned front_index = numX * (jdx+1) - 1;
149  unsigned top_front_index = numX * (jdx+2) - 1;
150  unsigned back_index = numX * (jdx + 1) - 1 + numX*numY;
151  unsigned top_back_index = numX * (jdx+2) -1 + numX*numY;
152 
153  std::vector<boost::shared_ptr<DimensionalChastePoint<3> > > poly_vertices;
154  poly_vertices.push_back(vertices[front_index]);
155  poly_vertices.push_back(vertices[top_front_index]);
156  poly_vertices.push_back(vertices[top_back_index]);
157  poly_vertices.push_back(vertices[back_index]);
158  polygons.push_back(Polygon<3>::Create(poly_vertices));
159  }
160  }
161 
162  // Bottom face
163  for(unsigned idx=0; idx< numX - 1; idx++)
164  {
165  unsigned front_index = idx;
166  unsigned front_right_index = idx + 1;
167  unsigned back_index = idx + numX*numY;
168  unsigned back_right_index = idx + 1 + numX*numY;
169 
170  std::vector<boost::shared_ptr<DimensionalChastePoint<3> > > poly_vertices;
171  poly_vertices.push_back(vertices[front_index]);
172  poly_vertices.push_back(vertices[front_right_index]);
173  poly_vertices.push_back(vertices[back_right_index]);
174  poly_vertices.push_back(vertices[back_index]);
175  polygons.push_back(Polygon<3>::Create(poly_vertices));
176  }
177 
178  // Top face
179  for(unsigned idx=0; idx< numX - 1; idx++)
180  {
181  unsigned front_index = idx + numX*(numY-1);
182  unsigned front_right_index = idx + 1 + numX*(numY-1);
183  unsigned back_index = idx + + numX*(numY-1) + numX*numY;
184  unsigned back_right_index = idx + numX*(numY-1) + 1 + numX*numY;
185 
186  std::vector<boost::shared_ptr<DimensionalChastePoint<3> > > poly_vertices;
187  poly_vertices.push_back(vertices[front_index]);
188  poly_vertices.push_back(vertices[front_right_index]);
189  poly_vertices.push_back(vertices[back_right_index]);
190  poly_vertices.push_back(vertices[back_index]);
191  polygons.push_back(Polygon<3>::Create(poly_vertices));
192  }
193 
194  // Create a part
195  boost::shared_ptr<Part<3> > p_part = Part<3>::Create();
196  for(unsigned idx=0; idx<polygons.size(); idx++)
197  {
198  p_part->AddPolygon(polygons[idx], true);
199  }
200  return p_part;
201 }
202 
203 boost::shared_ptr<Part<3> > MappableGridGenerator::GenerateCylinder(double cylinderRadius,
204  double cylinderThickness,
205  double cylinderHeight,
206  unsigned numX,
207  unsigned numY,
208  double cylinderAngle)
209 {
210  if(cylinderAngle > 2.0 * M_PI)
211  {
212  EXCEPTION("The cylinder angle should be <= 2*pi");
213  }
214 
215  boost::shared_ptr<Part<3> > p_part = GeneratePlane(numX, numY, !(cylinderAngle == 2.0 * M_PI));
216 
217  // Get the part extents
218  std::vector<units::quantity<unit::length> > bbox = p_part->GetBoundingBox();
219 
220  // Get the vertices
221  std::vector<boost::shared_ptr<DimensionalChastePoint<3> > > vertices = p_part->GetVertices();
222  for(unsigned idx =0; idx<vertices.size(); idx++)
223  {
224  c_vector<double, 3> vertex_location = vertices[idx]->GetLocation(mReferenceLength);
225  double x_frac = vertex_location[0]*mReferenceLength / (bbox[1] - bbox[0]);
226  double angle = x_frac * cylinderAngle;
227 
228  double y_frac = vertex_location[1]*mReferenceLength / (bbox[3] - bbox[2]);
229  double height = y_frac * cylinderHeight;
230 
231  double z_frac = vertex_location[2]*mReferenceLength / (bbox[5] - bbox[4]);
232  double radius = cylinderRadius - cylinderThickness * z_frac;
233 
234  // Get the new x
235  c_vector<double, 3> new_position;
236  new_position[0] = radius * std::cos(angle);
237 
238  // Get the new y
239  new_position[1] = height;
240 
241  // Get the new z
242  new_position[2] = radius * std::sin(angle);
243  vertices[idx]->TranslateTo(DimensionalChastePoint<3>(new_position, mReferenceLength));
244  }
245 
246  p_part->MergeCoincidentVertices();
247  return p_part;
248 }
249 
250 boost::shared_ptr<Part<3> > MappableGridGenerator::GenerateHemisphere(double sphereRadius,
251  double sphereThickness,
252  unsigned numX,
253  unsigned numY,
254  double sphereAzimuthAngle,
255  double spherePolarAngle)
256 {
257  if(sphereAzimuthAngle >= 2.0 * M_PI)
258  {
259  EXCEPTION("The azimuth angle should be < 2*pi");
260  }
261 
262  if(spherePolarAngle >= M_PI)
263  {
264  EXCEPTION("The polar angle should be < pi");
265  }
266 
267  boost::shared_ptr<Part<3> > p_part = GeneratePlane(numX, numY);
268 
269  // The part extents
270  std::vector<units::quantity<unit::length> > bbox = p_part->GetBoundingBox();
271 
272  // Get the vertices
273  std::vector<boost::shared_ptr<DimensionalChastePoint<3> > > vertices = p_part->GetVertices();
274  for(unsigned idx =0; idx<vertices.size(); idx++)
275  {
276  c_vector<double, 3> vertex_location = vertices[idx]->GetLocation(mReferenceLength);
277  double x_frac = vertex_location[0]*mReferenceLength / (bbox[1] - bbox[0]);
278  double azimuth_angle = x_frac * sphereAzimuthAngle;
279 
280  double y_frac = vertex_location[1]*mReferenceLength / (bbox[3] - bbox[2]);
281  double polar_angle = y_frac * spherePolarAngle;
282 
283  double z_frac = vertex_location[2]*mReferenceLength / (bbox[5] - bbox[4]);
284  double radius = sphereRadius - sphereThickness * z_frac;
285 
286  // Get the new x
287  c_vector<double, 3> new_position;
288  new_position[0] = radius * std::cos(azimuth_angle) * std::sin(polar_angle);
289 
290  // Get the new y
291  new_position[1] = radius * std::cos(polar_angle);
292 
293  // Get the new z
294  new_position[2] = radius * std::sin(azimuth_angle) * std::sin(polar_angle);
295 
296  vertices[idx]->TranslateTo(DimensionalChastePoint<3>(new_position, mReferenceLength));
297  }
298 
299  p_part->MergeCoincidentVertices();
300  return p_part;
301 }
boost::shared_ptr< Part< 3 > > GenerateCylinder(double cylinderRadius, double cylinderThickness, double cylinderHeight, unsigned numX, unsigned numY, double cylinderAngle=2.0 *M_PI)
boost::shared_ptr< Part< 3 > > GenerateHemisphere(double sphereRadius, double sphereThickness, unsigned numX, unsigned numY, double sphereAzimuthAngle=2.0 *M_PI, double spherePolarAngle=0.5 *M_PI)
static boost::shared_ptr< Part< DIM > > Create()
Definition: Part.cpp:63
boost::shared_ptr< Part< 3 > > GeneratePlane(unsigned numX, unsigned numY, bool withEndCaps=true)
units::quantity< unit::length > mReferenceLength