MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
vector3.h
Go to the documentation of this file.
1 /*
2  This file is part of the MinGfx Project.
3 
4  Copyright (c) 2017,2018 Regents of the University of Minnesota.
5  All Rights Reserved.
6 
7  Original Author(s) of this File:
8  Dan Keefe, 2018, University of Minnesota
9 
10  Author(s) of Significant Updates/Modifications to the File:
11  ...
12  */
13 
14 #ifndef SRC_VECTOR3_H_
15 #define SRC_VECTOR3_H_
16 
17 #include <iostream>
18 
19 #include "point3.h"
20 
21 
22 namespace mingfx {
23 
24 
62 class Vector3 {
63 public:
64 
67 
70  Vector3(float x, float y, float z);
71 
73  Vector3(float *v);
74 
76  Vector3(const Vector3& v);
77 
79  virtual ~Vector3();
80 
82  bool operator==(const Vector3& v) const;
83 
85  bool operator!=(const Vector3& v) const;
86 
89 
91  float operator[](const int i) const;
92 
100  float& operator[](const int i);
101 
104  float x() const { return v[0]; }
105 
108  float y() const { return v[1]; }
109 
112  float z() const { return v[2]; }
113 
115  float w() const { return 0.0; }
116 
117 
118  // --- Vector operations ---
119 
127  float Dot(const Vector3& v) const;
128 
136  Vector3 Cross(const Vector3& v) const;
137 
139  float Length() const;
140 
142  void Normalize();
143 
146  Vector3 ToUnit() const;
147 
149  const float * value_ptr() const;
150 
154  Vector3 Lerp(const Vector3 &b, float alpha) const;
155 
156 
158  static const Vector3& Zero();
159 
161  static const Vector3& One();
162 
164  static const Vector3& UnitX();
165 
167  static const Vector3& UnitY();
168 
170  static const Vector3& UnitZ();
171 
172 
182  static Vector3 Normalize(const Vector3 &v);
183 
194  static Vector3 Cross(const Vector3 &v1, const Vector3 &v2);
195 
206  static float Dot(const Vector3 &v1, const Vector3 &v2);
207 
210  static Vector3 Lerp(const Vector3 &a, const Vector3 &b, float alpha);
211 
212 private:
213  float v[3];
214 };
215 
216 
217 // ---------- Operator Overloads for Working with Vectors ----------
218 
219 
220 // --- Scalers ---
221 
223 Vector3 operator/(const Vector3& v, const float s);
224 
226 Vector3 operator*(const float s, const Vector3& v);
227 
229 Vector3 operator*(const Vector3& v, const float s);
230 
233 
234 // Note: no -(point) operator, that's an undefined operation
235 
236 
237 // --- Point and Vector Arithmetic ---
238 
240 Point3 operator+(const Vector3& v, const Point3& p);
241 
243 Point3 operator+(const Point3& p, const Vector3& v);
244 
246 Vector3 operator+(const Vector3& v1, const Vector3& v2);
247 
248 // Note: no (point + point) operator, that's an undefined operation
249 
251 Point3 operator-(const Point3& p, const Vector3& v);
252 
254 Vector3 operator-(const Vector3& v1, const Vector3& v2);
255 
257 Vector3 operator-(const Point3& p1, const Point3& p2);
258 
259 // Note: no (vector - point) operator, that's an undefined operation
260 
261 
262 
263 
264 // --- Stream operators ---
265 
266 // Vector3
267 std::ostream & operator<< ( std::ostream &os, const Vector3 &v);
268 std::istream & operator>> ( std::istream &is, Vector3 &v);
269 
270 
271 } // end namespace
272 
273 #endif
A 3D Point with floating point coordinates, used for storing vertices and all sorts of other 3D graph...
Definition: point3.h:52
A 3D Vector with floating point coordinates, used for storing normals and all sorts of other 3D graph...
Definition: vector3.h:62
static float Dot(const Vector3 &v1, const Vector3 &v2)
Returns v1 dot v2.
float w() const
In homogeneous coordinates, the w coordinate for all vectors is 0.0.
Definition: vector3.h:115
Vector3(float x, float y, float z)
Constructs a vector (x,y,z,0), where the 0 comes from the use of homogeneous coordinates in computer ...
float Dot(const Vector3 &v) const
Returns "this dot v", for example:
Vector3 Cross(const Vector3 &v) const
Returns "this cross v", for example:
Vector3 ToUnit() const
Returns a normalized (i.e., unit length) version of the vector without modifying the original 'this' ...
static Vector3 Cross(const Vector3 &v1, const Vector3 &v2)
Returns v1 cross v2.
static Vector3 Lerp(const Vector3 &a, const Vector3 &b, float alpha)
Linear interpolation between two vectors. Alpha=0.0 returns 'a' and alpha=1.0 returns 'b',...
static const Vector3 & UnitZ()
(0,0,1) - a shortcut for a special vector that is frequently needed
static const Vector3 & Zero()
(0,0,0) - a shortcut for a special vector that is frequently needed
virtual ~Vector3()
Vector destructor.
float Length() const
Returns the length of the vector.
float z() const
Read only access to the z coordinate. Can also use my_vector[2]. Use the my_vector[2] = 1....
Definition: vector3.h:112
Vector3()
Default constructor to create zero vector.
bool operator==(const Vector3 &v) const
Check for "equality", taking floating point imprecision into account.
static const Vector3 & UnitY()
(0,1,0) - a shortcut for a special vector that is frequently needed
Vector3 Lerp(const Vector3 &b, float alpha) const
Linear interpolation between this vector and another. Alpha=0.0 returns this vector,...
Vector3(float *v)
Constructs a vector given a pointer to x,y,z data.
void Normalize()
Normalizes the vector by making it unit length.
float operator[](const int i) const
Read only access to the ith coordinate of the vector.
Vector3 & operator=(const Vector3 &v)
Vector assignment operator.
const float * value_ptr() const
Returns a const pointer to the raw data array.
bool operator!=(const Vector3 &v) const
Check for "inequality", taking floating point imprecision into account.
float & operator[](const int i)
Returns a reference to the ith coordinate of the vector. Use this accessor if you wish to set the coo...
float x() const
Read only access to the x coordinate. Can also use my_vector[0]. Use the my_vector[0] = 1....
Definition: vector3.h:104
static const Vector3 & UnitX()
(1,0,0) - a shortcut for a special vector that is frequently needed
float y() const
Read only access to the y coordinate. Can also use my_vector[1]. Use the my_vector[1] = 1....
Definition: vector3.h:108
static Vector3 Normalize(const Vector3 &v)
Returns a new vector that is the unit version of v.
Vector3(const Vector3 &v)
Copy constructor for vector.
static const Vector3 & One()
(1,1,1) - a shortcut for a special vector that is frequently needed
Namespace for the MinGfx Toolkit.
Definition: aabb.h:21
Quaternion operator-(const Quaternion &q)
AABB operator+(const AABB &A, const AABB &B)
Quaternion operator/(const Quaternion &q, const float s)
std::ostream & operator<<(std::ostream &os, const Color &c)
std::istream & operator>>(std::istream &is, Color &c)
Matrix4 operator*(const Matrix4 &m, const float &s)
Multiply matrix and scalar, returns the new matrix.