1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkMergeCells.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm 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 /*----------------------------------------------------------------------------
16  Copyright (c) Sandia Corporation
17  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
18 ----------------------------------------------------------------------------*/
19 
20 /**
21  * @class   vtkMergeCells
22  * @brief   merges any number of vtkDataSets back into a single
23  *   vtkUnstructuredGrid
24  *
25  *
26  *    Designed to work with distributed vtkDataSets, this class will take
27  *    vtkDataSets and merge them back into a single vtkUnstructuredGrid.
28  *
29  *    The vtkPoints object of the unstructured grid will have data type
30  *    VTK_FLOAT if input is not of type vtkPointSet, otherwise it will have same
31  *    data type than the input point set.
32  *
33  *    It is assumed the different DataSets have the same field arrays.  If
34  *    the name of a global point ID array is provided, this class will
35  *    refrain from including duplicate points in the merged Ugrid.  This
36  *    class differs from vtkAppendFilter in these ways: (1) it uses less
37  *    memory than that class (which uses memory equal to twice the size
38  *    of the final Ugrid) but requires that you know the size of the
39  *    final Ugrid in advance (2) this class assumes the individual DataSets have
40  *    the same field arrays, while vtkAppendFilter intersects the field
41  *    arrays (3) this class knows duplicate points may be appearing in
42  *    the DataSets and can filter those out, (4) this class is not a filter.
43  */
44 
45 #ifndef vtkMergeCells_h
46 #define vtkMergeCells_h
47 
48 #include "vtkAlgorithm.h"            // for vtkAlgorithm::DEFAULT_PRECISION
49 #include "vtkDataSetAttributes.h"    // Needed for FieldList
50 #include "vtkFiltersGeneralModule.h" // For export macro
51 #include "vtkObject.h"
52 #include "vtkSmartPointer.h" //fot vtkSmartPointer
53 
54 class vtkCellData;
55 class vtkDataSet;
56 class vtkMergeCellsSTLCloak;
57 class vtkMergePoints;
58 class vtkIncrementalPointLocator;
59 class vtkPointData;
60 class vtkUnstructuredGrid;
61 
62 class VTKFILTERSGENERAL_EXPORT vtkMergeCells : public vtkObject
63 {
64 public:
65   vtkTypeMacro(vtkMergeCells, vtkObject);
66   void PrintSelf(ostream& os, vtkIndent indent) override;
67 
68   static vtkMergeCells* New();
69 
70   ///@{
71   /**
72    * Set the vtkUnstructuredGrid object that will become the
73    * union of the DataSets specified in MergeDataSet calls.
74    * vtkMergeCells assumes this grid is empty at first.
75    */
76   virtual void SetUnstructuredGrid(vtkUnstructuredGrid*);
77   vtkGetObjectMacro(UnstructuredGrid, vtkUnstructuredGrid);
78   ///@}
79 
80   ///@{
81   /**
82    * Specify the total number of cells in the final vtkUnstructuredGrid.
83    * Make this call before any call to MergeDataSet().
84    */
85   vtkSetMacro(TotalNumberOfCells, vtkIdType);
86   vtkGetMacro(TotalNumberOfCells, vtkIdType);
87   ///@}
88 
89   ///@{
90   /**
91    * Specify the total number of points in the final vtkUnstructuredGrid
92    * Make this call before any call to MergeDataSet().  This is an
93    * upper bound, since some points may be duplicates.
94    */
95   vtkSetMacro(TotalNumberOfPoints, vtkIdType);
96   vtkGetMacro(TotalNumberOfPoints, vtkIdType);
97   ///@}
98 
99   ///@{
100   /**
101    * vtkMergeCells attempts eliminate duplicate points when merging
102    * data sets.  This is done most efficiently if a global point ID
103    * field array is available.  Set the name of the point array if you
104    * have one.
105    */
106   vtkSetMacro(UseGlobalIds, int);
107   vtkGetMacro(UseGlobalIds, int);
108   vtkBooleanMacro(UseGlobalIds, int);
109   ///@}
110 
111   ///@{
112   /**
113    * vtkMergeCells attempts eliminate duplicate points when merging
114    * data sets.  If no global point ID field array name is provided,
115    * it will use a point locator to find duplicate points.  You can
116    * set a tolerance for that locator here.  The default tolerance
117    * is 10e-4.
118    */
119   vtkSetClampMacro(PointMergeTolerance, double, 0.0, VTK_DOUBLE_MAX);
120   vtkGetMacro(PointMergeTolerance, double);
121   ///@}
122 
123   ///@{
124   /**
125    * vtkMergeCells will detect and filter out duplicate cells if you
126    * provide it the name of a global cell ID array.
127    */
128   vtkSetMacro(UseGlobalCellIds, int);
129   vtkGetMacro(UseGlobalCellIds, int);
130   vtkBooleanMacro(UseGlobalCellIds, int);
131   ///@}
132 
133   ///@{
134   /**
135    * vtkMergeCells attempts eliminate duplicate points when merging
136    * data sets.  If for some reason you don't want it to do this,
137    * than MergeDuplicatePointsOff().
138    */
139   vtkSetMacro(MergeDuplicatePoints, bool);
140   vtkGetMacro(MergeDuplicatePoints, bool);
141   vtkBooleanMacro(MergeDuplicatePoints, bool);
142   ///@}
143 
144   /**
145    * Clear the Locator and set it to nullptr.
146    */
147   void InvalidateCachedLocator();
148 
149   ///@{
150   /**
151    * We need to know the number of different data sets that will
152    * be merged into one so we can pre-allocate some arrays.
153    * This can be an upper bound, not necessarily exact.
154    */
155   vtkSetMacro(TotalNumberOfDataSets, int);
156   vtkGetMacro(TotalNumberOfDataSets, int);
157   ///@}
158 
159   /**
160    * Provide a DataSet to be merged in to the final UnstructuredGrid.
161    * This call returns after the merge has completed.  Be sure to call
162    * SetTotalNumberOfCells, SetTotalNumberOfPoints, and SetTotalNumberOfDataSets
163    * before making this call.  Return 0 if OK, -1 if error.
164    */
165   int MergeDataSet(vtkDataSet* set);
166 
167   ///@{
168   /**
169    * Set/get the desired precision for the output points. See the documentation
170    * for the vtkAlgorithm::DesiredOutputPrecision enum for an explanation of
171    * the available precision settings.
172    */
173   vtkSetMacro(OutputPointsPrecision, int);
174   vtkGetMacro(OutputPointsPrecision, int);
175   ///@}
176 
177   /**
178    * Call Finish() after merging last DataSet to free unneeded memory and to
179    * make sure the ugrid's GetNumberOfPoints() reflects the actual
180    * number of points set, not the number allocated.
181    */
182   void Finish();
183 
184 protected:
185   vtkMergeCells();
186   ~vtkMergeCells() override;
187 
188   void FreeLists();
189   void StartUGrid(vtkDataSet* set);
190   vtkIdType* MapPointsToIdsUsingGlobalIds(vtkDataSet* set);
191   vtkIdType* MapPointsToIdsUsingLocator(vtkDataSet* set);
192   vtkIdType AddNewCellsUnstructuredGrid(vtkDataSet* set, vtkIdType* idMap);
193   vtkIdType AddNewCellsDataSet(vtkDataSet* set, vtkIdType* idMap);
194 
195   int TotalNumberOfDataSets;
196 
197   vtkIdType TotalNumberOfCells;
198   vtkIdType TotalNumberOfPoints;
199 
200   vtkIdType NumberOfCells; // so far
201   vtkIdType NumberOfPoints;
202 
203   int UseGlobalIds;     // point, or node, IDs
204   int UseGlobalCellIds; // cell IDs
205 
206   double PointMergeTolerance;
207   bool MergeDuplicatePoints;
208 
209   int OutputPointsPrecision = vtkAlgorithm::DEFAULT_PRECISION;
210 
211   char InputIsUGrid;
212   char InputIsPointSet;
213 
214   vtkMergeCellsSTLCloak* GlobalIdMap;
215   vtkMergeCellsSTLCloak* GlobalCellIdMap;
216 
217   vtkDataSetAttributes::FieldList* PointList;
218   vtkDataSetAttributes::FieldList* CellList;
219 
220   vtkUnstructuredGrid* UnstructuredGrid;
221 
222   int NextGrid;
223 
224   vtkSmartPointer<vtkIncrementalPointLocator> Locator;
225 
226 private:
227   vtkMergeCells(const vtkMergeCells&) = delete;
228   void operator=(const vtkMergeCells&) = delete;
229 };
230 #endif
231