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