Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RPadLength.hxx
Go to the documentation of this file.
1/// \defgroup ROOT7PadCoordSystems ROOT7 RPad coordinate systems
2/// \ingroup ROOT7Graphics
3/// \brief The ROOT7 RPad coordinate systems.
4///
5/// \author Axel Naumann <axel@cern.ch>
6/// \date 2017-07-06
7
8/*************************************************************************
9 * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT7_RPadLength
17#define ROOT7_RPadLength
18
19#include <vector>
20#include <string>
21#include <cmath>
22
23namespace ROOT {
24namespace Experimental {
25
26 /** \class RPadLength
27 \ingroup ROOT7PadCoordSystems
28 \brief A length in RPad.
29 */
30
32
33protected:
34
35 std::vector<double> fArr; ///< components [0] - normalized, [1] - pixel, [2] - user
36
37public:
38 template <class DERIVED>
39 struct CoordSysBase {
40 double fVal{0.}; ///<! Coordinate value
41
42 CoordSysBase() = default;
43 CoordSysBase(double val): fVal(val) {}
44 DERIVED &ToDerived() { return static_cast<DERIVED &>(*this); }
45
46 DERIVED operator-() { return DERIVED(-fVal); }
47
48 friend DERIVED operator+(DERIVED lhs, DERIVED rhs) { return DERIVED{lhs.fVal + rhs.fVal}; }
49 friend DERIVED operator-(DERIVED lhs, DERIVED rhs) { return DERIVED{lhs.fVal - rhs.fVal}; }
50 friend double operator/(DERIVED lhs, DERIVED rhs) { return lhs.fVal / rhs.fVal; }
51 DERIVED &operator+=(const DERIVED &rhs)
52 {
53 fVal += rhs.fVal;
54 return ToDerived();
55 }
56 DERIVED &operator-=(const DERIVED &rhs)
57 {
58 fVal -= rhs.fVal;
59 return ToDerived();
60 }
61 DERIVED &operator*=(double scale)
62 {
63 fVal *= scale;
64 return ToDerived();
65 }
66 friend DERIVED operator*(const DERIVED &lhs, double rhs) { return DERIVED(lhs.fVal * rhs); }
67 friend DERIVED operator*(double lhs, const DERIVED &rhs) { return DERIVED(lhs * rhs.fVal); }
68 friend DERIVED operator/(const DERIVED &lhs, double rhs) { return DERIVED(lhs.fVal * rhs); }
69 friend bool operator<(const DERIVED &lhs, const DERIVED &rhs) { return lhs.fVal < rhs.fVal; }
70 friend bool operator>(const DERIVED &lhs, const DERIVED &rhs) { return lhs.fVal > rhs.fVal; }
71 friend bool operator<=(const DERIVED &lhs, const DERIVED &rhs) { return lhs.fVal <= rhs.fVal; }
72 friend bool operator>=(const DERIVED &lhs, const DERIVED &rhs) { return lhs.fVal >= rhs.fVal; }
73 // no ==, !=
74 };
75
76 /// \defgroup TypesafeCoordinates Typesafe Coordinates
77 /// \ingroup ROOT7PadCoordSystems
78 /// These define typesafe coordinates used by RPad to identify which coordinate system a coordinate is referring to
79 /// The origin (0,0) is in the `RPad`'s bottom left corner for all of them.
80 /// \{
81
82 /** \struct Normal
83 \brief A normalized coordinate.
84
85 0 in the left, bottom corner, 1 in the top, right corner of the `RPad`.
86 Resizing the pad will resize the objects with it.
87 */
88 struct Normal: CoordSysBase<Normal> {
90 };
91
92 /** \struct Pixel
93 \brief A pixel coordinate.
94
95 0 in the left, bottom corner, 1 in the top, right corner of the `RPad`.
96 Resizing the pad will keep the pixel-position of the objects positioned in `Pixel` coordinates.
97 */
98 struct Pixel: CoordSysBase<Pixel> {
100 };
101
102 /** \struct User
103 \brief A user coordinate.
104
105 as defined by the EUserCoordSystem parameter of the `RPad`.
106 */
107 struct User: CoordSysBase<User> {
109 };
110 /// \}
111
113
114 /// Constructor from a `Normal` coordinate.
115 RPadLength(Normal normal): RPadLength() { SetNormal(normal.fVal); }
116
117 /// By default all numeric values are normal values
118 RPadLength(double normal): RPadLength() { SetNormal(normal); }
119
120 /// Constructor from a `Pixel` coordinate.
122
123 /// Constructor from a `User` coordinate.
124 RPadLength(User user) : RPadLength() { SetUser(user.fVal); }
125
126 /// Constructor for normal and pixel coordinate.
127 RPadLength(Normal normal, Pixel px): RPadLength() { SetPixel(px.fVal); SetNormal(normal.fVal); }
128
129 /// Constructor for normal, pixel and user coordinate.
130 RPadLength(Normal normal, Pixel px, User user): RPadLength() { SetUser(user.fVal); SetPixel(px.fVal); SetNormal(normal.fVal); }
131
132 /// Constructor from string representation
133 RPadLength(const std::string &csscode) : RPadLength() { if (!csscode.empty()) ParseString(csscode); }
134
135 bool HasNormal() const { return !fArr.empty(); }
136 bool HasPixel() const { return fArr.size() > 1; }
137 bool HasUser() const { return fArr.size() > 2; }
138
140 {
141 if (fArr.empty())
142 fArr.resize(1);
143 fArr[0] = v;
144 return *this;
145 }
146
148 {
149 if (fArr.size() < 2)
150 fArr.resize(2, 0.);
151 fArr[1] = v;
152 return *this;
153 }
154
156 {
157 if (fArr.size() < 3)
158 fArr.resize(3, 0.);
159 fArr[2] = v;
160 return *this;
161 }
162
163 double GetNormal() const { return !fArr.empty() ? fArr[0] : 0.; }
164 double GetPixel() const { return fArr.size() > 1 ? fArr[1] : 0.; }
165 double GetUser() const { return fArr.size() > 2 ? fArr[2] : 0.; }
166
168 {
169 if (fArr.size() > 2)
170 fArr.resize(2);
171 }
172
174 {
175 if (fArr.size() > 1)
176 fArr.resize(1);
177 }
178
179 void Clear() { fArr.clear(); }
180
181 bool Empty() const { return fArr.empty(); }
182
183 /// Add two `RPadLength`s.
185 {
186 RPadLength res;
187 if (lhs.HasUser() || rhs.HasUser())
188 res.SetUser(lhs.GetUser() + rhs.GetUser());
189 if (lhs.HasPixel() || rhs.HasPixel())
190 res.SetPixel(lhs.GetPixel() + rhs.GetPixel());
191 if (lhs.HasNormal() || rhs.HasNormal())
192 res.SetNormal(lhs.GetNormal() + rhs.GetNormal());
193 return res;
194 }
195
196 /// Subtract two `RPadLength`s.
198 {
199 RPadLength res;
200 if (lhs.HasUser() || rhs.HasUser())
201 res.SetUser(lhs.GetUser() - rhs.GetUser());
202 if (lhs.HasPixel() || rhs.HasPixel())
203 res.SetPixel(lhs.GetPixel() - rhs.GetPixel());
204 if (lhs.HasNormal() || rhs.HasNormal())
205 res.SetNormal(lhs.GetNormal() - rhs.GetNormal());
206 return res;
207 }
208
209 /// Unary -.
211 {
212 RPadLength res;
213 if (HasUser()) res.SetUser(-GetUser());
214 if (HasPixel()) res.SetPixel(-GetPixel());
215 if (HasNormal()) res.SetNormal(-GetNormal());
216 return res;
217 }
218
219 /// Add a `RPadLength`.
221 {
222 if (HasUser() || rhs.HasUser())
223 SetUser(GetUser() + rhs.GetUser());
224 if (HasPixel() || rhs.HasPixel())
225 SetPixel(GetPixel() + rhs.GetPixel());
226 if (HasNormal() || rhs.HasNormal())
227 SetNormal(GetNormal() + rhs.GetNormal());
228 return *this;
229 }
230
231 /// Subtract a `RPadLength`.
233 {
234 if (HasUser() || rhs.HasUser())
235 SetUser(GetUser() - rhs.GetUser());
236 if (HasPixel() || rhs.HasPixel())
237 SetPixel(GetPixel() - rhs.GetPixel());
238 if (HasNormal() || rhs.HasNormal())
239 SetNormal(GetNormal() - rhs.GetNormal());
240 return *this;
241 }
242
243 /// Multiply a `RPadLength`.
244 RPadLength &operator*=(double scale)
245 {
246 if (HasUser()) SetUser(scale*GetUser());
247 if (HasPixel()) SetPixel(scale*GetPixel());
248 if (HasNormal()) SetNormal(scale*GetNormal());
249 return *this;
250 }
251
252 /// Compare a `RPadLength`.
253 bool operator==(const RPadLength &rhs) const
254 {
255 if ((HasUser() != rhs.HasUser()) ||
256 (HasUser() && std::fabs(GetUser() - rhs.GetUser()) > 1e-30)) return false;
257
258 if(std::fabs(GetPixel() - rhs.GetPixel()) > 1e-4) return false;
259
260 if (std::fabs(GetNormal() - rhs.GetNormal()) > 1e-6) return false;
261
262 return true;
263 };
264
265 std::string AsString() const;
266
267 bool ParseString(const std::string &val);
268
269};
270
271/// User-defined literal for `RPadLength::Normal`
272///
273/// Use as
274/// ```
275/// using namespace ROOT::Experimental;
276/// RLine(0.1_normal, 0.0_normal, RLineExtent(0.2_normal, 0.5_normal));
277/// ```
278inline RPadLength::Normal operator"" _normal(long double val)
279{
280 return RPadLength::Normal{(double)val};
281}
282inline RPadLength::Normal operator"" _normal(unsigned long long int val)
283{
284 return RPadLength::Normal{(double)val};
285}
286
287/// User-defined literal for `RPadLength::Pixel`
288///
289/// Use as
290/// ```
291/// using namespace ROOT::Experimental;
292/// RLine(100_px, 0_px, RLineExtent(20_px, 50_px));
293/// ```
294inline RPadLength::Pixel operator"" _px(long double val)
295{
296 return RPadLength::Pixel{(double)val};
297}
298inline RPadLength::Pixel operator"" _px(unsigned long long int val)
299{
300 return RPadLength::Pixel{(double)val};
301}
302
303/// User-defined literal for `RPadLength::User`
304///
305/// Use as
306/// ```
307/// using namespace ROOT::Experimental;
308/// RLine(0.1_user, 0.0_user, RLineExtent(0.2_user, 0.5_user));
309/// ```
310inline RPadLength::User operator"" _user(long double val)
311{
312 return RPadLength::User{(double)val};
313}
314inline RPadLength::User operator"" _user(unsigned long long int val)
315{
316 return RPadLength::User{(double)val};
317}
318
319} // namespace Experimental
320} // namespace ROOT
321
322#endif
#define e(i)
Definition RSha256.hxx:103
RPadLength & SetNormal(double v)
friend RPadLength operator+(RPadLength lhs, const RPadLength &rhs)
Add two RPadLengths.
friend RPadLength operator-(RPadLength lhs, const RPadLength &rhs)
Subtract two RPadLengths.
RPadLength(User user)
Constructor from a User coordinate.
std::string AsString() const
Converts RPadLength to string like "0.1 + 25px" User coordinates not (yet) supported.
RPadLength(Normal normal, Pixel px, User user)
Constructor for normal, pixel and user coordinate.
RPadLength(Normal normal)
Constructor from a Normal coordinate.
RPadLength(Pixel px)
Constructor from a Pixel coordinate.
bool operator==(const RPadLength &rhs) const
Compare a RPadLength.
RPadLength & operator-=(const RPadLength &rhs)
Subtract a RPadLength.
RPadLength & SetPixel(double v)
RPadLength(double normal)
By default all numeric values are normal values.
bool ParseString(const std::string &val)
Parse string and fill RPadLength attributes String can be like "0.1 + 25px" User coordinates not (yet...
RPadLength operator-()
Unary -.
RPadLength & SetUser(double v)
RPadLength & operator*=(double scale)
Multiply a RPadLength.
std::vector< double > fArr
components [0] - normalized, [1] - pixel, [2] - user
RPadLength(const std::string &csscode)
Constructor from string representation.
RPadLength & operator+=(const RPadLength &rhs)
Add a RPadLength.
RPadLength(Normal normal, Pixel px)
Constructor for normal and pixel coordinate.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
friend bool operator>=(const DERIVED &lhs, const DERIVED &rhs)
friend double operator/(DERIVED lhs, DERIVED rhs)
friend bool operator<=(const DERIVED &lhs, const DERIVED &rhs)
friend DERIVED operator*(double lhs, const DERIVED &rhs)
friend bool operator>(const DERIVED &lhs, const DERIVED &rhs)
friend DERIVED operator*(const DERIVED &lhs, double rhs)
friend DERIVED operator/(const DERIVED &lhs, double rhs)
DERIVED & operator+=(const DERIVED &rhs)
friend DERIVED operator+(DERIVED lhs, DERIVED rhs)
friend DERIVED operator-(DERIVED lhs, DERIVED rhs)
friend bool operator<(const DERIVED &lhs, const DERIVED &rhs)
DERIVED & operator-=(const DERIVED &rhs)