1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkBridgeCellIteratorOne.cxx
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 // .NAME vtkBridgeCellIteratorOne - Iterate over cells of a dataset.
16 // .SECTION See Also
17 // vtkBridgeCellIterator, vtkBridgeDataSet, vtkBridgeCellIteratorStrategy
18
19 #include "vtkBridgeCellIteratorOne.h"
20
21 #include <cassert>
22
23 #include "vtkObjectFactory.h"
24 #include "vtkBridgeCell.h"
25 #include "vtkBridgeDataSet.h"
26 #include "vtkDataSet.h"
27
28 #include "vtkTriangle.h"
29 #include "vtkPolygon.h"
30 #include "vtkLine.h"
31 #include "vtkPolyLine.h"
32 #include "vtkVertex.h"
33 #include "vtkPolyVertex.h"
34
35 vtkStandardNewMacro(vtkBridgeCellIteratorOne);
36
37 //-----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)38 void vtkBridgeCellIteratorOne::PrintSelf(ostream& os, vtkIndent indent)
39 {
40 this->Superclass::PrintSelf(os,indent);
41 }
42
43 //-----------------------------------------------------------------------------
vtkBridgeCellIteratorOne()44 vtkBridgeCellIteratorOne::vtkBridgeCellIteratorOne()
45 {
46 this->DataSet=0;
47 this->InternalCell=0;
48 this->Cell=0;
49 this->Id=0;
50 this->cIsAtEnd=0;
51 // this->DebugOn();
52 }
53
54 //-----------------------------------------------------------------------------
~vtkBridgeCellIteratorOne()55 vtkBridgeCellIteratorOne::~vtkBridgeCellIteratorOne()
56 {
57 if((this->Cell!=0)&&((this->DataSet!=0)||(this->InternalCell!=0)))
58 {
59 // dataset mode or points mode
60 this->Cell->Delete();
61 this->Cell=0;
62 }
63 if(this->DataSet!=0)
64 {
65 this->DataSet->Delete();
66 this->DataSet=0;
67 }
68
69 if(this->InternalCell!=0)
70 {
71 this->InternalCell->Delete();
72 this->InternalCell=0;
73 }
74 }
75
76 //-----------------------------------------------------------------------------
77 // Description:
78 // Move iterator to first position if any (loop initialization).
Begin()79 void vtkBridgeCellIteratorOne::Begin()
80 {
81 this->cIsAtEnd=0;
82 }
83
84 //-----------------------------------------------------------------------------
85 // Description:
86 // Is there no cell at iterator position? (exit condition).
IsAtEnd()87 int vtkBridgeCellIteratorOne::IsAtEnd()
88 {
89 return this->cIsAtEnd;
90 }
91
92 //-----------------------------------------------------------------------------
93 // Description:
94 // Cell at current position
95 // \pre not_at_end: !IsAtEnd()
96 // \pre c_exists: c!=0
97 // THREAD SAFE
GetCell(vtkGenericAdaptorCell * c)98 void vtkBridgeCellIteratorOne::GetCell(vtkGenericAdaptorCell *c)
99 {
100 assert("pre: not_at_end" && !this->IsAtEnd());
101 assert("pre: c_exists" && c!=0);
102
103 vtkBridgeCell *c2=static_cast<vtkBridgeCell *>(c);
104 if(this->DataSet!=0)
105 {
106 c2->Init(this->DataSet,this->Id);
107 }
108 else
109 {
110 if(this->InternalCell!=0)
111 {
112 c2->InitWithCell(this->InternalCell,this->Id);
113 }
114 else
115 {
116 c2->DeepCopy(this->Cell);
117 }
118 }
119 }
120
121 //-----------------------------------------------------------------------------
122 // Description:
123 // Cell at current position.
124 // NOT THREAD SAFE
125 // \pre not_at_end: !IsAtEnd()
126 // \post result_exits: result!=0
GetCell()127 vtkGenericAdaptorCell *vtkBridgeCellIteratorOne::GetCell()
128 {
129 assert("pre: not_at_end" && !this->IsAtEnd());
130
131 vtkGenericAdaptorCell *result=this->Cell;
132
133 assert("post: result_exits" && result!=0);
134 return result;
135 }
136
137 //-----------------------------------------------------------------------------
138 // Description:
139 // Move iterator to next position. (loop progression).
140 // \pre not_at_end: !IsAtEnd()
Next()141 void vtkBridgeCellIteratorOne::Next()
142 {
143 assert("pre: not_off" && !this->IsAtEnd());
144
145 this->cIsAtEnd=1;
146 }
147
148 //-----------------------------------------------------------------------------
149 // Description:
150 // Used internally by vtkBridgeDataSet.
151 // Iterate on one cell `id' of `ds'.
152 // \pre ds_exists: ds!=0
153 // \pre valid_id: (id>=0)&&(id<=ds->GetNumberOfCells())
InitWithOneCell(vtkBridgeDataSet * ds,vtkIdType cellid)154 void vtkBridgeCellIteratorOne::InitWithOneCell(vtkBridgeDataSet *ds,
155 vtkIdType cellid)
156 {
157 assert("pre: ds_exists" && ds!=0);
158 assert("pre: valid_id" && ((cellid>=0)&&(cellid<=ds->GetNumberOfCells())));
159
160 if((this->Cell!=0)&&(this->DataSet==0)&&(this->InternalCell==0))
161 {
162 // previous mode was InitWithOneCell(vtkBridgeCell *c)
163 // this->Cell->Delete();
164 this->Cell=0;
165 }
166
167 if(this->Cell==0)
168 {
169 // first init or previous mode was InitWithOneCell(vtkBridgeCell *c)
170 this->Cell=vtkBridgeCell::New();
171 }
172
173 vtkSetObjectBodyMacro(InternalCell,vtkCell,0);
174 vtkSetObjectBodyMacro(DataSet,vtkBridgeDataSet,ds);
175 this->Id=cellid;
176 this->cIsAtEnd=1;
177 this->Cell->Init(this->DataSet,this->Id);
178 }
179 //-----------------------------------------------------------------------------
180 // Description:
181 // Used internally by vtkBridgeCell.
182 // Iterate on one cell `c'.
183 // \pre c_exists: c!=0
InitWithOneCell(vtkBridgeCell * c)184 void vtkBridgeCellIteratorOne::InitWithOneCell(vtkBridgeCell *c)
185 {
186 assert("pre: c_exists" && c!=0);
187
188 if((this->Cell!=0)&&((this->DataSet!=0)||(this->InternalCell!=0)))
189 {
190 // dataset mode or points mode
191 this->Cell->Delete();
192 }
193 vtkSetObjectBodyMacro(InternalCell,vtkCell,0);
194 vtkSetObjectBodyMacro(DataSet,vtkBridgeDataSet,0);
195
196 this->Cell=c; // no register to prevent reference cycle with vtkBridgeCell
197 this->Id=c->GetId();
198 this->cIsAtEnd=1;
199 }
200
201 //-----------------------------------------------------------------------------
202 // Description:
203 // Used internally by vtkBridgeCell.
204 // Iterate on a boundary cell (defined by its points `pts' with coordinates
205 // `coords', dimension `dim' and unique id `cellid') of a cell.
206 // \pre coords_exist: coords!=0
207 // \pre pts_exist: pts!=0
208 // \pre valid_dim: dim>=0 && dim<=2
209 // \pre valid_points: pts->GetNumberOfIds()>dim
InitWithPoints(vtkPoints * coords,vtkIdList * pts,int dim,vtkIdType cellid)210 void vtkBridgeCellIteratorOne::InitWithPoints(vtkPoints *coords,
211 vtkIdList *pts,
212 int dim,
213 vtkIdType cellid)
214 {
215 assert("pre: coords_exist" && coords!=0);
216 assert("pre: pts_exist" && pts!=0);
217 assert("pre: valid_dim" && dim>=0 && dim<=2);
218 assert("pre: valid_points" && pts->GetNumberOfIds()>dim);
219
220
221 if((this->DataSet==0)&&(this->InternalCell==0))
222 {
223 // previous mode was InitWithOneCell(vtkBridgeCell *c)
224 // this->Cell->Delete();
225 this->Cell=0;
226 }
227
228 if(this->Cell==0)
229 {
230 // first init or previous mode was InitWithOneCell(vtkBridgeCell *c)
231 this->Cell=vtkBridgeCell::New();
232 }
233
234 vtkCell *cell=0;
235
236 switch(dim)
237 {
238 case 2:
239 if (pts->GetNumberOfIds()==3)
240 {
241 cell=vtkTriangle::New();
242 }
243 else
244 {
245 cell=vtkPolygon::New();
246 }
247 break;
248 case 1:
249 // line or polyline
250 if(pts->GetNumberOfIds()==2)
251 {
252 cell=vtkLine::New();
253 }
254 else
255 {
256 cell=vtkPolyLine::New();
257 }
258 break;
259 case 0:
260 // vertex polyvertex
261 if(pts->GetNumberOfIds()==1)
262 {
263 cell=vtkVertex::New();
264 }
265 else
266 {
267 cell=vtkPolyVertex::New();
268 }
269 break;
270 }
271 cell->Points=coords;
272 cell->PointIds=pts;
273 vtkSetObjectBodyMacro(InternalCell,vtkCell,cell);
274 vtkSetObjectBodyMacro(DataSet,vtkBridgeDataSet,0);
275 this->Id=cellid;
276 this->cIsAtEnd=1;
277 this->Cell->InitWithCell(this->InternalCell,this->Id);
278 }
279