ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSimplexMeshVolumeCalculator.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkSimplexMeshVolumeCalculator_h
00019 #define __itkSimplexMeshVolumeCalculator_h
00020 
00021 #include "itkIntTypes.h"
00022 #include "itkPolygonCell.h"
00023 #include "itkVector.h"
00024 #include "itkSimplexMesh.h"
00025 #include "itkVectorContainer.h"
00026 
00027 namespace itk
00028 {
00049 template< class TInputMesh >
00050 class ITK_EXPORT SimplexMeshVolumeCalculator:public Object
00051 {
00052 public:
00054   typedef SimplexMeshVolumeCalculator Self;
00055 
00057   typedef Object Superclass;
00058 
00060   typedef SmartPointer< Self >       Pointer;
00061   typedef SmartPointer< const Self > ConstPointer;
00062 
00064   itkNewMacro(Self);
00065 
00067   itkTypeMacro(SimplexMeshVolumeCalculator, Object);
00068 
00069   typedef TInputMesh                           InputMeshType;
00070   typedef typename InputMeshType::Pointer      InputMeshPointer;
00071   typedef typename InputMeshType::ConstPointer InputMeshConstPointer;
00072 
00073   typedef typename InputMeshType::PointType              InputPointType;
00074   typedef typename InputMeshType::PixelType              InputPixelType;
00075   typedef typename InputMeshType::MeshTraits::CellTraits InputCellTraitsType;
00076 
00077   typedef typename InputMeshType::PointsContainer      InputPointsContainer;
00078   typedef typename InputPointsContainer::ConstPointer  InputPointsContainerPointer;
00079   typedef typename InputPointsContainer::ConstIterator InputPointsContainerIterator;
00080 
00081   typedef typename InputMeshType::NeighborListType           InputNeighbors;
00082   typedef typename InputMeshType::NeighborListType::iterator InputNeighborsIterator;
00083 
00084   typedef typename InputMeshType::CellType             SimplexCellType;
00085   typedef          itk::PolygonCell< SimplexCellType > SimplexPolygonType;
00086 
00087   // stores the center for each simplex mesh cell, key is the point id
00088   typedef          itk::MapContainer< IdentifierType, InputPointType > PointMapType;
00089   typedef typename PointMapType::Pointer                               PointMapPointer;
00090 
00091   typedef typename InputPointType::VectorType VectorType;
00092   typedef CovariantVector<
00093     typename VectorType::ValueType, 3 >   CovariantVectorType;
00094 
00102   class SimplexCellVisitor
00103   {
00104 public:
00105 
00109     SimplexCellVisitor()
00110     {
00111       m_CenterMap = PointMapType::New();
00112     }
00113     virtual ~SimplexCellVisitor() {}
00115 
00119     void Visit(IdentifierType cellId, SimplexPolygonType *poly)
00120     {
00121       typedef typename SimplexPolygonType::PointIdIterator PointIdIterator;
00122       PointIdIterator it =  poly->PointIdsBegin();
00123       InputPointType  center, p;
00124       center.Fill(0);
00125       p.Fill(0.0);
00127 
00128       while ( it != poly->PointIdsEnd() )
00129         {
00130         m_Mesh->GetPoint(*it, &p);
00131         center += p.GetVectorFromOrigin();
00132         it++;
00133         }
00134 
00135       center[0] /= poly->GetNumberOfPoints();
00136       center[1] /= poly->GetNumberOfPoints();
00137       center[2] /= poly->GetNumberOfPoints();
00138 
00139       m_CenterMap->InsertElement(cellId, center);
00140     }
00141 
00142     PointMapPointer GetCenterMap()
00143     {
00144       return m_CenterMap;
00145     }
00146 
00147     void SetMesh(InputMeshPointer mesh)
00148     {
00149       m_Mesh = mesh;
00150     }
00151 
00152 protected:
00153     InputMeshPointer m_Mesh;
00154     PointMapPointer  m_CenterMap;
00155   };
00156 
00157   typedef itk::CellInterfaceVisitorImplementation< InputPixelType,
00158                                                    InputCellTraitsType,
00159                                                    SimplexPolygonType,
00160                                                    SimplexCellVisitor >
00161   SimplexVisitorInterfaceType;
00162 
00163   typedef typename SimplexVisitorInterfaceType::Pointer SimplexVisitorInterfacePointer;
00164   typedef typename SimplexCellType::MultiVisitor        CellMultiVisitorType;
00165   typedef typename CellMultiVisitorType::Pointer        CellMultiVisitorPointer;
00166 
00168   itkSetObjectMacro(SimplexMesh, InputMeshType);
00169 
00171   void Compute(void);
00172 
00174   itkGetConstMacro(Volume, double);
00175 
00177   itkGetConstMacro(Area, double);
00178 protected:
00179   SimplexMeshVolumeCalculator();
00180   virtual ~SimplexMeshVolumeCalculator();
00181   void PrintSelf(std::ostream & os, Indent indent) const;
00183 
00184 private:
00185   SimplexMeshVolumeCalculator(const Self &); //purposely not implemented
00186   void operator=(const Self &);              //purposely not implemented
00187 
00188   void Initialize();
00189 
00190   void Finalize();
00191 
00193   void CreateTriangles();
00194 
00196   void CalculateTriangleVolume(InputPointType p1, InputPointType p2, InputPointType p3);
00197 
00199   IdentifierType FindCellId(IdentifierType id1, IdentifierType id2, IdentifierType id3);
00200 
00202   PointMapPointer m_Centers;
00203 
00204   InputMeshPointer m_SimplexMesh;
00205 
00206   double m_Volume;
00207   double m_VolumeX;
00208   double m_VolumeY;
00209   double m_VolumeZ;
00210   double m_Area;
00211   double m_Kx;
00212   double m_Ky;
00213   double m_Kz;
00214   double m_Wxyz;
00215   double m_Wxy;
00216   double m_Wxz;
00217   double m_Wyz;
00218 
00219   IndexValueType   m_Muncx;
00220   IndexValueType   m_Muncy;
00221   IndexValueType   m_Muncz;
00222 
00223   SizeValueType m_NumberOfTriangles;
00224 };
00225 } //end of namespace
00226 
00227 #ifndef ITK_MANUAL_INSTANTIATION
00228 #include "itkSimplexMeshVolumeCalculator.hxx"
00229 #endif
00230 
00231 #endif /* __SimplexMeshVolumeCalculator_h */
00232