1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkChacoReader.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   vtkChacoReader
22  * @brief   Read a Chaco file and create a vtkUnstructuredGrid.
23  *
24  * vtkChacoReader is an unstructured grid source object that reads Chaco
25  * files.  The reader DOES NOT respond to piece requests. Chaco
26  * is a graph partitioning package developed at Sandia National Laboratories
27  * in the early 1990s.  (http://www.cs.sandia.gov/~bahendr/chaco.html)
28  *
29  * Note that the Chaco "edges" become VTK "cells", and the Chaco
30  * "vertices" become VTK "points".
31 */
32 
33 #ifndef vtkChacoReader_h
34 #define vtkChacoReader_h
35 
36 #include "vtkIOGeometryModule.h" // For export macro
37 #include "vtkUnstructuredGridAlgorithm.h"
38 
39 class VTKIOGEOMETRY_EXPORT vtkChacoReader : public vtkUnstructuredGridAlgorithm
40 {
41 public:
42   static vtkChacoReader *New();
43   vtkTypeMacro(vtkChacoReader,vtkUnstructuredGridAlgorithm);
44   void PrintSelf(ostream& os, vtkIndent indent) override;
45 
46   /**
47    * Specify the base name of the Chaco files.  The reader will try to
48    * open BaseName.coords and BaseName.graph.
49    */
50 
51   vtkSetStringMacro(BaseName);
52   vtkGetStringMacro(BaseName);
53 
54   /**
55    * Indicate whether this reader should create a cell array containing
56    * global IDs for the cells in the output vtkUnstructuredGrid.  These cells
57    * represent the edges that were in the Chaco file.  Each edge is a vtkLine.
58    * Default is ON.
59    */
60 
61   vtkSetMacro(GenerateGlobalElementIdArray, vtkTypeBool);
62   vtkGetMacro(GenerateGlobalElementIdArray, vtkTypeBool);
63   vtkBooleanMacro(GenerateGlobalElementIdArray, vtkTypeBool);
64 
GetGlobalElementIdArrayName()65   static const char *GetGlobalElementIdArrayName() { return "GlobalElementId"; }
66 
67   /**
68    * Indicate whether this reader should create a point array of global IDs for
69    * the points in the output vtkUnstructuredGrid.  These points are the vertices
70    * that were in the Chaco file.  Global point IDs start at "1" for the first
71    * vertex in BaseName.coords and go up from there.
72    * Default is ON.
73    */
74 
75   vtkSetMacro(GenerateGlobalNodeIdArray, vtkTypeBool);
76   vtkGetMacro(GenerateGlobalNodeIdArray, vtkTypeBool);
77   vtkBooleanMacro(GenerateGlobalNodeIdArray, vtkTypeBool);
78 
GetGlobalNodeIdArrayName()79   static const char *GetGlobalNodeIdArrayName() { return "GlobalNodeId"; }
80 
81   /**
82    * Indicate whether this reader should create a point array for each
83    * vertex weight in the Chaco file.
84    * Default is OFF.
85    */
86 
87   vtkSetMacro(GenerateVertexWeightArrays, vtkTypeBool);
88   vtkGetMacro(GenerateVertexWeightArrays, vtkTypeBool);
89   vtkBooleanMacro(GenerateVertexWeightArrays, vtkTypeBool);
90 
91   /**
92    * Returns the number of weights per vertex in the Chaco file, whether or
93    * not GenerateVertexWeightArrays is ON.
94    */
95 
96   vtkGetMacro(NumberOfVertexWeights, int);
97 
98   /**
99    * This method returns the name of the selected Vertex weight point
100    * array.  If you did not turn on GenerateVertexWeightArrays, or
101    * if the weight you requested is invalid, it returns nullptr.
102    * Weights begin at one and go up to NumberOfVertexWeights.
103    * This is a pointer to our copy of the name, so don't "delete" it.
104    */
105 
106   const char *GetVertexWeightArrayName(int weight);
107 
108   /**
109    * Each edge in the Chaco file connects two vertices.  The file may
110    * specify one or more weights for each edge.  (The weight for an
111    * edge from vertex A to vertex B equals the weight from B to A.)
112    * Indicate with the following parameter whether this reader should
113    * create a cell array for each weight for every edge.
114    * Default is OFF.
115    */
116 
117   vtkSetMacro(GenerateEdgeWeightArrays, vtkTypeBool);
118   vtkGetMacro(GenerateEdgeWeightArrays, vtkTypeBool);
119   vtkBooleanMacro(GenerateEdgeWeightArrays, vtkTypeBool);
120 
121   /**
122    * Returns the number of weights per edge in the Chaco file, whether or
123    * not GenerateEdgeWeightArrays is ON.
124    */
125 
126   vtkGetMacro(NumberOfEdgeWeights, int);
127 
128   /**
129    * This method returns the name of the selected Edge weight cell
130    * array.  If you did not turn on GenerateEdgeWeightArrays, or
131    * if the weight you requested is invalid, it returns nullptr.
132    * Weights begin at one and go up to NumberOfEdgeWeights.
133    * This is a pointer to our copy of the name, so don't "delete" it.
134    */
135 
136   const char *GetEdgeWeightArrayName(int weight);
137 
138   //@{
139   /**
140    * Access to meta data generated by RequestInformation.
141    */
142   vtkGetMacro(Dimensionality, int);
143   vtkGetMacro(NumberOfEdges, vtkIdType);
144   vtkGetMacro(NumberOfVertices, vtkIdType);
145   //@}
146 
147   /**
148    * After this filter executes, this method returns the number of
149    * cell arrays that were created to hold the edge weights.  It
150    * is equal to NumberOfEdgeWeights if GenerateEdgeWeightArrays was ON.
151    */
152 
153   vtkGetMacro(NumberOfCellWeightArrays, int);
154 
155   /**
156    * After this filter executes, this method returns the number of
157    * point arrays that were created to hold the vertex weights.  It
158    * is equal to NumberOfVertexWeights if GenerateVertexWeightArrays was ON.
159    */
160 
161   vtkGetMacro(NumberOfPointWeightArrays, int);
162 
163 protected:
164   vtkChacoReader();
165   ~vtkChacoReader() override;
166 
167   int BuildOutputGrid(vtkUnstructuredGrid *gr);
168 
169   // methods for parsing Chaco files
170 
171   void CloseCurrentFile();
172   int OpenCurrentFile();
173 
174   int InputGeom(vtkIdType nvtxs, int igeom, double *x, double *y, double *z);
175   int InputGraph1();
176   int InputGraph2( vtkIdType **start, vtkIdType **adjacency, double **vweights,
177       double **eweights);
178   int GetCoordsMetadata();
179   void GetGraphMetadata();
180 
181   // methods for creating vtkUnstructuredGrid from Chaco file data
182 
183   int ReadFile(vtkUnstructuredGrid* output);
184   void AddElementIds(vtkUnstructuredGrid* output);
185   void AddNodeIds(vtkUnstructuredGrid* output);
186 
187   void MakeWeightArrayNames(int nv, int ne);
188 
189   // Parameters for controlling what is read in.
190   char *BaseName;
191   vtkTypeBool GenerateGlobalElementIdArray;
192   vtkTypeBool GenerateGlobalNodeIdArray;
193 
194   vtkTypeBool GenerateVertexWeightArrays;
195   vtkTypeBool GenerateEdgeWeightArrays;
196 
197   FILE *CurrentGeometryFP;
198   FILE *CurrentGraphFP;
199   char *CurrentBaseName;
200   vtkSetStringMacro(CurrentBaseName);
201 
202   char **VarrayName;
203   char **EarrayName;
204 
205   //----------------------------------------------------------------------
206   // The following metadata is read during RequestInformation.  If you
207   // add new metadata, you must modify vtkPChacoReader::RequestInformation
208   // to include it when process 0 broadcasts the metadata.
209 
210   int Dimensionality;
211   vtkIdType NumberOfVertices;
212   vtkIdType NumberOfEdges;
213   int NumberOfVertexWeights;   // in file
214   int NumberOfEdgeWeights;     // in file
215   int GraphFileHasVertexNumbers;
216 
217   //----------------------------------------------------------------------
218 
219   int NumberOfPointWeightArrays;   // in output unstructured grid
220   int NumberOfCellWeightArrays;    // in output unstructured grid
221 
222   // Keep the points and cells
223   // around so they don't need to be re-read when the
224   // options change.
225   vtkUnstructuredGrid *DataCache;
226 
227   // Should I re-read in the geometry and topology of the dataset
228   int RemakeDataCacheFlag;
229 
230   int RequestInformation(
231     vtkInformation *, vtkInformationVector **, vtkInformationVector *) override;
232   int RequestData(
233     vtkInformation *, vtkInformationVector **, vtkInformationVector *) override;
234 
235 private:
236   vtkChacoReader(const vtkChacoReader&) = delete;
237   void operator=(const vtkChacoReader&) = delete;
238 
239   double ReadVal(FILE *infile, int *end_flag);
240   vtkIdType ReadInt(FILE *infile, int *end_flag);
241   void FlushLine( FILE *infile);
242   void ResetInputBuffers();
243 
244   char *Line;
245   int Line_length;
246   int Offset;
247   int Break_pnt;
248   int Save_pnt;
249 
250   void ClearWeightArrayNames();
251 };
252 
253 #endif
254