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#include "CsgOps.h"
25
26#include "TGeoBoolNode.h"
27#include "TGeoCompositeShape.h"
28#include "TGeoMatrix.h"
29
30using namespace ROOT::Experimental;
31
32/** \class REveGeoPolyShape
33\ingroup REve
34Description of REveGeoPolyShape
35*/
36
39
44
45////////////////////////////////////////////////////////////////////////
46/// Function produces mesh for provided shape, applying matrix to the result
47
48std::unique_ptr<RootCsg::TBaseMesh> MakeGeoMesh(TGeoMatrix *matr, TGeoShape *shape)
49{
50 TGeoCompositeShape *comp = dynamic_cast<TGeoCompositeShape *> (shape);
51
52 std::unique_ptr<RootCsg::TBaseMesh> res;
53
54 if (!comp) {
55 std::unique_ptr<TBuffer3D> b3d(shape->MakeBuffer3D());
56
57 if (matr) {
58 Double_t *v = b3d->fPnts;
59 Double_t buf[3];
60 for (UInt_t i = 0; i < b3d->NbPnts(); ++i) {
61 buf[0] = v[i*3];
62 buf[1] = v[i*3+1];
63 buf[2] = v[i*3+2];
64 matr->LocalToMaster(buf, &v[i*3]);
65 }
66 }
67
68 res.reset(RootCsg::ConvertToMesh(*b3d.get()));
69 } else {
70 auto node = comp->GetBoolNode();
71
72 TGeoHMatrix mleft, mright;
73 if (matr) { mleft = *matr; mright = *matr; }
74
75 mleft.Multiply(node->GetLeftMatrix());
76 auto left = MakeGeoMesh(&mleft, node->GetLeftShape());
77
78 mright.Multiply(node->GetRightMatrix());
79 auto right = MakeGeoMesh(&mright, node->GetRightShape());
80
81 if (node->IsA() == TGeoUnion::Class()) res.reset(RootCsg::BuildUnion(left.get(), right.get()));
82 if (node->IsA() == TGeoIntersection::Class()) res.reset(RootCsg::BuildIntersection(left.get(), right.get()));
83 if (node->IsA() == TGeoSubtraction::Class()) res.reset(RootCsg::BuildDifference(left.get(), right.get()));
84 }
85
86 return res;
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// Produce all polygons from composite shape
91
93{
94 fOrigin[0] = cshape->GetOrigin()[0];
95 fOrigin[1] = cshape->GetOrigin()[1];
96 fOrigin[2] = cshape->GetOrigin()[2];
97 fDX = cshape->GetDX();
98 fDY = cshape->GetDY();
99 fDZ = cshape->GetDZ();
100
102
103 auto mesh = MakeGeoMesh(nullptr, cshape);
104
105 Int_t nv = mesh->NumberOfVertices();
106 fVertices.reserve(3 * nv);
107
108 for (Int_t i = 0; i < nv; ++i) {
109 auto v = mesh->GetVertex(i);
110 fVertices.insert(fVertices.end(), v, v + 3);
111 }
112
113 fNbPols = mesh->NumberOfPolys();
114
115 Int_t descSize = 0;
116
117 for (Int_t i = 0; i < fNbPols; ++i) descSize += mesh->SizeOfPoly(i) + 1;
118
119 fPolyDesc.reserve(descSize);
120
121 for (Int_t polyIndex = 0; polyIndex < fNbPols; ++polyIndex) {
122 Int_t polySize = mesh->SizeOfPoly(polyIndex);
123
124 fPolyDesc.push_back(polySize);
125
126 for (Int_t i = 0; i < polySize; ++i)
127 fPolyDesc.push_back(mesh->GetVertexIndex(polyIndex, i));
128 }
129
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// Produce all polygons from normal shape
136
138{
139 TGeoBBox *box = dynamic_cast<TGeoBBox *> (shape);
140
141 if (box) {
142 fOrigin[0] = box->GetOrigin()[0];
143 fOrigin[1] = box->GetOrigin()[1];
144 fOrigin[2] = box->GetOrigin()[2];
145 fDX = box->GetDX();
146 fDY = box->GetDY();
147 fDZ = box->GetDZ();
148 }
149
151
152 if (!shape->MakeBuffer3D())
153 {
154 std::cout << "Shape " << shape->GetName() << " has empty TBuffer3D\n";
155 return;
156 }
157
158 std::unique_ptr<TBuffer3D> b3d(shape->MakeBuffer3D());
159
160 SetFromBuff3D(*b3d.get());
161}
162
163////////////////////////////////////////////////////////////////////////////////
164
166{
167 // We know all elements are triangles. Or at least they should be.
168
169 rd.Reserve(fVertices.size(), fNormals.size(), 2 + fNbPols * 3);
170
171 for (auto &v: fVertices)
172 rd.PushV(v);
173
174 for (auto &n: fNormals)
175 rd.PushN(n);
176
178 rd.PushI(fNbPols);
179
180 // count number of index entries etc
181 for (Int_t i = 0, j = 0; i < fNbPols; ++i) {
182 assert(fPolyDesc[j] == 3);
183
184 rd.PushI(fPolyDesc[j + 1], fPolyDesc[j + 2], fPolyDesc[j + 3]);
185 j += 1 + fPolyDesc[j];
186 }
187}
188
189////////////////////////////////////////////////////////////////////////////////
190/// Set data-members from a Csg mesh.
191
192
194{
195 fNbPols = (Int_t) buffer.NbPols();
196
197 if (fNbPols == 0) return;
198
199 fVertices.insert(fVertices.end(), buffer.fPnts, buffer.fPnts + 3 * buffer.NbPnts());
200
201 Int_t *segs = buffer.fSegs;
202 Int_t *pols = buffer.fPols;
203
204 Int_t descSize = 0;
205
206 for (Int_t i = 0, j = 1; i < fNbPols; ++i, ++j)
207 {
208 descSize += pols[j] + 1;
209 j += pols[j] + 1;
210 }
211
212 fPolyDesc.resize(descSize);
213
214 for (Int_t numPol = 0, currInd = 0, j = 1; numPol < fNbPols; ++numPol)
215 {
216 Int_t segmentInd = pols[j] + j;
217 Int_t segmentCol = pols[j];
218 Int_t s1 = pols[segmentInd];
219 segmentInd--;
220 Int_t s2 = pols[segmentInd];
221 segmentInd--;
222 Int_t segEnds[] = {segs[s1 * 3 + 1], segs[s1 * 3 + 2],
223 segs[s2 * 3 + 1], segs[s2 * 3 + 2]};
224 Int_t numPnts[3];
225
226 if (segEnds[0] == segEnds[2]) {
227 numPnts[0] = segEnds[1]; numPnts[1] = segEnds[0]; numPnts[2] = segEnds[3];
228 } else if (segEnds[0] == segEnds[3]) {
229 numPnts[0] = segEnds[1]; numPnts[1] = segEnds[0]; numPnts[2] = segEnds[2];
230 } else if (segEnds[1] == segEnds[2]) {
231 numPnts[0] = segEnds[0]; numPnts[1] = segEnds[1]; numPnts[2] = segEnds[3];
232 } else {
233 numPnts[0] = segEnds[0]; numPnts[1] = segEnds[1]; numPnts[2] = segEnds[2];
234 }
235
236 fPolyDesc[currInd] = 3;
237 Int_t sizeInd = currInd++;
238 fPolyDesc[currInd++] = numPnts[0];
239 fPolyDesc[currInd++] = numPnts[1];
240 fPolyDesc[currInd++] = numPnts[2];
241 Int_t lastAdded = numPnts[2];
242
243 Int_t end = j + 1;
244 for (; segmentInd != end; segmentInd--) {
245 segEnds[0] = segs[pols[segmentInd] * 3 + 1];
246 segEnds[1] = segs[pols[segmentInd] * 3 + 2];
247 if (segEnds[0] == lastAdded) {
248 fPolyDesc[currInd++] = segEnds[1];
249 lastAdded = segEnds[1];
250 } else {
251 fPolyDesc[currInd++] = segEnds[0];
252 lastAdded = segEnds[0];
253 }
254 ++fPolyDesc[sizeInd];
255 }
256 j += segmentCol + 2;
257 }
258
261}
262
263////////////////////////////////////////////////////////////////////////////////
264/// Use GLU tesselator to replace all polygons with N > 3 with triangles.
265/// After this call polygon descriptions are changed.
266/// New vertices are not expected -- exception is thrown if this is
267/// requested by the triangulator. Support for adding of new vertices can be
268/// provided.
269
279
280////////////////////////////////////////////////////////////////////////////////
281/// CalculateNormals per polygon (flat shading)
282
284{
285 fNormals.resize(3 * fNbPols);
286 if (fNbPols == 0) return;
287 Double_t *pnts = &fVertices[0];
288 for (Int_t i = 0, j = 0; i < fNbPols; ++i)
289 {
290 Int_t polEnd = fPolyDesc[j] + j + 1;
291 UInt_t norm[] = {fPolyDesc[j + 1], fPolyDesc[j + 2], fPolyDesc[j + 3]};
292 j += 4;
293 Int_t check = CheckPoints(norm, norm);
294 Int_t ngood = check;
295 if (check == 3) {
296 TMath::Normal2Plane(pnts + norm[0] * 3, pnts + norm[1] * 3,
297 pnts + norm[2] * 3, &fNormals[i * 3]);
298 j = polEnd;
299 continue;
300 }
301 while (j < polEnd)
302 {
303 norm[ngood++] = fPolyDesc[j++];
304 if (ngood == 3) {
305 ngood = CheckPoints(norm, norm);
306 if (ngood == 3) {
307 TMath::Normal2Plane(pnts + norm[0] * 3, pnts + norm[1] * 3,
308 pnts + norm[2] * 3, &fNormals[i * 3]);
309 j = polEnd;
310 break;
311 }
312 }
313 }
314 }
315}
316
317////////////////////////////////////////////////////////////////////////////////
318/// CheckPoints
319
320Int_t REveGeoPolyShape::CheckPoints(const UInt_t *source, UInt_t *dest) const
321{
322 const Double_t * p1 = &fVertices[source[0] * 3];
323 const Double_t * p2 = &fVertices[source[1] * 3];
324 const Double_t * p3 = &fVertices[source[2] * 3];
325 Int_t retVal = 1;
326
327 if (Eq(p1, p2)) {
328 dest[0] = source[0];
329 if (!Eq(p1, p3) ) {
330 dest[1] = source[2];
331 retVal = 2;
332 }
333 } else if (Eq(p1, p3)) {
334 dest[0] = source[0];
335 dest[1] = source[1];
336 retVal = 2;
337 } else {
338 dest[0] = source[0];
339 dest[1] = source[1];
340 retVal = 2;
341 if (!Eq(p2, p3)) {
342 dest[2] = source[2];
343 retVal = 3;
344 }
345 }
346
347 return retVal;
348}
349
350////////////////////////////////////////////////////////////////////////////////
351/// Test equality of points with epsilon 1e-10.
352
354{
355 Double_t dx = TMath::Abs(p1[0] - p2[0]);
356 Double_t dy = TMath::Abs(p1[1] - p2[1]);
357 Double_t dz = TMath::Abs(p1[2] - p2[2]);
358 return (dx < 1e-10) && (dy < 1e-10) && (dz < 1e-10);
359}
360
361////////////////////////////////////////////////////////////////////////////////
362/// Fill the passed buffer 3D.
363
365{
366 if (reqSections & TBuffer3D::kCore)
367 {
368 // If writing core section all others will be invalid
369 b.ClearSectionsValid();
370
371 b.fID = const_cast<REveGeoPolyShape*>(this);
372 b.fColor = kMagenta;
373 b.fTransparency = 0;
374 b.fLocalFrame = kFALSE;
375 b.fReflection = kTRUE;
376
377 b.SetSectionsValid(TBuffer3D::kCore);
378 }
379
380 if ((reqSections & TBuffer3D::kRawSizes) || (reqSections & TBuffer3D::kRaw))
381 {
382 Int_t nvrt = fVertices.size() / 3;
383 Int_t nseg = 0;
384
385 std::map<Edge_t, Int_t> edges;
386
387 const UInt_t *pd = &fPolyDesc[0];
388 for (Int_t i = 0; i < fNbPols; ++i) {
389 Int_t nv = pd[0];
390 ++pd;
391 for (Int_t j = 0; j < nv; ++j) {
392 Edge_t e(pd[j], (j != nv - 1) ? pd[j + 1] : pd[0]);
393 if (edges.find(e) == edges.end()) {
394 edges.insert(std::make_pair(e, 0));
395 ++nseg;
396 }
397 }
398 pd += nv;
399 }
400
401 b.SetRawSizes(nvrt, 3*nvrt, nseg, 3*nseg, fNbPols, fNbPols+fPolyDesc.size());
402
403 memcpy(b.fPnts, &fVertices[0], sizeof(Double_t)*fVertices.size());
404
405 Int_t si = 0, scnt = 0;
406 for (auto &edge : edges) {
407 b.fSegs[si++] = 0;
408 b.fSegs[si++] = edge.first.fI;
409 b.fSegs[si++] = edge.first.fJ;
410 edge.second = scnt++;
411 }
412
413 Int_t pi = 0;
414 pd = &fPolyDesc[0];
415 for (Int_t i = 0; i < fNbPols; ++i) {
416 Int_t nv = pd[0];
417 ++pd;
418 b.fPols[pi++] = 0;
419 b.fPols[pi++] = nv;
420 for (Int_t j = 0; j < nv; ++j) {
421 b.fPols[pi++] = edges[Edge_t(pd[j], (j != nv - 1) ? pd[j + 1] : pd[0])];
422 }
423 pd += nv;
424 }
425
426 b.SetSectionsValid(TBuffer3D::kRawSizes | TBuffer3D::kRaw);
427 }
428}
429
430////////////////////////////////////////////////////////////////////////////////
431/// Fill static buffer 3D.
432
433const TBuffer3D& REveGeoPolyShape::GetBuffer3D(Int_t reqSections, Bool_t localFrame) const
434{
436
437 FillBuffer3D(buf, reqSections, localFrame);
438
439 return buf;
440}
441
442////////////////////////////////////////////////////////////////////////////////
443/// Create buffer 3D and fill it with point/segment/poly data.
444
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:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
@ kMagenta
Definition Rtypes.h:67
segment * segs
Definition X3DBuffer.c:23
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:90
static void SetAutoEnforceTriangles(Bool_t f)
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.
REveGeoPolyShape(const REveGeoPolyShape &)=delete
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.
void Reserve(int size_vert=0, int size_norm=0, int size_idx=0)
Reserve place for render data.
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
virtual const Double_t * GetOrigin() const
Definition TGeoBBox.h:81
Double_t fDX
Definition TGeoBBox.h:21
TGeoBBox(const TGeoBBox &)=delete
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
TGeoShape()
Default constructor.
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:1299
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122