Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
REveShape.cxx
Go to the documentation of this file.
1// @(#)root/eve7:$Id$
2// Author: Matevz Tadel, 2010
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 <ROOT/REveShape.hxx>
13
14#include <nlohmann/json.hpp>
15
16using namespace ROOT::Experimental;
17
18/** \class REveShape
19\ingroup REve
20Abstract base-class for 2D/3D shapes.
21
22It provides:
23 - fill color / transparency, accessible via Get/SetMainColor/Transparency;
24 - frame line color / width;
25 - flag if frame should be drawn;
26 - flag specifying whether frame or whole shape should be emphasised for
27 highlight.
28*/
29
30////////////////////////////////////////////////////////////////////////////////
31/// Constructor.
32
33REveShape::REveShape(const std::string &n, const std::string &t) :
34 REveElement(n, t)
35{
39}
40
41////////////////////////////////////////////////////////////////////////////////
42/// Destructor.
43
45{
46}
47
48////////////////////////////////////////////////////////////////////////////////
49/// Fill core part of JSON representation.
50
51Int_t REveShape::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
52{
53 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
54
55 j["fFillColor"] = fFillColor;
56 j["fLineColor"] = fLineColor;
57 j["fFillAlpha"] = fFillAlpha;
58 j["fLineAlpha"] = fLineAlpha;
59 j["fLineWidth"] = fLineWidth;
60 j["fDrawFrame"] = fDrawFrame;
61
62 return ret;
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// Set main color.
67/// Override so that line-color can also be changed if it is equal
68/// to fill color (which is treated as main color).
69
71{
72 if (fFillColor == fLineColor) {
73 fLineColor = color;
75 }
77}
78
79////////////////////////////////////////////////////////////////////////////////
80/// Copy visualization parameters from element el.
81
83{
84 const REveShape* m = dynamic_cast<const REveShape*>(el);
85 if (m)
86 {
87 fFillColor = m->fFillColor;
88 fLineColor = m->fLineColor;
89 fLineWidth = m->fLineWidth;
90 fDrawFrame = m->fDrawFrame;
91 fHighlightFrame = m->fHighlightFrame;
92 fMiniFrame = m->fMiniFrame;
93 }
94
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Write visualization parameters.
100
101void REveShape::WriteVizParams(std::ostream& out, const TString& var)
102{
104
105 TString t = " " + var + "->";
106 out << t << "SetFillColor(" << fFillColor << ");\n";
107 out << t << "SetLineColor(" << fLineColor << ");\n";
108 out << t << "SetLineWidth(" << fLineWidth << ");\n";
109 out << t << "SetDrawFrame(" << ToString(fDrawFrame) << ");\n";
110 out << t << "SetHighlightFrame(" << ToString(fHighlightFrame) << ");\n";
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Determines the convex-hull of points in pin.
115///
116/// Adds the hull points to pout and returns the number of added points.
117/// If size of pout is less then 3 then either the number of input points
118/// was too low or they were degenerate so that the hull is actually a line
119/// segment or even a point.
120
122{
123 Int_t N = pin.size();
124
125 // Find the minimum (bottom-left) point.
126 Int_t min_point = 0;
127 for (Int_t i = 1; i < N; ++i)
128 {
129 if (pin[i].fY < pin[min_point].fY || (pin[i].fY == pin[min_point].fY && pin[i].fX < pin[min_point].fX))
130 min_point = i;
131 }
132
133 // Calculate angles and sort.
134 std::vector<Float_t> angles(N);
135 for (Int_t i = 0; i < N; ++i)
136 {
137 angles[i] = (pin[i] - pin[min_point]).Phi();
138 }
139 std::vector<Int_t> idcs(N);
140 TMath::Sort(N, &angles[0], &idcs[0], kFALSE);
141
142 // Weed out points with the same angle -- keep the furthest only.
143 // The first point must stay.
144 if (N > 2)
145 {
146 std::vector<Int_t> new_idcs;
147 new_idcs.push_back(idcs[0]);
148 auto a = idcs.begin(); ++a;
149 auto b = a; ++b;
150 while (b != idcs.end())
151 {
152 if (TMath::Abs(angles[*a] - angles[*b]) < 1e-5f)
153 {
154 if (pin[idcs[0]].SquareDistance(pin[*a]) < pin[idcs[0]].SquareDistance(pin[*b]))
155 a = b;
156 }
157 else
158 {
159 new_idcs.push_back(*a);
160 a = b;
161 }
162 ++b;
163 }
164 new_idcs.push_back(*a);
165 idcs.swap(new_idcs);
166 }
167
168 N = idcs.size();
169
170 // Find hull.
171 std::vector<Int_t> hull;
172 if (N > 2)
173 {
174 hull.push_back(idcs[0]);
175 hull.push_back(idcs[1]);
176 hull.push_back(idcs[2]);
177 {
178 Int_t i = 3;
179 while (i < N)
180 {
181 Int_t n = hull.size() - 1;
182 if ((pin[hull[n]] - pin[hull[n-1]]).Cross(pin[idcs[i]] - pin[hull[n]]) > 0)
183 {
184 hull.push_back(idcs[i]);
185 ++i;
186 }
187 else
188 {
189 hull.pop_back();
190 }
191 }
192 }
193 }
194 else
195 {
196 ::Warning("REveShape::FindConvexHull()", "Polygon reduced to %d points. for '%s'.",
197 N, caller ? caller->GetCName() : "unknown");
198 hull.swap(idcs);
199 }
200
201 // Add hull points into the output vector.
202 N = hull.size();
203 Int_t Nold = pout.size();
204 pout.resize(Nold + N);
205 for (Int_t i = 0; i < N; ++i)
206 {
207 pout[Nold + i] = pin[hull[i]];
208 }
209
210 // Print the hull.
211 // for (Int_t i = 0; i < N; ++i)
212 // {
213 // const REveVector2 &p = pin[hull[i]];
214 // printf("%d [%d] (%5.1f, %5.1f) %f\n", i, hull[i], p.fX, p.fY, angles[hull[i]]);
215 // }
216
217 return N;
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// Checks if the first face normal is pointing into the other
222/// direction as the vector pointing towards the opposite face.
223/// This assumes standard box vertex arrangement.
224
226{
227 REveVector f1 = box[1] - box[0];
228 REveVector f2 = box[3] - box[0];
229 REveVector up = box[4] - box[0];
230
231 return up.Dot(f1.Cross(f2)) < 0;
232}
233
234////////////////////////////////////////////////////////////////////////////////
235/// Checks if the first face normal is pointing into the other
236/// direction as the vector pointing towards the opposite face.
237/// This assumes standard box vertex arrangement.
238
240{
241 REveVector b0(box[0]);
242 REveVector f1(box[1]); f1 -= b0;
243 REveVector f2(box[3]); f2 -= b0;
244 REveVector up(box[4]); up -= b0;
245
246 return up.Dot(f1.Cross(f2)) < 0;
247}
248
249////////////////////////////////////////////////////////////////////////////////
250/// Make sure box orientation is consistent with standard arrangement.
251
253{
255 {
256 std::swap(box[1], box[3]);
257 std::swap(box[5], box[7]);
258 }
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Make sure box orientation is consistent with standard arrangement.
263
265{
267 {
268 std::swap(box[1][0], box[3][0]);
269 std::swap(box[1][1], box[3][1]);
270 std::swap(box[1][2], box[3][2]);
271 std::swap(box[5][0], box[7][0]);
272 std::swap(box[5][1], box[7][1]);
273 std::swap(box[5][2], box[7][2]);
274 }
275}
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
short Color_t
Definition RtypesCore.h:85
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
#define N
TGLVector3 Cross(const TGLVector3 &v1, const TGLVector3 &v2)
Definition TGLUtil.h:323
void SetMainColorPtr(Color_t *colptr)
virtual Int_t WriteCoreJson(nlohmann::json &cj, Int_t rnr_offset)
Write core json.
const char * GetCName() const
virtual void WriteVizParams(std::ostream &out, const TString &var)
Write-out visual parameters for this object.
static const std::string & ToString(Bool_t b)
Convert Bool_t to string - kTRUE or kFALSE.
virtual void CopyVizParams(const REveElement *el)
Copy visualization parameters from element el.
virtual void SetMainColor(Color_t color)
Set main color of the element.
void WriteVizParams(std::ostream &out, const TString &var) override
Write visualization parameters.
static Int_t FindConvexHull(const vVector2_t &pin, vVector2_t &pout, REveElement *caller=nullptr)
Determines the convex-hull of points in pin.
void CopyVizParams(const REveElement *el) override
Copy visualization parameters from element el.
Definition REveShape.cxx:82
std::vector< REveVector2 > vVector2_t
Definition REveShape.hxx:37
static void CheckAndFixBoxOrientationFv(Float_t box[8][3])
Make sure box orientation is consistent with standard arrangement.
static Bool_t IsBoxOrientationConsistentEv(const REveVector box[8])
Checks if the first face normal is pointing into the other direction as the vector pointing towards t...
static Bool_t IsBoxOrientationConsistentFv(const Float_t box[8][3])
Checks if the first face normal is pointing into the other direction as the vector pointing towards t...
Int_t WriteCoreJson(nlohmann::json &j, Int_t rnr_offset) override
Fill core part of JSON representation.
Definition REveShape.cxx:51
static void CheckAndFixBoxOrientationEv(REveVector box[8])
Make sure box orientation is consistent with standard arrangement.
~REveShape() override
Destructor.
Definition REveShape.cxx:44
void SetMainColor(Color_t color) override
Set main color.
Definition REveShape.cxx:70
TT Dot(const REveVectorT &a) const
Basic string class.
Definition TString.h:139
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
TF1 * f1
Definition legend1.C:11
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Sort the n elements of the array a of generic templated type Element.
Definition TMathBase.h:431
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
TMarker m
Definition textangle.C:8