37 #define _BACKWARD_BACKWARD_WARNING_H 1 //Cut out the vtk deprecated warning 38 #include "vtkCleanPolyData.h" 39 #include "vtkSelectEnclosedPoints.h" 40 #include <vtkVersion.h> 41 #include <boost/random.hpp> 42 #include <boost/generator_iterator.hpp> 43 #include "VesselNode.hpp" 44 #include "VesselSegment.hpp" 46 #include "VesselSurfaceGenerator.hpp" 48 #include "GeometryTools.hpp" 49 #include "BaseUnits.hpp" 51 template<
unsigned DIM>
54 mVtkPart(vtkSmartPointer<vtkPolyData>()),
57 mReferenceLength(
BaseUnits::Instance()->GetReferenceLengthScale()),
62 template<
unsigned DIM>
69 template<
unsigned DIM>
75 template<
unsigned DIM>
79 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > vertices;
80 double seg_angle = 2.0 * M_PI / double(numSegments);
81 for (
unsigned idx = 0; idx < numSegments; idx++)
83 double angle = seg_angle * double(idx);
99 template<
unsigned DIM>
101 units::quantity<unit::length> depth,
103 unsigned numSegments)
105 boost::shared_ptr<Polygon<DIM> > p_circle =
AddCircle(radius, centre, numSegments);
110 template<
unsigned DIM>
112 units::quantity<unit::length> sizeY,
113 units::quantity<unit::length> sizeZ,
116 boost::shared_ptr<Polygon<DIM> > p_rectangle =
AddRectangle(sizeX, sizeY, origin);
121 template<
unsigned DIM>
127 template<
unsigned DIM>
137 template<
unsigned DIM>
143 if (
mFacets.size() == 0 || newFacet)
149 mFacets[0]->AddPolygon(pPolygon);
154 pFacet->AddPolygon(pPolygon);
160 template<
unsigned DIM>
162 units::quantity<unit::length> sizeY,
166 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > vertices;
188 template<
unsigned DIM>
193 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > vertices;
194 std::vector<boost::shared_ptr<VesselNode<DIM> > > nodes = pVesselNetwork->GetNodes();
195 for (
unsigned idx = 0; idx < nodes.size(); idx++)
197 units::quantity<unit::length> length_Scale = nodes[idx]->rGetLocation().GetReferenceLengthScale();
202 for (
unsigned kdx = 0; kdx < vertices.size(); kdx++)
204 for (
unsigned idx = 0; idx <
mFacets.size(); idx++)
206 if(
mFacets[idx]->ContainsPoint(*(vertices[kdx])))
214 std::vector<boost::shared_ptr<Vessel<DIM> > > vessels = pVesselNetwork->GetVessels();
215 for (
unsigned idx = 0; idx < vessels.size(); idx++)
217 std::vector<boost::shared_ptr<VesselSegment<DIM> > > segments = vessels[idx]->GetSegments();
218 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > segment_vertices;
219 for (
unsigned jdx = 0; jdx < segments.size(); jdx++)
221 unsigned node0_index = pVesselNetwork->GetNodeIndex(segments[jdx]->GetNode(0));
222 unsigned node1_index = pVesselNetwork->GetNodeIndex(segments[jdx]->GetNode(1));
225 segment_vertices.push_back(vertices[node0_index]);
227 segment_vertices.push_back(vertices[node1_index]);
237 std::vector<boost::shared_ptr<Polygon<DIM> > > polygons = generator.
GetSurfacePolygons();
238 std::vector<bool> polygon_on_facet;
240 for (
unsigned idx = 0; idx < polygons.size(); idx++)
242 bool on_facet =
false;
244 for (
unsigned jdx = 0; jdx <
mFacets.size(); jdx++)
246 if (
mFacets[jdx]->ContainsPoint(poly_centroid))
249 mFacets[jdx]->AddPolygon(polygons[idx]);
252 polygon_on_facet.push_back(on_facet);
256 for (
unsigned idx = 0; idx < polygons.size(); idx++)
258 if (!polygon_on_facet[idx])
264 std::vector<DimensionalChastePoint<DIM> > hole_locations = generator.
GetHoles();
265 for(
unsigned idx=0; idx<hole_locations.size(); idx++)
273 template<
unsigned DIM>
278 EXCEPTION(
"Only parts in 3D space can be extruded.");
281 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > original_vertices = pPolygon->GetVertices();
282 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > new_vertices;
283 for (
unsigned idx = 0; idx < original_vertices.size(); idx++)
285 c_vector<double, DIM> vertex_location = original_vertices[idx]->GetLocation(
mReferenceLength);
298 for (
unsigned idx = 0; idx < original_vertices.size(); idx++)
301 if (idx != original_vertices.size() - 1)
310 p_polygon->AddVertex(original_vertices[index2]);
311 p_polygon->AddVertex(new_vertices[index2]);
312 p_polygon->AddVertex(new_vertices[idx]);
322 template <
unsigned DIM>
326 std::vector<boost::shared_ptr<Vessel<DIM> > > vessels = pVesselNetwork->GetVessels();
327 for(
unsigned idx=0;idx<vessels.size();idx++)
329 if(!
IsPointInPart(vessels[idx]->GetStartNode()->rGetLocation()) &&
332 pVesselNetwork->RemoveVessel(vessels[idx],
true);
335 pVesselNetwork->UpdateAll();
338 template<
unsigned DIM>
344 template<
unsigned DIM>
350 template<
unsigned DIM>
353 for(
unsigned idx=0; idx<
mFacets.size(); idx++)
355 if(
mFacets[idx]->ContainsPoint(location))
360 EXCEPTION(
"No facet found at input location");
363 template<
unsigned DIM>
366 std::vector<boost::shared_ptr<Polygon<DIM> > > polygons =
GetPolygons();
367 std::set<boost::shared_ptr<DimensionalChastePoint<DIM> > > unique_vertices;
368 for (
unsigned idx = 0; idx < polygons.size(); idx++)
370 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > polygon_vertices = polygons[idx]->GetVertices();
371 std::copy(polygon_vertices.begin(), polygon_vertices.end(),
372 std::inserter(unique_vertices, unique_vertices.end()));
375 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > vertices;
376 vertices.insert(vertices.end(), unique_vertices.begin(), unique_vertices.end());
378 for (
unsigned idx = 0; idx < vertices.size(); idx++)
380 vertices[idx]->SetIndex(idx);
385 template<
unsigned DIM>
389 std::vector<unsigned> location_indices;
390 for(
unsigned kdx=0; kdx<num_z; kdx++)
392 for(
unsigned jdx=0; jdx<num_y; jdx++)
394 for(
unsigned idx=0; idx<num_x; idx++)
397 unsigned index = idx + num_x * jdx + num_x * num_y * kdx;
400 location_indices.push_back(index);
405 return location_indices;
408 template<
unsigned DIM>
411 std::vector<DimensionalChastePoint<DIM> > vertex_locs;
412 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > vertices =
GetVertices();
413 for (
unsigned idx = 0; idx < vertices.size(); idx++)
415 vertex_locs.push_back(*vertices[idx]);
420 template<
unsigned DIM>
423 std::vector<boost::shared_ptr<Polygon<DIM> > > polygons;
424 for (
unsigned idx = 0; idx <
mFacets.size(); idx++)
426 std::vector<boost::shared_ptr<Polygon<DIM> > > facet_polygons =
mFacets[idx]->GetPolygons();
427 polygons.insert(polygons.end(), facet_polygons.begin(), facet_polygons.end());
432 template<
unsigned DIM>
435 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > vertices =
GetVertices();
436 c_vector<double, 6> box;
438 for (
unsigned idx = 0; idx < vertices.size(); idx++)
440 c_vector<double, DIM> vertex_location = vertices[idx]->GetLocation(
mReferenceLength);
441 for (
unsigned jdx = 0; jdx < DIM; jdx++)
445 box[2 * jdx] = vertex_location[jdx];
446 box[2 * jdx + 1] = vertex_location[jdx];
450 if (vertex_location[jdx] < box[2 * jdx])
452 box[2 * jdx] = vertex_location[jdx];
454 if (vertex_location[jdx] > box[2 * jdx + 1])
456 box[2 * jdx + 1] = vertex_location[jdx];
462 std::vector<units::quantity<unit::length> > box_vector(6, 0.0*unit::metres);
463 for(
unsigned idx=0; idx<6; idx++)
471 template<
unsigned DIM>
477 template<
unsigned DIM>
483 std::vector<std::pair<unsigned, unsigned> > indexes;
484 std::vector<boost::shared_ptr<Polygon<DIM> > > polygons =
mFacets[0]->GetPolygons();
486 for (
unsigned idx = 0; idx < polygons.size(); idx++)
491 std::pair<unsigned, unsigned>(polygons[idx]->
GetVertices()[0]->GetIndex(),
496 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > vertices = polygons[idx]->GetVertices();
497 for (
unsigned jdx = 0; jdx < vertices.size() - 1; jdx++)
500 std::pair<unsigned, unsigned>(vertices[jdx]->GetIndex(), vertices[jdx + 1]->GetIndex()));
503 std::pair<unsigned, unsigned>(vertices[vertices.size() - 1]->GetIndex(), vertices[0]->GetIndex()));
510 template<
unsigned DIM>
518 vtkSmartPointer<vtkPolyData> p_part_data = vtkSmartPointer<vtkPolyData>::New();
519 vtkSmartPointer<vtkPoints> p_vertices = vtkSmartPointer<vtkPoints>::New();
521 p_part_data->Allocate(1, 1);
524 std::vector<boost::shared_ptr<Polygon<DIM> > > polygons =
GetPolygons();
525 unsigned vert_counter = 0;
526 for (vtkIdType idx = 0; idx < vtkIdType(polygons.size()); idx++)
528 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > vertices = polygons[idx]->GetVertices();
529 vtkSmartPointer<vtkPolygon> p_polygon = vtkSmartPointer<vtkPolygon>::New();
530 p_polygon->GetPointIds()->SetNumberOfIds(vertices.size());
531 for (vtkIdType jdx = 0; jdx < vtkIdType(vertices.size()); jdx++)
533 c_vector<double, DIM> vertex_location = vertices[jdx]->GetLocation(
mReferenceLength);
536 p_vertices->InsertNextPoint(vertex_location[0], vertex_location[1], vertex_location[2]);
540 p_vertices->InsertNextPoint(vertex_location[0], vertex_location[1], 0.0);
543 p_polygon->GetPointIds()->SetId(jdx, vert_counter);
546 p_part_data->InsertNextCell(p_polygon->GetCellType(), p_polygon->GetPointIds());
548 p_part_data->SetPoints(p_vertices);
550 vtkSmartPointer<vtkCleanPolyData> p_clean_data = vtkSmartPointer<vtkCleanPolyData>::New();
551 #if VTK_MAJOR_VERSION <= 5 552 p_clean_data->SetInput(p_part_data);
554 p_clean_data->SetInputData(p_part_data);
556 p_clean_data->Update();
558 mVtkPart = p_clean_data->GetOutput();
563 template<
unsigned DIM>
566 vtkSmartPointer<vtkPolyData> p_part =
GetVtk();
567 vtkSmartPointer<vtkPoints> p_points = vtkSmartPointer<vtkPoints>::New();
578 vtkSmartPointer<vtkPolyData> p_point_data = vtkSmartPointer<vtkPolyData>::New();
579 p_point_data->SetPoints(p_points);
582 vtkSmartPointer<vtkSelectEnclosedPoints> selectEnclosedPoints = vtkSmartPointer<vtkSelectEnclosedPoints>::New();
583 #if VTK_MAJOR_VERSION <= 5 584 selectEnclosedPoints->SetInput(p_point_data);
586 selectEnclosedPoints->SetInputData(p_point_data);
588 #if VTK_MAJOR_VERSION <= 5 589 selectEnclosedPoints->SetSurface(p_part);
591 selectEnclosedPoints->SetSurfaceData(p_part);
593 selectEnclosedPoints->Update();
595 return selectEnclosedPoints->IsInside(0);
598 template<
unsigned DIM>
601 vtkSmartPointer<vtkPolyData> p_part =
GetVtk();
602 vtkSmartPointer<vtkPoints> p_points = vtkSmartPointer<vtkPoints>::New();
604 for(
unsigned idx=0; idx<rLocations.size(); idx++)
618 vtkSmartPointer<vtkPolyData> p_point_data = vtkSmartPointer<vtkPolyData>::New();
619 p_point_data->SetPoints(p_points);
622 vtkSmartPointer<vtkSelectEnclosedPoints> selectEnclosedPoints = vtkSmartPointer<vtkSelectEnclosedPoints>::New();
623 #if VTK_MAJOR_VERSION <= 5 624 selectEnclosedPoints->SetInput(p_point_data);
626 selectEnclosedPoints->SetInputData(p_point_data);
628 #if VTK_MAJOR_VERSION <= 5 629 selectEnclosedPoints->SetSurface(p_part);
631 selectEnclosedPoints->SetSurfaceData(p_part);
633 selectEnclosedPoints->Update();
635 std::vector<bool> is_inside(rLocations.size());
636 for(
unsigned idx=0; idx<rLocations.size(); idx++)
638 is_inside[idx] = selectEnclosedPoints->IsInside(idx);
643 template<
unsigned DIM>
647 std::vector<boost::shared_ptr<Polygon<DIM> > > polygons =
GetPolygons();
648 for(
unsigned idx=0; idx<polygons.size(); idx++)
650 for(
unsigned jdx=0; jdx<polygons.size(); jdx++)
654 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > p1_verts = polygons[idx]->GetVertices();
655 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > p2_verts = polygons[jdx]->GetVertices();
656 for(
unsigned mdx=0; mdx<p1_verts.size(); mdx++)
658 for(
unsigned ndx=0; ndx<p2_verts.size(); ndx++)
660 if(GetDistance(*(p2_verts[ndx]), *(p1_verts[mdx]))< 1.e-6*p2_verts[ndx]->GetReferenceLengthScale())
662 polygons[jdx]->ReplaceVertex(ndx, polygons[idx]->GetVertex(mdx));
670 for(
unsigned idx=0; idx<
mFacets.size(); idx++)
672 mFacets[idx]->UpdateVertices();
677 template<
unsigned DIM>
684 template<
unsigned DIM>
687 std::vector<boost::shared_ptr<DimensionalChastePoint<DIM> > > vertices =
GetVertices();
689 for (
unsigned idx = 0; idx < vertices.size(); idx++)
691 vertices[idx]->Translate(vector);
697 template<
unsigned DIM>
void SetReferenceLengthScale(units::quantity< unit::length > referenceLength)
std::vector< unsigned > GetContainingGridIndices(unsigned num_x, unsigned num_y, unsigned num_z, units::quantity< unit::length > spacing)
void MergeCoincidentVertices()
vtkSmartPointer< vtkPolyData > GetVtk()
void BooleanWithNetwork(boost::shared_ptr< VesselNetwork< DIM > > pVesselNetwork)
units::quantity< unit::length > GetReferenceLengthScale() const
boost::shared_ptr< Facet< DIM > > GetFacet(const DimensionalChastePoint< DIM > &rLocation)
bool IsPointInPart(DimensionalChastePoint< DIM > location)
void AddCylinder(units::quantity< unit::length > radius, units::quantity< unit::length > depth, DimensionalChastePoint< DIM > centre, unsigned numSegments=24)
boost::shared_ptr< Polygon< DIM > > AddRectangle(units::quantity< unit::length > sizeX, units::quantity< unit::length > sizeY, DimensionalChastePoint< DIM > origin)
std::vector< DimensionalChastePoint< DIM > > GetVertexLocations()
void SetOutputFormat(GeometryFormat::Value format)
std::vector< std::pair< unsigned, unsigned > > GetSegmentIndices()
std::vector< DimensionalChastePoint< DIM > > GetHoleMarkers()
std::vector< boost::shared_ptr< DimensionalChastePoint< DIM > > > GetVertices()
std::vector< boost::shared_ptr< Facet< DIM > > > GetFacets()
void Write(const std::string &rFilename, GeometryFormat::Value format=GeometryFormat::VTP)
units::quantity< unit::length > mReferenceLength
void Extrude(boost::shared_ptr< Polygon< DIM > > pPolygon, units::quantity< unit::length > distance)
void SetInput(vtkSmartPointer< vtkPolyData > pSurface)
void AddCuboid(units::quantity< unit::length > sizeX, units::quantity< unit::length > sizeY, units::quantity< unit::length > sizeZ, DimensionalChastePoint< DIM > origin)
std::vector< boost::shared_ptr< Facet< DIM > > > mFacets
std::vector< DimensionalChastePoint< DIM > > mHoleMarkers
void Translate(DimensionalChastePoint< DIM > vector)
static boost::shared_ptr< Polygon > Create(std::vector< boost::shared_ptr< DimensionalChastePoint< DIM > > > vertices)
std::vector< boost::shared_ptr< Polygon< DIM > > > GetSurfacePolygons()
boost::shared_ptr< Polygon< DIM > > AddPolygon(std::vector< boost::shared_ptr< DimensionalChastePoint< DIM > > > vertices, bool newFacet=false, boost::shared_ptr< Facet< DIM > > pFacet=boost::shared_ptr< Facet< DIM > >())
units::quantity< unit::length > GetReferenceLengthScale()
c_vector< double, DIM > GetLocation(units::quantity< unit::length > scale)
std::vector< DimensionalChastePoint< DIM > > GetHoles()
boost::shared_ptr< Polygon< DIM > > AddCircle(units::quantity< unit::length > radius, DimensionalChastePoint< DIM > centre, unsigned numSegments=24)
vtkSmartPointer< vtkPolyData > mVtkPart
std::vector< boost::shared_ptr< Polygon< DIM > > > GetPolygons()
std::vector< units::quantity< unit::length > > GetBoundingBox()
static boost::shared_ptr< Part< DIM > > Create()
void AddVesselNetwork(boost::shared_ptr< VesselNetwork< DIM > > pVesselNetwork, bool surface=false)
void SetFileName(const std::string &rFileName)
void AddHoleMarker(DimensionalChastePoint< DIM > location)