1 /*=========================================================================
2 
3 Program:   Visualization Toolkit
4 Module:    vtkHyperTreeGridNonOrientedSuperCursorLight.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 Nonice for more information.
13 
14 =========================================================================*/
15 #include "vtkHyperTreeGridNonOrientedSuperCursorLight.h"
16 #include "vtkHyperTree.h"
17 #include "vtkHyperTreeGrid.h"
18 #include "vtkHyperTreeGridNonOrientedGeometryCursor.h"
19 
20 #include "vtkBitArray.h"
21 #include "vtkObjectFactory.h"
22 #include "vtkSmartPointer.h"
23 
24 #include "vtkHyperTreeGridTools.h"
25 
26 #include <cassert>
27 #include <climits>
28 
29 //------------------------------------------------------------------------------
Clone()30 vtkHyperTreeGridNonOrientedSuperCursorLight* vtkHyperTreeGridNonOrientedSuperCursorLight::Clone()
31 {
32   vtkHyperTreeGridNonOrientedSuperCursorLight* clone = this->NewInstance();
33   assert("post: clone_exists" && clone != nullptr);
34   // Copy
35   clone->Grid = this->Grid;
36   clone->CentralCursor->Initialize(this->CentralCursor.Get());
37   clone->CurrentFirstNonValidEntryByLevel = this->CurrentFirstNonValidEntryByLevel;
38   {
39     clone->FirstNonValidEntryByLevel.resize(this->FirstNonValidEntryByLevel.size());
40     std::vector<unsigned int>::iterator in = this->FirstNonValidEntryByLevel.begin();
41     std::vector<unsigned int>::iterator out = clone->FirstNonValidEntryByLevel.begin();
42     for (; in != this->FirstNonValidEntryByLevel.end(); ++in, ++out)
43     {
44       (*out) = (*in);
45     }
46   }
47   {
48     clone->Entries.resize(this->Entries.size());
49     std::vector<vtkHyperTreeGridLevelEntry>::iterator in = this->Entries.begin();
50     std::vector<vtkHyperTreeGridLevelEntry>::iterator out = clone->Entries.begin();
51     for (; in != this->Entries.end(); ++in, ++out)
52     {
53       (*out).Copy(&(*in));
54     }
55   }
56   clone->FirstCurrentNeighboorReferenceEntry = this->FirstCurrentNeighboorReferenceEntry;
57   {
58     clone->ReferenceEntries.resize(this->ReferenceEntries.size());
59     std::vector<unsigned int>::iterator in = this->ReferenceEntries.begin();
60     std::vector<unsigned int>::iterator out = clone->ReferenceEntries.begin();
61     for (; in != this->ReferenceEntries.end(); ++in, ++out)
62     {
63       (*out) = (*in);
64     }
65   }
66   clone->IndiceCentralCursor = this->IndiceCentralCursor;
67   clone->NumberOfCursors = this->NumberOfCursors;
68   clone->ChildCursorToParentCursorTable = this->ChildCursorToParentCursorTable;
69   clone->ChildCursorToChildTable = this->ChildCursorToChildTable;
70   return clone;
71 }
72 
73 //------------------------------------------------------------------------------
GetGrid()74 vtkHyperTreeGrid* vtkHyperTreeGridNonOrientedSuperCursorLight::GetGrid()
75 {
76   return this->Grid;
77 }
78 
79 //------------------------------------------------------------------------------
HasTree()80 bool vtkHyperTreeGridNonOrientedSuperCursorLight::HasTree()
81 {
82   return this->CentralCursor->HasTree();
83 }
84 
85 //------------------------------------------------------------------------------
HasTree(unsigned int icursor)86 bool vtkHyperTreeGridNonOrientedSuperCursorLight::HasTree(unsigned int icursor)
87 {
88   if (icursor == this->IndiceCentralCursor)
89   {
90     return this->CentralCursor->HasTree();
91   }
92   return vtk::hypertreegrid::HasTree(this->Entries[this->GetIndiceEntry(icursor)]);
93 }
94 
95 //------------------------------------------------------------------------------
GetTree()96 vtkHyperTree* vtkHyperTreeGridNonOrientedSuperCursorLight::GetTree()
97 {
98   return this->CentralCursor->GetTree();
99 }
100 
101 //------------------------------------------------------------------------------
GetTree(unsigned int icursor)102 vtkHyperTree* vtkHyperTreeGridNonOrientedSuperCursorLight::GetTree(unsigned int icursor)
103 {
104   if (icursor == this->IndiceCentralCursor)
105   {
106     return this->CentralCursor->GetTree();
107   }
108   return this->Entries[this->GetIndiceEntry(icursor)].GetTree();
109 }
110 
111 //------------------------------------------------------------------------------
GetVertexId()112 vtkIdType vtkHyperTreeGridNonOrientedSuperCursorLight::GetVertexId()
113 {
114   return this->CentralCursor->GetVertexId();
115 }
116 
117 //------------------------------------------------------------------------------
GetVertexId(unsigned int icursor)118 vtkIdType vtkHyperTreeGridNonOrientedSuperCursorLight::GetVertexId(unsigned int icursor)
119 {
120   if (icursor == this->IndiceCentralCursor)
121   {
122     return this->CentralCursor->GetVertexId();
123   }
124   return this->Entries[this->GetIndiceEntry(icursor)].GetVertexId();
125 }
126 
127 //------------------------------------------------------------------------------
GetGlobalNodeIndex()128 vtkIdType vtkHyperTreeGridNonOrientedSuperCursorLight::GetGlobalNodeIndex()
129 {
130   return this->CentralCursor->GetGlobalNodeIndex();
131 }
132 
133 //------------------------------------------------------------------------------
GetGlobalNodeIndex(unsigned int icursor)134 vtkIdType vtkHyperTreeGridNonOrientedSuperCursorLight::GetGlobalNodeIndex(unsigned int icursor)
135 {
136   if (icursor == this->IndiceCentralCursor)
137   {
138     return this->CentralCursor->GetGlobalNodeIndex();
139   }
140   return this->Entries[this->GetIndiceEntry(icursor)].GetGlobalNodeIndex();
141 }
142 
143 //------------------------------------------------------------------------------
GetInformation(unsigned int icursor,unsigned int & level,bool & leaf,vtkIdType & id)144 vtkHyperTree* vtkHyperTreeGridNonOrientedSuperCursorLight::GetInformation(
145   unsigned int icursor, unsigned int& level, bool& leaf, vtkIdType& id)
146 {
147   if (icursor == this->IndiceCentralCursor)
148   {
149     level = this->CentralCursor->GetLevel();
150     leaf = this->CentralCursor->IsLeaf();
151     id = this->CentralCursor->GetGlobalNodeIndex();
152     return this->CentralCursor->GetTree();
153   }
154   vtkHyperTreeGridLevelEntry& entry = this->Entries[this->GetIndiceEntry(icursor)];
155   vtkHyperTree* tree = entry.GetTree();
156   if (tree)
157   {
158     level = entry.GetLevel();
159     leaf = entry.IsLeaf(this->Grid);
160     id = entry.GetGlobalNodeIndex();
161   }
162   return tree;
163 }
164 
165 //------------------------------------------------------------------------------
GetDimension()166 unsigned char vtkHyperTreeGridNonOrientedSuperCursorLight::GetDimension()
167 {
168   return this->Grid->GetDimension();
169 }
170 
171 //------------------------------------------------------------------------------
GetNumberOfChildren()172 unsigned char vtkHyperTreeGridNonOrientedSuperCursorLight::GetNumberOfChildren()
173 {
174   return this->CentralCursor->GetTree()->GetNumberOfChildren();
175 }
176 
177 //------------------------------------------------------------------------------
SetGlobalIndexStart(vtkIdType index)178 void vtkHyperTreeGridNonOrientedSuperCursorLight::SetGlobalIndexStart(vtkIdType index)
179 {
180   this->CentralCursor->SetGlobalIndexStart(index);
181 }
182 
183 //------------------------------------------------------------------------------
SetGlobalIndexFromLocal(vtkIdType index)184 void vtkHyperTreeGridNonOrientedSuperCursorLight::SetGlobalIndexFromLocal(vtkIdType index)
185 {
186   this->CentralCursor->SetGlobalIndexFromLocal(index);
187 }
188 
189 //------------------------------------------------------------------------------
GetOrigin()190 double* vtkHyperTreeGridNonOrientedSuperCursorLight::GetOrigin()
191 {
192   return this->CentralCursor->GetOrigin();
193 }
194 
195 //------------------------------------------------------------------------------
GetSize()196 double* vtkHyperTreeGridNonOrientedSuperCursorLight::GetSize()
197 {
198   return this->CentralCursor->GetSize();
199 }
200 
201 //------------------------------------------------------------------------------
GetBounds(double bounds[6])202 void vtkHyperTreeGridNonOrientedSuperCursorLight::GetBounds(double bounds[6])
203 {
204   this->CentralCursor->GetBounds(bounds);
205 }
206 
207 //------------------------------------------------------------------------------
SetMask(bool state)208 void vtkHyperTreeGridNonOrientedSuperCursorLight::SetMask(bool state)
209 {
210   assert("pre: not_tree" && this->CentralCursor->GetTree());
211   this->CentralCursor->SetMask(state);
212 }
213 
214 //------------------------------------------------------------------------------
SetMask(unsigned int icursor,bool state)215 void vtkHyperTreeGridNonOrientedSuperCursorLight::SetMask(unsigned int icursor, bool state)
216 {
217   if (icursor == this->IndiceCentralCursor)
218   {
219     this->SetMask(state);
220   }
221   else
222   {
223     vtkHyperTreeGridLevelEntry& entry = this->Entries[this->GetIndiceEntry(icursor)];
224     assert("pre: not_tree" && entry.GetTree());
225     entry.SetMask(this->Grid, state);
226   }
227 }
228 
229 //------------------------------------------------------------------------------
IsMasked()230 bool vtkHyperTreeGridNonOrientedSuperCursorLight::IsMasked()
231 {
232   return this->CentralCursor->IsMasked();
233 }
234 
235 //------------------------------------------------------------------------------
IsMasked(unsigned int icursor)236 bool vtkHyperTreeGridNonOrientedSuperCursorLight::IsMasked(unsigned int icursor)
237 {
238   if (icursor == this->IndiceCentralCursor)
239   {
240     return this->IsMasked();
241   }
242   vtkHyperTreeGridLevelEntry& entry = this->Entries[this->GetIndiceEntry(icursor)];
243   return entry.IsMasked(this->Grid);
244 }
245 
246 //------------------------------------------------------------------------------
GetPoint(double point[3])247 void vtkHyperTreeGridNonOrientedSuperCursorLight::GetPoint(double point[3])
248 {
249   this->CentralCursor->GetPoint(point);
250 }
251 
252 //------------------------------------------------------------------------------
IsLeaf()253 bool vtkHyperTreeGridNonOrientedSuperCursorLight::IsLeaf()
254 {
255   return this->CentralCursor->IsLeaf();
256 }
257 
258 //------------------------------------------------------------------------------
IsLeaf(unsigned int icursor)259 bool vtkHyperTreeGridNonOrientedSuperCursorLight::IsLeaf(unsigned int icursor)
260 {
261   if (icursor == this->IndiceCentralCursor)
262   {
263     return this->CentralCursor->IsLeaf();
264   }
265   return this->Entries[this->GetIndiceEntry(icursor)].IsLeaf(this->Grid);
266 }
267 
268 //------------------------------------------------------------------------------
SubdivideLeaf()269 void vtkHyperTreeGridNonOrientedSuperCursorLight::SubdivideLeaf()
270 {
271   this->CentralCursor->SubdivideLeaf();
272 }
273 
274 //------------------------------------------------------------------------------
IsRoot()275 bool vtkHyperTreeGridNonOrientedSuperCursorLight::IsRoot()
276 {
277   return this->CentralCursor->IsRoot();
278 }
279 
280 //------------------------------------------------------------------------------
GetLevel()281 unsigned int vtkHyperTreeGridNonOrientedSuperCursorLight::GetLevel()
282 {
283   return this->CentralCursor->GetLevel();
284 }
285 
286 //------------------------------------------------------------------------------
GetLevel(unsigned int icursor)287 unsigned int vtkHyperTreeGridNonOrientedSuperCursorLight::GetLevel(unsigned int icursor)
288 {
289   if (icursor == this->IndiceCentralCursor)
290   {
291     return this->CentralCursor->GetLevel();
292   }
293   return this->Entries[this->GetIndiceEntry(icursor)].GetLevel();
294 }
295 
296 //------------------------------------------------------------------------------
ToChild(unsigned char ichild)297 void vtkHyperTreeGridNonOrientedSuperCursorLight::ToChild(unsigned char ichild)
298 {
299   assert("pre: Non_leaf" && !this->IsLeaf());
300   //
301   ++this->CurrentFirstNonValidEntryByLevel;
302   if (this->FirstNonValidEntryByLevel.size() == this->CurrentFirstNonValidEntryByLevel)
303   {
304     this->FirstNonValidEntryByLevel.resize(this->CurrentFirstNonValidEntryByLevel + 1);
305   }
306   this->FirstNonValidEntryByLevel[this->CurrentFirstNonValidEntryByLevel] =
307     this->FirstNonValidEntryByLevel[this->CurrentFirstNonValidEntryByLevel - 1];
308   //
309   this->FirstCurrentNeighboorReferenceEntry += (this->NumberOfCursors - 1);
310   if (this->ReferenceEntries.size() == this->FirstCurrentNeighboorReferenceEntry)
311   {
312     this->ReferenceEntries.resize(
313       this->FirstCurrentNeighboorReferenceEntry + (this->NumberOfCursors - 1));
314   }
315   // Point into traversal tables at child location
316   int offset = ichild * this->NumberOfCursors;
317   const unsigned int* pTab = this->ChildCursorToParentCursorTable + offset;
318   const unsigned int* cTab = this->ChildCursorToChildTable + offset;
319 
320   // Move each cursor in the supercursor down to a child
321   for (unsigned int i = 0; i < this->NumberOfCursors; ++i)
322   {
323     if (i != this->IndiceCentralCursor)
324     {
325       // Make relevant cursor in parent cell point towards current child cursor
326       unsigned int j = pTab[i];
327 
328       // If neighnoring cell is further subdivided, then descend into it
329       unsigned int reference = UINT_MAX;
330       if (j == this->IndiceCentralCursor)
331       {
332         reference = this->FirstNonValidEntryByLevel[this->CurrentFirstNonValidEntryByLevel];
333         ++this->FirstNonValidEntryByLevel[this->CurrentFirstNonValidEntryByLevel];
334         if (this->Entries.size() <= reference)
335         {
336           this->Entries.resize(reference + 1);
337         }
338         //
339         if (i > this->IndiceCentralCursor)
340         {
341           this->ReferenceEntries[this->FirstCurrentNeighboorReferenceEntry + i - 1] = reference;
342         }
343         else
344         {
345           this->ReferenceEntries[this->FirstCurrentNeighboorReferenceEntry + i] = reference;
346         }
347         //
348         vtkHyperTreeGridLevelEntry& current = this->Entries[reference];
349         current.Initialize(this->CentralCursor->GetTree(), this->CentralCursor->GetLevel(),
350           this->CentralCursor->GetVertexId());
351         //
352         // JB1901 ne pas descendre si masque
353         if (!this->IsMasked()) // JB1901 new code
354         {                      // JB1901 new code
355           //
356           if (current.GetTree() && !current.IsLeaf(this->Grid))
357           {
358             // Move to child
359             current.ToChild(this->Grid, cTab[i]);
360           }
361           //
362         }
363       }
364       else
365       {
366         unsigned int previous = this->GetIndicePreviousEntry(j);
367         //
368         if (this->Entries[previous].GetTree() && !this->Entries[previous].IsLeaf(this->Grid) &&
369           !(this->GetGrid()->HasMask()
370               ? this->GetGrid()->GetMask()->GetValue(this->Entries[previous].GetGlobalNodeIndex())
371               : 0))
372         {
373           reference = this->FirstNonValidEntryByLevel[this->CurrentFirstNonValidEntryByLevel];
374           ++this->FirstNonValidEntryByLevel[this->CurrentFirstNonValidEntryByLevel];
375           if (this->Entries.size() <= reference)
376           {
377             this->Entries.resize(reference + 1);
378           }
379           if (i > this->IndiceCentralCursor)
380           {
381             this->ReferenceEntries[this->FirstCurrentNeighboorReferenceEntry + i - 1] = reference;
382           }
383           else
384           {
385             this->ReferenceEntries[this->FirstCurrentNeighboorReferenceEntry + i] = reference;
386           }
387           //
388           vtkHyperTreeGridLevelEntry& current = this->Entries[reference];
389           current.Copy(&(this->Entries[previous]));
390           current.ToChild(this->Grid, cTab[i]);
391         }
392         else
393         {
394           if (j > this->IndiceCentralCursor)
395           {
396             reference = this->ReferenceEntries[this->FirstCurrentNeighboorReferenceEntry -
397               (this->NumberOfCursors - 1) + j - 1];
398           }
399           else
400           {
401             reference = this->ReferenceEntries[this->FirstCurrentNeighboorReferenceEntry -
402               (this->NumberOfCursors - 1) + j];
403           }
404           if (i > this->IndiceCentralCursor)
405           {
406             this->ReferenceEntries[this->FirstCurrentNeighboorReferenceEntry + i - 1] = reference;
407           }
408           else
409           {
410             this->ReferenceEntries[this->FirstCurrentNeighboorReferenceEntry + i] = reference;
411           }
412         }
413       }
414     }
415   } // i
416   this->CentralCursor->ToChild(cTab[this->IndiceCentralCursor]);
417 }
418 
419 //------------------------------------------------------------------------------
ToRoot()420 void vtkHyperTreeGridNonOrientedSuperCursorLight::ToRoot()
421 {
422   assert("pre: hypertree_exist" && this->Entries.size() > 0);
423   this->CentralCursor->ToRoot();
424   this->CurrentFirstNonValidEntryByLevel = 0;
425   this->FirstCurrentNeighboorReferenceEntry = 0;
426 }
427 
428 //------------------------------------------------------------------------------
ToParent()429 void vtkHyperTreeGridNonOrientedSuperCursorLight::ToParent()
430 {
431   assert("pre: Non_root" && !this->IsRoot());
432   this->CentralCursor->ToParent();
433   this->CurrentFirstNonValidEntryByLevel--;
434   this->FirstCurrentNeighboorReferenceEntry -= (this->NumberOfCursors - 1);
435 }
436 
437 //------------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)438 void vtkHyperTreeGridNonOrientedSuperCursorLight::PrintSelf(ostream& os, vtkIndent indent)
439 {
440   os << indent << "--vtkHyperTreeGridNonOrientedSuperCursorLight--" << endl;
441   this->CentralCursor->PrintSelf(os, indent);
442   os << indent << "IndiceCentralCursor: " << this->IndiceCentralCursor << endl;
443   os << indent << "NumberOfCursors: " << this->NumberOfCursors << endl;
444 }
445 
446 //------------------------------------------------------------------------------
vtkHyperTreeGridNonOrientedSuperCursorLight()447 vtkHyperTreeGridNonOrientedSuperCursorLight::vtkHyperTreeGridNonOrientedSuperCursorLight()
448 {
449   this->Grid = nullptr;
450   this->IndiceCentralCursor = 0;
451   this->NumberOfCursors = 0;
452   this->ChildCursorToParentCursorTable = nullptr;
453   this->ChildCursorToChildTable = nullptr;
454   this->CurrentFirstNonValidEntryByLevel = 0;
455   this->FirstCurrentNeighboorReferenceEntry = 0;
456 
457   this->CentralCursor = vtkSmartPointer<vtkHyperTreeGridNonOrientedGeometryCursor>::New();
458 }
459 
460 //------------------------------------------------------------------------------
461 vtkHyperTreeGridNonOrientedSuperCursorLight::~vtkHyperTreeGridNonOrientedSuperCursorLight() =
462   default;
463 
464 //------------------------------------------------------------------------------
465