vtkCellArray.h 9.76 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

10
Copyright (c) 1993-1996 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


=========================================================================*/
Ken Martin's avatar
Ken Martin committed
41
// .NAME vtkCellArray - object represents 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 
Ken Martin's avatar
Ken Martin committed
52
// using the vtkCellList and vtkLinkList objects to extend the definition of 
53
// the data structure.
Will Schroeder's avatar
Will Schroeder committed
54
55
// .SECTION See Also
// vtkCellList vtkLinkList
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

Ken Martin's avatar
Ken Martin committed
63
class VTK_EXPORT vtkCellArray : public vtkRefCount
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();};
Ken Martin's avatar
Ken Martin committed
74
  char *GetClassName() {return "vtkCellArray";};
Will Schroeder's avatar
Will Schroeder committed
75

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

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

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

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

Will Schroeder's avatar
Will Schroeder committed
88
89
90
91
92
93
94
95
  // 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);

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

101
102
  int GetMaxCellSize();

103
104
105
106
  // miscellaneous pointer type operations (for fast read/write operations)
  int *GetPtr();
  int *WritePtr(const int ncells, const int size);

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

Will Schroeder's avatar
Will Schroeder committed
111
protected:
112
113
  int NumberOfCells;
  int Location;
Charles Law's avatar
Charles Law committed
114
  vtkIntArray *Ia;
Ken Martin's avatar
Ken Martin committed
115
116
};

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

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

Will Schroeder's avatar
Will Schroeder committed
130
131
  this->NumberOfCells++;
  this->Location += npts + 1;
Ken Martin's avatar
Ken Martin committed
132
133

  return this->NumberOfCells;
Will Schroeder's avatar
Will Schroeder committed
134
135
}

Will Schroeder's avatar
Will Schroeder committed
136
137
138
139
140
// 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
141
142
  int i = this->Ia->GetMaxId() + 1;
  int *ptr = this->Ia->WritePtr(i,npts+1);
Will Schroeder's avatar
Will Schroeder committed
143
144
145
  
  for ( *ptr++ = npts, i = 0; i < npts; i++) *ptr++ = pts.GetId(i);

Will Schroeder's avatar
Will Schroeder committed
146
147
148
149
150
151
  this->NumberOfCells++;
  this->Location += npts + 1;

  return this->NumberOfCells;
}

Will Schroeder's avatar
Will Schroeder committed
152
153
// Description:
// Create cells by specifying count, and then adding points one at a time using
Will Schroeder's avatar
Will Schroeder committed
154
155
// 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
156
inline int vtkCellArray::InsertNextCell(int npts)
Will Schroeder's avatar
Will Schroeder committed
157
{
Charles Law's avatar
Charles Law committed
158
  this->Location = this->Ia->InsertNextValue(npts) + 1;
Will Schroeder's avatar
Will Schroeder committed
159
  this->NumberOfCells++;
Ken Martin's avatar
Ken Martin committed
160
161

  return this->NumberOfCells;
Will Schroeder's avatar
Will Schroeder committed
162
163
164
165
166
}

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

Will Schroeder's avatar
Will Schroeder committed
172
173
174
175
176
// 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) 
{
Charles Law's avatar
Charles Law committed
177
  this->Ia->SetValue(this->Location-npts-1, npts);
Will Schroeder's avatar
Will Schroeder committed
178
179
}

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

Will Schroeder's avatar
Will Schroeder committed
190
191
  this->NumberOfCells++;
  this->Location += npts + 1;
Ken Martin's avatar
Ken Martin committed
192
193

  return this->NumberOfCells;
Will Schroeder's avatar
Will Schroeder committed
194
195
196
}

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

Will Schroeder's avatar
Will Schroeder committed
208
209
210
211
212
213
// Description:
// Reuse list. Reset to initial condition.
inline void vtkCellArray::Reset() 
{
  this->NumberOfCells = 0;
  this->Location = 0;
Charles Law's avatar
Charles Law committed
214
  this->Ia->Reset();
Will Schroeder's avatar
Will Schroeder committed
215
216
}

Will Schroeder's avatar
Will Schroeder committed
217
218
// Description:
// Reclaim any extra memory.
Charles Law's avatar
Charles Law committed
219
inline void vtkCellArray::Squeeze() {this->Ia->Squeeze();}
Will Schroeder's avatar
Will Schroeder committed
220
221

// Description:
Will Schroeder's avatar
Will Schroeder committed
222
// A cell traversal methods that is more efficient than vtkDataSet traversal
Will Schroeder's avatar
Will Schroeder committed
223
// methods.  InitTraversal() initializes the traversal of the list of cells.
Ken Martin's avatar
Ken Martin committed
224
inline void vtkCellArray::InitTraversal() {this->Location=0;}
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
229
// 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
230
inline int vtkCellArray::GetNextCell(int& npts, int* &pts)
Will Schroeder's avatar
Will Schroeder committed
231
{
Charles Law's avatar
Charles Law committed
232
  if ( this->Ia->GetMaxId() >= 0 && this->Location <= this->Ia->GetMaxId() ) 
Will Schroeder's avatar
Will Schroeder committed
233
    {
Charles Law's avatar
Charles Law committed
234
235
    npts = this->Ia->GetValue(this->Location++);
    pts = this->Ia->GetPtr(this->Location);
Will Schroeder's avatar
Will Schroeder committed
236
237
238
239
240
241
242
243
244
245
    this->Location += npts;
    return 1;
    }
  else
    {
    return 0;
    }
}

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

Will Schroeder's avatar
Will Schroeder committed
249
250
251
252
253
254
// 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
255
  return Ia->GetMaxId()+1;
Will Schroeder's avatar
Will Schroeder committed
256
257
}

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

// Description:
// Computes the current location within the internal array. Used in conjunction
// with GetCell(int loc,...).
Ken Martin's avatar
Ken Martin committed
269
inline int vtkCellArray::GetLocation(int npts) {
Will Schroeder's avatar
Will Schroeder committed
270
271
272
273
274
275
  return (this->Location - npts - 1);
}

// 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
276
inline void vtkCellArray::ReverseCell(int loc)
Will Schroeder's avatar
Will Schroeder committed
277
278
{
  int i, tmp;
Charles Law's avatar
Charles Law committed
279
280
  int npts=this->Ia->GetValue(loc);
  int *pts=this->Ia->GetPtr(loc+1);
Will Schroeder's avatar
Will Schroeder committed
281
282
283
284
285
286
287
288
289
290
  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
291
inline void vtkCellArray::ReplaceCell(int loc, int npts, int *pts)
Will Schroeder's avatar
Will Schroeder committed
292
{
Charles Law's avatar
Charles Law committed
293
  int *oldPts=this->Ia->GetPtr(loc+1);
294
  for (int i=0; i < npts; i++)  oldPts[i] = pts[i];
Will Schroeder's avatar
Will Schroeder committed
295
296
}

297
298
// Description:
// Get pointer to array of cell data.
Ken Martin's avatar
Ken Martin committed
299
inline int *vtkCellArray::GetPtr()
300
{
Charles Law's avatar
Charles Law committed
301
  return this->Ia->GetPtr(0);
302
303
304
305
306
307
}

// 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.
Ken Martin's avatar
Ken Martin committed
308
inline int *vtkCellArray::WritePtr(const int ncells, const int size)
309
310
311
{
  this->NumberOfCells = ncells;
  this->Location = 0;
Charles Law's avatar
Charles Law committed
312
  return this->Ia->WritePtr(0,size);
313
314
}

Ken Martin's avatar
Ken Martin committed
315
#endif