Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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>
20 #include <ROOT/REveRenderData.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 
30 using namespace ROOT::Experimental;
31 namespace REX = ROOT::Experimental;
32 
33 /** \class REveGeoPolyShape
34 \ingroup REve
35 Description of REveGeoPolyShape
36 */
37 
38 Bool_t REX::REveGeoPolyShape::fgAutoEnforceTriangles = kTRUE;
39 Bool_t REX::REveGeoPolyShape::fgAutoCalculateNormals = kFALSE;
40 
41 void REveGeoPolyShape::SetAutoEnforceTriangles(Bool_t f) { fgAutoEnforceTriangles = f; }
42 Bool_t REveGeoPolyShape::GetAutoEnforceTriangles() { return fgAutoEnforceTriangles; }
43 void REveGeoPolyShape::SetAutoCalculateNormals(Bool_t f) { fgAutoCalculateNormals = f; }
44 Bool_t REveGeoPolyShape::GetAutoCalculateNormals() { return fgAutoCalculateNormals; }
45 
46 ////////////////////////////////////////////////////////////////////////
47 /// Function produces mesh for provided shape, applying matrix to the result
48 
49 std::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 
93 void REveGeoPolyShape::BuildFromComposite(TGeoCompositeShape *cshape, Int_t n_seg)
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 
102  REveGeoManagerHolder gmgr(REveGeoShape::GetGeoManager(), n_seg);
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 
131  if (fgAutoEnforceTriangles) EnforceTriangles();
132  if (fgAutoCalculateNormals) CalculateNormals();
133 }
134 
135 ////////////////////////////////////////////////////////////////////////////////
136 /// Produce all polygons from normal shape
137 
138 void REveGeoPolyShape::BuildFromShape(TGeoShape *shape, Int_t n_seg)
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 
151  REveGeoManagerHolder gmgr(REveGeoShape::GetGeoManager(), n_seg);
152 
153  std::unique_ptr<TBuffer3D> b3d(shape->MakeBuffer3D());
154 
155  SetFromBuff3D(*b3d.get());
156 }
157 
158 ////////////////////////////////////////////////////////////////////////////////
159 
160 void REveGeoPolyShape::FillRenderData(REveRenderData &rd)
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 
172  rd.PushI(REveRenderData::GL_TRIANGLES);
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 
188 void REveGeoPolyShape::SetFromBuff3D(const TBuffer3D& buffer)
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 
254  if (fgAutoEnforceTriangles) EnforceTriangles();
255  if (fgAutoCalculateNormals) CalculateNormals();
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 
265 void REveGeoPolyShape::EnforceTriangles()
266 {
267  EveGlu::TriangleCollector tc;
268 
269  tc.ProcessData(fVertices, fPolyDesc, fNbPols);
270 
271  fPolyDesc.swap(tc.RefPolyDesc());
272  fNbPols = tc.GetNTrianlges();
273 }
274 
275 ////////////////////////////////////////////////////////////////////////////////
276 /// CalculateNormals per polygon (flat shading)
277 
278 void REveGeoPolyShape::CalculateNormals()
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 
315 Int_t REveGeoPolyShape::CheckPoints(const Int_t *source, Int_t *dest) const
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 
348 Bool_t REveGeoPolyShape::Eq(const Double_t *p1, const Double_t *p2)
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 
359 void REveGeoPolyShape::FillBuffer3D(TBuffer3D& b, Int_t reqSections, Bool_t) const
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 
428 const TBuffer3D& REveGeoPolyShape::GetBuffer3D(Int_t reqSections, Bool_t localFrame) const
429 {
430  static TBuffer3D buf(TBuffer3DTypes::kGeneric);
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 
440 TBuffer3D *REveGeoPolyShape::MakeBuffer3D() const
441 {
442  auto *buf = new TBuffer3D(TBuffer3DTypes::kGeneric);
443 
444  FillBuffer3D(*buf, TBuffer3D::kCore | TBuffer3D::kRawSizes | TBuffer3D::kRaw, kFALSE);
445 
446  return buf;
447 }