vtkCellArray.h 10.5 KB
Newer Older
1
2
/*=========================================================================

Ken Martin's avatar
Ken Martin committed
3
  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
  Module:    vtkCellArray.h
5
6
7
8
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

Ken Martin's avatar
Ken Martin committed
9

Ken Martin's avatar
Ken Martin committed
10
Copyright (c) 1993-1998 Ken Martin, Will Schroeder, Bill Lorensen.
Ken Martin's avatar
Ken Martin committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.

The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.

IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
38
39
40


=========================================================================*/
41
// .NAME vtkCellArray - object to represent cell connectivity
Will Schroeder's avatar
Will Schroeder committed
42
// .SECTION Description
Ken Martin's avatar
Ken Martin committed
43
// vtkCellArray is a supporting object that explicitly represents cell 
Will Schroeder's avatar
Will Schroeder committed
44
45
// connectivity. The cell array structure is a raw integer list
// of the form: (n,id1,id2,...,idn, n,id1,id2,...,idn, ...)
Will Schroeder's avatar
Will Schroeder committed
46
47
// where n is the number of points in the cell, and id is a zero-offset index 
// into an associated point list.
Will Schroeder's avatar
Will Schroeder committed
48
49
//
// Advantages of this data structure are its compactness, simplicity, and 
Will Schroeder's avatar
Will Schroeder committed
50
51
// easy interface to external data.  However, it is totally inadequate for 
// random access.  This functionality (when necessary) is accomplished by 
52
// using the vtkCellTypes and vtkCellLinks objects to extend the definition of 
53
// the data structure.
Will Schroeder's avatar
Will Schroeder committed
54
// .SECTION See Also
55
// vtkCellTypes vtkCellLinks
Will Schroeder's avatar
Will Schroeder committed
56

Ken Martin's avatar
Ken Martin committed
57
58
#ifndef __vtkCellArray_h
#define __vtkCellArray_h
Ken Martin's avatar
Ken Martin committed
59

Ken Martin's avatar
Ken Martin committed
60
61
#include "vtkIntArray.h"
#include "vtkCell.h"
Ken Martin's avatar
Ken Martin committed
62

Will Schroeder's avatar
Will Schroeder committed
63
class VTK_EXPORT vtkCellArray : public vtkReferenceCount
64
{
Ken Martin's avatar
Ken Martin committed
65
public:
Charles Law's avatar
Charles Law committed
66
67
  vtkCellArray();
  vtkCellArray (const int sz, const int ext=1000);
Ken Martin's avatar
Ken Martin committed
68
  vtkCellArray(const vtkCellArray& ca);
Charles Law's avatar
Charles Law committed
69
70
71
72
73
  ~vtkCellArray();

  int Allocate(const int sz, const int ext=1000) 
    {return this->Ia->Allocate(sz,ext);};
  void Initialize() {this->Ia->Initialize();};
74
  static vtkCellArray *New() {return new vtkCellArray;};
75
  const char *GetClassName() {return "vtkCellArray";};
Will Schroeder's avatar
Will Schroeder committed
76

Will Schroeder's avatar
Will Schroeder committed
77
78
  int GetNumberOfCells();

Will Schroeder's avatar
Will Schroeder committed
79
  // estimator for storage allocation
Will Schroeder's avatar
Will Schroeder committed
80
81
82
83
84
85
  int EstimateSize(int numCells, int maxPtsPerCell);

  void InitTraversal();
  int GetNextCell(int& npts, int* &pts);

  int GetSize();
Will Schroeder's avatar
Will Schroeder committed
86
  int GetNumberOfConnectivityEntries();
Will Schroeder's avatar
Will Schroeder committed
87
88
  void GetCell(int loc, int &npts, int* &pts);

Will Schroeder's avatar
Will Schroeder committed
89
90
91
92
93
94
95
96
  // methods to insert cells. Can be used in combination.
  int InsertNextCell(vtkCell *cell);
  int InsertNextCell(int npts, int* pts);
  int InsertNextCell(vtkIdList &pts);
  int InsertNextCell(int npts); //this and next two methods work together
  void InsertCellPoint(int id);
  void UpdateCellCount(int npts);

97
98
  int GetInsertLocation(int npts);
  int GetTraversalLocation(int npts);
Ken Martin's avatar
Ken Martin committed
99
  
Will Schroeder's avatar
Will Schroeder committed
100
  void ReverseCell(int loc);
Will Schroeder's avatar
Will Schroeder committed
101
  void ReplaceCell(int loc, int npts, int *pts);
Will Schroeder's avatar
Will Schroeder committed
102

103
104
  int GetMaxCellSize();

105
  // miscellaneous pointer type operations (for fast read/write operations)
Will Schroeder's avatar
Will Schroeder committed
106
107
  int *GetPointer();
  int *WritePointer(const int ncells, const int size);
108

Will Schroeder's avatar
Will Schroeder committed
109
110
111
112
  // reuse memory
  void Reset();
  void Squeeze();

Will Schroeder's avatar
Will Schroeder committed
113
protected:
114
  int NumberOfCells;
115
116
  int InsertLocation;     //keep track of current insertion point
  int TraversalLocation;   //keep track of traversal position
Charles Law's avatar
Charles Law committed
117
  vtkIntArray *Ia;
Ken Martin's avatar
Ken Martin committed
118
119
};

