-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathLayerBlueprintNode.cpp
More file actions
288 lines (235 loc) · 8.96 KB
/
LayerBlueprintNode.cpp
File metadata and controls
288 lines (235 loc) · 8.96 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// This file is part of the ACTS project.
//
// Copyright (C) 2016 CERN for the benefit of the ACTS project
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#include "Acts/Geometry/LayerBlueprintNode.hpp"
#include "Acts/Definitions/Algebra.hpp"
#include "Acts/Geometry/CuboidVolumeBounds.hpp"
#include "Acts/Geometry/CylinderVolumeBounds.hpp"
#include "Acts/Geometry/ProtoLayer.hpp"
#include "Acts/Geometry/VolumeBounds.hpp"
#include "Acts/Utilities/GraphViz.hpp"
namespace Acts::Experimental {
namespace detail {
struct LayerBlueprintNodeImpl {
using LayerType = LayerBlueprintNode::LayerType;
std::string m_name;
using LayerNodePtr = std::variant<std::shared_ptr<Surface>,
std::shared_ptr<SurfacePlacementBase>>;
std::vector<LayerNodePtr> m_nodes{};
/// If a proto layer is already given externally, this node will not perform
/// sizing from surfaces
std::optional<MutableProtoLayer> m_protoLayer;
Transform3 m_transform = Transform3::Identity();
ExtentEnvelope m_envelope = ExtentEnvelope::Zero();
LayerType m_layerType = LayerType::Cylinder;
std::array<bool, 3> m_useCenterOfGravity = {true, true, true};
};
} // namespace detail
LayerBlueprintNode::LayerBlueprintNode(std::string_view name)
: StaticBlueprintNode(nullptr) {
m_impl = std::make_unique<detail::LayerBlueprintNodeImpl>();
m_impl->m_name = name;
}
LayerBlueprintNode::~LayerBlueprintNode() = default;
detail::LayerBlueprintNodeImpl& LayerBlueprintNode::impl() {
assert(m_impl != nullptr);
return *m_impl;
}
const detail::LayerBlueprintNodeImpl& LayerBlueprintNode::impl() const {
assert(m_impl != nullptr);
return *m_impl;
}
Volume& LayerBlueprintNode::build(const BlueprintOptions& options,
const GeometryContext& gctx,
const Logger& logger) {
if (impl().m_nodes.empty()) {
ACTS_ERROR("LayerBlueprintNode: no surfaces provided");
throw std::invalid_argument("LayerBlueprintNode: no surfaces provided");
}
ACTS_DEBUG(prefix() << "Building Layer " << name() << " from "
<< impl().m_nodes.size() << " nodes");
ACTS_VERBOSE(prefix() << " -> layer type: " << impl().m_layerType);
ACTS_VERBOSE(prefix() << " -> transform:\n" << impl().m_transform.matrix());
Extent extent;
if (!impl().m_protoLayer.has_value()) {
impl().m_protoLayer.emplace(gctx, surfaces(), impl().m_transform.inverse());
ACTS_VERBOSE(prefix() << "Built proto layer: "
<< impl().m_protoLayer.value());
} else {
ACTS_VERBOSE(prefix() << "Using provided proto layer");
}
auto& protoLayer = impl().m_protoLayer.value();
extent.addConstrain(protoLayer.extent, impl().m_envelope);
ACTS_VERBOSE(prefix() << " -> layer extent: " << extent);
buildVolume(extent, logger);
assert(m_volume != nullptr && "Volume not built from proto layer");
for (auto& surface : surfaces()) {
m_volume->addSurface(surface);
}
auto visitor = overloaded{
[](const std::shared_ptr<Surface>&) {
},
[this](const std::shared_ptr<SurfacePlacementBase>& placement) {
m_volume->cachePlacement(placement);
}};
for (auto& node : impl().m_nodes) {
std::visit(visitor, node);
}
return StaticBlueprintNode::build(options, gctx, logger);
}
void LayerBlueprintNode::buildVolume(const Extent& extent,
const Logger& logger) {
ACTS_VERBOSE(prefix() << "Building volume for layer " << name());
using enum AxisDirection;
using enum LayerType;
std::shared_ptr<VolumeBounds> bounds;
switch (impl().m_layerType) {
case Cylinder:
case Disc: {
double minR = extent.min(AxisR);
double maxR = extent.max(AxisR);
double hlZ = extent.interval(AxisZ) / 2.0;
bounds = std::make_shared<CylinderVolumeBounds>(minR, maxR, hlZ);
break;
}
case Plane: {
double hlX = extent.interval(AxisX) / 2.0;
double hlY = extent.interval(AxisY) / 2.0;
double hlZ = extent.interval(AxisZ) / 2.0;
bounds = std::make_shared<CuboidVolumeBounds>(hlX, hlY, hlZ);
break;
}
}
assert(bounds != nullptr);
ACTS_VERBOSE(prefix() << " -> bounds: " << *bounds);
Transform3 transform = impl().m_transform;
Vector3 translation = Vector3::Zero();
if (impl().m_useCenterOfGravity.at(toUnderlying(AxisX))) {
translation.x() = extent.medium(AxisX);
}
if (impl().m_useCenterOfGravity.at(toUnderlying(AxisY))) {
translation.y() = extent.medium(AxisY);
}
if (impl().m_useCenterOfGravity.at(toUnderlying(AxisZ))) {
translation.z() = extent.medium(AxisZ);
}
transform.translation() = translation;
ACTS_VERBOSE(prefix() << " -> adjusted transform:\n" << transform.matrix());
m_volume = std::make_unique<TrackingVolume>(transform, std::move(bounds),
impl().m_name);
}
const std::string& LayerBlueprintNode::name() const {
return impl().m_name;
}
LayerBlueprintNode& LayerBlueprintNode::setSurfaces(
std::vector<std::shared_ptr<Surface>> surfaces) {
impl().m_nodes.reserve(impl().m_nodes.size() + surfaces.size());
for (auto& surface : surfaces) {
impl().m_nodes.emplace_back(std::move(surface));
}
impl().m_protoLayer.reset();
return *this;
}
LayerBlueprintNode& LayerBlueprintNode::setPlacements(
std::vector<std::shared_ptr<SurfacePlacementBase>> placements) {
impl().m_nodes.reserve(impl().m_nodes.size() + placements.size());
for (auto& placement : placements) {
impl().m_nodes.emplace_back(std::move(placement));
}
impl().m_protoLayer.reset();
return *this;
}
/// Register a new surface with the layer
/// @note This will clear any previously registered proto layer
/// @return Reference to this node for chaining
LayerBlueprintNode& LayerBlueprintNode::addSurface(
std::shared_ptr<Surface> surface) {
impl().m_nodes.emplace_back(std::move(surface));
return *this;
}
/// Register a new placement with the layer
/// @note This will clear any previously registered proto layer
/// @return Reference to this node for chaining
LayerBlueprintNode& LayerBlueprintNode::addPlacement(
std::shared_ptr<SurfacePlacementBase> placement) {
impl().m_nodes.emplace_back(std::move(placement));
return *this;
}
std::vector<std::shared_ptr<Surface>> LayerBlueprintNode::surfaces() const {
std::vector<std::shared_ptr<Surface>> surfaces{};
surfaces.reserve(impl().m_nodes.size());
auto visitor = overloaded{
[&surfaces](const std::shared_ptr<Surface>& surface) {
surfaces.emplace_back(surface);
},
[&surfaces](const std::shared_ptr<SurfacePlacementBase>& placement) {
surfaces.emplace_back(placement->surface().getSharedPtr());
}};
for (const auto& node : impl().m_nodes) {
std::visit(visitor, node);
}
auto nonUniqueRange = std::ranges::unique(
surfaces.begin(), surfaces.end(),
[](const std::shared_ptr<Surface>& a, const std::shared_ptr<Surface>& b) {
return a == b;
});
surfaces.erase(nonUniqueRange.begin(), nonUniqueRange.end());
return surfaces;
}
LayerBlueprintNode& LayerBlueprintNode::setProtoLayer(
std::optional<MutableProtoLayer> protoLayer) {
impl().m_protoLayer = std::move(protoLayer);
impl().m_nodes.clear();
// also take ownership of the surfaces now
for (auto& surface : impl().m_protoLayer.value().surfaces()) {
impl().m_nodes.emplace_back(surface->getSharedPtr());
}
return *this;
}
const MutableProtoLayer* LayerBlueprintNode::protoLayer() const {
return impl().m_protoLayer.has_value() ? &impl().m_protoLayer.value()
: nullptr;
}
LayerBlueprintNode& LayerBlueprintNode::setTransform(
const Transform3& transform) {
impl().m_transform = transform;
return *this;
}
const Transform3& LayerBlueprintNode::transform() const {
return impl().m_transform;
}
LayerBlueprintNode& LayerBlueprintNode::setEnvelope(
const ExtentEnvelope& envelope) {
impl().m_envelope = envelope;
return *this;
}
const ExtentEnvelope& LayerBlueprintNode::envelope() const {
return impl().m_envelope;
}
LayerBlueprintNode& LayerBlueprintNode::setLayerType(LayerType layerType) {
impl().m_layerType = layerType;
return *this;
}
const LayerBlueprintNode::LayerType& LayerBlueprintNode::layerType() const {
return impl().m_layerType;
}
LayerBlueprintNode& LayerBlueprintNode::setUseCenterOfGravity(bool x, bool y,
bool z) {
impl().m_useCenterOfGravity = {x, y, z};
return *this;
}
void LayerBlueprintNode::addToGraphviz(std::ostream& os) const {
std::stringstream ss;
ss << "<br/><b>" + name() + "</b>";
ss << "<br/>Layer";
ss << "<br/><i>" << impl().m_layerType << "</i>";
GraphViz::Node node{
.id = name(), .label = ss.str(), .shape = GraphViz::Shape::Diamond};
os << node;
BlueprintNode::addToGraphviz(os);
}
} // namespace Acts::Experimental