vector3d.hh 3.82 KB
Newer Older
1
2
3
4
5
6
7
8
/*
 * File:   vector3d.hh
 * Author: Jordan
 *
 * Created on August 9, 2012, 6:06 PM
 */

#ifndef RADIX_RADIXMATH_VECTOR3D_H
9
#define RADIX_RADIXMATH_VECTOR3D_H
10
#include <stdexcept>
11
#include <string>
12
13
#include "radixmath/constants.hh"

14
#include "radixcore/visibility.hh"
15

16
17
namespace radix
{
18
19
20
class Normal;
class Point3D;
class Matrix;
21
22
23
/**
 * @class Vector3D
 */
24
25
class RADIX_PUBLIC Vector3D
{
26
27
28
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
 public:
  Real x, y, z;

 public:
  /**
   * Constructor
   */
  Vector3D(void);
  /**
   * Constructor
   * @param a
   */
  Vector3D(Real a);
  /**
   * Constructor
   * @param _x
   * @param _y
   * @param _z
   */
  Vector3D(Real _x, Real _y, Real _z);
  /**
   * Copy constructor
   * @param v
   */
  Vector3D(const Vector3D &v);

  /**
   * Copy Normal Constructor
   * @param n
   */
  Vector3D(const Normal &n);

  /**
   * Copy Point3D constructor
   * @param p
   */
  Vector3D(const Point3D &p);

  /**
   * Destructor
   */
  ~Vector3D(void);

  /**
   *
   * @param rhs
   * @return
   */
  Vector3D &operator=(const Vector3D &rhs);
  bool operator==(const Vector3D &rhs) const;
  bool operator!=(const Vector3D &rhs) const;
  /**
   * Assign a {@code Normal} to a vector
   * @param rhs
   * @return
   */
  Vector3D &operator=(const Normal &rhs);

  /**
   * Assign a {@code Point3D to a vector
   * @param rhs
   * @return
   */
  Vector3D &operator=(const Point3D &rhs);

  /**
   * Unary minus
   * @return
   */
  Vector3D operator-(void) const;
96
97
98
99
  Real &operator[](size_t i)
  {
    switch (i)
    {
100
101
102
103
104
105
      case 0:
        return x;
      case 1:
        return y;
      case 2:
        return z;
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    throw std::runtime_error("Unknown Vector3D member!");
  }
  std::string toString() const;
  /**
   * Length
   * @return
   */
  Real length(void) const;

  /**
   * Square of the length
   * @return
   */
  Real len_squared(void) const;

  /**
   * Multiplication by a Real on the right
   * @param a
   * @return
   */
  Vector3D operator*(const Real a) const;
  /**
   * Division by a Real
   * @param a
   * @return
   */
  Vector3D operator/(const Real a) const;

  /**
   * Addition
   * @param v
   * @return
   */
  Vector3D operator+(const Vector3D &v) const;

  /**
   * Compound addition
   * @param v
   * @return
   */
  Vector3D &operator+=(const Vector3D &v);

  /**
   * Subtraction
   * @param v
   * @return
   */
  Vector3D operator-(const Vector3D &v) const;

  /**
   * Dot Production
   * @param b
   * @return
   */
  Real operator*(const Vector3D &b) const;
  /**
   * Cross Product
   * @param v
   * @return
   */
  Vector3D operator^(const Vector3D &v) const;

  /**
   * Convert vector to a unit vector
   * @return
   */
  void normalize(void);

  /**
   * Return a unit vector, and normalize the vector
   * @return
   */
  Vector3D &normalized_vector(void);
  /**
   * @brief Obtain an orthogonal vector to this vector
   * If this vector is a zero vector, an exception is thrown.
   * The vector is not necessarily normalized
   * @return Vector3D - an orthogonal/perpendicular vector to this vector
   */
  Vector3D orthogonal(void) const;
  /**
   * @brief spherical - creates a spherical Vector3D
   * Note x=>r, y=>theta, z=>phi
   * @return Vector3D where x=>r, y=>theta, z=>phi
   */
  Vector3D spherical(void) const;
  Vector3D cartesian(void) const;
194
195
196
197
198
199
};

/**
 * @function operator*
 * @brief multiplication by a Real on the left
 */
200
Vector3D operator*(const Real a, const Vector3D &v);
201
202
203
204
205

/**
 * @function operator*
 * @brief multiplication by a matrix on the left
 */
206
Vector3D operator*(const Matrix &mat, const Vector3D &v);
207
208
209
210
211
212
/**
 * Allow for printing a Vector3D to stream
 * @param os - the stream to receive the string representation of the vector
 * @param v - the vector to be stringified
 * @return the stream
 */
213
214
215
RADIX_PUBLIC std::ostream &operator<<(::std::ostream &os, const Vector3D &v);
}  // namespace radix
#endif /* RADIX_RADIXMATH_VECTOR3D_H */