forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsgUnitBuilder.cc
171 lines (148 loc) · 5.63 KB
/
CsgUnitBuilder.cc
1
2
3
4
5
6
7
8
9
10
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file orange/orangeinp/detail/CsgUnitBuilder.cc
//---------------------------------------------------------------------------//
#include "CsgUnitBuilder.hh"
#include "corecel/io/Logger.hh"
#include "corecel/io/StreamableVariant.hh"
#include "orange/transform/TransformIO.hh"
namespace celeritas
{
namespace orangeinp
{
namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Construct with an empty unit (which doesn't yet have any elements).
*/
CsgUnitBuilder::CsgUnitBuilder(CsgUnit* u, Tolerance<> const& tol)
: unit_{u}
, tol_{tol}
, insert_surface_{&unit_->surfaces, tol}
, insert_transform_{&unit_->transforms}
{
CELER_EXPECT(unit_);
CELER_EXPECT(unit_->empty());
// Resize because the tree comes prepopulated with true/false
unit_->metadata.resize(unit_->tree.size());
}
//---------------------------------------------------------------------------//
/*!
* Set a bounding zone and transform for a node.
*/
void CsgUnitBuilder::insert_region(NodeId n,
BoundingZone const& bzone,
TransformId trans_id)
{
CELER_EXPECT(n < unit_->tree.size());
CELER_EXPECT(trans_id < unit_->transforms.size());
auto&& [iter, inserted]
= unit_->regions.insert({n, CsgUnit::Region{bzone, trans_id}});
if (CELERITAS_DEBUG && !inserted)
{
// The existing bounding zone *SHOULD BE IDENTICAL*.
// For now this is a rough check...
CsgUnit::Region const& existing = iter->second;
CELER_ASSERT(bzone.negated == existing.bounds.negated);
CELER_ASSERT(static_cast<bool>(bzone.interior)
== static_cast<bool>(existing.bounds.interior));
CELER_ASSERT(static_cast<bool>(bzone.exterior)
== static_cast<bool>(existing.bounds.exterior));
if (trans_id != existing.transform_id)
{
// TODO: we need to implement transform soft equivalence
// and simplification
CELER_LOG(warning)
<< "While re-inserting region for node " << n.get()
<< ": existing transform "
<< StreamableVariant{this->transform(existing.transform_id)}
<< " differs from new transform "
<< StreamableVariant{this->transform(trans_id)};
}
}
}
//---------------------------------------------------------------------------//
/*!
* Mark a CSG node as a volume of real space.
*
* *After* construction is complete, the list of volumes should be checked for
* duplicate nodes.
*/
LocalVolumeId CsgUnitBuilder::insert_volume(NodeId n)
{
CELER_EXPECT(n < unit_->tree.size());
LocalVolumeId result{static_cast<size_type>(unit_->volumes.size())};
unit_->volumes.push_back(n);
unit_->fills.resize(unit_->volumes.size());
CELER_ENSURE(*unit_);
return result;
}
//---------------------------------------------------------------------------//
/*!
* Fill a volume node with a material.
*/
void CsgUnitBuilder::fill_volume(LocalVolumeId v, MaterialId m)
{
CELER_EXPECT(v < unit_->fills.size());
CELER_EXPECT(m);
unit_->fills[v.unchecked_get()] = m;
}
//---------------------------------------------------------------------------//
/*!
* Fill a volume node with a daughter.
*/
void CsgUnitBuilder::fill_volume(LocalVolumeId v, UniverseId u)
{
CELER_EXPECT(v < unit_->fills.size());
CELER_EXPECT(!is_filled(unit_->fills[v.unchecked_get()]));
CELER_EXPECT(u);
// Get the region associated with this node ID for the volume
CELER_ASSERT(unit_->volumes.size() == unit_->fills.size());
auto iter = unit_->regions.find(unit_->volumes[v.unchecked_get()]);
// The iterator should be valid since the whole volume should never be a
// pure surface (created outside a convex region)
CELER_ASSERT(iter != unit_->regions.end());
Daughter new_daughter;
new_daughter.universe_id = u;
new_daughter.transform_id = iter->second.transform_id;
CELER_ASSERT(new_daughter.transform_id < unit_->transforms.size());
// Save fill
unit_->fills[v.unchecked_get()] = std::move(new_daughter);
CELER_ENSURE(is_filled(unit_->fills[v.unchecked_get()]));
}
//---------------------------------------------------------------------------//
/*!
* Set an exterior node.
*
* This should be called only once (but this could be relaxed if needed).
*/
void CsgUnitBuilder::set_exterior(NodeId n)
{
CELER_EXPECT(n < unit_->tree.size());
CELER_EXPECT(!unit_->exterior);
unit_->exterior = n;
}
//---------------------------------------------------------------------------//
/*!
* Get a variant surface from a node ID.
*/
VariantSurface const& CsgUnitBuilder::get_surface_impl(NodeId nid) const
{
CELER_EXPECT(nid < unit_->tree.size());
using SurfaceNode = ::celeritas::orangeinp::Surface;
// Get the surface ID from the tree
auto const& node = unit_->tree[nid];
CELER_ASSUME(std::holds_alternative<SurfaceNode>(node));
LocalSurfaceId lsid = std::get<SurfaceNode>(node).id;
// Get the variant surfaces from the unit
CELER_EXPECT(lsid < unit_->surfaces.size());
return unit_->surfaces[lsid.unchecked_get()];
}
//---------------------------------------------------------------------------//
} // namespace detail
} // namespace orangeinp
} // namespace celeritas