point3d.hh 6.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/*
 * File:   point3d.hh
 * Author: Jordan P. Lefebvre, lefebvre.jordan@gmail.com
 *
 * Created on August 9, 2012, 5:47 PM
 */

#ifndef RADIX_RADIXMATH_POINT3D_HH_
#define RADIX_RADIXMATH_POINT3D_HH_
#include <ostream>
#include <stdexcept>
#include <string>
13
14

#include "radixmath/constants.hh"
15
16
17
#include "radixmath/matrix.hh"
#include "radixmath/vector3d.hh"

18
19
#include "radixdl/visibility.hh"

20
namespace radix {
21
22
23
24
25

/**
 * @class Point3D
 * @brief class object describing object in 3D space
 */
26
27
28
class RADIX_PUBLIC Point3D {
 public:
  Real x, y, z;
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  /**
   * Default constructor
   */
  Point3D();
  /**
   * Constructor
   * @param a
   */
  Point3D(const Real a);
  /**
   * Constructor
   * @param a
   * @param b
   * @param c
   */
  Point3D(const Real a, const Real b, const Real c);
  /**
   * Copy Constructor
   * @param p
   */
  Point3D(const Point3D &p);
  /**
   * Copy Constructor
   * @param p
   */
55
56
57
58
  Point3D(const Vector3D &v)
      : x(v.x)
      , y(v.y)
      , z(v.z) {}
59
60
61
62
  /**
   * Destructor
   */
  ~Point3D();
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  /**
   * Equality function
   * @param p
   * @return
   */
  bool equals(const Point3D &p) const;
  inline Real &operator[](const size_t index) {
    switch (index) {
      case 0:
        return x;
      case 1:
        return y;
      case 2:
        return z;
      default:
        throw std::out_of_range(
            "Point3D range issue, index is beyond known members!");
81
    }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  }
  /**
   * Operator==
   * @param rhs
   * @return true x1==x2 && y1==y2 && z1==z2, false otherwise
   */
  bool operator==(const Point3D &rhs) const;
  bool operator!=(const Point3D &rhs) const;
  /**
   * Operator<
   * @param rhs
   * @return true x1<x2 && y1<y2 && z1<z2, false otherwise
   */
  bool operator<(const Point3D &rhs) const;
  /**
   * Operator<=
   * @param rhs
   * @return true x1<=x2 && y1<=y2 && z1<=z2, false otherwise
   */
  bool operator<=(const Point3D &rhs) const;
  /**
   * Operator>
   * @param rhs
   * @return true x1>x2 && y1>y2 && z1>z2, false otherwise
   */
  bool operator>(const Point3D &rhs) const;
  /**
   * Operator>=
   * @param rhs
   * @return true x1>=x2 && y1>=y2 && z1>=z2, false otherwise
   */
  bool operator>=(const Point3D &rhs) const;
  /**
   * Assignment operation
   * @param p
   * @return
   */
  Point3D &operator=(const Point3D &p);
  /**
   * Assignment operation
   * @param p
   * @return
   */
  Point3D &operator=(const Vector3D &v) {
    x = v.x;
    y = v.y;
    z = v.z;
    return *this;
  }
131

132
133
134
135
136
137
138
139
140
141
142
  /**
   * unary minus
   * @return
   */
  Point3D operator-(void) const;
  /**
   * Vector joing two points
   * @param v
   * @return
   */
  Vector3D operator-(const Point3D &p) const;
143

144
145
146
147
148
149
  /**
   * Addition of a vector
   * @param v
   * @return
   */
  Point3D operator+(const Vector3D &v) const;
150

151
152
153
154
155
156
  /**
   * Subtraction of a vector
   * @param v
   * @return
   */
  Point3D operator-(const Vector3D &v) const;
157

158
159
160
161
162
163
  /**
   * Multiplication by a Real on the right
   * @param a
   * @return
   */
  Point3D operator*(const Real a) const;
164

165
166
167
168
169
170
  /**
   * Square of distance between two points
   * @param p
   * @return
   */
  Real distance_squared(const Point3D &p) const;
171

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  /**
   * Distance between two points
   * @param p
   * @return
   */
  Real distance(const Point3D &p) const;
  /**
   * @brief move - moves the point along the given direction vector a distance
   * of distance*direction.length()
   * @param direction - a hopefully normalized direction vector
   * @param distance - the track length
   */
  void move(const Vector3D &direction, Real distance);
  std::string toString() const;
};  // Point3D class
inline void Point3D::move(const Vector3D &direction, Real distance) {
  x += direction.x * distance;
  y += direction.y * distance;
  z += direction.z * distance;
191
192
193
194
195
196
}

/**
 * @function operator*
 * @brief multiplication by a matrix on the left
 */
197
RADIX_PUBLIC Point3D operator*(const Matrix &mat, const Point3D &p);
198
199
200
201
202

/**
 * @function operator*
 * @brief multiplication by a Real on the left
 */
203
RADIX_PUBLIC Point3D operator*(Real a, const Point3D &p);
204
205
206
207
208

/**
 * @function operation-
 * @brief subtraction operator
 */
209
inline Point3D Point3D::operator-(void) const { return (Point3D(-x, -y, -z)); }
210
211
212
213
214

/**
 * @function operator-
 * @brief subtraction of a vector from a point that returns a new point
 */
215
216
inline Point3D Point3D::operator-(const Vector3D &v) const {
  return (Point3D(x - v.x, y - v.y, z - v.z));
217
218
219
220
221
222
}

/*
 * @function operator*
 * @brief multiplication by a Real on the right
 */
223
224
inline Point3D Point3D::operator*(const Real a) const {
  return (Point3D(x * a, y * a, z * a));
225
226
227
228
229
230
}

/**
 * @function operator+
 * @brief addition of a vector to a point that returns a new point
 */
231
232
inline Point3D Point3D::operator+(const Vector3D &v) const {
  return (Point3D(x + v.x, y + v.y, z + v.z));
233
234
235
236
237
238
}

/**
 * @function distance_squared
 * @brief the square of the distance between the two points as a member function
 */
239
240
241
inline Real Point3D::distance_squared(const Point3D &p) const {
  return ((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y) +
          (z - p.z) * (z - p.z));
242
243
244
245
246
247
248
249
}

/**
 * @function operation*
 * @param a
 * @param p
 * @return
 */
250
251
inline RADIX_PUBLIC Point3D operator*(Real a, const Point3D &p) {
  return (Point3D(a * p.x, a * p.y, a * p.z));
252
253
}

254
255
256
inline bool Point3D::equals(const Point3D &p) const {
  if (x == p.x && y == p.y && z == p.z) return true;
  return false;
257
258
}

259
260
261
inline bool Point3D::operator==(const Point3D &rhs) const {
  if (x == rhs.x && y == rhs.y && z == rhs.z) return true;
  return false;
262
}
263
264
265
inline bool Point3D::operator!=(const Point3D &rhs) const {
  if (x == rhs.x && y == rhs.y && z == rhs.z) return false;
  return true;
266
}
267
268
269
inline bool Point3D::operator<(const Point3D &rhs) const {
  if (x < rhs.x && y < rhs.y && z < rhs.z) return true;
  return false;
270
271
}

272
273
274
inline bool Point3D::operator<=(const Point3D &rhs) const {
  if (x <= rhs.x && y <= rhs.y && z <= rhs.z) return true;
  return false;
275
276
}

277
278
279
inline bool Point3D::operator>(const Point3D &rhs) const {
  if (x > rhs.x && y > rhs.y && z > rhs.z) return true;
  return false;
280
281
}

282
283
284
inline bool Point3D::operator>=(const Point3D &rhs) const {
  if (x >= rhs.x && y >= rhs.y && z >= rhs.z) return true;
  return false;
285
286
287
288
289
290
291
}
/**
 * Allow for printing a Point3D to stream
 * @param os - the stream to receive the string representation of the point
 * @param p - the point to be stringified
 * @return the stream
 */
292
293
294
RADIX_PUBLIC std::ostream &operator<<(std::ostream &os, const Point3D &p);
}  // namespace radix
#endif /* RADIX_RADIXMATH_POINT3D_HH_*/