Will Schroeder's avatar
Will Schroeder committed
120
121
// Description:
// Get the number of cells in the array.
Ken Martin's avatar
Ken Martin committed
122
inline int vtkCellArray::GetNumberOfCells() {return this->NumberOfCells;};
Will Schroeder's avatar
Will Schroeder committed
123
124

// Description:
125
// Create a cell by specifying the number of points and an array of point id's.
Ken Martin's avatar
Ken Martin committed
126
inline int vtkCellArray::InsertNextCell(int npts, int* pts)
Will Schroeder's avatar
Will Schroeder committed
127
{
Charles Law's avatar
Charles Law committed
128
  int i = this->Ia->GetMaxId() + 1;
Will Schroeder's avatar
Will Schroeder committed
129
  int *ptr = this->Ia->WritePointer(i,npts+1);
Will Schroeder's avatar
Will Schroeder committed
130
131
132
  
  for ( *ptr++ = npts, i = 0; i < npts; i++) *ptr++ = *pts++;

Will Schroeder's avatar
Will Schroeder committed
133
  this->NumberOfCells++;
134
  this->InsertLocation += npts + 1;
Ken Martin's avatar
Ken Martin committed
135
136

  return this->NumberOfCells;
Will Schroeder's avatar
Will Schroeder committed
137
138
}

Will Schroeder's avatar
Will Schroeder committed
139
140
141
142
143
// Description:
// Create a cell by specifying a list of point ids.
inline int vtkCellArray::InsertNextCell(vtkIdList &pts)
{
  int npts = pts.GetNumberOfIds();
Charles Law's avatar
Charles Law committed
144
  int i = this->Ia->GetMaxId() + 1;
Will Schroeder's avatar
Will Schroeder committed
145
  int *ptr = this->Ia->WritePointer(i,npts+1);
Will Schroeder's avatar
Will Schroeder committed
146
147
148
  
  for ( *ptr++ = npts, i = 0; i < npts; i++) *ptr++ = pts.GetId(i);

Will Schroeder's avatar
Will Schroeder committed
149
  this->NumberOfCells++;
150
  this->InsertLocation += npts + 1;
Will Schroeder's avatar
Will Schroeder committed
151
152
153
154

  return this->NumberOfCells;
}

Will Schroeder's avatar
Will Schroeder committed
155
156
// Description:
// Create cells by specifying count, and then adding points one at a time using
Will Schroeder's avatar
Will Schroeder committed
157
158
// method InsertCellPoint(). If you don't know the count initially, use the
// method UpdateCellCount() to complete the cell.
Ken Martin's avatar
Ken Martin committed
159
inline int vtkCellArray::InsertNextCell(int npts)
Will Schroeder's avatar
Will Schroeder committed
160
{
161
  this->InsertLocation = this->Ia->InsertNextValue(npts) + 1;
Will Schroeder's avatar
Will Schroeder committed
162
  this->NumberOfCells++;
Ken Martin's avatar
Ken Martin committed
163
164

  return this->NumberOfCells;
Will Schroeder's avatar
Will Schroeder committed
165
166
167
168
169
}

// Description:
// Used in conjunction with InsertNextCell(int npts) to add another point
// to the list of cells.
Ken Martin's avatar
Ken Martin committed
170
inline void vtkCellArray::InsertCellPoint(int id) 
Will Schroeder's avatar
Will Schroeder committed
171
{
172
  this->Ia->InsertValue(this->InsertLocation++,id);
Will Schroeder's avatar
Will Schroeder committed
173
174
}

