1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkXdmf3SILBuilder.cxx
5   Language:  C++
6 
7   Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen
8   All rights reserved.
9   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
10 
11      This software is distributed WITHOUT ANY WARRANTY; without even
12      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13      PURPOSE.  See the above copyright notice for more information.
14 
15 =========================================================================*/
16 
17 #include "vtkXdmf3SILBuilder.h"
18 
19 #include "vtkDataSetAttributes.h"
20 #include "vtkStringArray.h"
21 #include "vtkUnsignedCharArray.h"
22 #include "vtkMutableDirectedGraph.h"
23 
24 // As soon as num-grids (sub-grids and all) grows beyond this number, we assume
25 // that the grids are too numerous for the user to select individually and
26 // hence only the top-level grids are made accessible.
27 #define MAX_COLLECTABLE_NUMBER_OF_GRIDS 1000
28 
29 //------------------------------------------------------------------------------
vtkXdmf3SILBuilder()30 vtkXdmf3SILBuilder::vtkXdmf3SILBuilder()
31 {
32   this->SIL = nullptr;
33   this->NamesArray = nullptr;
34   this->CrossEdgesArray = nullptr;
35   this->RootVertex = -1;
36   this->BlocksRoot = -1;
37   this->HierarchyRoot = -1;
38   this->VertexCount = 0;
39 }
40 
41 //------------------------------------------------------------------------------
~vtkXdmf3SILBuilder()42 vtkXdmf3SILBuilder::~vtkXdmf3SILBuilder()
43 {
44   if (this->SIL)
45   {
46     this->SIL->Delete();
47   }
48   if (this->NamesArray)
49   {
50     this->NamesArray->Delete();
51   }
52   if (this->CrossEdgesArray)
53   {
54     this->CrossEdgesArray->Delete();
55   }
56 }
57 
58 //------------------------------------------------------------------------------
Initialize()59 void vtkXdmf3SILBuilder::Initialize()
60 {
61   if (this->SIL)
62   {
63     this->SIL->Delete();
64   }
65   this->SIL = vtkMutableDirectedGraph::New();
66   this->SIL->Initialize();
67 
68   if (this->NamesArray)
69   {
70     this->NamesArray->Delete();
71   }
72   this->NamesArray = vtkStringArray::New();
73   this->NamesArray->SetName("Names");
74   this->SIL->GetVertexData()->AddArray(this->NamesArray);
75 
76   if (this->CrossEdgesArray)
77   {
78     this->CrossEdgesArray->Delete();
79   }
80 
81   this->CrossEdgesArray = vtkUnsignedCharArray::New();
82   this->CrossEdgesArray->SetName("CrossEdges");
83   this->SIL->GetEdgeData()->AddArray(this->CrossEdgesArray);
84 
85   this->RootVertex = this->AddVertex("SIL");
86   this->BlocksRoot = this->AddVertex("Blocks");
87   this->HierarchyRoot = this->AddVertex("Hierarchy");
88   this->AddChildEdge(RootVertex, BlocksRoot);
89   this->AddChildEdge(RootVertex, HierarchyRoot);
90 
91   this->VertexCount = 0;
92 }
93 
94 //------------------------------------------------------------------------------
AddVertex(const char * name)95 vtkIdType vtkXdmf3SILBuilder::AddVertex(const char* name)
96 {
97   this->VertexCount++;
98   vtkIdType vertex = this->SIL->AddVertex();
99   this->NamesArray->InsertValue(vertex, name);
100   return vertex;
101 }
102 
103 //------------------------------------------------------------------------------
AddChildEdge(vtkIdType parent,vtkIdType child)104 vtkIdType vtkXdmf3SILBuilder::AddChildEdge(vtkIdType parent, vtkIdType child)
105 {
106   vtkIdType id = this->SIL->AddEdge(parent, child).Id;
107   this->CrossEdgesArray->InsertValue(id, 0);
108   return id;
109 }
110 
111 //------------------------------------------------------------------------------
AddCrossEdge(vtkIdType src,vtkIdType dst)112 vtkIdType vtkXdmf3SILBuilder::AddCrossEdge(vtkIdType src, vtkIdType dst)
113 {
114   vtkIdType id = this->SIL->AddEdge(src, dst).Id;
115   this->CrossEdgesArray->InsertValue(id, 1);
116   return id;
117 }
118 
119 //------------------------------------------------------------------------------
GetRootVertex()120 vtkIdType vtkXdmf3SILBuilder::GetRootVertex()
121 {
122   return this->RootVertex;
123 }
124 
125 //------------------------------------------------------------------------------
GetBlocksRoot()126 vtkIdType vtkXdmf3SILBuilder::GetBlocksRoot()
127 {
128   return this->BlocksRoot;
129 }
130 
131 //------------------------------------------------------------------------------
GetHierarchyRoot()132 vtkIdType vtkXdmf3SILBuilder::GetHierarchyRoot()
133 {
134   return this->HierarchyRoot;
135 }
136 
137 //------------------------------------------------------------------------------
IsMaxedOut()138 bool vtkXdmf3SILBuilder::IsMaxedOut()
139 {
140   return (this->VertexCount >= MAX_COLLECTABLE_NUMBER_OF_GRIDS);
141 }
142