1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkBridgeCellIteratorOnDataSet.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 vtkBridgeCellIteratorOnDataSet - Iterate over cells of a dataset.
16 // .SECTION See Also
17 // vtkBridgeCellIterator, vtkBridgeDataSet, vtkBridgeCellIteratorStrategy
18 
19 #include "vtkBridgeCellIteratorOnDataSet.h"
20 
21 #include <cassert>
22 
23 #include "vtkObjectFactory.h"
24 #include "vtkBridgeCell.h"
25 #include "vtkBridgeDataSet.h"
26 #include "vtkDataSet.h"
27 #include "vtkCell.h"
28 
29 vtkStandardNewMacro(vtkBridgeCellIteratorOnDataSet);
30 
31 //-----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)32 void vtkBridgeCellIteratorOnDataSet::PrintSelf(ostream& os, vtkIndent indent)
33 {
34   this->Superclass::PrintSelf(os,indent);
35 }
36 
37 //-----------------------------------------------------------------------------
vtkBridgeCellIteratorOnDataSet()38 vtkBridgeCellIteratorOnDataSet::vtkBridgeCellIteratorOnDataSet()
39 {
40   this->DataSet=0;
41   this->Cell=vtkBridgeCell::New();
42   this->Id=0;
43   this->Size=0;
44 //  this->DebugOn();
45 }
46 
47 //-----------------------------------------------------------------------------
~vtkBridgeCellIteratorOnDataSet()48 vtkBridgeCellIteratorOnDataSet::~vtkBridgeCellIteratorOnDataSet()
49 {
50   if(this->DataSet!=0)
51     {
52     this->DataSet->Delete();
53     this->DataSet=0;
54     }
55   this->Cell->Delete();
56   this->Cell=0;
57 }
58 
59 //-----------------------------------------------------------------------------
60 // Description:
61 // Move iterator to first position if any (loop initialization).
Begin()62 void vtkBridgeCellIteratorOnDataSet::Begin()
63 {
64   this->Id=-1;
65   this->Next(); // skip cells of other dimensions
66 }
67 
68 //-----------------------------------------------------------------------------
69 // Description:
70 // Is there no cell at iterator position? (exit condition).
IsAtEnd()71 int vtkBridgeCellIteratorOnDataSet::IsAtEnd()
72 {
73   return (this->Id>=this->Size);
74 }
75 
76 //-----------------------------------------------------------------------------
77 // Description:
78 // Cell at current position
79 // \pre not_at_end: !IsAtEnd()
80 // \pre c_exists: c!=0
81 // THREAD SAFE
GetCell(vtkGenericAdaptorCell * c)82 void vtkBridgeCellIteratorOnDataSet::GetCell(vtkGenericAdaptorCell *c)
83 {
84   assert("pre: not_at_end" && !IsAtEnd());
85   assert("pre: c_exists" && c!=0);
86 
87   vtkBridgeCell *c2=static_cast<vtkBridgeCell *>(c);
88   c2->Init(this->DataSet,this->Id);
89 }
90 
91 //-----------------------------------------------------------------------------
92 // Description:
93 // Cell at current position.
94 // NOT THREAD SAFE
95 // \pre not_at_end: !IsAtEnd()
96 // \post result_exits: result!=0
GetCell()97 vtkGenericAdaptorCell *vtkBridgeCellIteratorOnDataSet::GetCell()
98 {
99   assert("pre: not_at_end" && !IsAtEnd());
100 
101   this->Cell->Init(this->DataSet,this->Id);
102   vtkGenericAdaptorCell *result=this->Cell;
103 
104   assert("post: result_exits" && result!=0);
105   return result;
106 }
107 
108 //-----------------------------------------------------------------------------
109 // Description:
110 // Move iterator to next position. (loop progression).
111 // \pre not_at_end: !IsAtEnd()
Next()112 void vtkBridgeCellIteratorOnDataSet::Next()
113 {
114   assert("pre: not_off" && !IsAtEnd());
115 
116   vtkIdType size=this->Size;
117   vtkCell *c;
118   int found;
119 
120   this->Id++;
121 
122   if(this->Dim>=0) // skip cells of other dimensions than this->Dim
123     {
124     found=0;
125     while( (this->Id<size) && (!found) )
126       {
127       c=this->DataSet->Implementation->GetCell(this->Id);
128       found=c->GetCellDimension()==this->Dim;
129       this->Id++;
130       }
131     if(found)
132       {
133       this->Id--;
134       }
135     }
136 }
137 
138 //-----------------------------------------------------------------------------
139 // Description:
140 // Used internally by vtkBridgeDataSet.
141 // Iterate over cells of `ds' of some dimension `dim'.
142 // \pre ds_exists: ds!=0
143 // \pre valid_dim_range: (dim>=-1) && (dim<=3)
InitWithDataSet(vtkBridgeDataSet * ds,int dim)144 void vtkBridgeCellIteratorOnDataSet::InitWithDataSet(vtkBridgeDataSet *ds,
145                                                      int dim)
146 {
147   assert("pre: ds_exists" && ds!=0);
148   assert("pre: valid_dim_range" && (dim>=-1) && (dim<=3));
149 
150   this->Dim=dim;
151   vtkSetObjectBodyMacro(DataSet,vtkBridgeDataSet,ds);
152   this->Size=ds->GetNumberOfCells();
153   this->Id=this->Size; // at end
154 }
155