Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REveGeoPolyShape.cxx
Go to the documentation of this file.
1// @(#)root/eve7:$Id$
2// Author: Matevz Tadel 2007, 2018
3
4/*************************************************************************
5 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "Rtypes.h"
13#include <cassert>
14
15
17#include <ROOT/REveGeoShape.hxx>
18#include <ROOT/REveUtil.hxx>
19#include <ROOT/REveGluTess.hxx>
21
22#include "TBuffer3D.h"
23#include "TBuffer3DTypes.h"
24#define ROOT_CsgOps_cxx
25#include "CsgOps.h"
26
27#include "TGeoBoolNode.h"
28#include "TGeoCompositeShape.h"
29#include "TGeoMatrix.h"
30
31using namespace ROOT::Experimental;
32
33/** \class REveGeoPolyShape
34\ingroup REve
35Description of REveGeoPolyShape
36*/
37
38Bool_t REveGeoPolyShape::fgAutoEnforceTriangles = kTRUE;
39Bool_t REveGeoPolyShape::fgAutoCalculateNormals = kFALSE;
40
41void REveGeoPolyShape::SetAutoEnforceTriangles(Bool_t f) { fgAutoEnforceTriangles = f; }
45
46////////////////////////////////////////////////////////////////////////
47/// Function produces mesh for provided shape, applying matrix to the result
48
49std::unique_ptr<RootCsg::TBaseMesh> MakeGeoMesh(TGeoMatrix *matr, TGeoShape *shape)
50{
51 TGeoCompositeShape *comp = dynamic_cast<TGeoCompositeShape *> (shape);
52
53 std::unique_ptr<RootCsg::TBaseMesh> res;
54
55 if (!comp) {
56 std::unique_ptr<TBuffer3D> b3d(shape->MakeBuffer3D());
57
58 if (matr) {
59 Double_t *v = b3d->fPnts;
60 Double_t buf[3];
61 for (UInt_t i = 0; i < b3d->NbPnts(); ++i) {
62 buf[0] = v[i*3];
63 buf[1] = v[i*3+1];
64 buf[2] = v[i*3+2];
65 matr->LocalToMaster(buf, &v[i*3]);
66 }
67 }
68
69 res.reset(RootCsg::ConvertToMesh(*b3d.get()));
70 } else {
71 auto node = comp->GetBoolNode();
72
73 TGeoHMatrix mleft, mright;
74 if (matr) { mleft = *matr; mright = *matr; }
75
76 mleft.Multiply(node->GetLeftMatrix());
77 auto left = MakeGeoMesh(&mleft, node->GetLeftShape());
78
79 mright.Multiply(node->GetRightMatrix());
80 auto right = MakeGeoMesh(&mright, node->GetRightShape());
81
82 if (node->IsA() == TGeoUnion::Class()) res.reset(RootCsg::BuildUnion(left.get(), right.get()));
83 if (node->IsA() == TGeoIntersection::Class()) res.reset(RootCsg::BuildIntersection(left.get(), right.get()));
84 if (node->IsA() == TGeoSubtraction::Class()) res.reset(RootCsg::BuildDifference(left.get(), right.get()));
85 }
86
87 return res;
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Produce all polygons from composite shape
92
94{
95 fOrigin[0] = cshape->GetOrigin()[0];
96 fOrigin[1] = cshape->GetOrigin()[1];
97 fOrigin[2] = cshape->GetOrigin()[2];
98 fDX = cshape->GetDX();
99 fDY = cshape->GetDY();
100 fDZ = cshape->GetDZ();
101
103
104 auto mesh = MakeGeoMesh(nullptr, cshape);
105
106 Int_t nv = mesh->NumberOfVertices();
107 fVertices.reserve(3 * nv);
108
109 for (Int_t i = 0; i < nv; ++i) {
110 auto v = mesh->GetVertex(i);
111 fVertices.insert(fVertices.end(), v, v + 3);
112 }
113
114 fNbPols = mesh->NumberOfPolys();
115
116 Int_t descSize = 0;
117
118 for (Int_t i = 0; i < fNbPols; ++i) descSize += mesh->SizeOfPoly(i) + 1;
119
120 fPolyDesc.reserve(descSize);
121
122 for (Int_t polyIndex = 0; polyIndex < fNbPols; ++polyIndex) {
123 Int_t polySize = mesh->SizeOfPoly(polyIndex);
124
125 fPolyDesc.push_back(polySize);
126
127 for (Int_t i = 0; i < polySize; ++i)
128 fPolyDesc.push_back(mesh->GetVertexIndex(polyIndex, i));
129 }
130
133}
134
135////////////////////////////////////////////////////////////////////////////////
136/// Produce all polygons from normal shape
137
139{
140 TGeoBBox *box = dynamic_cast<TGeoBBox *> (shape);
141
142 if (box) {
143 fOrigin[0] = box->GetOrigin()[0];
144 fOrigin[1] = box->GetOrigin()[1];
145 fOrigin[2] = box->GetOrigin()[2];
146 fDX = box->GetDX();
147 fDY = box->GetDY();
148 fDZ = box->GetDZ();
149 }
150
152
153 if (!shape->MakeBuffer3D())
154 {
155 std::cout << "Shape " << shape->GetName() << " has empty TBuffer3D\n";
156 return;
157 }
158
159 std::unique_ptr<TBuffer3D> b3d(shape->MakeBuffer3D());
160
161 SetFromBuff3D(*b3d.get());
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// Write the render data entries of the tessellated shape into rd:
166/// - `pos`, f32 x 3, role vertex: the vertices;
167/// - `nrm`, f32 x 3, role data: one normal per triangle, written only when
168/// CalculateNormals() made them, see SetAutoCalculateNormals(). These are
169/// face normals, not vertex normals, so they are not a vertex attribute;
170/// the client does not read them;
171/// - `idx`, u32 x 3, role index, primitive triangles: one item per triangle,
172/// its three indices into `pos`, so the entry's count is the triangle count.
173/// All polygons must be triangles, see SetAutoEnforceTriangles().
174
176{
177 using RD = REveRenderData;
178
179 auto &pos = rd.NewEntry("pos", RD::EType::kF32, 3).Role(RD::ERole::kVertex).Reserve(fVertices.size() / 3);
180 for (auto v : fVertices)
181 pos.Push((Float_t)v);
182
183 if (!fNormals.empty()) {
184 auto &nrm = rd.NewEntry("nrm", RD::EType::kF32, 3).Role(RD::ERole::kData).Reserve(fNormals.size() / 3);
185 for (auto n : fNormals)
186 nrm.Push((Float_t)n);
187 }
188
189 auto &idx =
190 rd.NewEntry("idx", RD::EType::kU32, 3).Role(RD::ERole::kIndex).Prim(RD::EPrim::kTriangles).Reserve(fNbPols);
191 for (Int_t i = 0, j = 0; i < fNbPols; ++i) {
192 assert(fPolyDesc[j] == 3);
193
194 idx.Push(&fPolyDesc[j + 1], 3);
195 j += 1 + fPolyDesc[j];
196 }
197}
198
199////////////////////////////////////////////////////////////////////////////////
200/// Set data-members from a Csg mesh.
201
202
204{
205 fNbPols = (Int_t) buffer.NbPols();
206
207 if (fNbPols == 0) return;
208
209 fVertices.insert(fVertices.end(), buffer.fPnts, buffer.fPnts + 3 * buffer.NbPnts());
210
211 Int_t *segs = buffer.fSegs;
212 Int_t *pols = buffer.fPols;
213
214 Int_t descSize = 0;
215
216 for (Int_t i = 0, j = 1; i < fNbPols; ++i, ++j)
217 {
218 descSize += pols[j] + 1;
219 j += pols[j] + 1;
220 }
221
222 fPolyDesc.resize(descSize);
223
224 for (Int_t numPol = 0, currInd = 0, j = 1; numPol < fNbPols; ++numPol)
225 {
226 Int_t segmentInd = pols[j] + j;
227 Int_t segmentCol = pols[j];
228 Int_t s1 = pols[segmentInd];
229 segmentInd--;
230 Int_t s2 = pols[segmentInd];
231 segmentInd--;
232 Int_t segEnds[] = {segs[s1 * 3 + 1], segs[s1 * 3 + 2],
233 segs[s2 * 3 + 1], segs[s2 * 3 + 2]};
234 Int_t numPnts[3];
235
236 if (segEnds[0] == segEnds[2]) {
237 numPnts[0] = segEnds[1]; numPnts[1] = segEnds[0]; numPnts[2] = segEnds[3];
238 } else if (segEnds[0] == segEnds[3]) {
239 numPnts[0] = segEnds[1]; numPnts[1] = segEnds[0]; numPnts[2] = segEnds[2];
240 } else if (segEnds[1] == segEnds[2]) {
241 numPnts[0] = segEnds[0]; numPnts[1] = segEnds[1]; numPnts[2] = segEnds[3];
242 } else {
243 numPnts[0] = segEnds[0]; numPnts[1] = segEnds[1]; numPnts[2] = segEnds[2];
244 }
245
246 fPolyDesc[currInd] = 3;
247 Int_t sizeInd = currInd++;
248 fPolyDesc[currInd++] = numPnts[0];
249 fPolyDesc[currInd++] = numPnts[1];
250 fPolyDesc[currInd++] = numPnts[2];
251 Int_t lastAdded = numPnts[2];
252
253 Int_t end = j + 1;
254 for (; segmentInd != end; segmentInd--) {
255 segEnds[0] = segs[pols[segmentInd] * 3 + 1];
256 segEnds[1] = segs[pols[segmentInd] * 3 + 2];
257 if (segEnds[0] == lastAdded) {
258 fPolyDesc[currInd++] = segEnds[1];
259 lastAdded = segEnds[1];
260 } else {
261 fPolyDesc[currInd++] = segEnds[0];
262 lastAdded = segEnds[0];
263 }
264 ++fPolyDesc[sizeInd];
265 }
266 j += segmentCol + 2;
267 }
268
271}
272
273////////////////////////////////////////////////////////////////////////////////
274/// Use GLU tesselator to replace all polygons with N > 3 with triangles.
275/// After this call polygon descriptions are changed.
276/// New vertices are not expected -- exception is thrown if this is
277/// requested by the triangulator. Support for adding of new vertices can be
278/// provided.
279
289
290////////////////////////////////////////////////////////////////////////////////
291/// CalculateNormals per polygon (flat shading)
292
294{
295 fNormals.resize(3 * fNbPols);
296 if (fNbPols == 0) return;
297 Double_t *pnts = &fVertices[0];
298 for (Int_t i = 0, j = 0; i < fNbPols; ++i)
299 {
300 Int_t polEnd = fPolyDesc[j] + j + 1;
301 UInt_t norm[] = {fPolyDesc[j + 1], fPolyDesc[j + 2], fPolyDesc[j + 3]};
302 j += 4;
303 Int_t check = CheckPoints(norm, norm);
304 Int_t ngood = check;
305 if (check == 3) {
306 TMath::Normal2Plane(pnts + norm[0] * 3, pnts + norm[1] * 3,
307 pnts + norm[2] * 3, &fNormals[i * 3]);
308 j = polEnd;
309 continue;
310 }
311 while (j < polEnd)
312 {
313 norm[ngood++] = fPolyDesc[j++];
314 if (ngood == 3) {
315 ngood = CheckPoints(norm, norm);
316 if (ngood == 3) {
317 TMath::Normal2Plane(pnts + norm[0] * 3, pnts + norm[1] * 3,
318 pnts + norm[2] * 3, &fNormals[i * 3]);
319 j = polEnd;
320 break;
321 }
322 }
323 }
324 }
325}
326
327////////////////////////////////////////////////////////////////////////////////
328/// CheckPoints
329
331{
332 const Double_t * p1 = &fVertices[source[0] * 3];
333 const Double_t * p2 = &fVertices[source[1] * 3];
334 const Double_t * p3 = &fVertices[source[2] * 3];
335 Int_t retVal = 1;
336
337 if (Eq(p1, p2)) {
338 dest[0] = source[0];
339 if (!Eq(p1, p3) ) {
340 dest[1] = source[2];
341 retVal = 2;
342 }
343 } else if (Eq(p1, p3)) {
344 dest[0] = source[0];
345 dest[1] = source[1];
346 retVal = 2;
347 } else {
348 dest[0] = source[0];
349 dest[1] = source[1];
350 retVal = 2;
351 if (!Eq(p2, p3)) {
352 dest[2] = source[2];
353 retVal = 3;
354 }
355 }
356
357 return retVal;
358}
359
360////////////////////////////////////////////////////////////////////////////////
361/// Test equality of points with epsilon 1e-10.
362
364{
365 Double_t dx = TMath::Abs(p1[0] - p2[0]);
366 Double_t dy = TMath::Abs(p1[1] - p2[1]);
367 Double_t dz = TMath::Abs(p1[2] - p2[2]);
368 return (dx < 1e-10) && (dy < 1e-10) && (dz < 1e-10);
369}
370
371////////////////////////////////////////////////////////////////////////////////
372/// Fill the passed buffer 3D.
373
375{
376 if (reqSections & TBuffer3D::kCore)
377 {
378 // If writing core section all others will be invalid
379 b.ClearSectionsValid();
380
381 b.fID = const_cast<REveGeoPolyShape*>(this);
382 b.fColor = kMagenta;
383 b.fTransparency = 0;
384 b.fLocalFrame = kFALSE;
385 b.fReflection = kTRUE;
386
387 b.SetSectionsValid(TBuffer3D::kCore);
388 }
389
390 if ((reqSections & TBuffer3D::kRawSizes) || (reqSections & TBuffer3D::kRaw))
391 {
392 Int_t nvrt = fVertices.size() / 3;
393 Int_t nseg = 0;
394
395 std::map<Edge_t, Int_t> edges;
396
397 const UInt_t *pd = &fPolyDesc[0];
398 for (Int_t i = 0; i < fNbPols; ++i) {
399 Int_t nv = pd[0];
400 ++pd;
401 for (Int_t j = 0; j < nv; ++j) {
402 Edge_t e(pd[j], (j != nv - 1) ? pd[j + 1] : pd[0]);
403 if (edges.find(e) == edges.end()) {
404 edges.insert(std::make_pair(e, 0));
405 ++nseg;
406 }
407 }
408 pd += nv;
409 }
410
411 b.SetRawSizes(nvrt, 3*nvrt, nseg, 3*nseg, fNbPols, fNbPols+fPolyDesc.size());
412
413 memcpy(b.fPnts, &fVertices[0], sizeof(Double_t)*fVertices.size());
414
415 Int_t si = 0, scnt = 0;
416 for (auto &edge : edges) {
417 b.fSegs[si++] = 0;
418 b.fSegs[si++] = edge.first.fI;
419 b.fSegs[si++] = edge.first.fJ;
420 edge.second = scnt++;
421 }
422
423 Int_t pi = 0;
424 pd = &fPolyDesc[0];
425 for (Int_t i = 0; i < fNbPols; ++i) {
426 Int_t nv = pd[0];
427 ++pd;
428 b.fPols[pi++] = 0;
429 b.fPols[pi++] = nv;
430 for (Int_t j = 0; j < nv; ++j) {
431 b.fPols[pi++] = edges[Edge_t(pd[j], (j != nv - 1) ? pd[j + 1] : pd[0])];
432 }
433 pd += nv;
434 }
435
436 b.SetSectionsValid(TBuffer3D::kRawSizes | TBuffer3D::kRaw);
437 }
438}
439
440////////////////////////////////////////////////////////////////////////////////
441/// Fill static buffer 3D.
442
443const TBuffer3D& REveGeoPolyShape::GetBuffer3D(Int_t reqSections, Bool_t localFrame) const
444{
446
447 FillBuffer3D(buf, reqSections, localFrame);
448
449 return buf;
450}
451
452////////////////////////////////////////////////////////////////////////////////
453/// Create buffer 3D and fill it with point/segment/poly data.
454
std::unique_ptr< RootCsg::TBaseMesh > MakeGeoMesh(TGeoMatrix *matr, TGeoShape *shape)
Function produces mesh for provided shape, applying matrix to the result.
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define s1(x)
Definition RSha256.hxx:91
#define e(i)
Definition RSha256.hxx:103
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
@ kMagenta
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t dest
void ProcessData(const std::vector< Double_t > &verts, const std::vector< UInt_t > &polys, const Int_t n_polys)
ProcessData.
REveGeoManagerHolder Exception-safe global variable holders.
Definition REveUtil.hxx:95
void FillRenderData(REveRenderData &rd)
Write the render data entries of the tessellated shape into rd:
const TBuffer3D & GetBuffer3D(Int_t reqSections, Bool_t localFrame) const override
Fill static buffer 3D.
static void SetAutoCalculateNormals(Bool_t f)
void EnforceTriangles()
Use GLU tesselator to replace all polygons with N > 3 with triangles.
void BuildFromShape(TGeoShape *shape, Int_t n_seg=60)
Produce all polygons from normal shape.
void BuildFromComposite(TGeoCompositeShape *cshp, Int_t n_seg=60)
Produce all polygons from composite shape.
void FillBuffer3D(TBuffer3D &buffer, Int_t reqSections, Bool_t localFrame) const override
Fill the passed buffer 3D.
void CalculateNormals()
CalculateNormals per polygon (flat shading)
TBuffer3D * MakeBuffer3D() const override
Create buffer 3D and fill it with point/segment/poly data.
Int_t CheckPoints(const UInt_t *source, UInt_t *dest) const
CheckPoints.
void SetFromBuff3D(const TBuffer3D &buffer)
Set data-members from a Csg mesh.
static Bool_t Eq(const Double_t *p1, const Double_t *p2)
Test equality of points with epsilon 1e-10.
static TGeoManager * GetGeoManager()
Return static geo-manager that is used internally to make shapes lead a happy life.
Entry & Reserve(size_t n_items)
Reserve storage for n_items items of the item size.
Entry & Push(T v)
Append one element; T must be the entry's type.
Render data of an element: a client maker name and a list of tagged entries, each a typed array with ...
Entry & NewEntry(std::string_view tag, EType type=EType::kF32, int item_size=1)
Create an entry. The reference stays valid for the life of this object.
Generic 3D primitive description class.
Definition TBuffer3D.h:18
Int_t * fPols
Definition TBuffer3D.h:124
UInt_t NbPols() const
Definition TBuffer3D.h:91
UInt_t NbPnts() const
Definition TBuffer3D.h:89
Int_t * fSegs
Definition TBuffer3D.h:123
Double_t * fPnts
Definition TBuffer3D.h:122
Box class.
Definition TGeoBBox.h:18
virtual const Double_t * GetOrigin() const
Definition TGeoBBox.h:81
Double_t fDX
Definition TGeoBBox.h:21
virtual Double_t GetDX() const
Definition TGeoBBox.h:78
virtual Double_t GetDZ() const
Definition TGeoBBox.h:80
virtual Double_t GetDY() const
Definition TGeoBBox.h:79
Double_t fOrigin[3]
Definition TGeoBBox.h:24
Double_t fDY
Definition TGeoBBox.h:22
Double_t fDZ
Definition TGeoBBox.h:23
Composite shapes are Boolean combinations of two or more shape components.
TGeoBoolNode * GetBoolNode() const
Matrix class used for computing global transformations Should NOT be used for node definition.
Definition TGeoMatrix.h:459
void Multiply(const TGeoMatrix *right)
multiply to the right with an other transformation if right is identity matrix, just return
static TClass * Class()
Geometrical transformation package.
Definition TGeoMatrix.h:39
virtual void LocalToMaster(const Double_t *local, Double_t *master) const
convert a point by multiplying its column vector (x, y, z, 1) to matrix inverse
Base abstract class for all shapes.
Definition TGeoShape.h:25
const char * GetName() const override
Get the shape name.
virtual TBuffer3D * MakeBuffer3D() const
Definition TGeoShape.h:156
static TClass * Class()
static TClass * Class()
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition fillpatterns.C:1
const Int_t n
Definition legend1.C:16
Namespace for ROOT features in testing.
Definition TROOT.h:100
T * Normal2Plane(const T v1[3], const T v2[3], const T v3[3], T normal[3])
Calculates a normal vector of a plane.
Definition TMath.h:1305
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122