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 fFillColor(5),
36 fLineColor(5),
37 fLineWidth(1),
38 fDrawFrame(kTRUE),
39 fHighlightFrame(kFALSE),
40 fMiniFrame(kTRUE)
41{
45}
46
47////////////////////////////////////////////////////////////////////////////////
48/// Destructor.
49
51{
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Fill core part of JSON representation.
56
57Int_t REveShape::WriteCoreJson(nlohmann::json &j, Int_t rnr_offset)
58{
59 Int_t ret = REveElement::WriteCoreJson(j, rnr_offset);
60
61 j["fFillColor"] = fFillColor;
62 j["fLineColor"] = fLineColor;
63 j["fLineWidth"] = fLineWidth;
64 j["fDrawFrame"] = fDrawFrame;
65
66 return ret;
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Set main color.
71/// Override so that line-color can also be changed if it is equal
72/// to fill color (which is treated as main color).
73
75{
76 if (fFillColor == fLineColor) {
77 fLineColor = color;
79 }
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Copy visualization parameters from element el.
85
87{
88 const REveShape* m = dynamic_cast<const REveShape*>(el);
89 if (m)
90 {
91 fFillColor = m->fFillColor;
92 fLineColor = m->fLineColor;
93 fLineWidth = m->fLineWidth;
94 fDrawFrame = m->fDrawFrame;
95 fHighlightFrame = m->fHighlightFrame;
96 fMiniFrame = m->fMiniFrame;
97 }
98
100}
101
102////////////////////////////////////////////////////////////////////////////////
103/// Write visualization parameters.
104
105void REveShape::WriteVizParams(std::ostream& out, const TString& var)
106{
108
109 TString t = " " + var + "->";
110 out << t << "SetFillColor(" << fFillColor << ");\n";
111 out << t << "SetLineColor(" << fLineColor << ");\n";
112 out << t << "SetLineWidth(" << fLineWidth << ");\n";
113 out << t << "SetDrawFrame(" << ToString(fDrawFrame) << ");\n";
114 out << t << "SetHighlightFrame(" << ToString(fHighlightFrame) << ");\n";
115}
116
117////////////////////////////////////////////////////////////////////////////////
118/// Determines the convex-hull of points in pin.
119///
120/// Adds the hull points to pout and returns the number of added points.
121/// If size of pout is less then 3 then either the number of input points
122/// was too low or they were degenerate so that the hull is actually a line
123/// segment or even a point.
124
126{
127 Int_t N = pin.size();
128
129 // Find the minimum (bottom-left) point.
130 Int_t min_point = 0;
131 for (Int_t i = 1; i < N; ++i)
132 {
133 if (pin[i].fY < pin[min_point].fY || (pin[i].fY == pin[min_point].fY && pin[i].fX < pin[min_point].fX))
134 min_point = i;
135 }
136
137 // Calculate angles and sort.
138 std::vector<Float_t> angles(N);
139 for (Int_t i = 0; i < N; ++i)
140 {
141 angles[i] = (pin[i] - pin[min_point]).Phi();
142 }
143 std::vector<Int_t> idcs(N);
144 TMath::Sort(N, &angles[0], &idcs[0], kFALSE);
145
146 // Weed out points with the same angle -- keep the furthest only.
147 // The first point must stay.
148 if (N > 2)
149 {
150 std::vector<Int_t> new_idcs;
151 new_idcs.push_back(idcs[0]);
152 auto a = idcs.begin(); ++a;
153 auto b = a; ++b;
154 while (b != idcs.end())
155 {
156 if (TMath::Abs(angles[*a] - angles[*b]) < 1e-5f)
157 {
158 if (pin[idcs[0]].SquareDistance(pin[*a]) < pin[idcs[0]].SquareDistance(pin[*b]))
159 a = b;
160 }
161 else
162 {
163 new_idcs.push_back(*a);
164 a = b;
165 }
166 ++b;
167 }
168 new_idcs.push_back(*a);
169 idcs.swap(new_idcs);
170 }
171
172 N = idcs.size();
173
174 // Find hull.
175 std::vector<Int_t> hull;
176 if (N > 2)
177 {
178 hull.push_back(idcs[0]);
179 hull.push_back(idcs[1]);
180 hull.push_back(idcs[2]);
181 {
182 Int_t i = 3;
183 while (i < N)
184 {
185 Int_t n = hull.size() - 1;
186 if ((pin[hull[n]] - pin[hull[n-1]]).Cross(pin[idcs[i]] - pin[hull[n]]) > 0)
187 {
188 hull.push_back(idcs[i]);
189 ++i;
190 }
191 else
192 {
193 hull.pop_back();
194 }
195 }
196 }
197 }
198 else
199 {
200 ::Warning("REveShape::FindConvexHull()", "Polygon reduced to %d points. for '%s'.",
201 N, caller ? caller->GetCName() : "unknown");
202 hull.swap(idcs);
203 }
204
205 // Add hull points into the output vector.
206 N = hull.size();
207 Int_t Nold = pout.size();
208 pout.resize(Nold + N);
209 for (Int_t i = 0; i < N; ++i)
210 {
211 pout[Nold + i] = pin[hull[i]];
212 }
213
214 // Print the hull.
215 // for (Int_t i = 0; i < N; ++i)
216 // {
217 // const REveVector2 &p = pin[hull[i]];
218 // printf("%d [%d] (%5.1f, %5.1f) %f\n", i, hull[i], p.fX, p.fY, angles[hull[i]]);
219 // }
220
221 return N;
222}
223
224////////////////////////////////////////////////////////////////////////////////
225/// Checks if the first face normal is pointing into the other
226/// direction as the vector pointing towards the opposite face.
227/// This assumes standard box vertex arrangement.
228
230{
231 REveVector f1 = box[1] - box[0];
232 REveVector f2 = box[3] - box[0];
233 REveVector up = box[4] - box[0];
234
235 return up.Dot(f1.Cross(f2)) < 0;
236}
237
238////////////////////////////////////////////////////////////////////////////////
239/// Checks if the first face normal is pointing into the other
240/// direction as the vector pointing towards the opposite face.
241/// This assumes standard box vertex arrangement.
242
244{
245 REveVector b0(box[0]);
246 REveVector f1(box[1]); f1 -= b0;
247 REveVector f2(box[3]); f2 -= b0;
248 REveVector up(box[4]); up -= b0;
249
250 return up.Dot(f1.Cross(f2)) < 0;
251}
252
253////////////////////////////////////////////////////////////////////////////////
254/// Make sure box orientation is consistent with standard arrangement.
255
257{
259 {
260 std::swap(box[1], box[3]);
261 std::swap(box[5], box[7]);
262 }
263}
264
265////////////////////////////////////////////////////////////////////////////////
266/// Make sure box orientation is consistent with standard arrangement.
267
269{
271 {
272 std::swap(box[1][0], box[3][0]);
273 std::swap(box[1][1], box[3][1]);
274 std::swap(box[1][2], box[3][2]);
275 std::swap(box[5][0], box[7][0]);
276 std::swap(box[5][1], box[7][1]);
277 std::swap(box[5][2], box[7][2]);
278 }
279}
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
short Color_t
Definition RtypesCore.h:92
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
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:86
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:57
static void CheckAndFixBoxOrientationEv(REveVector box[8])
Make sure box orientation is consistent with standard arrangement.
~REveShape() override
Destructor.
Definition REveShape.cxx:50
void SetMainColor(Color_t color) override
Set main color.
Definition REveShape.cxx:74
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