37 #define _BACKWARD_BACKWARD_WARNING_H 1 //Cut out the vtk deprecated warning for now (gcc4.3) 38 #include <vtkImageData.h> 39 #include <vtkDoubleArray.h> 40 #include <vtkPointData.h> 41 #include <vtkProbeFilter.h> 42 #include <vtkPolyData.h> 43 #include <vtkPoints.h> 44 #include <vtkSmartPointer.h> 46 #include "Exception.hpp" 47 #include "RegularGrid.hpp" 48 #include "RegularGridWriter.hpp" 49 #include "BaseUnits.hpp" 51 template<
unsigned DIM>
53 mSpacing(
BaseUnits::Instance()->GetReferenceLengthScale()),
54 mExtents(std::vector<unsigned>(3, 10)),
58 mCellPopulationReferenceLength(
BaseUnits::Instance()->GetReferenceLengthScale()),
63 mVtkGridIsSetUp(false),
65 mHasCellPopulation(false),
66 mReferenceLength(
BaseUnits::Instance()->GetReferenceLengthScale())
71 template<
unsigned DIM>
75 MAKE_PTR(Reg_Grid_Templated, pSelf);
79 template<
unsigned DIM>
85 template<
unsigned DIM>
89 for (
unsigned kdx = 0; kdx <
mExtents[2]; kdx++)
91 for (
unsigned jdx = 0; jdx < mExtents[1]; jdx++)
93 for (
unsigned idx = 0; idx < mExtents[0]; idx++)
100 if (idx < mExtents[0] - 1)
108 if (jdx < mExtents[1] - 1)
116 if (kdx < mExtents[2] - 1)
125 template<
unsigned DIM>
135 template<
unsigned DIM>
141 unsigned x_index = round(offsets[0]*scale_factor);
142 unsigned y_index = round(offsets[1]*scale_factor);
143 unsigned z_index = 0;
146 z_index = round(offsets[2]*scale_factor);
151 template<
unsigned DIM>
155 std::vector<units::quantity<unit::length> > spatial_extents = pPart->GetBoundingBox();
156 mExtents[0] = unsigned(std::floor((spatial_extents[1] - spatial_extents[0]) / gridSize))+1;
157 mExtents[1] = unsigned(std::floor((spatial_extents[3] - spatial_extents[2]) / gridSize))+1;
160 mExtents[2] = unsigned(std::floor((spatial_extents[5] - spatial_extents[4]) / gridSize))+1;
170 template<
unsigned DIM>
176 template<
unsigned DIM>
182 template<
unsigned DIM>
188 template<
unsigned DIM>
194 for (
unsigned idx = 0; idx < inputPoints.size(); idx++)
197 unsigned x_index = round(offsets[0]*scale_factor);
198 unsigned y_index = round(offsets[1]*scale_factor);
199 unsigned z_index = 0;
202 z_index = round(offsets[2]*scale_factor);
208 point_point_map[grid_index].push_back(idx);
211 return point_point_map;
214 template<
unsigned DIM>
218 std::vector<double> sampled_values(locations.size(), 0.0);
225 vtkSmartPointer<vtkDoubleArray> pPointData = vtkSmartPointer<vtkDoubleArray>::New();
226 pPointData->SetNumberOfComponents(1);
228 const std::string ny_name =
"test";
229 pPointData->SetName(ny_name.c_str());
232 pPointData->SetValue(idx, values[idx]);
234 mpVtkGrid->GetPointData()->AddArray(pPointData);
237 vtkSmartPointer<vtkPolyData> p_polydata = vtkSmartPointer<vtkPolyData>::New();
238 vtkSmartPointer<vtkPoints> p_points = vtkSmartPointer<vtkPoints>::New();
239 p_points->SetNumberOfPoints(locations.size());
240 for (
unsigned idx = 0; idx < locations.size(); idx++)
242 c_vector<double, DIM> scaled_location = locations[idx].GetLocation(
mReferenceLength);
245 p_points->SetPoint(idx, scaled_location[0], scaled_location[1], scaled_location[2]);
249 p_points->SetPoint(idx, scaled_location[0], scaled_location[1], 0.0);
252 p_polydata->SetPoints(p_points);
254 vtkSmartPointer<vtkProbeFilter> p_probe_filter = vtkSmartPointer<vtkProbeFilter>::New();
255 #if VTK_MAJOR_VERSION <= 5 256 p_probe_filter->SetInput(p_polydata);
259 p_probe_filter->SetInputData(p_polydata);
260 p_probe_filter->SetSourceData(
mpVtkGrid);
262 p_probe_filter->Update();
263 vtkSmartPointer<vtkPointData> p_point_data = p_probe_filter->GetPolyDataOutput()->GetPointData();
265 unsigned num_points = p_point_data->GetArray(
"test")->GetNumberOfTuples();
266 for (
unsigned idx = 0; idx < num_points; idx++)
268 sampled_values[idx] = p_point_data->GetArray(
"test")->GetTuple1(idx);
270 return sampled_values;
273 template<
unsigned DIM>
283 EXCEPTION(
"A vessel network has not been set. Can not create a point node map.");
290 std::vector<boost::shared_ptr<VesselNode<DIM> > > empty_node_pointers;
294 std::vector<boost::shared_ptr<VesselNode<DIM> > > nodes =
mpNetwork->GetNodes();
297 for (
unsigned idx = 0; idx < nodes.size(); idx++)
300 unsigned x_index = round(offsets[0]*scale_factor);
301 unsigned y_index = round(offsets[1]*scale_factor);
302 unsigned z_index = 0;
305 z_index = round(offsets[2]*scale_factor);
320 template<
unsigned DIM>
330 EXCEPTION(
"A cell population has not been set. Can not create a cell point map.");
340 std::vector<CellPtr> empty_cell_pointers;
344 for (
typename AbstractCellPopulation<DIM>::Iterator cell_iter =
mpCellPopulation->Begin();
347 c_vector<double, DIM> location =
mpCellPopulation->GetLocationOfCellCentre(*cell_iter);
349 unsigned x_index = round(offsets[0]*scale_factor);
350 unsigned y_index = round(offsets[1]*scale_factor);
351 unsigned z_index = 0;
354 z_index = round(offsets[2]*scale_factor);
370 template<
unsigned DIM>
380 EXCEPTION(
"Out of range index requested for point-segment map");
385 template<
unsigned DIM>
387 bool update,
bool useVesselSurface)
396 EXCEPTION(
"A vessel network has not been set. Can not create a vessel point map.");
403 std::vector<boost::shared_ptr<VesselSegment<DIM> > > empty_seg_pointers;
407 std::vector<boost::shared_ptr<VesselSegment<DIM> > > segments =
mpNetwork->GetVesselSegments();
409 units::quantity<unit::length> cut_off_length = sqrt(1.0 / 2.0) *
mSpacing;
410 for (
unsigned jdx = 0; jdx < segments.size(); jdx++)
412 double start_point[3];
414 c_vector<double,DIM> start_location = segments[jdx]->GetNode(0)->rGetLocation().GetLocation(
mReferenceLength);
415 start_point[0] = start_location[0];
416 start_point[1] = start_location[1];
419 start_point[2] = start_location[2];
423 start_point[2] = 0.0;
425 c_vector<double,DIM> end_location = segments[jdx]->GetNode(1)->rGetLocation().GetLocation(
mReferenceLength);
426 end_point[0] = end_location[0];
427 end_point[1] = end_location[1];
430 end_point[2] = end_location[2];
437 for (
unsigned idx = 0; idx < num_points; idx++)
439 if (!useVesselSurface)
441 double parametric_distance = 0.0;
442 double closest_point[3];
443 double grid_point[3];
445 grid_point[0] = grid_location[0];
446 grid_point[1] = grid_location[1];
449 grid_point[2] = grid_location[2];
456 double distance = vtkLine::DistanceToLine(grid_point,
459 parametric_distance, closest_point);
467 if (segments[jdx]->GetDistance(
GetLocationOf1dIndex(idx))< (segments[jdx]->GetRadius() + cut_off_length))
478 template<
unsigned DIM>
484 template<
unsigned DIM>
492 double(y_index) * scale_factor + dimensionless_origin[1], 0.0,
mReferenceLength);
497 double(y_index) * scale_factor + dimensionless_origin[1],
498 double(z_index) * scale_factor + dimensionless_origin[2],
mReferenceLength);
502 template<
unsigned DIM>
507 unsigned mod_y = mod_z % mExtents[0];
508 unsigned y_index = (mod_z - mod_y) / mExtents[0];
509 unsigned x_index = mod_y;
515 double(y_index) * dimless_spacing + dimensionless_origin[1], 0.0,
mReferenceLength);
520 double(y_index) * dimless_spacing + dimensionless_origin[1],
521 double(z_index) * dimless_spacing + dimensionless_origin[2],
mReferenceLength);
525 template<
unsigned DIM>
536 template<
unsigned DIM>
542 template<
unsigned DIM>
548 template<
unsigned DIM>
554 template<
unsigned DIM>
559 unsigned mod_y = mod_z % mExtents[0];
560 unsigned y_index = (mod_z - mod_y) / mExtents[0];
561 unsigned x_index = mod_y;
565 template<
unsigned DIM>
568 if (x_index == 0 || x_index ==
mExtents[0] - 1)
572 if (y_index == 0 || y_index ==
mExtents[1] - 1)
578 if (z_index == 0 || z_index ==
mExtents[2] - 1)
586 template<
unsigned DIM>
589 bool point_in_box =
false;
592 unsigned mod_y = mod_z % mExtents[0];
593 unsigned y_index = (mod_z - mod_y) / mExtents[0];
594 unsigned x_index = mod_y;
599 double loc_x = (double(x_index) * dimensionless_spacing + dimensionless_origin[0]);
600 double loc_y = (double(y_index) * dimensionless_spacing + dimensionless_origin[1]);
604 loc_z = (double(z_index) * dimensionless_spacing + dimensionless_origin[2]);
608 if (dimensionless_point[0] >= loc_x - dimensionless_spacing / 2.0 && dimensionless_point[0] <= loc_x + dimensionless_spacing / 2.0)
610 if (dimensionless_point[1] >= loc_y - dimensionless_spacing / 2.0 && dimensionless_point[1] <= loc_y +dimensionless_spacing / 2.0)
614 if (dimensionless_point[2] >= loc_z - dimensionless_spacing / 2.0 && dimensionless_point[2] <= loc_z + dimensionless_spacing / 2.0)
628 template<
unsigned DIM>
635 template<
unsigned DIM>
638 if (extents.size() < 3)
640 EXCEPTION(
"The extents should be of dimension 3, regardless of element or space dimension");
645 template<
unsigned DIM>
651 template<
unsigned DIM>
657 template<
unsigned DIM>
661 mpVtkGrid = vtkSmartPointer<vtkImageData>::New();
675 mpVtkGrid->SetOrigin(dimless_origin[0], dimless_origin[1], dimless_origin[2]);
679 mpVtkGrid->SetOrigin(dimless_origin[0], dimless_origin[1], 0.0);
684 template<
unsigned DIM>
690 template<
unsigned DIM>
700 vtkSmartPointer<vtkDoubleArray> pPointData = vtkSmartPointer<vtkDoubleArray>::New();
701 pPointData->SetNumberOfComponents(1);
703 pPointData->SetName(
"Point Values");
708 mpVtkGrid->GetPointData()->AddArray(pPointData);
713 template<
unsigned DIM>
717 writer.
SetFilename(pFileHandler->GetOutputDirectoryFullPath() +
"/grid.vti");
boost::shared_ptr< VesselNetwork< DIM > > mpNetwork
AbstractCellPopulation< DIM > * mpCellPopulation
void Write(boost::shared_ptr< OutputFileHandler > pFileHandler)
std::vector< unsigned > GetExtents()
std::vector< unsigned > mExtents
units::quantity< unit::length > mCellPopulationReferenceLength
const std::vector< std::vector< CellPtr > > & GetPointCellMap(bool update=true)
unsigned GetNumberOfPoints()
void SetOrigin(DimensionalChastePoint< DIM > origin)
const std::vector< std::vector< boost::shared_ptr< VesselNode< DIM > > > > & GetPointNodeMap(bool update=true)
vtkSmartPointer< vtkImageData > mpVtkGrid
void SetFilename(const std::string &rFilename)
void SetCellPopulation(AbstractCellPopulation< DIM > &rCellPopulation, units::quantity< unit::length > cellPopulationLengthScale)
DimensionalChastePoint< DIM > mOrigin
units::quantity< unit::length > mReferenceLength
std::vector< double > InterpolateGridValues(std::vector< DimensionalChastePoint< DIM > > locations, std::vector< double > values, bool useVtk=false)
void SetPointValues(std::vector< double > pointSolution)
const std::vector< std::vector< unsigned > > & GetNeighbourData()
std::vector< std::vector< boost::shared_ptr< VesselSegment< DIM > > > > GetPointSegmentMap(bool update=true, bool useVesselSurface=false)
std::vector< DimensionalChastePoint< DIM > > GetLocations()
DimensionalChastePoint< DIM > GetLocation(unsigned xIndex, unsigned yIndex, unsigned zIndex)
std::vector< std::vector< unsigned > > GetPointPointMap(std::vector< DimensionalChastePoint< DIM > > inputPoints)
void CalculateNeighbourData()
void SetSpacing(units::quantity< unit::length > spacing)
static boost::shared_ptr< RegularGrid< DIM > > Create()
c_vector< double, DIM > GetLocation(units::quantity< unit::length > scale)
void SetExtents(std::vector< unsigned > extents)
std::vector< std::vector< CellPtr > > mPointCellMap
unsigned Get1dGridIndex(unsigned xIndex, unsigned yIndex, unsigned zIndex)
DimensionalChastePoint< DIM > GetLocationOf1dIndex(unsigned gridIndex)
std::vector< double > mPointSolution
vtkSmartPointer< vtkImageData > GetVtkGrid()
std::vector< std::vector< unsigned > > mNeighbourData
std::vector< std::vector< boost::shared_ptr< VesselSegment< DIM > > > > mPointSegmentMap
bool IsSegmentAtLatticeSite(unsigned index, bool update)
std::vector< std::vector< boost::shared_ptr< VesselNode< DIM > > > > mPointNodeMap
units::quantity< unit::length > mSpacing
bool IsLocationInPointVolume(DimensionalChastePoint< DIM > point, unsigned gridIndex)
DimensionalChastePoint< DIM > GetOrigin()
void SetImage(vtkSmartPointer< vtkImageData > pImage)
units::quantity< unit::length > GetSpacing()
bool IsOnBoundary(unsigned gridIndex)
void GenerateFromPart(boost::shared_ptr< Part< DIM > > pPart, units::quantity< unit::length > gridSize)
units::quantity< unit::length > GetReferenceLengthScale()
unsigned GetNearestGridIndex(const DimensionalChastePoint< DIM > &rLocation)
void SetVesselNetwork(boost::shared_ptr< VesselNetwork< DIM > > pNetwork)