38 #ifndef __itkStandardMeshRepresenter_hxx
39 #define __itkStandardMeshRepresenter_hxx
41 #include "itkStandardMeshRepresenter.h"
45 #include <itkIdentityTransform.h>
46 #include <itkMeshFileReader.h>
47 #include <itkMeshFileWriter.h>
49 #include <itkTransformMeshFilter.h>
50 #include <itkVector.h>
52 #include "HDF5Utils.h"
53 #include "StatismoUtils.h"
57 template <
class TPixel,
unsigned MeshDimension>
58 StandardMeshRepresenter<TPixel, MeshDimension>::StandardMeshRepresenter()
61 template <
class TPixel,
unsigned MeshDimension>
62 StandardMeshRepresenter<TPixel, MeshDimension>::~StandardMeshRepresenter() {
65 template <
class TPixel,
unsigned MeshDimension>
66 StandardMeshRepresenter<TPixel, MeshDimension>*
67 StandardMeshRepresenter<TPixel, MeshDimension>::Clone()
const {
69 StandardMeshRepresenter* clone =
new StandardMeshRepresenter();
72 typename MeshType::Pointer clonedReference = this->CloneDataset(m_reference);
73 clone->SetReference(clonedReference);
78 template <
class TPixel,
unsigned MeshDimension>
80 StandardMeshRepresenter<TPixel, MeshDimension>::Load(
const H5::Group& fg) {
83 if (repName ==
"vtkPolyDataRepresenter" || repName ==
"itkMeshRepresenter") {
84 this->SetReference(LoadRefLegacy(fg));
86 this->SetReference(LoadRef(fg));
90 template <
class TPixel,
unsigned MeshDimension>
91 typename StandardMeshRepresenter<TPixel, MeshDimension>::MeshType::Pointer
92 StandardMeshRepresenter<TPixel, MeshDimension>::LoadRef(
const H5::Group& fg)
const {
94 statismo::MatrixType vertexMat;
97 typedef typename statismo::GenericEigenType<unsigned int>::MatrixType UIntMatrixType;
98 UIntMatrixType cellsMat;
99 statismo::HDF5Utils::readMatrixOfType<unsigned int>(fg,
"./cells", cellsMat);
101 unsigned nVertices = vertexMat.cols();
102 unsigned nCells = cellsMat.cols();
103 unsigned cellDim = cellsMat.rows();
106 typename MeshType::Pointer mesh = MeshType::New();
109 for (
unsigned i = 0; i < nVertices; i++) {
110 typename MeshType::PointType p;
111 p[0] = vertexMat(0, i);
112 p[1] = vertexMat(1, i);
113 p[2] = vertexMat(2, i);
114 mesh->SetPoint(i, p);
118 typedef typename MeshType::CellType::CellAutoPointer CellAutoPointer;
119 typedef itk::LineCell< typename MeshType::CellType > LineType;
120 typedef itk::TriangleCell < typename MeshType::CellType > TriangleCellType;
122 CellAutoPointer cell;
124 for (
unsigned i = 0; i < nCells; i++) {
126 cell.TakeOwnership(
new LineType );
127 }
else if (cellDim == 3) {
128 cell.TakeOwnership(
new TriangleCellType);
133 for (
unsigned d = 0; d < cellDim; d++) {
134 cell->SetPointId(d, cellsMat(d, i));
136 mesh->SetCell( i, cell );
141 H5::Group pdGroup = fg.openGroup(
"./pointData");
144 H5::DataSet ds = pdGroup.openDataSet(
"scalars");
146 if (type != PixelConversionTrait<TPixel>::GetDataType()) {
147 std::cout <<
"Warning: The datatype specified for the scalars does not match the TPixel template argument used in this representer." << std::endl;
149 statismo::MatrixTypeDoublePrecision scalarMatDouble;
150 statismo::HDF5Utils::readMatrixOfType<double>(pdGroup,
"scalars", scalarMatDouble);
152 assert(static_cast<unsigned>(scalarMatDouble.cols()) == mesh->GetNumberOfPoints());
153 typename MeshType::PointDataContainerPointer pd = MeshType::PointDataContainer::New();
155 for (
unsigned i = 0; i < scalarMatDouble.cols(); i++) {
156 TPixel v = PixelConversionTrait<TPixel>::FromVector(scalarMat.col(i));
157 pd->InsertElement(i, v);
159 mesh->SetPointData(pd);
169 template <
class TPixel,
unsigned MeshDimension>
170 typename StandardMeshRepresenter<TPixel, MeshDimension>::MeshType::Pointer
171 StandardMeshRepresenter<TPixel, MeshDimension>::LoadRefLegacy(
const H5::Group& fg)
const {
173 std::string tmpfilename = statismo::Utils::CreateTmpName(
".vtk");
177 typename itk::MeshFileReader<MeshType>::Pointer reader = itk::MeshFileReader<MeshType>::New();
178 reader->SetFileName(tmpfilename);
181 }
catch (itk::MeshFileReaderException& e) {
185 typename MeshType::Pointer mesh = reader->GetOutput();
186 std::remove(tmpfilename.c_str());
192 template <
class TPixel,
unsigned MeshDimension>
195 m_reference = reference;
200 typename DomainType::DomainPointsListType domainPointList;
202 typename PointsContainerType::Pointer points = m_reference->GetPoints();
203 typename PointsContainerType::Iterator pointIterator= points->Begin();
205 while( pointIterator != points->End() ) {
206 domainPointList.push_back(pointIterator.Value());
207 m_pointCache.insert(std::pair<PointType, unsigned>(pointIterator.Value(), id));
215 template <
class TPixel,
unsigned MeshDimension>
218 statismo::VectorType v(PointType::GetPointDimension());
219 for (
unsigned i = 0; i < PointType::GetPointDimension(); i++) {
226 template <
class TPixel,
unsigned MeshDimension>
227 typename StandardMeshRepresenter<TPixel, MeshDimension>::DatasetPointerType
230 return this->CloneDataset(ds);
233 template <
class TPixel,
unsigned MeshDimension>
236 statismo::VectorType sample(GetNumberOfPoints() * GetDimensions());
238 typename PointsContainerType::Pointer points = mesh->GetPoints();
240 typename PointsContainerType::Iterator pointIterator= points->Begin();
242 while( pointIterator != points->End() ) {
243 for (
unsigned d = 0; d < GetDimensions(); d++) {
244 unsigned idx = this->MapPointIdToInternalIdx(
id, d);
245 sample[idx] = pointIterator.Value()[d];
255 template <
class TPixel,
unsigned MeshDimension>
256 typename StandardMeshRepresenter<TPixel, MeshDimension>::DatasetPointerType
258 typename MeshType::Pointer mesh = this->CloneDataset(m_reference);
259 typename PointsContainerType::Pointer points = mesh->GetPoints();
260 typename PointsContainerType::Iterator pointsIterator = points->Begin();
263 while( pointsIterator != points->End() ) {
265 for (
unsigned d = 0; d < GetDimensions(); d++) {
266 unsigned idx = this->MapPointIdToInternalIdx(ptId, d);
269 mesh->SetPoint(ptId, v);
277 template <
class TPixel,
unsigned MeshDimension>
278 typename StandardMeshRepresenter<TPixel, MeshDimension>::ValueType
280 if (ptid >= sample->GetNumberOfPoints()) {
284 return sample->GetPoint(ptid);
288 template <
class TPixel,
unsigned MeshDimension>
289 typename StandardMeshRepresenter<TPixel, MeshDimension>::ValueType
292 for (
unsigned d = 0; d < GetDimensions(); d++) {
293 value[d] = pointSample[d];
297 template <
class TPixel,
unsigned MeshDimension>
300 statismo::VectorType vec(GetDimensions());
301 for (
unsigned d = 0; d < GetDimensions(); d++) {
308 template <
class TPixel,
unsigned MeshDimension>
313 statismo::MatrixType vertexMat = statismo::MatrixType::Zero(3, m_reference->GetNumberOfPoints());
315 for (
unsigned i = 0; i < m_reference->GetNumberOfPoints(); i++) {
316 typename MeshType::PointType pt = m_reference->GetPoint(i);
317 for (
unsigned d = 0; d < 3; d++) {
318 vertexMat(d, i) = pt[d];
325 unsigned numPointsPerCell = 0;
326 if (m_reference->GetNumberOfCells() > 0) {
327 typename MeshType::CellAutoPointer cellPtr;
328 m_reference->GetCell(0, cellPtr);
329 numPointsPerCell = cellPtr->GetNumberOfPoints();
332 typedef typename statismo::GenericEigenType<unsigned int>::MatrixType UIntMatrixType;
333 UIntMatrixType facesMat = UIntMatrixType::Zero(numPointsPerCell, m_reference->GetNumberOfCells());
336 for (
unsigned i = 0; i < m_reference->GetNumberOfCells(); i++) {
337 typename MeshType::CellAutoPointer cellPtr;
338 m_reference->GetCell(i, cellPtr);
339 assert(numPointsPerCell == cellPtr->GetNumberOfPoints());
340 for (
unsigned d = 0; d < numPointsPerCell; d++) {
341 facesMat(d, i) = cellPtr->GetPointIds()[d];
345 H5::Group pdGroup = fg.createGroup(
"pointData");
347 typename MeshType::PointDataContainerPointer pd = m_reference->GetPointData();
348 if (pd.IsNotNull() && pd->Size() == m_reference->GetNumberOfPoints()) {
349 unsigned numComponents = PixelConversionTrait<TPixel>::ToVector(pd->GetElement(0)).rows();
351 statismo::MatrixType scalarsMat = statismo::MatrixType::Zero(numComponents, m_reference->GetNumberOfPoints());
352 for (
unsigned i = 0; i < m_reference->GetNumberOfPoints(); i++) {
353 scalarsMat.col(i) = PixelConversionTrait<TPixel>::ToVector(pd->GetElement(i));
355 statismo::MatrixTypeDoublePrecision scalarsMatDouble = scalarsMat.cast<
double>();
356 H5::DataSet ds = statismo::HDF5Utils::writeMatrixOfType<double>(pdGroup,
"scalars", scalarsMatDouble);
361 statismo::HDF5Utils::writeMatrixOfType<unsigned int>(fg,
"./cells", facesMat);
365 template <
class TPixel,
unsigned MeshDimension>
368 return this->m_reference->GetNumberOfPoints();
372 template <
class TPixel,
unsigned MeshDimension>
378 typename PointCacheType::const_iterator got = m_pointCache.find (pt);
379 if (got == m_pointCache.end()) {
380 ptId = FindClosestPoint(m_reference, pt);
381 m_pointCache.insert(std::pair<PointType, unsigned>(pt, ptId));
386 return static_cast<unsigned>(ptId);
391 template <
class TPixel,
unsigned MeshDimension>
392 typename StandardMeshRepresenter<TPixel, MeshDimension>::DatasetPointerType
398 typedef itk::IdentityTransform<TPixel, MeshDimension> IdentityTransformType;
399 typedef itk::TransformMeshFilter<MeshType, MeshType, IdentityTransformType> TransformMeshFilterType;
401 typename TransformMeshFilterType::Pointer tf = TransformMeshFilterType::New();
403 typename IdentityTransformType::Pointer idTrans = IdentityTransformType::New();
404 tf->SetTransform(idTrans);
407 typename MeshType::Pointer clone = tf->GetOutput();
408 clone->DisconnectPipeline();
412 template <
class TPixel,
unsigned MeshDimension>
414 StandardMeshRepresenter<TPixel, MeshDimension>::FindClosestPoint(
const MeshType* mesh,
const PointType pt)
const {
static bool existsObjectWithName(const H5::CommonFG &fg, const std::string &name)
Definition: HDF5Utils.hxx:542
static void writeIntAttribute(const H5::H5Object &fg, const char *name, int value)
Definition: HDF5Utils.hxx:406
statismo::VectorType SampleToSampleVector(DatasetConstPointerType sample) const
Definition: itkStandardMeshRepresenter.hxx:235
A representer for scalar valued itk Meshs.
Definition: itkStandardMeshRepresenter.h:90
static void readMatrix(const H5::CommonFG &fg, const char *name, MatrixType &matrix)
Definition: HDF5Utils.hxx:154
ValueType PointSampleFromSample(DatasetConstPointerType sample, unsigned ptid) const
Definition: itkStandardMeshRepresenter.hxx:279
virtual unsigned GetPointIdForPoint(const PointType &point) const
Definition: itkStandardMeshRepresenter.hxx:374
static H5::DataSet writeMatrix(const H5::CommonFG &fg, const char *name, const MatrixType &matrix)
Definition: HDF5Utils.hxx:248
statismo::VectorType PointSampleToPointSampleVector(const ValueType &v) const
Definition: itkStandardMeshRepresenter.hxx:299
void Save(const H5::Group &fg) const
Definition: itkStandardMeshRepresenter.hxx:310
static std::string readStringAttribute(const H5::H5Object &group, const char *name)
Definition: HDF5Utils.hxx:397
DatasetPointerType SampleVectorToSample(const statismo::VectorType &sample) const
Definition: itkStandardMeshRepresenter.hxx:257
Generic Exception class for the statismo Library.
Definition: Exceptions.h:68
void SetReference(DatasetPointerType ds)
Definition: itkStandardMeshRepresenter.hxx:194
float ScalarType
the type that is used for all vector and matrices throughout the library.
Definition: CommonTypes.h:60
ValueType PointSampleVectorToPointSample(const statismo::VectorType &pointSample) const
Definition: itkStandardMeshRepresenter.hxx:290
static void getFileFromHDF5(const H5::CommonFG &fg, const char *name, const char *filename)
Definition: HDF5Utils.hxx:462
DatasetPointerType DatasetToSample(DatasetConstPointerType ds) const
Definition: itkStandardMeshRepresenter.hxx:228
virtual unsigned GetNumberOfPoints() const
return the number of points of the reference
Definition: itkStandardMeshRepresenter.hxx:367
static int readIntAttribute(const H5::H5Object &group, const char *name)
Definition: HDF5Utils.hxx:416