Logo ROOT  
Reference Guide
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;
31namespace REX = ROOT::Experimental;
32
33/** \class REveGeoPolyShape
34\ingroup REve
35Description of REveGeoPolyShape
36*/
37
40
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 std::unique_ptr<TBuffer3D> b3d(shape->MakeBuffer3D());
154
155 SetFromBuff3D(*b3d.get());
156}
157
158////////////////////////////////////////////////////////////////////////////////
159
161{
162 // We know all elements are triangles. Or at least they should be.
163
164 rd.Reserve(fVertices.size(), fNormals.size(), 2 + fNbPols * 3);
165
166 for (auto &v: fVertices)
167 rd.PushV(v);
168
169 for (auto &n: fNormals)
170 rd.PushN(n);
171
173 rd.PushI(fNbPols);
174
175 // count number of index entries etc
176 for (Int_t i = 0, j = 0; i < fNbPols; ++i) {
177 assert(fPolyDesc[j] == 3);
178
179 rd.PushI(fPolyDesc[j + 1], fPolyDesc[j + 2], fPolyDesc[j + 3]);
180 j += 1 + fPolyDesc[j];
181 }
182}
183
184////////////////////////////////////////////////////////////////////////////////
185/// Set data-members from a Csg mesh.
186
187
189{
190 fNbPols = (Int_t) buffer.NbPols();
191
192 if (fNbPols == 0) return;
193
194 fVertices.insert(fVertices.end(), buffer.fPnts, buffer.fPnts + 3 * buffer.NbPnts());
195
196 Int_t *segs = buffer.fSegs;
197 Int_t *pols = buffer.fPols;
198
199 Int_t descSize = 0;
200
201 for (Int_t i = 0, j = 1; i < fNbPols; ++i, ++j)
202 {
203 descSize += pols[j] + 1;
204 j += pols[j] + 1;
205 }
206
207 fPolyDesc.resize(descSize);
208
209 for (Int_t numPol = 0, currInd = 0, j = 1; numPol < fNbPols; ++numPol)
210 {
211 Int_t segmentInd = pols[j] + j;
212 Int_t segmentCol = pols[j];
213 Int_t s1 = pols[segmentInd];
214 segmentInd--;
215 Int_t s2 = pols[segmentInd];
216 segmentInd--;
217 Int_t segEnds[] = {segs[s1 * 3 + 1], segs[s1 * 3 + 2],
218 segs[s2 * 3 + 1], segs[s2 * 3 + 2]};
219 Int_t numPnts[3];
220
221 if (segEnds[0] == segEnds[2]) {
222 numPnts[0] = segEnds[1]; numPnts[1] = segEnds[0]; numPnts[2] = segEnds[3];
223 } else if (segEnds[0] == segEnds[3]) {
224 numPnts[0] = segEnds[1]; numPnts[1] = segEnds[0]; numPnts[2] = segEnds[2];
225 } else if (segEnds[1] == segEnds[2]) {
226 numPnts[0] = segEnds[0]; numPnts[1] = segEnds[1]; numPnts[2] = segEnds[3];
227 } else {
228 numPnts[0] = segEnds[0]; numPnts[1] = segEnds[1]; numPnts[2] = segEnds[2];
229 }
230
231 fPolyDesc[currInd] = 3;
232 Int_t sizeInd = currInd++;
233 fPolyDesc[currInd++] = numPnts[0];
234 fPolyDesc[currInd++] = numPnts[1];
235 fPolyDesc[currInd++] = numPnts[2];
236 Int_t lastAdded = numPnts[2];
237
238 Int_t end = j + 1;
239 for (; segmentInd != end; segmentInd--) {
240 segEnds[0] = segs[pols[segmentInd] * 3 + 1];
241 segEnds[1] = segs[pols[segmentInd] * 3 + 2];
242 if (segEnds[0] == lastAdded) {
243 fPolyDesc[currInd++] = segEnds[1];
244 lastAdded = segEnds[1];
245 } else {
246 fPolyDesc[currInd++] = segEnds[0];
247 lastAdded = segEnds[0];
248 }
249 ++fPolyDesc[sizeInd];
250 }
251 j += segmentCol + 2;
252 }
253
256}
257
258////////////////////////////////////////////////////////////////////////////////
259/// Use GLU tesselator to replace all polygons with N > 3 with triangles.
260/// After this call polygon descriptions are changed.
261/// New vertices are not expected -- exception is thrown if this is
262/// requested by the triangulator. Support for adding of new vertices can be
263/// provided.
264
266{
268
270
271 fPolyDesc.swap(tc.RefPolyDesc());
272 fNbPols = tc.GetNTrianlges();
273}
274
275////////////////////////////////////////////////////////////////////////////////
276/// CalculateNormals per polygon (flat shading)
277
279{
280 fNormals.resize(3 * fNbPols);
281 if (fNbPols == 0) return;
282 Double_t *pnts = &fVertices[0];
283 for (Int_t i = 0, j = 0; i < fNbPols; ++i)
284 {
285 Int_t polEnd = fPolyDesc[j] + j + 1;
286 Int_t norm[] = {fPolyDesc[j + 1], fPolyDesc[j + 2], fPolyDesc[j + 3]};
287 j += 4;
288 Int_t check = CheckPoints(norm, norm);
289 Int_t ngood = check;
290 if (check == 3) {
291 TMath::Normal2Plane(pnts + norm[0] * 3, pnts + norm[1] * 3,
292 pnts + norm[2] * 3, &fNormals[i * 3]);
293 j = polEnd;
294 continue;
295 }
296 while (j < polEnd)
297 {
298 norm[ngood++] = fPolyDesc[j++];
299 if (ngood == 3) {
300 ngood = CheckPoints(norm, norm);
301 if (ngood == 3) {
302 TMath::Normal2Plane(pnts + norm[0] * 3, pnts + norm[1] * 3,
303 pnts + norm[2] * 3, &fNormals[i * 3]);
304 j = polEnd;
305 break;
306 }
307 }
308 }
309 }
310}
311
312////////////////////////////////////////////////////////////////////////////////
313/// CheckPoints
314
316{
317 const Double_t * p1 = &fVertices[source[0] * 3];
318 const Double_t * p2 = &fVertices[source[1] * 3];
319 const Double_t * p3 = &fVertices[source[2] * 3];
320 Int_t retVal = 1;
321
322 if (Eq(p1, p2)) {
323 dest[0] = source[0];
324 if (!Eq(p1, p3) ) {
325 dest[1] = source[2];
326 retVal = 2;
327 }
328 } else if (Eq(p1, p3)) {
329 dest[0] = source[0];
330 dest[1] = source[1];
331 retVal = 2;
332 } else {
333 dest[0] = source[0];
334 dest[1] = source[1];
335 retVal = 2;
336 if (!Eq(p2, p3)) {
337 dest[2] = source[2];
338 retVal = 3;
339 }
340 }
341
342 return retVal;
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Test equality of points with epsilon 1e-10.
347
349{
350 Double_t dx = TMath::Abs(p1[0] - p2[0]);
351 Double_t dy = TMath::Abs(p1[1] - p2[1]);
352 Double_t dz = TMath::Abs(p1[2] - p2[2]);
353 return (dx < 1e-10) && (dy < 1e-10) && (dz < 1e-10);
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// Fill the passed buffer 3D.
358
360{
361 if (reqSections & TBuffer3D::kCore)
362 {
363 // If writing core section all others will be invalid
364 b.ClearSectionsValid();
365
366 b.fID = const_cast<REveGeoPolyShape*>(this);
367 b.fColor = kMagenta;
368 b.fTransparency = 0;
369 b.fLocalFrame = kFALSE;
370 b.fReflection = kTRUE;
371
372 b.SetSectionsValid(TBuffer3D::kCore);
373 }
374
375 if ((reqSections & TBuffer3D::kRawSizes) || (reqSections & TBuffer3D::kRaw))
376 {
377 Int_t nvrt = fVertices.size() / 3;
378 Int_t nseg = 0;
379
380 std::map<Edge_t, Int_t> edges;
381
382 const Int_t *pd = &fPolyDesc[0];
383 for (Int_t i = 0; i < fNbPols; ++i) {
384 Int_t nv = pd[0];
385 ++pd;
386 for (Int_t j = 0; j < nv; ++j) {
387 Edge_t e(pd[j], (j != nv - 1) ? pd[j + 1] : pd[0]);
388 if (edges.find(e) == edges.end()) {
389 edges.insert(std::make_pair(e, 0));
390 ++nseg;
391 }
392 }
393 pd += nv;
394 }
395
396 b.SetRawSizes(nvrt, 3*nvrt, nseg, 3*nseg, fNbPols, fNbPols+fPolyDesc.size());
397
398 memcpy(b.fPnts, &fVertices[0], sizeof(Double_t)*fVertices.size());
399
400 Int_t si = 0, scnt = 0;
401 for (auto &edge : edges) {
402 b.fSegs[si++] = 0;
403 b.fSegs[si++] = edge.first.fI;
404 b.fSegs[si++] = edge.first.fJ;
405 edge.second = scnt++;
406 }
407
408 Int_t pi = 0;
409 pd = &fPolyDesc[0];
410 for (Int_t i = 0; i < fNbPols; ++i) {
411 Int_t nv = pd[0];
412 ++pd;
413 b.fPols[pi++] = 0;
414 b.fPols[pi++] = nv;
415 for (Int_t j = 0; j < nv; ++j) {
416 b.fPols[pi++] = edges[Edge_t(pd[j], (j != nv - 1) ? pd[j + 1] : pd[0])];
417 }
418 pd += nv;
419 }
420
421 b.SetSectionsValid(TBuffer3D::kRawSizes | TBuffer3D::kRaw);
422 }
423}
424
425////////////////////////////////////////////////////////////////////////////////
426/// Fill static buffer 3D.
427
428const TBuffer3D& REveGeoPolyShape::GetBuffer3D(Int_t reqSections, Bool_t localFrame) const
429{
431
432 FillBuffer3D(buf, reqSections, localFrame);
433
434 return buf;
435}
436
437////////////////////////////////////////////////////////////////////////////////
438/// Create buffer 3D and fill it with point/segment/poly data.
439
441{
442 auto *buf = new TBuffer3D(TBuffer3DTypes::kGeneric);
443
445
446 return buf;
447}
void Class()
Definition: Class.C:29
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
Definition: RtypesCore.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:90
bool Bool_t
Definition: RtypesCore.h:61
const Bool_t kTRUE
Definition: RtypesCore.h:89
@ kMagenta
Definition: Rtypes.h:64
segment * segs
Definition: X3DBuffer.c:23
void ProcessData(const std::vector< Double_t > &verts, const std::vector< Int_t > &polys, const Int_t n_polys)
ProcessData.
REveGeoManagerHolder Exception-safe global variable holders.
Definition: REveUtil.hxx:87
void FillRenderData(REveRenderData &rd)
static void SetAutoEnforceTriangles(Bool_t f)
virtual void FillBuffer3D(TBuffer3D &buffer, Int_t reqSections, Bool_t localFrame) const
Fill the passed buffer 3D.
static void SetAutoCalculateNormals(Bool_t f)
void EnforceTriangles()
Use GLU tesselator to replace all polygons with N > 3 with triangles.
Int_t CheckPoints(const Int_t *source, Int_t *dest) const
CheckPoints.
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.
virtual TBuffer3D * MakeBuffer3D() const
Create buffer 3D and fill it with point/segment/poly data.
virtual const TBuffer3D & GetBuffer3D(Int_t reqSections, Bool_t localFrame) const
Fill static buffer 3D.
void CalculateNormals()
CalculateNormals per polygon (flat shading)
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:114
UInt_t NbPols() const
Definition: TBuffer3D.h:82
UInt_t NbPnts() const
Definition: TBuffer3D.h:80
@ kRawSizes
Definition: TBuffer3D.h:53
Int_t * fSegs
Definition: TBuffer3D.h:113
Double_t * fPnts
Definition: TBuffer3D.h:112
Box class.
Definition: TGeoBBox.h:18
virtual const Double_t * GetOrigin() const
Definition: TGeoBBox.h:73
Double_t fDX
Definition: TGeoBBox.h:21
virtual Double_t GetDX() const
Definition: TGeoBBox.h:70
virtual Double_t GetDZ() const
Definition: TGeoBBox.h:72
virtual Double_t GetDY() const
Definition: TGeoBBox.h:71
Double_t fOrigin[3]
Definition: TGeoBBox.h:24
Double_t fDY
Definition: TGeoBBox.h:22
Double_t fDZ
Definition: TGeoBBox.h:23
Class handling Boolean composition of shapes.
TGeoBoolNode * GetBoolNode() const
Matrix class used for computing global transformations Should NOT be used for node definition.
Definition: TGeoMatrix.h:421
void Multiply(const TGeoMatrix *right)
multiply to the right with an other transformation if right is identity matrix, just return
Geometrical transformation package.
Definition: TGeoMatrix.h:41
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
Definition: TGeoMatrix.cxx:339
Base abstract class for all shapes.
Definition: TGeoShape.h:26
virtual TBuffer3D * MakeBuffer3D() const
Definition: TGeoShape.h:143
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
static constexpr double pi
T * Normal2Plane(const T v1[3], const T v2[3], const T v3[3], T normal[3])
Calculate a normal vector of a plane.
Definition: TMath.h:1179
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
#define dest(otri, vertexptr)
Definition: triangle.c:1040