point3d.hh 6.4 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
#include "radixcore/visibility.hh"
19

20
21
namespace radix
{
22
23
24
25
/**
 * @class Point3D
 * @brief class object describing object in 3D space
 */
26
27
class RADIX_PUBLIC Point3D
{
28
29
 public:
  Real x, y, z;
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
55
  /**
   * 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
   */
56
57
58
  Point3D(const Vector3D &v)
      : x(v.x)
      , y(v.y)
59
60
61
      , z(v.z)
  {
  }
62
63
64
65
  /**
   * Destructor
   */
  ~Point3D();
66

67
68
69
70
71
72
  /**
   * Equality function
   * @param p
   * @return
   */
  bool equals(const Point3D &p) const;
73
74
75
76
  inline Real &operator[](const size_t index)
  {
    switch (index)
    {
77
78
79
80
81
82
83
84
85
      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!");
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
  }
  /**
   * 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
   */
130
131
  Point3D &operator=(const Vector3D &v)
  {
132
133
134
135
136
    x = v.x;
    y = v.y;
    z = v.z;
    return *this;
  }
137

138
139
140
141
142
143
144
145
146
147
148
  /**
   * unary minus
   * @return
   */
  Point3D operator-(void) const;
  /**
   * Vector joing two points
   * @param v
   * @return
   */
  Vector3D operator-(const Point3D &p) const;
149

150
151
152
153
154
155
  /**
   * Addition of a vector
   * @param v
   * @return
   */
  Point3D operator+(const Vector3D &v) const;
156

157
158
159
160
161
162
  /**
   * Subtraction of a vector
   * @param v
   * @return
   */
  Point3D operator-(const Vector3D &v) const;
163

164
165
166
167
168
169
  /**
   * Multiplication by a Real on the right
   * @param a
   * @return
   */
  Point3D operator*(const Real a) const;
170

171
172
173
174
175
176
  /**
   * Square of distance between two points
   * @param p
   * @return
   */
  Real distance_squared(const Point3D &p) const;
177

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  /**
   * 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
193
194
inline void Point3D::move(const Vector3D &direction, Real distance)
{
195
196
197
  x += direction.x * distance;
  y += direction.y * distance;
  z += direction.z * distance;
198
199
200
201
202
203
}

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

/**
 * @function operator*
 * @brief multiplication by a Real on the left
 */
210
RADIX_PUBLIC Point3D operator*(Real a, const Point3D &p);
211
212
213
214
215

/**
 * @function operation-
 * @brief subtraction operator
 */
216
inline Point3D Point3D::operator-(void) const { return (Point3D(-x, -y, -z)); }
217
218
219
220
221

/**
 * @function operator-
 * @brief subtraction of a vector from a point that returns a new point
 */
222
223
inline Point3D Point3D::operator-(const Vector3D &v) const
{
224
  return (Point3D(x - v.x, y - v.y, z - v.z));
225
226
227
228
229
230
}

/*
 * @function operator*
 * @brief multiplication by a Real on the right
 */
231
232
inline Point3D Point3D::operator*(const Real a) const
{
233
  return (Point3D(x * a, y * a, z * a));
234
235
236
237
238
239
}

/**
 * @function operator+
 * @brief addition of a vector to a point that returns a new point
 */
240
241
inline Point3D Point3D::operator+(const Vector3D &v) const
{
242
  return (Point3D(x + v.x, y + v.y, z + v.z));
243
244
245
246
247
248
}

/**
 * @function distance_squared
 * @brief the square of the distance between the two points as a member function
 */
249
250
inline Real Point3D::distance_squared(const Point3D &p) const
{
251
252
  return ((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y) +
          (z - p.z) * (z - p.z));
253
254
255
256
257
258
259
260
}

/**
 * @function operation*
 * @param a
 * @param p
 * @return
 */
261
262
inline RADIX_PUBLIC Point3D operator*(Real a, const Point3D &p)
{
263
  return (Point3D(a * p.x, a * p.y, a * p.z));
264
265
}

266
267
inline bool Point3D::equals(const Point3D &p) const
{
268
269
  if (x == p.x && y == p.y && z == p.z) return true;
  return false;
270
271
}

272
273
inline bool Point3D::operator==(const Point3D &rhs) const
{
274
275
  if (x == rhs.x && y == rhs.y && z == rhs.z) return true;
  return false;
276
}
277
278
inline bool Point3D::operator!=(const Point3D &rhs) const
{
279
280
  if (x == rhs.x && y == rhs.y && z == rhs.z) return false;
  return true;
281
}
282
283
inline bool Point3D::operator<(const Point3D &rhs) const
{
284
285
  if (x < rhs.x && y < rhs.y && z < rhs.z) return true;
  return false;
286
287
}

288
289
inline bool Point3D::operator<=(const Point3D &rhs) const
{
290
291
  if (x <= rhs.x && y <= rhs.y && z <= rhs.z) return true;
  return false;
292
293
}

294
295
inline bool Point3D::operator>(const Point3D &rhs) const
{
296
297
  if (x > rhs.x && y > rhs.y && z > rhs.z) return true;
  return false;
298
299
}

300
301
inline bool Point3D::operator>=(const Point3D &rhs) const
{
302
303
  if (x >= rhs.x && y >= rhs.y && z >= rhs.z) return true;
  return false;
304
305
306
307
308
309
310
}
/**
 * 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
 */
311
312
313
RADIX_PUBLIC std::ostream &operator<<(std::ostream &os, const Point3D &p);
}  // namespace radix
#endif /* RADIX_RADIXMATH_POINT3D_HH_*/