vtkCellArray.h 10.6 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
  // methods to insert cells. Can be used in combination.
  int InsertNextCell(vtkCell *cell);
  int InsertNextCell(int npts, int* pts);
  int InsertNextCell(vtkIdList &pts);
Bill Lorensen's avatar
Bill Lorensen committed
93
94
95

  //this and next two methods work together
  int InsertNextCell(int npts);
Will Schroeder's avatar
Will Schroeder committed
96
97
98
  void InsertCellPoint(int id);
  void UpdateCellCount(int npts);

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

105
106
  int GetMaxCellSize();

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

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

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

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

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

Will Schroeder's avatar
Will Schroeder committed
136
  this->NumberOfCells++;
137
  this->InsertLocation += npts + 1;
Ken Martin's avatar
Ken Martin committed
138

139
  return this->NumberOfCells - 1;
Will Schroeder's avatar
Will Schroeder committed
140
141
}

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

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

155
  return this->NumberOfCells - 1;
Will Schroeder's avatar
Will Schroeder committed
156
157
}

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

167
  return this->NumberOfCells - 1;
Will Schroeder's avatar
Will Schroeder committed
168
169
170
171
172
}

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

Will Schroeder's avatar
Will Schroeder committed
178
179
180
181
182
// 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) 
{
183
  this->Ia->SetValue(this->InsertLocation-npts-1, npts);
Will Schroeder's avatar
Will Schroeder committed
184
185
}

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

Will Schroeder's avatar
Will Schroeder committed
196
  this->NumberOfCells++;
197
  this->InsertLocation += npts + 1;
Ken Martin's avatar
Ken Martin committed
198

199
  return this->NumberOfCells - 1;
Will Schroeder's avatar
Will Schroeder committed
200
201
202
}

// Description:
203
// Utility routines help manage memory of cell array. EstimateSize()
Will Schroeder's avatar
Will Schroeder committed
204
205
// 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 
206
// every cell is the same size (in terms of number of points), then the 
Will Schroeder's avatar
Will Schroeder committed
207
// memory estimate is guaranteed exact. (If not exact, use Squeeze() to
208
// reclaim any extra memory.)
Ken Martin's avatar
Ken Martin committed
209
inline int vtkCellArray::EstimateSize(int numCells, int maxPtsPerCell) 
Will Schroeder's avatar
Will Schroeder committed
210
211
212
213
{
  return numCells*(1+maxPtsPerCell);
}

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

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

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

// Description:
Will Schroeder's avatar
Will Schroeder committed
234
// A cell traversal methods that is more efficient than vtkDataSet traversal
Will Schroeder's avatar
Will Schroeder committed
235
236
// 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
237
inline int vtkCellArray::GetNextCell(int& npts, int* &pts)
Will Schroeder's avatar
Will Schroeder committed
238
{
239
240
  if ( this->Ia->GetMaxId() >= 0 && 
  this->TraversalLocation <= this->Ia->GetMaxId() ) 
Will Schroeder's avatar
Will Schroeder committed
241
    {
242
243
244
    npts = this->Ia->GetValue(this->TraversalLocation++);
    pts = this->Ia->GetPointer(this->TraversalLocation);
    this->TraversalLocation += npts;
Will Schroeder's avatar
Will Schroeder committed
245
246
247
248
249
250
251
252
253
    return 1;
    }
  else
    {
    return 0;
    }
}

// Description:
Will Schroeder's avatar
Will Schroeder committed
254
// Get the size of the allocated connectivity array.
Jim Miller's avatar
Jim Miller committed
255
inline int vtkCellArray::GetSize() {return Ia->GetSize();}
Will Schroeder's avatar
Will Schroeder committed
256

Will Schroeder's avatar
Will Schroeder committed
257
258
259
260
261
262
// 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
263
  return Ia->GetMaxId()+1;
Will Schroeder's avatar
Will Schroeder committed
264
265
}

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

// Description:
275
276
277
278
279
280
281
282
283
284
285
// 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
286
287
288
289
290
}

// 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
291
inline void vtkCellArray::ReverseCell(int loc)
Will Schroeder's avatar
Will Schroeder committed
292
293
{
  int i, tmp;
Charles Law's avatar
Charles Law committed
294
  int npts=this->Ia->GetValue(loc);
Will Schroeder's avatar
Will Schroeder committed
295
  int *pts=this->Ia->GetPointer(loc+1);
Will Schroeder's avatar
Will Schroeder committed
296
297
298
299
300
301
302
303
304
305
  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
306
inline void vtkCellArray::ReplaceCell(int loc, int npts, int *pts)
Will Schroeder's avatar
Will Schroeder committed
307
{
Will Schroeder's avatar
Will Schroeder committed
308
  int *oldPts=this->Ia->GetPointer(loc+1);
309
  for (int i=0; i < npts; i++)  oldPts[i] = pts[i];
Will Schroeder's avatar
Will Schroeder committed
310
311
}

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

// 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
323
inline int *vtkCellArray::WritePointer(const int ncells, const int size)
324
325
{
  this->NumberOfCells = ncells;
326
327
  this->InsertLocation = 0;
  this->TraversalLocation = 0;
Will Schroeder's avatar
Will Schroeder committed
328
  return this->Ia->WritePointer(0,size);
329
330
}

Ken Martin's avatar
Ken Martin committed
331
#endif