Project
Loading...
Searching...
No Matches
TGeoGeometryUtils.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
17#include <TGeoShape.h>
18#include <TGeoTessellated.h>
19#include <TBuffer3D.h>
20#include <vector>
21
22namespace o2
23{
24namespace base
25{
26
27namespace
28{
29// some helpers to interpret TGeo TBuffer3D output
30// and convert it to surface triangles (reengineered from TGeo code)
31
32std::vector<int> BuildVertexLoop(const TBuffer3D& buf,
33 const std::vector<int>& segs)
34{
35 // adjacency list
36 std::unordered_map<int, std::vector<int>> adj;
37
38 for (int s : segs) {
39 int a = buf.fSegs[3 * s + 1];
40 int b = buf.fSegs[3 * s + 2];
41 adj[a].push_back(b);
42 adj[b].push_back(a);
43 }
44
45 // start from any vertex
46 int start = adj.begin()->first;
47 int prev = -1;
48 int curr = start;
49
50 std::vector<int> loop;
51
52 while (true) {
53 loop.push_back(curr);
54
55 const auto& nbrs = adj[curr];
56 int next = -1;
57
58 for (int n : nbrs) {
59 if (n != prev) {
60 next = n;
61 break;
62 }
63 }
64
65 if (next == -1 || next == start) {
66 break;
67 }
68
69 prev = curr;
70 curr = next;
71 }
72 return loop;
73}
74
75std::vector<std::vector<int>> ExtractPolygons(const TBuffer3D& buf)
76{
77 std::vector<std::vector<int>> polys;
78 Int_t idx = 0;
79
80 for (Int_t ip = 0; ip < buf.NbPols(); ++ip) {
81
82 idx++; // color
83 Int_t nseg = buf.fPols[idx++];
84
85 std::vector<int> segs(nseg);
86 for (Int_t i = 0; i < nseg; ++i) {
87 segs[i] = buf.fPols[idx++];
88 }
89
90 auto verts = BuildVertexLoop(buf, segs);
91 if (verts.size() >= 3) {
92 polys.push_back(std::move(verts));
93 }
94 }
95
96 return polys;
97}
98
99std::vector<std::array<int, 3>>
100 Triangulate(const std::vector<std::vector<int>>& polys)
101{
102 std::vector<std::array<int, 3>> tris;
103 for (const auto& poly : polys) {
104 int nv = poly.size();
105 if (nv < 3) {
106 continue;
107 }
108
109 int v0 = poly[0];
110 for (int i = 1; i < nv - 1; ++i) {
111 tris.push_back({{v0, poly[i], poly[i + 1]}});
112 }
113 }
114 return tris;
115}
116
117TGeoTessellated* MakeTessellated(const TBuffer3D& buf)
118{
119 auto polys = ExtractPolygons(buf);
120 auto tris = Triangulate(polys);
121 int i = 0;
122 auto* tess = new TGeoTessellated("tess");
123 const Double_t* p = buf.fPnts;
124 for (auto& t : tris) {
125 tess->AddFacet(
126 TGeoTessellated::Vertex_t{p[3 * t[0]], p[3 * t[0] + 1], p[3 * t[0] + 2]},
127 TGeoTessellated::Vertex_t{p[3 * t[1]], p[3 * t[1] + 1], p[3 * t[1] + 2]},
128 TGeoTessellated::Vertex_t{p[3 * t[2]], p[3 * t[2] + 1], p[3 * t[2] + 2]});
129 }
130 tess->CloseShape();
131 return tess;
132}
133} // end anonymous namespace
134
136TGeoTessellated* TGeoGeometryUtils::TGeoShapeToTGeoTessellated(TGeoShape const* shape)
137{
138 auto& buf = shape->GetBuffer3D(TBuffer3D::kRawSizes | TBuffer3D::kRaw | TBuffer3D::kCore, false);
139 auto tes = MakeTessellated(buf);
140 return tes;
141}
142
143} // namespace base
144} // namespace o2
int32_t i
Collection of utility functions for TGeo.
static TGeoTessellated * TGeoShapeToTGeoTessellated(TGeoShape const *)
< Transform any (primitive) TGeoShape to a tessellated representation
GLdouble n
Definition glcorearb.h:1982
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLfloat v0
Definition glcorearb.h:811
GLuint start
Definition glcorearb.h:469
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition glcorearb.h:2514
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...