Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REvePolygonSetProjected.cxx
Go to the documentation of this file.
1// @(#)root/eve7:$Id$
2// Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
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
13#include <ROOT/REveGeoShape.hxx>
15#include <ROOT/REveGluTess.hxx>
17
18#include "TBuffer3D.h"
19#include "TBuffer3DTypes.h"
20
21#include <cassert>
22
23#include <nlohmann/json.hpp>
24
25using namespace ROOT::Experimental;
26
27namespace
28{
29 struct Seg_t
30 {
31 // Helper class for building 2D polygons from TBuffer3D.
32 Int_t fV1;
33 Int_t fV2;
34
35 Seg_t(Int_t i1=-1, Int_t i2=-1) : fV1(i1), fV2(i2) {}
36 };
37
38 typedef std::list<Seg_t> LSeg_t;
39}
40
41/** \class REvePolygonSetProjected
42\ingroup REve
43A set of projected polygons.
44Used for storage of projected geometrical shapes.
45
46Internal struct Polygon_t holds only indices into the master vertex
47array in REvePolygonSetProjected.
48*/
49
50////////////////////////////////////////////////////////////////////////////////
51/// Constructor.
52
53REvePolygonSetProjected::REvePolygonSetProjected(const std::string &n, const std::string &t) :
54 REveShape(n, t),
55 fBuff(),
56 fPnts()
57{
58}
59
60////////////////////////////////////////////////////////////////////////////////
61/// Destructor.
62
64{
65 fPols.clear();
66}
67
68////////////////////////////////////////////////////////////////////////////////
69/// Fill core part of JSON representation.
70
72{
73 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
74
75 j["fNPnts"] = fPnts.size();
76
77 return ret;
78}
79
80////////////////////////////////////////////////////////////////////////////////
81/// Crates representation for rendering.
82/// This is complicated as we need to:
83/// - generate outlines;
84/// - convert polygons to triangles.
85/// ??? Should we check if polygons are front facing? It was not done in old EVE,
86/// just GL_FRONT_AND_BACK was used on top of gluTess.
87
89{
90 fRenderData = std::make_unique<REveRenderData>("makePolygonSetProjected", 3 * fPnts.size());
91
92 Int_t n_pols = fPols.size();
93 Int_t n_poly_info = 0;
94 for (auto &p : fPols) n_poly_info += 1 + p.NPoints();
95
96 std::vector<Double_t> verts;
97 verts.reserve(3 * fPnts.size());
98 std::vector<UInt_t> polys;
99 polys.reserve(n_poly_info);
100
101 for (auto &p : fPols)
102 {
103 polys.emplace_back(p.NPoints());
104 polys.insert(polys.end(), p.fPnts.begin(), p.fPnts.end());
105 }
106
107 for (unsigned i = 0; i < fPnts.size(); ++i)
108 {
109 verts.push_back(fPnts[i].fX);
110 verts.push_back(fPnts[i].fY);
111 verts.push_back(fPnts[i].fZ);
112 fRenderData->PushV(fPnts[i]);
113 }
114
115 Int_t n_trings = 0;
116 {
118
119 tc.ProcessData(verts, polys, n_pols);
120
121 polys.swap(tc.RefPolyDesc());
122 n_trings = tc.GetNTrianlges();
123 }
124
125 // Calculate size of index buffer.
126 Int_t n_idxbuff = 2 + 3 * n_trings + n_pols + n_poly_info;
127 fRenderData->Reserve(0,0,n_idxbuff);
128
129 assert(n_trings * 4 == (int)polys.size());
130
131 // Export triangles.
133 fRenderData->PushI(n_trings);
134 for (int i = 0; i < n_trings; ++i)
135 {
136 fRenderData->PushI(&polys[i*4 + 1], 3);
137 }
138
139 assert (fRenderData->SizeI() == 2 + 3 * n_trings);
140
141 // Export outlines.
142 for (auto &p : fPols)
143 {
145 fRenderData->PushI(p.NPoints());
146 fRenderData->PushI(p.fPnts);
147 }
148
149 assert (fRenderData->SizeI() == n_idxbuff);
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Override of virtual method from TAttBBox.
154
156{
157 if (fPnts.size() > 0) {
158 BBoxInit();
159 for (unsigned pi = 0; pi < fPnts.size(); ++pi)
160 BBoxCheckPoint(fPnts[pi].fX, fPnts[pi].fY, fPnts[pi].fZ);
161 } else {
162 BBoxZero();
163 }
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// This is virtual method from base-class REveProjected.
168
170 REveProjectable* model)
171{
173
174 REveGeoShape* gre = dynamic_cast<REveGeoShape*>(model);
175 fBuff = gre->MakeBuffer3D();
176 CopyVizParams(gre);
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// Set depth (z-coordinate) of the projected points.
181
183{
184 SetDepthCommon(d, this, fBBox);
185
186 for (unsigned i = 0; i < fPnts.size(); ++i)
187 fPnts[i].fZ = fDepth;
188}
189
190////////////////////////////////////////////////////////////////////////////////
191/// This is virtual method from base-class REveProjected.
192
194{
195 if (!fBuff) return;
196
197 // drop polygons and projected/reduced points
198 fPols.clear();
200}
201
202////////////////////////////////////////////////////////////////////////////////
203/// Compare the two segments and check if the first index of first segment is starting.
204
206{
207 Int_t v0 = fBuff->fSegs[3*s0 + 1];
208 Int_t v2 = fBuff->fSegs[3*s1 + 1];
209 Int_t v3 = fBuff->fSegs[3*s1 + 2];
210 return v0 != v2 && v0 != v3;
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// Project and reduce buffer points.
215
217{
218 REveProjection* projection = fManager->GetProjection();
219
220 Int_t buffN = fBuff->NbPnts();
221 std::vector<REveVector> pnts; pnts.resize(buffN);
222 for (Int_t i = 0; i < buffN; ++i)
223 {
224 pnts[i].Set(fBuff->fPnts[3*i],fBuff->fPnts[3*i+1], fBuff->fPnts[3*i+2]);
225 projection->ProjectPoint(pnts[i].fX, pnts[i].fY, pnts[i].fZ, 0,
227 }
228
229 int npoints = 0;
230 std::vector<UInt_t> idxMap;
231 idxMap.resize(buffN);
232
233 std::vector<int> ra;
234 ra.resize(buffN); // list of reduced vertices
235 for (UInt_t v = 0; v < (UInt_t)buffN; ++v)
236 {
237 bool found = false;
238 for (Int_t k = 0; k < npoints; ++k)
239 {
240 if (pnts[v].SquareDistance(pnts[ra[k]]) < REveProjection::fgEpsSqr)
241 {
242 idxMap[v] = k;
243 found = true;
244 break;
245 }
246 }
247 // have not found a point inside epsilon, add new point in scaled array
248 if (!found)
249 {
250 idxMap[v] = npoints;
251 ra[npoints] = v;
252 ++npoints;
253 }
254 }
255
256 // write the array of scaled points
257 fPnts.resize(npoints);
258 for (Int_t idx = 0; idx < npoints; ++idx)
259 {
260 Int_t i = ra[idx];
261 projection->ProjectPoint(pnts[i].fX, pnts[i].fY, pnts[i].fZ, fDepth,
263 fPnts[idx].Set(pnts[i]);
264 }
265 // printf("reduced %d points of %d\n", fNPnts, N);
266
267 return idxMap;
268}
269
270////////////////////////////////////////////////////////////////////////////////
271/// Check if polygon has dimensions above REveProjection::fgEps and add it
272/// to a list if it is not a duplicate.
273
275{
276 if (pp.size() <= 2) return 0;
277
278 Float_t bbox[4] = { 1e6, -1e6, 1e6, -1e6 };
279 for (auto &&idx: pp)
280 {
281 if (fPnts[idx].fX < bbox[0]) bbox[0] = fPnts[idx].fX;
282 if (fPnts[idx].fX > bbox[1]) bbox[1] = fPnts[idx].fX;
283
284 if (fPnts[idx].fY < bbox[2]) bbox[2] = fPnts[idx].fY;
285 if (fPnts[idx].fY > bbox[3]) bbox[3] = fPnts[idx].fY;
286 }
288 if ((bbox[1]-bbox[0]) < eps || (bbox[3]-bbox[2]) < eps) return 0;
289
290 // Duplication
291 for (auto &&refP : pols)
292 {
293 if ((Int_t) pp.size() != refP.NPoints())
294 continue;
295
296 int start_idx = refP.FindPoint(pp.front());
297 if (start_idx < 0)
298 continue;
299 if (++start_idx >= refP.NPoints()) start_idx = 0;
300
301 // Same orientation duplicate
302 {
303 auto u = ++pp.begin();
304 Int_t pidx = start_idx;
305 while (u != pp.end())
306 {
307 if ((*u) != refP.fPnts[pidx])
308 break;
309 ++u;
310 if (++pidx >= refP.NPoints()) pidx = 0;
311 }
312 if (u == pp.end()) return 0;
313 }
314 // Inverse orientation duplicate
315 {
316 auto u = --pp.end();
317 Int_t pidx = start_idx;
318 while (u != pp.begin())
319 {
320 if ((*u) != refP.fPnts[pidx])
321 break;
322 --u;
323 if (++pidx >= refP.NPoints()) pidx = 0;
324 }
325 if (u == pp.begin()) return 0;
326 }
327 }
328
329 std::vector<UInt_t> pv(pp.size(), 0);
330 int count = 0;
331 for (auto &&u : pp) {
332 pv[count++] = u;
333 }
334
335 pols.emplace_back(std::move(pv));
336
337 return (bbox[1]-bbox[0]) * (bbox[3]-bbox[2]);
338}
339
340////////////////////////////////////////////////////////////////////////////////
341/// Build polygons from list of buffer polygons.
342
344{
345 REveProjection* projection = fManager->GetProjection();
346 Int_t *bpols = fBuff->fPols;
347 Float_t surf = 0; // surface of projected polygons
348 for (UInt_t pi = 0; pi < fBuff->NbPols(); ++pi)
349 {
350 std::list<UInt_t> pp; // points in current polygon
351 UInt_t segN = bpols[1];
352 Int_t *seg = &bpols[2];
353 // start idx in the fist segment depends of second segment
354 UInt_t tail, head;
355 if (IsFirstIdxHead(seg[0], seg[1]))
356 {
357 head = idxMap[fBuff->fSegs[3*seg[0] + 1]];
358 tail = idxMap[fBuff->fSegs[3*seg[0] + 2]];
359 }
360 else
361 {
362 head = idxMap[fBuff->fSegs[3*seg[0] + 2]];
363 tail = idxMap[fBuff->fSegs[3*seg[0] + 1]];
364 }
365 pp.emplace_back(head);
366 // printf("start idx head %d, tail %d\n", head, tail);
367 LSeg_t segs;
368 for (UInt_t s = 1; s < segN; ++s)
369 segs.emplace_back(fBuff->fSegs[3*seg[s] + 1],fBuff->fSegs[3*seg[s] + 2]);
370
371 for (auto &it: segs)
372 {
373 UInt_t mv1 = idxMap[it.fV1];
374 UInt_t mv2 = idxMap[it.fV2];
375
376 if ( ! projection->AcceptSegment(fPnts[mv1], fPnts[mv2], REveProjection::fgEps))
377 {
378 pp.clear();
379 break;
380 }
381 if (tail != pp.back()) pp.push_back(tail);
382 tail = (mv1 == tail) ? mv2 : mv1;
383 }
384
385 if ( ! pp.empty())
386 {
387 // DirectDraw() implementation: last and first vertices should not be equal
388 if (pp.front() == pp.back()) pp.pop_front();
389 surf += AddPolygon(pp, fPolsBP);
390 }
391 bpols += (segN+2);
392 }
393 return surf;
394}
395
396////////////////////////////////////////////////////////////////////////////////
397/// Build polygons from the set of buffer segments.
398/// First creates a segment pool according to reduced and projected points
399/// and then build polygons from the pool.
400
402{
403 LSeg_t segs;
404 Float_t surf = 0; // surface of projected polygons
405 REveProjection *projection = fManager->GetProjection();
406 for (UInt_t s = 0; s < fBuff->NbSegs(); ++s)
407 {
408 Bool_t duplicate = kFALSE;
409 Int_t vo1, vo2; // idx from fBuff segment
410 Int_t vor1, vor2; // mapped idx
411 vo1 = fBuff->fSegs[3*s + 1];
412 vo2 = fBuff->fSegs[3*s + 2]; //... skip color info
413 vor1 = idxMap[vo1];
414 vor2 = idxMap[vo2];
415 if (vor1 == vor2) continue;
416 // check duplicate
417 for (auto &seg: segs)
418 {
419 Int_t vv1 = seg.fV1;
420 Int_t vv2 = seg.fV2;
421 if((vv1 == vor1 && vv2 == vor2) || (vv1 == vor2 && vv2 == vor1))
422 {
423 duplicate = kTRUE;
424 continue;
425 }
426 }
427 if (duplicate == kFALSE && projection->AcceptSegment(fPnts[vor1], fPnts[vor2], REveProjection::fgEps))
428 segs.emplace_back(vor1, vor2);
429 }
430
431 while (!segs.empty())
432 {
433 std::list<UInt_t> pp; // points in current polygon
434 pp.push_back(segs.front().fV1);
435 UInt_t tail = segs.front().fV2;
436 segs.pop_front();
437 Bool_t match = kTRUE;
438 while (match && ! segs.empty())
439 {
440 for (auto k = segs.begin(); k != segs.end(); ++k)
441 {
442 UInt_t cv1 = (*k).fV1;
443 UInt_t cv2 = (*k).fV2;
444 if (cv1 == tail || cv2 == tail)
445 {
446 pp.emplace_back(tail);
447 tail = (cv1 == tail) ? cv2 : cv1;
448 segs.erase(k);
449 match = kTRUE;
450 break;
451 }
452 else
453 {
454 match = kFALSE;
455 }
456 } // end for loop in the segment pool
457 if (tail == pp.front())
458 break;
459 }
460 surf += AddPolygon(pp, fPolsBS);
461 }
462 return surf;
463}
464
465////////////////////////////////////////////////////////////////////////////////
466/// Project current buffer.
467
469{
470 // create map from original to projected and reduced point needed only for geometry
471 auto idxMap = ProjectAndReducePoints();
472
474 switch (mode) {
476 MakePolygonsFromBP(idxMap);
477 fPolsBP.swap(fPols);
478 break;
479 }
481 MakePolygonsFromBS(idxMap);
482 fPolsBS.swap(fPols);
483 break;
484 }
486 // take projection with largest surface
487 Float_t surfBP = MakePolygonsFromBP(idxMap);
488 Float_t surfBS = MakePolygonsFromBS(idxMap);
489 if (surfBS < surfBP) {
490 fPolsBP.swap(fPols);
491 fPolsBS.clear();
492 } else {
493 fPolsBS.swap(fPols);
494 fPolsBP.clear();
495 }
496 break;
497 }
498 default: break;
499 }
500
501 ResetBBox();
502}
503
504////////////////////////////////////////////////////////////////////////////////
505/// Calculate XY surface of a polygon.
506
508{
509 Float_t surf = 0;
510 Int_t nPnts = p.NPoints();
511 for (Int_t i = 0; i < nPnts - 1; ++i)
512 {
513 Int_t a = p.fPnts[i];
514 Int_t b = p.fPnts[i+1];
515 surf += fPnts[a].fX * fPnts[b].fY - fPnts[a].fY * fPnts[b].fX;
516 }
517 return 0.5f * TMath::Abs(surf);
518}
519
520////////////////////////////////////////////////////////////////////////////////
521/// Dump information about built polygons.
522
524{
525 printf("REvePolygonSetProjected %d polygons\n", (Int_t)fPols.size());
526 Int_t cnt = 0;
527 for ( auto &pol : fPols)
528 {
529 Int_t nPnts = pol.NPoints();
530 printf("Points of polygon %d [Np = %d]:\n", ++cnt, nPnts);
531 for (Int_t vi = 0; vi<nPnts; ++vi) {
532 Int_t pi = pol.fPnts[vi];
533 printf(" (%f, %f, %f)", fPnts[pi].fX, fPnts[pi].fY, fPnts[pi].fZ);
534 }
535 printf(", surf=%f\n", PolygonSurfaceXY(pol));
536 }
537}
538
539////////////////////////////////////////////////////////////////////////////////
540/// Dump information about currently projected buffer.
541
543{
544 Int_t* bpols = fBuff->fPols;
545
546 for (UInt_t pi = 0; pi< fBuff->NbPols(); ++pi)
547 {
548 UInt_t segN = bpols[1];
549 printf("%d polygon of %d has %d segments \n", pi, fBuff->NbPols(), segN);
550
551 Int_t* seg = &bpols[2];
552 for (UInt_t a=0; a<segN; ++a)
553 {
554 Int_t a1 = fBuff->fSegs[3*seg[a] + 1];
555 Int_t a2 = fBuff->fSegs[3*seg[a] + 2];
556 printf("(%d, %d) \n", a1, a2);
557 printf("ORIG points :(%f, %f, %f) (%f, %f, %f)\n",
558 fBuff->fPnts[3*a1],fBuff->fPnts[3*a1+1], fBuff->fPnts[3*a1+2],
559 fBuff->fPnts[3*a2],fBuff->fPnts[3*a2+1], fBuff->fPnts[3*a2+2]);
560 }
561 printf("\n");
562 bpols += (segN+2);
563 }
564}
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define s0(x)
Definition RSha256.hxx:90
#define a(i)
Definition RSha256.hxx:99
#define s1(x)
Definition RSha256.hxx:91
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char mode
segment * segs
Definition X3DBuffer.c:23
polygon * polys
Definition X3DBuffer.c:24
void ProcessData(const std::vector< Double_t > &verts, const std::vector< UInt_t > &polys, const Int_t n_polys)
ProcessData.
virtual Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset)
Write core json.
std::unique_ptr< REveRenderData > fRenderData
Externally assigned and controlled user data.
virtual std::unique_ptr< TBuffer3D > MakeBuffer3D()
Create a TBuffer3D suitable for presentation of the shape.
void SetProjection(REveProjectionManager *mng, REveProjectable *model) override
This is virtual method from base-class REveProjected.
virtual void DumpPolys() const
Dump information about built polygons.
Bool_t IsFirstIdxHead(Int_t s0, Int_t s1)
Compare the two segments and check if the first index of first segment is starting.
vpPolygon_t fPolsBS
! polygons build from TBuffer3D segments
void BuildRenderData() override
Crates representation for rendering.
void DumpBuffer3D()
Dump information about currently projected buffer.
void UpdateProjection() override
This is virtual method from base-class REveProjected.
vpPolygon_t fPolsBP
! polygons build from TBuffer3D polygons
std::vector< UInt_t > ProjectAndReducePoints()
Project and reduce buffer points.
std::vector< REveVector > fPnts
! reduced and projected points
Float_t PolygonSurfaceXY(const Polygon_t &poly) const
Calculate XY surface of a polygon.
void ComputeBBox() override
Override of virtual method from TAttBBox.
Float_t MakePolygonsFromBS(std::vector< UInt_t > &idxMap)
Build polygons from the set of buffer segments.
Int_t WriteCoreJson(nlohmann::json &j, Int_t rnr_offset) override
Fill core part of JSON representation.
void SetDepthLocal(Float_t d) override
Set depth (z-coordinate) of the projected points.
Float_t MakePolygonsFromBP(std::vector< UInt_t > &idxMap)
Build polygons from list of buffer polygons.
Float_t AddPolygon(std::list< UInt_t > &pp, std::list< Polygon_t > &p)
Check if polygon has dimensions above REveProjection::fgEps and add it to a list if it is not a dupli...
virtual void SetProjection(REveProjectionManager *mng, REveProjectable *model)
Sets projection manager and reference in the projectable object.
void SetDepthCommon(Float_t d, REveElement *el, Float_t *bbox)
Utility function to update the z-values of the bounding-box.
REveProjectionManager Manager class for steering of projections and managing projected objects.
REveProjection Base for specific classes that implement non-linear projections.
virtual void ProjectPoint(Float_t &x, Float_t &y, Float_t &z, Float_t d, EPProc_e p=kPP_Full)=0
virtual Bool_t AcceptSegment(REveVector &, REveVector &, Float_t) const
void CopyVizParams(const REveElement *el) override
Copy visualization parameters from element el.
Definition REveShape.cxx:86
void BBoxCheckPoint(Float_t x, Float_t y, Float_t z)
Definition TAttBBox.h:69
void ResetBBox()
Definition TAttBBox.h:57
void BBoxZero(Float_t epsilon=0, Float_t x=0, Float_t y=0, Float_t z=0)
Create cube of volume (2*epsilon)^3 at (x,y,z).
Definition TAttBBox.cxx:42
void BBoxInit(Float_t infinity=1e6)
Dynamic Float_t[6] X(min,max), Y(min,max), Z(min,max)
Definition TAttBBox.cxx:29
Float_t * fBBox
Definition TAttBBox.h:20
const Int_t n
Definition legend1.C:16
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
basic_json<> json