Will Schroeder's avatar
Will Schroeder committed
175
176
177
178
179
// Description:
// Used in conjunction with InsertNextCell(int npts) and InsertCellPoint() to
// update the number of points defining the cell.
inline void vtkCellArray::UpdateCellCount(int npts) 
{
180
  this->Ia->SetValue(this->InsertLocation-npts-1, npts);
Will Schroeder's avatar
Will Schroeder committed
181
182
}

Will Schroeder's avatar
Will Schroeder committed
183
184
// Description:
// Insert a cell object.
Ken Martin's avatar
Ken Martin committed
185
inline int vtkCellArray::InsertNextCell(vtkCell *cell)
Will Schroeder's avatar
Will Schroeder committed
186
187
{
  int npts = cell->GetNumberOfPoints();
Charles Law's avatar
Charles Law committed
188
  int i = this->Ia->GetMaxId() + 1;
Will Schroeder's avatar
Will Schroeder committed
189
  int *ptr = this->Ia->WritePointer(i,npts+1);
Will Schroeder's avatar
Will Schroeder committed
190
191
192
  
  for ( *ptr++ = npts, i = 0; i < npts; i++) *ptr++ = cell->PointIds.GetId(i);

Will Schroeder's avatar
Will Schroeder committed
193
  this->NumberOfCells++;
194
  this->InsertLocation += npts + 1;
Ken Martin's avatar
Ken Martin committed
195
196

  return this->NumberOfCells;
Will Schroeder's avatar
Will Schroeder committed
197
198
199
}

// Description:
200
// Utility routines help manage memory of cell array. EstimateSize()
Will Schroeder's avatar
Will Schroeder committed
201
202
// returns a value used to initialize and allocate memory for array based
// on number of cells and maximum number of points making up cell.  If 
203
// every cell is the same size (in terms of number of points), then the 
Will Schroeder's avatar
Will Schroeder committed
204
// memory estimate is guaranteed exact. (If not exact, use Squeeze() to
205
// reclaim any extra memory.)
Ken Martin's avatar
Ken Martin committed
206
inline int vtkCellArray::EstimateSize(int numCells, int maxPtsPerCell) 
Will Schroeder's avatar
Will Schroeder committed
207
208
209
210
{
  return numCells*(1+maxPtsPerCell);
}

Will Schroeder's avatar
Will Schroeder committed
211
212
213
214
215
// Description:
// Reuse list. Reset to initial condition.
inline void vtkCellArray::Reset() 
{
  this->NumberOfCells = 0;
216
217
  this->InsertLocation = 0;
  this->TraversalLocation = 0;
Charles Law's avatar
Charles Law committed
218
  this->Ia->Reset();
Will Schroeder's avatar
Will Schroeder committed
219
220
}

Will Schroeder's avatar
Will Schroeder committed
221
222
// Description:
// Reclaim any extra memory.
Charles Law's avatar
Charles Law committed
223
inline void vtkCellArray::Squeeze() {this->Ia->Squeeze();}
Will Schroeder's avatar
Will Schroeder committed
224
225

// Description:
Will Schroeder's avatar
Will Schroeder committed
226
// A cell traversal methods that is more efficient than vtkDataSet traversal
Will Schroeder's avatar
Will Schroeder committed
227
// methods.  InitTraversal() initializes the traversal of the list of cells.
228
inline void vtkCellArray::InitTraversal() {this->TraversalLocation=0;}
Will Schroeder's avatar
Will Schroeder committed
229
230

// Description:
Will Schroeder's avatar
Will Schroeder committed
231
// A cell traversal methods that is more efficient than vtkDataSet traversal
Will Schroeder's avatar
Will Schroeder committed
232
233
// methods.  GetNextCell() gets the next cell in the list. If end of list
// is encountered, 0 is returned.
Ken Martin's avatar
Ken Martin committed
234
inline int vtkCellArray::GetNextCell(int& npts, int* &pts)
Will Schroeder's avatar
Will Schroeder committed
235
{
236
237
  if ( this->Ia->GetMaxId() >= 0 && 
  this->TraversalLocation <= this->Ia->GetMaxId() ) 
Will Schroeder's avatar
Will Schroeder committed
238
    {
239
240
241
    npts = this->Ia->GetValue(this->TraversalLocation++);
    pts = this->Ia->GetPointer(this->TraversalLocation);
    this->TraversalLocation += npts;
Will Schroeder's avatar
Will Schroeder committed
242
243
244
245
246
247
248
249
250
    return 1;
    }
  else
    {
    return 0;
    }
}

