Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 1b4028fb90ed369b0ea2d3dc0a3caa139e1fe2d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
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
/*
   math/vector3f.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#ifndef __INCLUDED_MATH_VECTOR3F_H__
#define __INCLUDED_MATH_VECTOR3F_H__

// project headers
#include "math/vector3f.h"

// C++ headers
#include <iostream>

namespace math
{

/// a point or vector in 3D-space
/** An instance of this class represents a point in 3D-space or a 3D-vector
 *  and forms the basic building block for all spatial calculations.
 */
class Vector3f
{

public:
	/// Default constructor, creates a Vector3f (0,0,0)
	Vector3f();

	/// Copy constructor
	/** Create a new Vector3f that is a copy from an other
	 *  @param other the vector to copy values from
	 */
	Vector3f(const Vector3f &other);

	/// Create a Vector3f with given coordinates
	/** @param x	the x-coordinate of the location
	 *  @param y 	the y-coordinate of the location
	 *  @param z	the z-coordinate of the location
	 */
	Vector3f(const float x, const float y, const float z);

	/// Destructor
	~Vector3f();

	/* -- Assignment operators -- */
	/// assign (0, 0, 0)
	void clear();

	/// assignment
	void assign(const float x, const float y, const float z);

	/// assignment
	void assign(Vector3f const & other);

	/// assignment operator
	Vector3f& operator=(const Vector3f &other);

	/// multiplicate each element of the vector with a given value
	/** @param scalar multiplication factor
	*/
	Vector3f& operator*=(const float scalar);

	/// divide each element of the vector by a given value
	/** @param scalar divider
	*/
	Vector3f& operator/=(const float scalar);

	/// perform an element-wise subtraction
	Vector3f& operator-=(const Vector3f &other);

	/// perform an element-wise addition
	Vector3f& operator+=(const Vector3f &other);

	/* -- Mathematical operators -- */

	/// return this Vector multiplied with scalar
	Vector3f operator*(const float scalar) const;

	/// return this vector divided by a scalar
	Vector3f operator/(const float scalar) const;

	/// return the element-wise difference between two vectors
	Vector3f operator-(const Vector3f &other) const;

	/// return the element-wise sum of two vectors
	Vector3f operator+(const Vector3f &other) const;

	/// comparison operator
	bool operator==(const Vector3f &other) const;

	/// assign a value to an element of this vector
	/*! WARNING: range is not checked
	 *  @param index the index of the element to assign to ( 0 <= index < 3 )
	*/
	inline float& operator[](const unsigned int index) {
		return coord[index];
	}

	/// returns the value of an element of this vector
	/*! WARNING: range is not checked
	 *  @param index the index of the element to return ( 0 <= index < 3 )
	 */
	inline float operator[](const unsigned int index) const {
		return coord[index];
	}

	/// Return the cartesian length of this vector
	float length() const;

	/// Return the cartesian length squared (to speed up calculations)
	float lengthsquared() const;

	/// Divide this Vector by it's length
	/// @see normalized()
	/// WARNING: vector must not be (0, 0, 0)
	void normalize();

	/// a pointer to the internal data
	inline float *ptr() const { return (float *) coord; }

	/* static functions */

	/// Returns the unity vector on the X-axis
	static inline Vector3f Xaxis() { return Vector3f(1.0f, 0.0f, 0.0f); }

	/// Returns the unity vector on the Y-axis
	static inline Vector3f Yaxis() { return Vector3f(0.0f, 1.0f, 0.0f); }

	/// Returns the unity vector on the Z-axis
	static inline Vector3f Zaxis() { return Vector3f(0.0f, 0.0f, 1.0f); }

	/// Return the cartesian length of a vector
	static inline float length(const Vector3f& vector) { return vector.length(); }

	/// Return a vector divided by it's length
	/// @see normalize()
	/// WARNING: vector must not be (0, 0, 0)
	static inline Vector3f normalized(const Vector3f& vector) { return (vector / vector.length()); }

	float &x;
	float &y;
	float &z;

private:
	float coord[3];
};

/// Write a Vector3f to a std::ostream
std::ostream &operator<<(std::ostream & os, Vector3f const & vector);

/// Read a Vector3d from a std::istream
std::istream &operator>>(std::istream & is, Vector3f & vector);

/// scalar * Vector3f operators
Vector3f operator*(float scalar, const Vector3f& vector);

/// vector cross product
const Vector3f crossproduct(Vector3f const & first, Vector3f const & second);

/// vector dot product
float dotproduct(const Vector3f& first, const Vector3f& second);

/// distance between two vectors
float distance(const Vector3f& first, const Vector3f& second);

/// distance between two vectors squared
float distancesquared(const Vector3f& first, const Vector3f& second);

} // namespace math

#endif // __INCLUDED_MATH_VECTOR3F_H__