21 #include "GeoModelKernel/GeoShapeIntersection.h"
22 #include "GeoModelKernel/GeoShapeShift.h"
23 #include "GeoModelKernel/GeoShapeSubtraction.h"
24 #include "GeoModelKernel/GeoShapeUnion.h"
25 #include "GeoModelKernel/GeoTrd.h"
35 return std::make_unique<Amg::Transform3D>(
trf).release();
43 bool simplify,
bool blend,
44 double blendMassLimit)
const {
46 const std::string
name = gv->getLogVol()->getName();
50 gv->getLogVol()->getShape(),
ident)};
64 double volumeFromGeoShape =
72 double volume = volumeFromGeoShape >= 0 ? volumeFromGeoShape : -1.;
73 if ((simplify || blend) && volumeFromGeoShape < 0) {
88 if (constituents.size() == 1 && !constituents[0].second) {
89 return std::make_unique<TrackingVolume>(*volGeo,
mat,
nullptr,
nullptr,
93 std::unique_ptr<Volume> envelope{};
94 std::string envName =
name;
96 std::unique_ptr<TrackingVolume> trEnv{};
100 if (constituents.size() == 1) {
102 envelope = std::make_unique<Volume>(*(constituents.front().first),
103 volGeo->transform());
106 if (blend && volume > 0 && volEnv > 0 &&
107 volume *
mat.rho < blendMassLimit)
110 if ((simplify || blended) && volume > 0 && volEnv > 0) {
112 double fraction = volume / volEnv;
114 mat.Z, fraction *
mat.rho);
115 if (blend && !blended)
116 envName = envName +
"_PERM";
117 trEnv = std::make_unique<TrackingVolume>(*envelope, matScaled,
118 nullptr,
nullptr, envName);
120 auto confinedVols =std::make_unique<std::vector<TrackingVolume*>>();
121 confinedVols->push_back(std::make_unique<TrackingVolume>(*volGeo,
mat,
nullptr,
nullptr,
name).release());
122 envName =
name +
"_envelope";
123 trEnv = std::make_unique<TrackingVolume>(*envelope, dummyMaterial, confinedVols.release(), envName);
131 std::unique_ptr<VolumeSpan>
span =
135 for (
const auto& fv : constituents) {
138 &(fv.first->volumeBounds()));
146 <<
"envelope estimate: object contains cylinder:"
147 <<
name <<
":" << isCyl);
149 <<
"complex volume span for envelope:" <<
name
150 <<
":x range:" << (*span).xMin <<
","
153 <<
"complex volume span for envelope:" <<
name
154 <<
":y range:" << (*span).yMin <<
","
157 <<
"complex volume span for envelope:" <<
name
158 <<
":z range:" << (*span).zMin <<
","
161 <<
"complex volume span for envelope:" <<
name
162 <<
":R range:" << (*span).rMin <<
","
165 <<
"complex volume span for envelope:" <<
name
166 <<
":phi range:" << (*span).phiMin <<
","
172 0.5 * ((*span).yMin + (*span).yMax),
173 0.5 * ((*span).zMin + (*span).zMax)}};
175 std::unique_ptr<VolumeBounds> bounds =
176 std::make_unique<CuboidVolumeBounds>(
177 0.5 * ((*span).xMax - (*span).xMin),
178 0.5 * ((*span).yMax - (*span).yMin),
179 0.5 * ((*span).zMax - (*span).zMin));
180 envelope = std::make_unique<Volume>(
183 double dPhi = (*span).phiMin > (*span).phiMax
184 ? (*span).phiMax - (*span).phiMin + 2 *
M_PI
185 : (*span).phiMax - (*span).phiMin;
186 std::unique_ptr<VolumeBounds> cylBounds{};
189 double aPhi = 0.5 * ((*span).phiMax + (*span).phiMin);
190 cylBounds = std::make_unique<CylinderVolumeBounds>(
191 (*span).rMin, (*span).rMax, 0.5 *
dPhi,
192 0.5 * ((*span).zMax - (*span).zMin));
195 cylBounds = std::make_unique<CylinderVolumeBounds>(
196 (*span).rMin, (*span).rMax,
197 0.5 * ((*span).zMax - (*span).zMin));
199 envelope = std::make_unique<Volume>(
205 if (blend && volume > 0 && volEnv > 0 && volume *
mat.rho < blendMassLimit)
208 if ((simplify || blended) && volume > 0 && volEnv > 0) {
209 double fraction = volume / volEnv;
212 if (blend && !blended)
213 envName = envName +
"_PERM";
214 trEnv = std::make_unique<TrackingVolume>(*envelope,
mat,
nullptr,
217 auto confinedVols = std::make_unique<std::vector<TrackingVolume*>>();
218 confinedVols->push_back( std::make_unique<TrackingVolume>(*volGeo,
mat,
nullptr,
nullptr,
name).release());
219 envName = envName +
"_envelope";
220 trEnv = std::make_unique<TrackingVolume>(*envelope, dummyMaterial, confinedVols.release(), envName);
231 inputVol.
parts.push_back(std::make_unique<Volume>(trVol));
232 constituents.push_back(std::move(inputVol));
237 while (sIter != constituents.end()) {
239 for (
unsigned int ii = 0; ii < (*sIter).parts.size(); ++ii) {
240 const VolumeBounds& bounds{((*sIter).parts[ii]->volumeBounds())};
246 (*sIter).parts[ii].reset(comb->
first()->
clone());
248 constituents.push_back(vp);
250 constituents.push_back(vp);
251 constituents.back().parts.emplace_back(comb->
second()->
clone());
252 constituents.back().sign = -1. * constituents.back().sign;
256 (*sIter).parts[ii].reset(sub->
outer()->
clone());
260 volume += -1. * (*sIter).sign * volSub;
262 constituents.emplace_back(*sIter);
263 constituents.back().parts.emplace_back(
265 constituents.back().sign = -1. * constituents.back().sign;
273 sIter=constituents.erase(sIter);
280 sIter = constituents.begin();
281 else if ((*sIter).parts.size() == 1) {
283 volume += (*sIter).sign * volSingle;
284 sIter=constituents.erase(sIter);
287 (*sIter).parts.begin();
288 bool noovrlp =
false;
289 while (
tit + 1 != (*sIter).parts.end()) {
290 std::pair<bool, std::unique_ptr<Volume>> overlap =
292 if (overlap.first && !overlap.second) {
293 sIter=constituents.erase(sIter);
297 else if (overlap.first && overlap.second) {
298 (*sIter).parts.erase(
tit,
tit + 2);
299 (*sIter).parts.push_back(std::move(overlap.second));
300 tit = (*sIter).parts.begin();
303 sIter=constituents.erase(sIter);
308 sIter=constituents.erase(sIter);
312 std::pair<bool, std::unique_ptr<Volume>> overlap =
316 if (overlap.second) {
317 (*sIter).parts.erase(
tit,
tit + 2);
318 (*sIter).parts.push_back(std::move(overlap.second));
319 tit = (*sIter).parts.begin();
321 sIter=constituents.erase(sIter);
330 }
else if ((*sIter).parts.size() == 1) {
332 volume += (*sIter).sign * volSingle;
333 sIter=constituents.erase(sIter);
340 if (!constituents.empty()) {
342 << constituents.size() <<
" items "
343 <<
":volume estimate:" << volume);
352 constituents.emplace_back(std::make_unique<Volume>(trVol),
nullptr);
354 std::unique_ptr<VolumeBounds> newBounds{};
355 while (sIter != constituents.end()) {
359 &((*sIter).first->volumeBounds()));
362 &((*sIter).first->volumeBounds()));
365 std::shared_ptr<Volume> subVol = (*sIter).second;
366 sIter = constituents.erase(sIter);
367 std::shared_ptr<Volume> combFirst{comb->
first()->
clone()};
368 std::shared_ptr<Volume> combSecond{comb->
second()->
clone()};
370 newBounds = std::make_unique<Trk::SubtractedVolumeBounds>(
371 combFirst->clone(), combSecond->clone());
372 std::unique_ptr<Trk::Volume> newSubVol =
373 std::make_unique<Volume>(
nullptr, newBounds.release());
375 newBounds = std::make_unique<CombinedVolumeBounds>(
376 subVol->clone(), newSubVol.release(),
false);
377 std::shared_ptr<Volume> newCSubVol =
378 std::make_unique<Volume>(
nullptr, newBounds.release());
379 constituents.insert(sIter,
380 std::make_pair(combFirst, newCSubVol));
383 sIter, std::make_pair(combFirst, std::move(newSubVol)));
386 constituents.insert(sIter, std::make_pair(combFirst, subVol));
388 newBounds = std::make_unique<CombinedVolumeBounds>(
389 subVol->clone(), combFirst->clone(),
false);
390 std::unique_ptr<Trk::Volume> newSubVol =
391 std::make_unique<Volume>(
nullptr, newBounds.release());
394 std::make_pair(combSecond, std::move(newSubVol)));
396 constituents.insert(sIter,
397 std::make_pair(combSecond, combFirst));
400 sIter = constituents.begin();
402 std::shared_ptr<Volume> subVol = (*sIter).second;
403 sIter = constituents.erase(sIter);
404 std::shared_ptr<Volume> innerVol{sub->
inner()->
clone()};
405 std::shared_ptr<Volume> outerVol{sub->
outer()->
clone()};
407 newBounds = std::make_unique<CombinedVolumeBounds>(
408 subVol->clone(), innerVol->clone(),
false);
409 std::unique_ptr<Volume> newSubVol =
410 std::make_unique<Trk::Volume>(
nullptr, newBounds.release());
412 sIter, std::make_pair(outerVol, std::move(newSubVol)));
414 constituents.insert(sIter, std::make_pair(outerVol, innerVol));
416 sIter = constituents.begin();
426 double zTol,
double phiTol)
const {
472 if ((*s1).phiMin < (*s1).phiMax && (*s2).phiMin < (*s2).phiMax) {
475 }
else if ((*s1).phiMin < (*s1).phiMax && (*s2).phiMin > (*s2).phiMax) {
476 if ((*s1).phiMin > (*s2).phiMax) {
478 scomb.
phiMax = (*s2).phiMax;
479 }
else if ((*s1).phiMax < (*s2).phiMin) {
480 scomb.
phiMin = (*s2).phiMin;
486 }
else if ((*s1).phiMin > (*s1).phiMax && (*s2).phiMin < (*s2).phiMax) {
487 if ((*s2).phiMin > (*s1).phiMax) {
489 scomb.
phiMax = (*s1).phiMax;
490 }
else if ((*s2).phiMax < (*s1).phiMin) {
491 scomb.
phiMin = (*s1).phiMin;
501 return std::make_unique<VolumeSpan>(scomb);
505 double minZ{1.e6}, maxZ{-1.e6}, minPhi{2 *
M_PI}, maxPhi{0.}, minR{1.e6},
506 maxR{0.}, minX{1.e6}, maxX{-1.e6}, minY{1.e6}, maxY{-1.e6};
509 std::vector<Amg::Vector3D> vtx;
510 std::vector<std::pair<int, int>> edges;
530 edges.emplace_back(0, 1);
531 edges.emplace_back(0, 2);
532 edges.emplace_back(1, 3);
533 edges.emplace_back(2, 3);
534 edges.emplace_back(4, 5);
535 edges.emplace_back(4, 6);
536 edges.emplace_back(5, 7);
537 edges.emplace_back(6, 7);
538 edges.emplace_back(0, 4);
539 edges.emplace_back(1, 5);
540 edges.emplace_back(2, 6);
541 edges.emplace_back(3, 7);
560 edges.emplace_back(0, 1);
561 edges.emplace_back(0, 2);
562 edges.emplace_back(1, 3);
563 edges.emplace_back(2, 3);
564 edges.emplace_back(4, 5);
565 edges.emplace_back(4, 6);
566 edges.emplace_back(5, 7);
567 edges.emplace_back(6, 7);
568 edges.emplace_back(0, 4);
569 edges.emplace_back(1, 5);
570 edges.emplace_back(2, 6);
571 edges.emplace_back(3, 7);
594 edges.emplace_back(0, 1);
595 edges.emplace_back(0, 2);
596 edges.emplace_back(1, 3);
597 edges.emplace_back(2, 4);
598 edges.emplace_back(3, 5);
599 edges.emplace_back(4, 5);
600 edges.emplace_back(6, 7);
601 edges.emplace_back(6, 8);
602 edges.emplace_back(7, 9);
603 edges.emplace_back(8, 10);
604 edges.emplace_back(9, 11);
605 edges.emplace_back(10, 11);
606 edges.emplace_back(0, 6);
607 edges.emplace_back(1, 7);
608 edges.emplace_back(2, 8);
609 edges.emplace_back(3, 9);
610 edges.emplace_back(4, 10);
611 edges.emplace_back(5, 11);
617 edges.emplace_back(0, 1);
648 edges.emplace_back(2, 3);
649 edges.emplace_back(4, 5);
650 edges.emplace_back(6, 7);
651 edges.emplace_back(8, 9);
652 if (bcyl->
type() == 1 || bcyl->
type() == 3) {
653 edges.emplace_back(3, 5);
654 edges.emplace_back(7, 9);
656 if (bcyl->
type() == 2 || bcyl->
type() == 3) {
657 edges.emplace_back(2, 4);
658 edges.emplace_back(6, 8);
666 edges.emplace_back(0, 1);
691 edges.emplace_back(2, 3);
692 edges.emplace_back(4, 5);
693 edges.emplace_back(6, 7);
694 edges.emplace_back(8, 9);
699 const std::vector<std::pair<double, double>> vtcs = spb->
xyVertices();
700 for (
const auto& vtc : vtcs) {
701 vtx.emplace_back(vtc.first, vtc.second, spb->
halflengthZ());
702 vtx.emplace_back(vtc.first, vtc.second, -spb->
halflengthZ());
703 edges.emplace_back(vtx.size() - 2, vtx.size() - 1);
704 if (vtx.size() > 2) {
706 vtx.size() - 4, vtx.size() - 2);
708 vtx.size() - 3, vtx.size() - 1);
710 if (vtx.size() > 4) {
711 edges.emplace_back(vtx.size() - 2, 1);
712 edges.emplace_back(vtx.size() - 1, 0);
715 edges.emplace_back(0, vtx.size() - 2);
716 edges.emplace_back(1, vtx.size() - 1);
720 const std::vector<std::pair<double, double>> vtcs = prism->
xyVertices();
721 for (
const auto& vtc : vtcs) {
722 vtx.emplace_back(vtc.first, vtc.second, prism->
halflengthZ());
723 vtx.emplace_back(vtc.first, vtc.second, -prism->
halflengthZ());
724 edges.emplace_back(vtx.size() - 2, vtx.size() - 1);
725 if (vtx.size() > 2) {
727 vtx.size() - 4, vtx.size() - 2);
729 vtx.size() - 3, vtx.size() - 1);
732 edges.emplace_back(0, vtx.size() - 2);
733 edges.emplace_back(1, vtx.size() - 1);
736 std::vector<Amg::Vector3D> vtxt;
738 for (
unsigned int ie = 0;
ie < vtx.size();
ie++) {
743 double rad = gp.perp();
764 (vtxt[edges[0].first] - vtxt[edges[0].second]).
unit();
765 maxZ += ro *
sin(
dir.theta());
766 minZ += -ro *
sin(
dir.theta());
772 double le = (vtxt[0] - vtxt[1]).
norm();
773 if ((closest.
position - vtxt[0]).norm() < le &&
775 if (minR > closest.
position.perp() - ro)
779 if (phiClosest < minPhi || phiClosest > maxPhi) {
780 double phiTmp = minPhi;
785 minR =
std::max(0., minR - ro * std::abs(
dir.z()));
787 const double aTan = std::atan2(ro, minR);
792 if (maxPhi > 2 *
M_PI)
795 maxR += ro * std::abs(
cos(
dir.theta()));
806 for (
unsigned int ie = 0;
ie < edges.size();
ie++) {
808 (vtxt[edges[
ie].first] - vtxt[edges[
ie].second]).
unit();
812 (vtxt[edges[
ie].first] - vtxt[edges[
ie].second]).
norm();
813 if ((closest.
position - vtxt[edges[
ie].first]).norm() < le &&
819 if (vtxt.size() > 10) {
840 if (vtxt[10].
phi() +
M_PI < minPhi ||
841 vtxt[10].
phi() +
M_PI > maxPhi) {
845 for (
unsigned int iv = 2; iv < vtxt.size(); iv++) {
846 phiTmp = vtxt[iv].phi() +
M_PI;
849 minPhi = phiTmp < minPhi ? phiTmp : minPhi;
850 maxPhi = phiTmp > maxPhi ? phiTmp : maxPhi;
852 if (minPhi > 2 *
M_PI)
854 if (maxPhi > 2 *
M_PI)
862 if (minPhi >= maxPhi && (minPhi - maxPhi) <
M_PI) {
872 for (
unsigned int ie = 0;
ie < edges.size();
ie++) {
874 (vtxt[edges[
ie].first] - vtxt[edges[
ie].second]).
unit();
877 double le = (vtxt[edges[
ie].first] - vtxt[edges[
ie].second]).
norm();
878 if ((closest.
position - vtxt[edges[
ie].first]).norm() < le &&
884 if (std::abs(maxPhi - minPhi) >
M_PI) {
885 double phiTmp = minPhi;
888 for (
unsigned int iv = 0; iv < vtxt.size(); iv++) {
889 phiTmp = vtxt[iv].phi() +
M_PI;
892 minPhi = phiTmp < minPhi ? phiTmp : minPhi;
893 maxPhi = phiTmp > maxPhi ? phiTmp : maxPhi;
895 if (minPhi > 2 *
M_PI)
897 if (maxPhi > 2 *
M_PI)
899 if (minPhi >= maxPhi && (minPhi - maxPhi) <
M_PI) {
906 if (cyl || bcyl || box || trd || dtrd || spb || prism) {
907 span.zMin = minZ - zTol;
908 span.zMax = maxZ - +zTol;
909 minPhi = (minPhi - phiTol) < 0 ? minPhi - phiTol + 2 *
M_PI
911 span.phiMin = minPhi;
912 maxPhi = (maxPhi + phiTol) > 2 *
M_PI ? maxPhi + phiTol - 2 *
M_PI
914 span.phiMax = maxPhi;
916 span.rMax = maxR + zTol;
917 span.xMin = minX - zTol;
918 span.xMax = maxX - +zTol;
919 span.yMin = minY - zTol;
920 span.yMax = maxY - +zTol;
924 return std::make_unique<VolumeSpan>(
span);
928 double precision)
const {
977 std::vector<std::pair<double, double>>
v = prism->
xyVertices();
978 double vv =
v[0].first * (
v[1].second -
v.back().second);
979 for (
unsigned int i = 1;
i <
v.size() - 1;
i++) {
980 vv +=
v[
i].first * (
v[
i + 1].second -
v[
i - 1].second);
982 vv +=
v.back().first * (
v[0].second -
v[
v.size() - 2].second);
986 std::vector<std::pair<double, double>>
v = spb->
xyVertices();
987 double vv =
v[0].first * (
v[1].second -
v.back().second);
988 for (
unsigned int i = 1;
i <
v.size() - 1;
i++) {
989 vv +=
v[
i].first * (
v[
i + 1].second -
v[
i - 1].second);
991 vv +=
v.back().first * (
v[0].second -
v[
v.size() - 2].second);
1005 double precision)
const {
1010 if (sub.first && !sub.second)
1012 double fraction = -1.;
1014 std::pair<bool, std::unique_ptr<Volume>> overlap =
1017 if (overlap.first && !overlap.second)
1018 return fraction = 1.;
1019 else if (overlap.first && overlap.second) {
1029 if ((volA > 0 && volA < precision) || (volB > 0 && volB < precision))
1047 std::vector<MaterialComponent> materialContent;
1050 for (
const auto&
mat : materialContent) {
1053 double d =
sf > 0 ?
mat.second /
sf : 0.;
1056 (
mat.first.X0 > 0 ?
d /
mat.first.X0 : 0.));
1061 const GeoVPhysVol* gv,
1062 std::vector<MaterialComponent>& materialContent)
const {
1071 const GeoLogVol* lv = gv->getLogVol();
1074 double motherVolume = 0.;
1078 const GeoShape*
sh = lv->getShape();
1079 while (
sh &&
sh->type() ==
"Shift") {
1080 const GeoShapeShift* shift =
dynamic_cast<const GeoShapeShift*
>(
sh);
1081 sh = shift ? shift->getOp() :
nullptr;
1085 sh && (
sh->type() ==
"Subtraction" ||
sh->type() ==
"Union" ||
1086 sh->type() ==
"Intersection");
1090 std::unique_ptr<Volume> vol{
1094 if (motherVolume < 0) {
1098 motherVolume = lv->getShape()->volume();
1101 double childVol = 0;
1102 std::string cPrevious =
" ";
1103 size_t nIdentical = 0;
1104 std::vector<Trk::MaterialComponent> childMat;
1106 for (
const GeoVPhysVol* cv :
children) {
1107 std::string cname = cv->getLogVol()->getName();
1108 if (cname == cPrevious)
1112 for (
const auto& cmat : childMat) {
1113 materialContent.emplace_back(cmat.first, nIdentical * cmat.second);
1114 childVol += materialContent.back().second;
1121 for (
const auto& cmat : childMat) {
1122 materialContent.emplace_back(cmat.first, nIdentical * cmat.second);
1123 childVol += materialContent.back().second;
1125 if (motherVolume > 0 && childVol > 0)
1126 motherVolume += -1. * childVol;
1128 ATH_MSG_DEBUG(
"collected material:" << lv->getName() <<
":made of:"
1129 << lv->getMaterial()->getName()
1130 <<
":density(g/mm3)" <<
mat.rho
1131 <<
":mass:" <<
mat.rho * motherVolume);
1132 materialContent.emplace_back(
mat, motherVolume);
1137 if (
sh->type() ==
"Subtraction") {
1138 const GeoShapeSubtraction* sub =
1139 dynamic_cast<const GeoShapeSubtraction*
>(
sh);
1143 if (
sh->type() ==
"Union") {
1144 const GeoShapeUnion* uni =
dynamic_cast<const GeoShapeUnion*
>(
sh);
1148 if (
sh->type() ==
"Intersection") {
1149 const GeoShapeIntersection* intr =
1150 dynamic_cast<const GeoShapeIntersection*
>(
sh);
1155 if (
sh->type() ==
"Shift") {
1156 const GeoShapeShift* shift =
dynamic_cast<const GeoShapeShift*
>(
sh);
1161 return sh->volume();