SMILX  1.01
milxModel.h
1 /*=========================================================================
2  The Software is copyright (c) Commonwealth Scientific and Industrial Research Organisation (CSIRO)
3  ABN 41 687 119 230.
4  All rights reserved.
5 
6  Licensed under the CSIRO BSD 3-Clause License
7  You may not use this file except in compliance with the License.
8  You may obtain a copy of the License in the file LICENSE.md or at
9 
10  https://stash.csiro.au/projects/SMILI/repos/smili/browse/license.txt
11 
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17 =========================================================================*/
18 #ifndef __MILXMODEL_H
19 #define __MILXMODEL_H
20 
21 //VTK
22 #include <vtkSmartPointer.h>
23 #include <vtkCellArray.h>
24 #include <vtkPolyData.h>
25 #include <vtkImageData.h> //voxelise
26 #include <vtkPointData.h>
27 #include <vtkMatrix4x4.h>
28 #include <vtkPolyDataCollection.h>
29 #include <vtkPolyDataAlgorithm.h>
30 #include <vtkTransform.h>
31 #include <vtkTransformCollection.h>
32 #include <vtkFloatArray.h>
33 #include <vtkMutableUndirectedGraph.h>
34 
35 #include <milxGlobal.h>
36 
37 //enums
38 enum GlyphType {Circle = 0, Triangle, Cross, Square, Diamond};
39 
40 namespace milx
41 {
114 {
115 public:
120  Model();
125  Model(vtkSmartPointer<vtkPolyData> model);
130  virtual ~Model() {}
131 
141  void AddInput(vtkSmartPointer<vtkPolyData> model);
146  inline void AddArray(vtkSmartPointer<vtkDataArray> array)
147  { CurrentModel->GetPointData()->AddArray(array); }
152  void SetInput(vtkSmartPointer<vtkPolyData> model);
157  inline void SetInputData(vtkSmartPointer<vtkPolyData> model)
158  { SetInput(model); }
163  void SetTransform(vtkSmartPointer<vtkAbstractTransform> newTransform);
168  void SetGraph(vtkSmartPointer<vtkMutableUndirectedGraph> graph);
173  void SetPoints(vtkSmartPointer<vtkPoints> modelPoints);
178  void SetPolys(vtkSmartPointer<vtkCellArray> modelPolys);
183  void SetVectors(vtkSmartPointer<vtkDataArray> modelVectors);
188  void SetScalars(vtkSmartPointer<vtkDataArray> modelScalars);
193  inline void SetActiveScalars(std::string nameOfArray)
194  { CurrentModel->GetPointData()->SetActiveScalars(nameOfArray.c_str()); }
195 
202  inline vtkSmartPointer<vtkPolyData>& Result()
203  { return CurrentModel; }
210  inline vtkSmartPointer<vtkPolyData>& PreviousResult()
211  { return PreviousModel; }
218  inline vtkSmartPointer<vtkPolyData>& GetInput()
219  { return InputModel; }
224  inline vtkSmartPointer<vtkPolyData>& GetOutput()
225  { return Result(); }
226 #if VTK_MAJOR_VERSION <=5
227 
231  inline vtkAlgorithmOutput* GetProducerPort()
232  { return Result()->GetProducerPort(); }
233 #endif
234 
238  inline vtkIdType GetNumberOfPoints()
239  { return Result()->GetNumberOfPoints(); }
244  inline vtkIdType GetNumberOfArrays()
245  { return Result()->GetPointData()->GetNumberOfArrays(); }
250  inline vtkSmartPointer<vtkTransform>& GetTransform()
251  { return CurrentTransform; }
256  inline vtkSmartPointer<vtkPoints> GetPoints()
257  { return Result()->GetPoints(); }
262  inline vtkSmartPointer<vtkDataArray> GetScalars()
263  { return Result()->GetPointData()->GetScalars(); }
268  inline void GetScalarRange(double *range)
269  { if(GetScalars()) Result()->GetScalarRange(range); }
274  inline vtkSmartPointer<vtkDataArray> GetVectors()
275  { return Result()->GetPointData()->GetVectors(); }
280  inline vtkSmartPointer<vtkDataArray> GetNormals()
281  { return Result()->GetPointData()->GetNormals(); }
286  inline vtkSmartPointer<vtkDataArray> GetTensors()
287  { return Result()->GetPointData()->GetTensors(); }
292  inline void Update()
293  {
294  #if VTK_MAJOR_VERSION > 5
295  CurrentModel->Modified();
296  #else
297  CurrentModel->Update();
298  #endif // VTK_MAJOR_VERSION
299  }
301 
311  void Clean();
316  void Triangulate();
321  void MatchInformation(vtkSmartPointer<vtkPolyData> matchMesh, const bool rescale);
326  void IterativeClosestPointsRegistration(vtkSmartPointer<vtkPolyData> fixedMesh, const bool similarity = false, const int maxPoints = 1024);
331  void LandmarkBasedRegistration(vtkSmartPointer<vtkPolyData> fixedMesh, const bool similarity = false);
336  void Decimate(const float reduceFactor);
341  void QuadricDecimate(const float reduceFactor);
348  void ClusterDecimate();
353  void LaplacianSmoothing(const int iterations);
358  void WindowedSincSmoothing(const int iterations, const float passBand = 0.1);
363  void Curvature(const bool meanCurvature = true);
368  void Gradient();
373  void DistanceMap(vtkSmartPointer<vtkPoints> loopPoints);
380  void GaussianWeightScalars(float stddev, float clampValue);
385  void Rotate(const bool xAxis, const bool yAxis, const bool zAxis, const float angle, const coordinate centre);
390  void Flip(const bool xAxis, const bool yAxis, const bool zAxis);
395  void Clip(const coordinateType belowValue, const coordinateType aboveValue);
400  void Threshold(const coordinateType belowVal, const coordinateType aboveVal, const coordinateType outsideVal);
405  void Threshold(const coordinateType belowVal, const coordinateType aboveVal, const coordinateType insideVal, const coordinateType outsideVal);
410  void Mask(vtkSmartPointer<vtkPolyData> maskMesh);
412  void IsoSurface(vtkSmartPointer<vtkImageData> img, double minValue);
414  vtkSmartPointer<vtkImageData> Voxelise(const unsigned char insideValue, double *spacing, double *bounds = NULL, const size_t padVoxels = 1);
419  void DelaunayGraph3D();
424  void Delaunay2DTriangulation();
429  void DelaunayTriangulation();
434  void Append(vtkSmartPointer<vtkPolyData> model);
443  static void Scale(vtkSmartPointer<vtkPolyData> model, const coordinateType scale, const bool useCentroid = true);
451  inline void Scale(const coordinateType scale, const bool useCentroid = true)
452  { Scale(CurrentModel, scale, useCentroid); }
454 
464  void GenerateVertices();
469  void GenerateVertexScalars();
474  void GenerateVerticesAs2DGlyphs(const GlyphType glyphType);
479  void GenerateTubes();
484  void GeneratePointModel(const double newScale);
489  inline void GenerateSpheres(const double newScale)
490  { GeneratePointModel(newScale); }
495  void GenerateSampledPoints(const double distance);
502  void GenerateVectorField(const double newScale = 0.0, const bool useNormals = false);
509  void GenerateTensorField(const double newScale = 0.0);
516  void GenerateHedgehog(const double newScale = 0.0);
521  void GenerateNormals(const int pointNormals = 0);
526  void GenerateKMeansClustering(int numberOfClusters);
531  void GenerateQuantisedPoints(float quantiseFactor);
536  void GenerateCappedBoundaries();
541  void GenerateRegions();
546  void GenerateElevation(double x, double y, double z);
551  void GenerateElevation();
556  //~ void GenerateReebGraph();
558 
570  void ResetScalars();
577  void FillScalars(const coordinateType value);
584  static void ClearScalars(vtkSmartPointer<vtkPolyData> &mesh);
591  static inline void ClearArrays(vtkSmartPointer<vtkPolyData> &mesh)
592  {
593  #if (VTK_MAJOR_VERSION == 5 && VTK_MINOR_VERSION < 8)
594  mesh->GetPointData()->SetActiveScalars(NULL);
595  #else
596  for(int j = 0; j < mesh->GetPointData()->GetNumberOfArrays(); j ++)
598  mesh->GetPointData()->RemoveArray(0);
599  #endif
600  }
605  void RemoveScalars();
610  void RemoveArrays();
617  static void ScalarDifference(vtkSmartPointer<vtkPolyData> model1, vtkSmartPointer<vtkPolyData> model2, vtkSmartPointer<vtkPolyData> result);
624  static void ThresholdScalars(vtkSmartPointer<vtkPolyData> model, const coordinateType aboveVal, const coordinateType belowVal, const coordinateType outsideVal);
631  static void BinaryThresholdScalars(vtkSmartPointer<vtkPolyData> model, const coordinateType aboveVal, const coordinateType belowVal, const coordinateType insideVal = 1.0, const coordinateType outsideVal = 0.0);
636  static void MaskScalars(vtkSmartPointer<vtkPolyData> model1, vtkSmartPointer<vtkPolyData> model2);
638 
652  static vtkSmartPointer<vtkMatrix4x4> ProcrustesAlign(vtkSmartPointer<vtkPolyData> source, vtkSmartPointer<vtkPolyData> target, bool rigid = false);
653 
654  //Batch (collection) operations
663  void ConcatenateCollection(vtkSmartPointer<vtkPolyDataCollection> collection);
670  void ScaleCollection(vtkSmartPointer<vtkPolyDataCollection> collection, const coordinateType scale);
677  void SmoothCollection(vtkSmartPointer<vtkPolyDataCollection> collection, const size_t iterations);
684  void LaplacianCollection(vtkSmartPointer<vtkPolyDataCollection> collection, const size_t iterations);
691  void FlipCollection(vtkSmartPointer<vtkPolyDataCollection> collection, const bool xAxis, const bool yAxis, const bool zAxis);
698  void DecimateCollection(vtkSmartPointer<vtkPolyDataCollection> collection, const coordinateType factor);
705  void SplitCollection(vtkSmartPointer<vtkPolyDataCollection> collection, vtkSmartPointer<vtkPolyDataCollection> components, std::vector< vtkSmartPointer<vtkPolyDataCollection> > &splitCollections);
712  void ScalarDifferenceCollection(vtkSmartPointer<vtkPolyDataCollection> collection);
713  void ScalarDifferenceCollection(vtkSmartPointer<vtkPolyDataCollection> collection1, vtkSmartPointer<vtkPolyDataCollection> collection2, vtkSmartPointer<vtkPolyDataCollection> &results);
720  void ScalarThresholdCollection(vtkSmartPointer<vtkPolyDataCollection> collection, const coordinateType aboveVal, const coordinateType belowVal);
727  void ClipCollection(vtkSmartPointer<vtkPolyDataCollection> collection, const coordinateType aboveVal, const coordinateType belowVal);
734  void ScalarStatisticsCollection(vtkSmartPointer<vtkPolyDataCollection> collection);
741  void ScalarRemoveCollection(vtkSmartPointer<vtkPolyDataCollection> collection);
748  void ScalarCopyCollection(vtkSmartPointer<vtkPolyDataCollection> collection);
753  double MeanSquaredErrorCollection(vtkSmartPointer<vtkPolyDataCollection> collection);
763  vtkSmartPointer<vtkPolyDataCollection> ProcrustesAlignCollection(vtkSmartPointer<vtkPolyDataCollection> collection, bool rigid = false);
772  vtkSmartPointer<vtkPolyDataCollection> IterativeClosestPointsAlignCollection(vtkSmartPointer<vtkPolyDataCollection> collection, bool rigid = false, vtkSmartPointer<vtkTransformCollection> tCollection=0);
774 
775 protected:
776  vtkSmartPointer<vtkPolyData> InputModel;
777  vtkSmartPointer<vtkPolyData> CurrentModel;
778  vtkSmartPointer<vtkPolyData> PreviousModel;
779  vtkSmartPointer<vtkTransform> CurrentTransform;
780 
782 
783  inline bool IsCurrentModel()
784  {
785  if(!CurrentModel)
786  {
787  PrintError("Input Model not set. Ignoring Operation.");
788  return false;
789  }
790 
791  return true;
792  }
793  inline void UpdateModelState(vtkSmartPointer<vtkPolyDataAlgorithm> filter)
794  {
795  CurrentModel = filter->GetOutput();
796  PreviousModel = filter->GetPolyDataInput(0);
797  if(!InputModel)
798  InputModel = PreviousModel;
799  }
800  inline void UpdateModelStateFromModel(vtkSmartPointer<vtkPolyData> mesh)
801  {
802  PreviousModel = CurrentModel;
803  CurrentModel = mesh;
804  if(!InputModel)
805  InputModel = PreviousModel;
806  }
807 
808 private:
809 
810 };
811 
812 } //end namespace milx
813 
814 #endif //__MILXMODEL_H
vtkSmartPointer< vtkPolyData > CurrentModel
Holds the current model in the pipeline.
Definition: milxModel.h:777
vtkAlgorithmOutput * GetProducerPort()
Returns the current model, i.e. the result of the latest operation ITK/VTK style. ...
Definition: milxModel.h:231
void Update()
Updates the model to be the most current.
Definition: milxModel.h:292
void AddArray(vtkSmartPointer< vtkDataArray > array)
Appends an array to the model.
Definition: milxModel.h:146
bool InternalInPlaceOperation
Used by the collection members to assign via pointers rather than deep copys.
Definition: milxModel.h:781
vtkSmartPointer< vtkDataArray > GetScalars()
Returns the active scalars of the model.
Definition: milxModel.h:262
void PrintError(const std::string msg)
Displays a generic msg to standard error with carriage return.
Definition: milxGlobal.h:202
#define SMILI_EXPORT
DLL Function Symbol for Windows. It is empty for other OSes.
Definition: milxGlobal.h:227
vtkSmartPointer< vtkPolyData > & Result()
Returns the current model, i.e. the result of the latest operation.
Definition: milxModel.h:202
vtkSmartPointer< vtkTransform > & GetTransform()
Returns the current net transform applied to the model, i.e. concatenations of all (rigid/linear) tra...
Definition: milxModel.h:250
vtkIdType GetNumberOfArrays()
Returns the total number of point-based arrays in the model.
Definition: milxModel.h:244
virtual ~Model()
Standard Destructor.
Definition: milxModel.h:130
vtkSmartPointer< vtkPolyData > PreviousModel
Holds the previous model in the pipeline.
Definition: milxModel.h:778
void SetActiveScalars(std::string nameOfArray)
Sets the array of name as the scalar field of the mesh (values per vertex of the surface) ...
Definition: milxModel.h:193
vtkSmartPointer< vtkDataArray > GetNormals()
Returns the normal vectors of the model.
Definition: milxModel.h:280
vtkSmartPointer< vtkDataArray > GetVectors()
Returns the active vectors of the model.
Definition: milxModel.h:274
vtkSmartPointer< vtkTransform > CurrentTransform
transform applied to model
Definition: milxModel.h:779
void GenerateSpheres(const double newScale)
Generate spheres of scaling for each point of the mesh. Same as GeneratePointModel().
Definition: milxModel.h:489
void SetInputData(vtkSmartPointer< vtkPolyData > model)
Assigns the input model to class. Same as SetInput and meant for VTK version > 5. ...
Definition: milxModel.h:157
static void ClearArrays(vtkSmartPointer< vtkPolyData > &mesh)
Removes all the arrays from the current model.
Definition: milxModel.h:591
void GetScalarRange(double *range)
Returns the scalar range of the model.
Definition: milxModel.h:268
vtkSmartPointer< vtkPolyData > & PreviousResult()
Returns the previous model, i.e. the result of the penultimate operation.
Definition: milxModel.h:210
vtkIdType GetNumberOfPoints()
Returns the total number of points of the model currently held.
Definition: milxModel.h:238
Represents a model (i.e. a model with cells and scalar values) and their common operations. Also allows batch operations on collection of models.
Definition: milxModel.h:113
vtkSmartPointer< vtkPolyData > & GetInput()
Returns the input model, i.e. the very most initial model.
Definition: milxModel.h:218
vtkSmartPointer< vtkDataArray > GetTensors()
Returns the active tensors (3x3 matrix per vertex) of the model.
Definition: milxModel.h:286
vtkSmartPointer< vtkPolyData > & GetOutput()
Returns the current model, i.e. the result of the latest operation ITK/VTK style. ...
Definition: milxModel.h:224
vtkSmartPointer< vtkPoints > GetPoints()
Returns the points of the model.
Definition: milxModel.h:256
vtkSmartPointer< vtkPolyData > InputModel
Holds the initial model in the pipeline.
Definition: milxModel.h:776