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
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.
Jim Miller's avatar
Jim Miller 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.
126
// Return the cell id of the cell.
Ken Martin's avatar
Ken Martin committed
127
inline int vtkCellArray::InsertNextCell(int npts, int* pts)
Will Schroeder's avatar
Will Schroeder committed
128
{
Charles Law's avatar
Charles Law committed
129
  int i = this->Ia->GetMaxId() + 1;
Will Schroeder's avatar
Will Schroeder committed
130
  int *ptr = this->Ia->WritePointer(i,npts+1);
Will Schroeder's avatar
Will Schroeder committed
131
132
133
  
  for ( *ptr++ = npts, i = 0; i < npts; i++) *ptr++ = *pts++;

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

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

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

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

153
  return this->NumberOfCells - 1;
Will Schroeder's avatar
Will Schroeder committed
154
155
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Ken Martin's avatar
Ken Martin committed
329
#endif