forked from acts-project/acts
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGeometry.cpp
More file actions
389 lines (348 loc) · 15.2 KB
/
Geometry.cpp
File metadata and controls
389 lines (348 loc) · 15.2 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// 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/Definitions/Algebra.hpp"
#include "Acts/Geometry/CylinderVolumeBounds.hpp"
#include "Acts/Geometry/CylinderVolumeStack.hpp"
#include "Acts/Geometry/Extent.hpp"
#include "Acts/Geometry/GeometryContext.hpp"
#include "Acts/Geometry/GeometryHierarchyMap.hpp"
#include "Acts/Geometry/GeometryIdentifier.hpp"
#include "Acts/Geometry/Portal.hpp"
#include "Acts/Geometry/PortalLinkBase.hpp"
#include "Acts/Geometry/PortalShell.hpp"
#include "Acts/Geometry/ProtoLayer.hpp"
#include "Acts/Geometry/TrackingGeometry.hpp"
#include "Acts/Geometry/TrackingGeometryVisitor.hpp"
#include "Acts/Geometry/Volume.hpp"
#include "Acts/Geometry/VolumeAttachmentStrategy.hpp"
#include "Acts/Geometry/VolumeBounds.hpp"
#include "Acts/Geometry/VolumeResizeStrategy.hpp"
#include "Acts/Material/ISurfaceMaterial.hpp"
#include "Acts/Surfaces/Surface.hpp"
#include "Acts/Surfaces/SurfaceArray.hpp"
#include "Acts/Utilities/AxisDefinitions.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/RangeXD.hpp"
#include "Acts/Visualization/ViewConfig.hpp"
#include "ActsPython/Utilities/Helpers.hpp"
#include "ActsPython/Utilities/Macros.hpp"
#include <array>
#include <memory>
#include <numbers>
#include <unordered_map>
#include <utility>
#include <vector>
#include <boost/algorithm/string/join.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;
using namespace pybind11::literals;
using namespace Acts;
using namespace ActsExamples;
namespace {
struct GeometryIdentifierHookBinding : public GeometryIdentifierHook {
py::object callable;
GeometryIdentifier decorateIdentifier(GeometryIdentifier identifier,
const Surface& surface) const override {
return callable(identifier, surface.getSharedPtr())
.cast<GeometryIdentifier>();
}
};
struct MaterialSurfaceSelector {
std::vector<const Surface*> surfaces = {};
/// @param surface is the test surface
void operator()(const Surface* surface) {
if (surface->surfaceMaterial() != nullptr &&
!rangeContainsValue(surfaces, surface)) {
surfaces.push_back(surface);
}
}
};
#define _INVOKE(method, name, arg) \
pybind11::gil_scoped_acquire gil; \
pybind11::function override = pybind11::get_override(this, name); \
if (!override) { \
method(arg); \
return; \
} \
override(&arg);
// We only implement the mutable visitor here, because pybind11 always casts
// away const ness in any case
class PyTrackingGeometryVisitor : public TrackingGeometryMutableVisitor {
public:
void visitVolume(TrackingVolume& volume) override {
_INVOKE(TrackingGeometryMutableVisitor::visitVolume, "visitVolume", volume);
}
void visitPortal(Portal& portal) override {
_INVOKE(TrackingGeometryMutableVisitor::visitPortal, "visitPortal", portal);
}
void visitLayer(Layer& layer) override {
_INVOKE(TrackingGeometryMutableVisitor::visitLayer, "visitLayer", layer);
}
void visitSurface(Surface& surface) override {
_INVOKE(TrackingGeometryMutableVisitor::visitSurface, "visitSurface",
surface);
}
void visitBoundarySurface(
Acts::BoundarySurfaceT<Acts::TrackingVolume>& boundary) override {
_INVOKE(Acts::TrackingGeometryMutableVisitor::visitBoundarySurface,
"visitBoundarySurface", boundary);
}
};
#undef _INVOKE
} // namespace
namespace ActsPython {
/// This adds the geometry bindings to the python module
/// @param m the module to add the bindings to
void addGeometry(py::module_& m) {
{
py::class_<GeometryContext>(m, "GeometryContext")
.def(py::init([]() {
// Issue Python warning about deprecated default constructor
auto warnings = py::module_::import("warnings");
auto builtins = py::module_::import("builtins");
warnings.attr("warn")(
"GeometryContext::dangerouslyDefaultConstruct() is deprecated. "
"Use "
"GeometryContext.dangerouslyDefaultConstruct() instead to "
"make empty context construction explicit.",
builtins.attr("DeprecationWarning"));
return GeometryContext::dangerouslyDefaultConstruct();
})) // Keep for backward compatibility but warn
.def_static("dangerouslyDefaultConstruct",
&GeometryContext::dangerouslyDefaultConstruct,
"Create a default GeometryContext (empty, no alignment "
"data)");
py::class_<GeometryIdentifier>(m, "GeometryIdentifier")
.def(py::init<>())
.def(py::init<GeometryIdentifier::Value>())
.def(py::init([](int volume, int boundary, int layer, int approach,
int sensitive, int extra) {
return GeometryIdentifier()
.withVolume(volume)
.withBoundary(boundary)
.withLayer(layer)
.withApproach(approach)
.withSensitive(sensitive)
.withExtra(extra);
}),
py::arg("volume") = 0, py::arg("boundary") = 0,
py::arg("layer") = 0, py::arg("approach") = 0,
py::arg("sensitive") = 0, py::arg("extra") = 0)
.def_property(
"layer", &GeometryIdentifier::layer,
[](GeometryIdentifier& self, GeometryIdentifier::Value value) {
self = self.withLayer(value);
})
.def_property(
"volume", &GeometryIdentifier::volume,
[](GeometryIdentifier& self, GeometryIdentifier::Value value) {
self = self.withVolume(value);
})
.def_property(
"boundary", &GeometryIdentifier::boundary,
[](GeometryIdentifier& self, GeometryIdentifier::Value value) {
self = self.withBoundary(value);
})
.def_property(
"approach", &GeometryIdentifier::approach,
[](GeometryIdentifier& self, GeometryIdentifier::Value value) {
self = self.withApproach(value);
})
.def_property(
"sensitive", &GeometryIdentifier::sensitive,
[](GeometryIdentifier& self, GeometryIdentifier::Value value) {
self = self.withSensitive(value);
})
.def_property(
"extra", &GeometryIdentifier::extra,
[](GeometryIdentifier& self, GeometryIdentifier::Value value) {
self = self.withExtra(value);
})
.def_property_readonly("value", &GeometryIdentifier::value)
.def("__str__", [](const GeometryIdentifier& self) {
std::stringstream ss;
ss << self;
return ss.str();
});
}
{
py::class_<SurfacePlacementBase, std::shared_ptr<SurfacePlacementBase>>(
m, "SurfacePlacementBase");
}
{
py::enum_<VolumeBounds::BoundsType>(m, "VolumeBoundsType")
.value("Cone", VolumeBounds::BoundsType::eCone)
.value("Cuboid", VolumeBounds::BoundsType::eCuboid)
.value("CutoutCylinder", VolumeBounds::BoundsType::eCutoutCylinder)
.value("Cylinder", VolumeBounds::BoundsType::eCylinder)
.value("GenericCuboid", VolumeBounds::BoundsType::eGenericCuboid)
.value("Trapezoid", VolumeBounds::BoundsType::eTrapezoid)
.value("Other", VolumeBounds::BoundsType::eOther);
}
{
auto trkGeo =
py::class_<TrackingGeometry, std::shared_ptr<TrackingGeometry>>(
m, "TrackingGeometry")
.def(py::init(
[](const MutableTrackingVolumePtr& volPtr,
const std::shared_ptr<const IMaterialDecorator>& matDec,
const GeometryIdentifierHook& hook,
Acts::Logging::Level level) {
auto logger =
Acts::getDefaultLogger("TrackingGeometry", level);
auto obj = std::make_shared<Acts::TrackingGeometry>(
volPtr, matDec.get(), hook, *logger);
return obj;
}))
.def("visitSurfaces",
[](TrackingGeometry& self, py::function& func) {
self.visitSurfaces(func);
})
.def("geoIdSurfaceMap", &TrackingGeometry::geoIdSurfaceMap)
.def("extractMaterialSurfaces",
[](TrackingGeometry& self) {
MaterialSurfaceSelector selector;
self.visitSurfaces(selector, false);
return selector.surfaces;
})
.def_property_readonly("highestTrackingVolume",
&TrackingGeometry::highestTrackingVolumePtr)
.def("visualize", &TrackingGeometry::visualize, py::arg("helper"),
py::arg("gctx"), py::arg("viewConfig") = s_viewVolume,
py::arg("portalViewConfig") = s_viewPortal,
py::arg("sensitiveViewConfig") = s_viewSensitive);
using apply_ptr_t =
void (TrackingGeometry::*)(TrackingGeometryMutableVisitor&);
trkGeo.def("apply", static_cast<apply_ptr_t>(&TrackingGeometry::apply));
}
{
py::class_<VolumeBounds, std::shared_ptr<VolumeBounds>>(m, "VolumeBounds")
.def("type", &VolumeBounds::type)
.def("__str__", [](const VolumeBounds& self) {
std::stringstream ss;
ss << self;
return ss.str();
});
auto cvb =
py::class_<CylinderVolumeBounds, std::shared_ptr<CylinderVolumeBounds>,
VolumeBounds>(m, "CylinderVolumeBounds")
.def(py::init<double, double, double, double, double, double,
double>(),
"rmin"_a, "rmax"_a, "halfz"_a, "halfphi"_a = std::numbers::pi,
"avgphi"_a = 0., "bevelMinZ"_a = 0., "bevelMaxZ"_a = 0.);
py::enum_<CylinderVolumeBounds::Face>(cvb, "Face")
.value("PositiveDisc", CylinderVolumeBounds::Face::PositiveDisc)
.value("NegativeDisc", CylinderVolumeBounds::Face::NegativeDisc)
.value("OuterCylinder", CylinderVolumeBounds::Face::OuterCylinder)
.value("InnerCylinder", CylinderVolumeBounds::Face::InnerCylinder)
.value("NegativePhiPlane", CylinderVolumeBounds::Face::NegativePhiPlane)
.value("PositivePhiPlane",
CylinderVolumeBounds::Face::PositivePhiPlane);
}
{
py::class_<Volume, std::shared_ptr<Volume>>(m, "Volume")
.def_property_readonly(
"volumeBounds",
py::overload_cast<>(&Volume::volumeBounds, py::const_),
py::return_value_policy::reference_internal);
py::class_<TrackingVolume, Volume, std::shared_ptr<TrackingVolume>>(
m, "TrackingVolume")
.def(py::init<const Transform3&, std::shared_ptr<VolumeBounds>,
std::string>())
.def_property_readonly("volumeName", &TrackingVolume::volumeName);
}
{
py::class_<GeometryIdentifierHook, std::shared_ptr<GeometryIdentifierHook>>(
m, "GeometryIdentifierHook")
.def(py::init([](py::object callable) {
auto hook = std::make_shared<GeometryIdentifierHookBinding>();
hook->callable = std::move(callable);
return hook;
}));
}
{
py::class_<TrackingGeometryMutableVisitor, PyTrackingGeometryVisitor,
std::shared_ptr<TrackingGeometryMutableVisitor>>(
m, "TrackingGeometryMutableVisitor")
.def(py::init<>());
}
py::class_<ExtentEnvelope>(m, "ExtentEnvelope")
.def(py::init<>())
.def(py::init<const Envelope&>())
.def(py::init([](Envelope x, Envelope y, Envelope z, Envelope r,
Envelope phi, Envelope rPhi, Envelope theta,
Envelope eta, Envelope mag) {
return ExtentEnvelope({.x = x,
.y = y,
.z = z,
.r = r,
.phi = phi,
.rPhi = rPhi,
.theta = theta,
.eta = eta,
.mag = mag});
}),
py::arg("x") = zeroEnvelope, py::arg("y") = zeroEnvelope,
py::arg("z") = zeroEnvelope, py::arg("r") = zeroEnvelope,
py::arg("phi") = zeroEnvelope, py::arg("rPhi") = zeroEnvelope,
py::arg("theta") = zeroEnvelope, py::arg("eta") = zeroEnvelope,
py::arg("mag") = zeroEnvelope)
.def_static("Zero", &ExtentEnvelope::Zero)
.def("__getitem__", [](ExtentEnvelope& self,
AxisDirection bValue) { return self[bValue]; })
.def("__setitem__", [](ExtentEnvelope& self, AxisDirection bValue,
const Envelope& value) { self[bValue] = value; })
.def("__str__", [](const ExtentEnvelope& self) {
std::array<std::string, numAxisDirections()> values;
std::stringstream ss;
for (AxisDirection val : allAxisDirections()) {
ss << val << "=(" << self[val][0] << ", " << self[val][1] << ")";
values.at(toUnderlying(val)) = ss.str();
ss.str("");
}
ss.str("");
ss << "ExtentEnvelope(";
ss << boost::algorithm::join(values, ", ");
ss << ")";
return ss.str();
});
py::class_<Extent>(m, "Extent")
.def(py::init<const ExtentEnvelope&>(),
py::arg("envelope") = ExtentEnvelope::Zero())
.def("range",
[](const Extent& self, AxisDirection bval) -> std::array<double, 2> {
return {self.min(bval), self.max(bval)};
})
.def("setRange",
[](Extent& self, AxisDirection bval,
const std::array<double, 2>& range) {
self.set(bval, range[0], range[1]);
})
.def("__str__", &Extent::toString);
{
py::enum_<VolumeAttachmentStrategy>(m, "VolumeAttachmentStrategy")
.value("Gap", VolumeAttachmentStrategy::Gap)
.value("First", VolumeAttachmentStrategy::First)
.value("Second", VolumeAttachmentStrategy::Second)
.value("Midpoint", VolumeAttachmentStrategy::Midpoint);
py::enum_<VolumeResizeStrategy>(m, "VolumeResizeStrategy")
.value("Gap", VolumeResizeStrategy::Gap)
.value("Expand", VolumeResizeStrategy::Expand);
}
py::class_<PortalShellBase>(m, "PortalShellBase");
py::class_<ProtoLayer>(m, "ProtoLayer")
.def(py::init<const GeometryContext&,
const std::vector<std::shared_ptr<Surface>>&,
const Transform3&>(),
"gctx"_a, "surfaces"_a, "transform"_a = Transform3::Identity())
.def("min", &ProtoLayer::min, "bval"_a, "addenv"_a = true)
.def("max", &ProtoLayer::max, "bval"_a, "addenv"_a = true)
.def_property_readonly("surfaces", &ProtoLayer::surfaces);
}
} // namespace ActsPython