// Description:
Will Schroeder's avatar
Will Schroeder committed
251
// Get the size of the allocated connectivity array.
Charles Law's avatar
Charles Law committed
252
inline int vtkCellArray::GetSize() {return Ia->GetSize();};
Will Schroeder's avatar
Will Schroeder committed
253

Will Schroeder's avatar
Will Schroeder committed
254
255
256
257
258
259
// Description:
// Get the total number of entries (i.e., data values) in the connectivity 
// array. This may be much less than the allocated size (i.e., return value 
// from GetSize().)
inline int vtkCellArray::GetNumberOfConnectivityEntries() 
{
Charles Law's avatar
Charles Law committed
260
  return Ia->GetMaxId()+1;
Will Schroeder's avatar
Will Schroeder committed
261
262
}

Will Schroeder's avatar
Will Schroeder committed
263
264
265
// Description:
// Internal method used to retrieve a cell given an offset into
// the internal array.
Ken Martin's avatar
Ken Martin committed
266
inline void vtkCellArray::GetCell(int loc, int &npts, int* &pts)
Will Schroeder's avatar
Will Schroeder committed
267
{
Will Schroeder's avatar
Will Schroeder committed
268
  npts=this->Ia->GetValue(loc++); pts=this->Ia->GetPointer(loc);
Will Schroeder's avatar
Will Schroeder committed
269
270
271
}

// Description:
272
273
274
275
276
277
278
279
280
281
282
// Computes the current insertion location within the internal array. 
// Used in conjunction with GetCell(int loc,...).
inline int vtkCellArray::GetInsertLocation(int npts) {
  return (this->InsertLocation - npts - 1);
}

// Description:
// Computes the current traversal location within the internal array. Used 
// in conjunction with GetCell(int loc,...).
inline int vtkCellArray::GetTraversalLocation(int npts) {
  return (this->TraversalLocation - npts - 1);
Will Schroeder's avatar
Will Schroeder committed
283
284
285
286
287
}

// Description:
// Special method inverts ordering of current cell. Must be called carefully or
// the cell topology may be corrupted.
Ken Martin's avatar
Ken Martin committed
288
inline void vtkCellArray::ReverseCell(int loc)
Will Schroeder's avatar
Will Schroeder committed
289
290
{
  int i, tmp;
Charles Law's avatar
Charles Law committed
291
  int npts=this->Ia->GetValue(loc);
Will Schroeder's avatar
Will Schroeder committed
292
  int *pts=this->Ia->GetPointer(loc+1);
Will Schroeder's avatar
Will Schroeder committed
293
294
295
296
297
298
299
300
301
302
  for (i=0; i < (npts/2); i++) 
    {
    tmp = pts[i];
    pts[i] = pts[npts-i-1];
    pts[npts-i-1] = tmp;
    }
}

// Description:
// Replace the point ids of the cell with a different list of point ids.
Ken Martin's avatar
Ken Martin committed
303
inline void vtkCellArray::ReplaceCell(int loc, int npts, int *pts)
Will Schroeder's avatar
Will Schroeder committed
304
{
Will Schroeder's avatar
Will Schroeder committed
305
  int *oldPts=this->Ia->GetPointer(loc+1);
306
  for (int i=0; i < npts; i++)  oldPts[i] = pts[i];
Will Schroeder's avatar
Will Schroeder committed
307
308
}

309
310
// Description:
// Get pointer to array of cell data.
Will Schroeder's avatar
Will Schroeder committed
311
inline int *vtkCellArray::GetPointer()
312
{
Will Schroeder's avatar
Will Schroeder committed
313
  return this->Ia->GetPointer(0);
314
315
316
317
318
319
}

// Description:
// Get pointer to data array for purpose of direct writes of data. Size is the
// total storage consumed by the cell array. ncells is the number of cells
// represented in the array.
Will Schroeder's avatar
Will Schroeder committed
320
inline int *vtkCellArray::WritePointer(const int ncells, const int size)
321
322
{
  this->NumberOfCells = ncells;
323
324
  this->InsertLocation = 0;
  this->TraversalLocation = 0;
Will Schroeder's avatar
Will Schroeder committed
325
  return this->Ia->WritePointer(0,size);
326
327
}

Ken Martin's avatar
Ken Martin committed
328
#endif