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 #include <cmath>
22 
23 namespace ROOT {
24 namespace Experimental {
25 
26  /** \class RPadLength
27  \ingroup ROOT7PadCoordSystems
28  \brief A length in RPad.
29  */
30 
31 class RPadLength {
32 
33 protected:
34 
35  std::vector<double> fArr; ///< components [0] - normalized, [1] - pixel, [2] - user
36 
37 public:
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 
112  RPadLength() {}
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.
121  RPadLength(Pixel px): RPadLength() { SetPixel(px.fVal); }
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.size() > 0; }
136  bool HasPixel() const { return fArr.size() > 1; }
137  bool HasUser() const { return fArr.size() > 2; }
138 
140  {
141  if (fArr.size() < 1)
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 
155  RPadLength &SetUser(double v)
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.size() > 0 ? 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 
167  void ClearUser()
168  {
169  if (fArr.size() > 2)
170  fArr.resize(2);
171  }
172 
173  void ClearPixelAndUser()
174  {
175  if (fArr.size() > 1)
176  fArr.resize(1);
177  }
178 
179  void Clear() { fArr.clear(); }
180 
181  bool Empty() const { return fArr.size() == 0; }
182 
183  /// Add two `RPadLength`s.
184  friend RPadLength operator+(RPadLength lhs, const RPadLength &rhs)
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.
197  friend RPadLength operator-(RPadLength lhs, const RPadLength &rhs)
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`.
220  RPadLength &operator+=(const RPadLength &rhs)
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 /// ```
278 inline RPadLength::Normal operator"" _normal(long double val)
279 {
280  return RPadLength::Normal{(double)val};
281 }
282 inline 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 /// ```
294 inline RPadLength::Pixel operator"" _px(long double val)
295 {
296  return RPadLength::Pixel{(double)val};
297 }
298 inline 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 /// ```
310 inline RPadLength::User operator"" _user(long double val)
311 {
312  return RPadLength::User{(double)val};
313 }
314 inline 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
ROOT::Experimental::RPadLength::CoordSysBase::operator>=
friend bool operator>=(const DERIVED &lhs, const DERIVED &rhs)
Definition: RPadLength.hxx:84
ROOT::Experimental::RPadLength::HasNormal
bool HasNormal() const
Definition: RPadLength.hxx:147
e
#define e(i)
Definition: RSha256.hxx:121
ROOT::Experimental::RPadLength::operator-=
RPadLength & operator-=(const RPadLength &rhs)
Subtract a RPadLength.
Definition: RPadLength.hxx:244
ROOT::Experimental::RPadLength::ClearUser
void ClearUser()
Definition: RPadLength.hxx:179
ROOT::Experimental::RPadLength::Empty
bool Empty() const
Definition: RPadLength.hxx:193
ROOT::Experimental::RPadLength::RPadLength
RPadLength(Pixel px)
Constructor from a Pixel coordinate.
Definition: RPadLength.hxx:133
ROOT::Experimental::RPadLength::AsString
std::string AsString() const
Converts RPadLength to string like "0.1 + 25px" User coordinates not (yet) supported.
Definition: RPadLength.cxx:19
ROOT::Experimental::RPadLength::SetUser
RPadLength & SetUser(double v)
Definition: RPadLength.hxx:167
ROOT::Experimental::RPadLength::operator*=
RPadLength & operator*=(double scale)
Multiply a RPadLength.
Definition: RPadLength.hxx:256
ROOT::Experimental::RPadLength::CoordSysBase::ToDerived
DERIVED & ToDerived()
Definition: RPadLength.hxx:56
ROOT::Experimental::RPadLength::GetUser
double GetUser() const
Definition: RPadLength.hxx:177
ROOT::Experimental::RPadLength::RPadLength
RPadLength()
Definition: RPadLength.hxx:124
ROOT::Experimental::RPadLength::Pixel
A pixel coordinate.
Definition: RPadLength.hxx:110
ROOT::Experimental::RPadLength::operator+
friend RPadLength operator+(RPadLength lhs, const RPadLength &rhs)
Add two RPadLengths.
Definition: RPadLength.hxx:196
ROOT::Experimental::RPadLength::operator-
RPadLength operator-()
Unary -.
Definition: RPadLength.hxx:222
ROOT::Experimental::RPadLength::SetPixel
RPadLength & SetPixel(double v)
Definition: RPadLength.hxx:159
ROOT::Experimental::RPadLength::operator==
bool operator==(const RPadLength &rhs) const
Compare a RPadLength.
Definition: RPadLength.hxx:265
ROOT::Experimental::RPadLength::CoordSysBase
Definition: RPadLength.hxx:51
ROOT::Experimental::RPadLength::CoordSysBase::operator*
friend DERIVED operator*(const DERIVED &lhs, double rhs)
Definition: RPadLength.hxx:78
ROOT::Experimental::RPadLength::HasPixel
bool HasPixel() const
Definition: RPadLength.hxx:148
v
@ v
Definition: rootcling_impl.cxx:3635
ROOT::Experimental::RPadLength::CoordSysBase::operator/
friend double operator/(DERIVED lhs, DERIVED rhs)
Definition: RPadLength.hxx:62
ROOT::Experimental::RPadLength::GetNormal
double GetNormal() const
Definition: RPadLength.hxx:175
ROOT::Experimental::RPadLength::CoordSysBase::operator>
friend bool operator>(const DERIVED &lhs, const DERIVED &rhs)
Definition: RPadLength.hxx:82
ROOT::Experimental::RPadLength
A length in RPad.
Definition: RPadLength.hxx:43
ROOT::Experimental::RPadLength::CoordSysBase::operator+=
DERIVED & operator+=(const DERIVED &rhs)
Definition: RPadLength.hxx:63
ROOT::Experimental::RPadLength::CoordSysBase::operator<=
friend bool operator<=(const DERIVED &lhs, const DERIVED &rhs)
Definition: RPadLength.hxx:83
ROOT::Experimental::RPadLength::GetPixel
double GetPixel() const
Definition: RPadLength.hxx:176
ROOT::Experimental::RPadLength::ParseString
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
ROOT::Math::fabs
VecExpr< UnaryOp< Fabs< T >, VecExpr< A, T, D >, T >, T, D > fabs(const VecExpr< A, T, D > &rhs)
Definition: UnaryOperators.h:131
ROOT::Experimental::RPadLength::Clear
void Clear()
Definition: RPadLength.hxx:191
ROOT::Experimental::RPadLength::User
A user coordinate.
Definition: RPadLength.hxx:119
ROOT::Experimental::RPadLength::operator+=
RPadLength & operator+=(const RPadLength &rhs)
Add a RPadLength.
Definition: RPadLength.hxx:232
ROOT::Experimental::RPadLength::CoordSysBase::CoordSysBase
CoordSysBase()=default
ROOT::Experimental::RPadLength::HasUser
bool HasUser() const
Definition: RPadLength.hxx:149
double
double
Definition: Converters.cxx:921
ROOT::Experimental::RPadLength::CoordSysBase::fVal
double fVal
! Coordinate value
Definition: RPadLength.hxx:52
ROOT::Experimental::RPadLength::fArr
std::vector< double > fArr
components [0] - normalized, [1] - pixel, [2] - user
Definition: RPadLength.hxx:47
ROOT::Experimental::RPadLength::SetNormal
RPadLength & SetNormal(double v)
Definition: RPadLength.hxx:151
ROOT::Experimental::RPadLength::CoordSysBase::operator*=
DERIVED & operator*=(double scale)
Definition: RPadLength.hxx:73
ROOT::Experimental::RPadLength::RPadLength
RPadLength(double normal)
By default all numeric values are normal values.
Definition: RPadLength.hxx:130
ROOT::Experimental::RPadLength::RPadLength
RPadLength(Normal normal)
Constructor from a Normal coordinate.
Definition: RPadLength.hxx:127
ROOT::Experimental::RPadLength::ClearPixelAndUser
void ClearPixelAndUser()
Definition: RPadLength.hxx:185
ROOT::Experimental::RPadLength::Normal
A normalized coordinate.
Definition: RPadLength.hxx:100
ROOT::Experimental::RPadLength::CoordSysBase::operator+
friend DERIVED operator+(DERIVED lhs, DERIVED rhs)
Definition: RPadLength.hxx:60
ROOT::Experimental::RPadLength::CoordSysBase::operator-
DERIVED operator-()
Definition: RPadLength.hxx:58
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::Experimental::RPadLength::CoordSysBase::operator-=
DERIVED & operator-=(const DERIVED &rhs)
Definition: RPadLength.hxx:68
ROOT::Experimental::RPadLength::CoordSysBase::operator<
friend bool operator<(const DERIVED &lhs, const DERIVED &rhs)
Definition: RPadLength.hxx:81