1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkExtractPoints.cxx
5 
6   Copyright (c) Kitware, Inc.
7   All rights reserved.
8   See LICENSE file for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkExtractPoints.h"
16 
17 #include "vtkObjectFactory.h"
18 #include "vtkPointSet.h"
19 #include "vtkPoints.h"
20 #include "vtkImplicitFunction.h"
21 #include "vtkSMPTools.h"
22 
23 vtkStandardNewMacro(vtkExtractPoints);
24 vtkCxxSetObjectMacro(vtkExtractPoints,ImplicitFunction,vtkImplicitFunction);
25 
26 //----------------------------------------------------------------------------
27 // Helper classes to support efficient computing, and threaded execution.
28 namespace {
29 
30 //----------------------------------------------------------------------------
31 // The threaded core of the algorithm
32 template <typename T>
33 struct ExtractPoints
34 {
35   const T *Points;
36   vtkImplicitFunction *Function;
37   bool ExtractInside;
38   vtkIdType *PointMap;
39 
ExtractPoints__anon399155c80111::ExtractPoints40   ExtractPoints(T *points, vtkImplicitFunction *f, bool inside, vtkIdType *map) :
41     Points(points), Function(f), ExtractInside(inside), PointMap(map)
42   {
43   }
44 
operator ()__anon399155c80111::ExtractPoints45   void operator() (vtkIdType ptId, vtkIdType endPtId)
46   {
47       const T *p = this->Points + 3*ptId;
48       vtkIdType *map = this->PointMap + ptId;
49       vtkImplicitFunction *f = this->Function;
50       double x[3];
51       double inside = (this->ExtractInside ? 1.0 : -1.0);
52 
53       for ( ; ptId < endPtId; ++ptId)
54       {
55         x[0] = static_cast<double>(*p++);
56         x[1] = static_cast<double>(*p++);
57         x[2] = static_cast<double>(*p++);
58 
59         *map++ = ((f->FunctionValue(x) * inside) <= 0.0 ? 1 : -1 );
60       }
61   }
62 
Execute__anon399155c80111::ExtractPoints63   static void Execute(vtkExtractPoints *self, vtkIdType numPts,
64                       T *points, vtkIdType *map)
65   {
66       ExtractPoints extract(points, self->GetImplicitFunction(),
67                             self->GetExtractInside(), map);
68       vtkSMPTools::For(0, numPts, extract);
69   }
70 
71 }; //ExtractPoints
72 
73 } //anonymous namespace
74 
75 //================= Begin class proper =======================================
76 //----------------------------------------------------------------------------
vtkExtractPoints()77 vtkExtractPoints::vtkExtractPoints()
78 {
79   this->ImplicitFunction = nullptr;
80   this->ExtractInside = true;
81 }
82 
83 //----------------------------------------------------------------------------
~vtkExtractPoints()84 vtkExtractPoints::~vtkExtractPoints()
85 {
86   this->SetImplicitFunction(nullptr);
87 }
88 
89 //----------------------------------------------------------------------------
90 // Overload standard modified time function. If implicit function is modified,
91 // then this object is modified as well.
GetMTime()92 vtkMTimeType vtkExtractPoints::GetMTime()
93 {
94   vtkMTimeType mTime=this->MTime.GetMTime();
95   vtkMTimeType impFuncMTime;
96 
97   if ( this->ImplicitFunction != nullptr )
98   {
99     impFuncMTime = this->ImplicitFunction->GetMTime();
100     mTime = ( impFuncMTime > mTime ? impFuncMTime : mTime );
101   }
102 
103   return mTime;
104 }
105 
106 //----------------------------------------------------------------------------
107 // Traverse all the input points and extract points that are contained within
108 // and implicit function.
FilterPoints(vtkPointSet * input)109 int vtkExtractPoints::FilterPoints(vtkPointSet *input)
110 {
111   // Check the input.
112   if ( !this->ImplicitFunction )
113   {
114     vtkErrorMacro(<<"Implicit function required\n");
115     return 0;
116   }
117 
118   // Determine which points, if any, should be removed. We create a map
119   // to keep track. The bulk of the algorithmic work is done in this pass.
120   vtkIdType numPts = input->GetNumberOfPoints();
121   void *inPtr = input->GetPoints()->GetVoidPointer(0);
122   switch (input->GetPoints()->GetDataType())
123   {
124     vtkTemplateMacro(ExtractPoints<VTK_TT>::
125                      Execute(this, numPts, (VTK_TT *)inPtr, this->PointMap));
126   }
127 
128   return 1;
129 }
130 
131 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)132 void vtkExtractPoints::PrintSelf(ostream& os, vtkIndent indent)
133 {
134   this->Superclass::PrintSelf(os,indent);
135 
136   os << indent << "Implicit Function: "
137      << static_cast<void *>(this->ImplicitFunction) << "\n";
138   os << indent << "Extract Inside: "
139      << (this->ExtractInside ? "On\n" : "Off\n");
140 }
141