npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
BoxND.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_BOXND_HH_
2 #define NPSTAT_BOXND_HH_
3 
4 /*!
5 // \file BoxND.hh
6 //
7 // \brief Template to represent rectangles, boxes, and hyperboxes
8 //
9 // Author: I. Volobouev
10 //
11 // March 2010
12 */
13 
14 #include <vector>
15 
16 #include "geners/ClassId.hh"
17 #include "npstat/nm/Interval.hh"
18 
19 namespace npstat {
20  /**
21  // Class to represent rectangles, boxes, and hyperboxes
22  */
23  template <typename Numeric>
24  struct BoxND : public std::vector<Interval<Numeric> >
25  {
26  typedef Numeric value_type;
27 
28  /** Default constructor makes a 0-dimensional box */
29  inline BoxND() {}
30 
31  /** Interval in each dimension is made by its default constructor */
32  inline explicit BoxND(const unsigned long dim) :
33  std::vector<Interval<Numeric> >(dim) {}
34 
35  /** Use the same interval in each dimension */
36  inline BoxND(const unsigned long dim, const Interval<Numeric>& v) :
37  std::vector<Interval<Numeric> >(dim, v) {}
38 
39  /**
40  // Constructor where one of the limits will be 0 and the other
41  // will be generated from the given vector (which also determines
42  // the dimensionality)
43  */
44  template <typename Num2>
45  explicit BoxND(const std::vector<Num2>& limits);
46 
47  /** Converting constructor */
48  template <typename Num2>
49  explicit BoxND(const BoxND<Num2>& r);
50 
51  /**
52  // Get the data from a box of a different type. This method
53  // works essentially as a converting assignment operator.
54  */
55  template <typename Num2>
57 
58  /** Box dimensionality */
59  inline unsigned long dim() const {return this->size();}
60 
61  /** Box volume */
62  Numeric volume() const;
63 
64  //@{
65  /**
66  // This method return "true" if the corresponding function
67  // of the Interval returns "true" for every coordinate.
68  // There must be an automatic conversion from Num2 type into Numeric.
69  */
70  template <typename Num2>
71  bool isInsideLower(const Num2* coord, unsigned long coordLen) const;
72  template <typename Num2>
73  bool isInsideUpper(const Num2* coord, unsigned long coordLen) const;
74  template <typename Num2>
75  bool isInsideWithBounds(const Num2* coord, unsigned long coordLen) const;
76  template <typename Num2>
77  bool isInside(const Num2* coord, unsigned long coordLen) const;
78  template <typename Num2>
79  bool isInside(const Num2* coord, unsigned long coordLen,
80  const BoundaryInclusion* bi, unsigned long nBi) const;
81  //@}
82 
83  //@{
84  /** Scaling of all limits by a constant */
85  BoxND& operator*=(double r);
86  BoxND& operator/=(double r);
87  //@}
88 
89  //@{
90  /** Scaling by a different constant in each dimension */
91  BoxND& operator*=(const std::vector<double>& scales);
92  BoxND& operator/=(const std::vector<double>& scales);
93  //@}
94 
95  /**
96  // Scaling of all limits by a constant in such a way that the midpoint
97  // remains unchanged
98  */
99  BoxND& expand(double r);
100 
101  //@{
102  /**
103  // Scaling of all limits in such a way that the midpoint
104  // remains unchanged, using a different scaling factor
105  // in each dimension
106  */
107  BoxND& expand(const std::vector<double>& scales);
108  BoxND& expand(const double* scales, unsigned long lenScales);
109  //@}
110 
111  //@{
112  /** Shifting this object */
113  template <typename Num2>
114  BoxND& operator+=(const std::vector<Num2>& shifts);
115  template <typename Num2>
116  BoxND& operator-=(const std::vector<Num2>& shifts);
117  template <typename Num2>
118  BoxND& shift(const Num2* shifts, unsigned long lenShifts);
119  //@}
120 
121  /** Moving this object so that the midpoint is (0, 0, ..., 0) */
123 
124  /** Overlap volume with another box */
125  Numeric overlapVolume(const BoxND& r) const;
126 
127  /** A faster way to calculate overlapVolume(r)/volume() */
128  double overlapFraction(const BoxND& r) const;
129 
130  //@{
131  /** Get lower corner */
132  void getLowerCorner(Numeric* coord, unsigned long coordLen) const;
133  std::vector<Numeric> lowerCorner() const;
134  //@}
135 
136  //@{
137  /** Get upper corner */
138  void getUpperCorner(Numeric* coord, unsigned long coordLen) const;
139  std::vector<Numeric> upperCorner() const;
140  //@}
141 
142  //@{
143  /** Get interval length in all dimensions */
144  void getLengths(Numeric* coord, unsigned long coordLen) const;
145  std::vector<Numeric> lengths() const;
146  //@}
147 
148  //@{
149  /** Get midpoint */
150  void getMidpoint(Numeric* coord, unsigned long coordLen) const;
151  std::vector<Numeric> midpoint() const;
152  //@}
153 
154  /** Box with lower limit 0 and upper limit 1 in all coordinates */
155  static BoxND unitBox(unsigned long ndim);
156 
157  /**
158  // Box with lower limit -1 and upper limit 1 in all coordinates.
159  // Note that this will produce nonsense in case the Numeric type
160  // is unsigned.
161  */
162  static BoxND sizeTwoBox(unsigned long ndim);
163 
164  /**
165  // Box with all upper limits set to maximum possible Numeric
166  // number and with lower limits set to negative maximum (or to
167  // zero with unsigned types)
168  */
169  static BoxND allSpace(unsigned long ndim);
170 
171  //@{
172  /** Methods related to I/O */
173  inline gs::ClassId classId() const {return gs::ClassId(*this);}
174  bool write(std::ostream& of) const;
175  //@}
176 
177  static const char* classname();
178  static inline unsigned version() {return 1;}
179  static void restore(const gs::ClassId& id, std::istream& in, BoxND* box);
180  };
181 }
182 
183 //@{
184 /** Binary comparison for equality */
185 template <typename Numeric>
187 
188 template <typename Numeric>
189 bool operator!=(const npstat::BoxND<Numeric>& l, const npstat::BoxND<Numeric>& r);
190 //@}
191 
192 #include "npstat/nm/BoxND.icc"
193 
194 #endif // NPSTAT_BOXND_HH_
bool operator==(const npstat::BoxND< Numeric > &l, const npstat::BoxND< Numeric > &r)
Template to represent intervals in one dimension.
Definition: Interval.hh:29
Definition: AbsArrayProjector.hh:14
Definition: BoxND.hh:25
void getMidpoint(Numeric *coord, unsigned long coordLen) const
Numeric overlapVolume(const BoxND &r) const
static BoxND sizeTwoBox(unsigned long ndim)
BoxND()
Definition: BoxND.hh:29
Numeric volume() const
BoxND & operator*=(double r)
void getLengths(Numeric *coord, unsigned long coordLen) const
BoxND & moveToOrigin()
BoxND & operator+=(const std::vector< Num2 > &shifts)
BoxND & copyFrom(const BoxND< Num2 > &r)
BoxND & operator*=(const std::vector< double > &scales)
bool isInsideLower(const Num2 *coord, unsigned long coordLen) const
BoxND(const std::vector< Num2 > &limits)
double overlapFraction(const BoxND &r) const
void getLowerCorner(Numeric *coord, unsigned long coordLen) const
unsigned long dim() const
Definition: BoxND.hh:59
BoxND & expand(double r)
BoxND & expand(const std::vector< double > &scales)
static BoxND allSpace(unsigned long ndim)
BoxND(const unsigned long dim)
Definition: BoxND.hh:32
static BoxND unitBox(unsigned long ndim)
void getUpperCorner(Numeric *coord, unsigned long coordLen) const
BoxND(const unsigned long dim, const Interval< Numeric > &v)
Definition: BoxND.hh:36
gs::ClassId classId() const
Definition: BoxND.hh:173
BoxND(const BoxND< Num2 > &r)