CUGL 1.3
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUVec3.h
1 //
2 // CUVec3.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides support for a 3d vector. It has support for basic
6 // arithmetic, as well as conversions to color formats.
7 //
8 // Because math objects are intended to be on the stack, we do not provide
9 // any shared pointer support in this class.
10 //
11 // This module is based on an original file from GamePlay3D: http://gameplay3d.org.
12 // It has been modified to support the CUGL framework.
13 //
14 // CUGL MIT License:
15 // This software is provided 'as-is', without any express or implied
16 // warranty. In no event will the authors be held liable for any damages
17 // arising from the use of this software.
18 //
19 // Permission is granted to anyone to use this software for any purpose,
20 // including commercial applications, and to alter it and redistribute it
21 // freely, subject to the following restrictions:
22 //
23 // 1. The origin of this software must not be misrepresented; you must not
24 // claim that you wrote the original software. If you use this software
25 // in a product, an acknowledgment in the product documentation would be
26 // appreciated but is not required.
27 //
28 // 2. Altered source versions must be plainly marked as such, and must not
29 // be misrepresented as being the original software.
30 //
31 // 3. This notice may not be removed or altered from any source distribution.
32 //
33 // Author: Walker White
34 // Version: 5/30/16
35 #ifndef __CU_VEC3_H__
36 #define __CU_VEC3_H__
37 
38 #include <math.h>
39 #include <functional>
40 #include <string>
41 #include "CUMathBase.h"
42 
43 namespace cugl {
44 
45 // Forward reference for conversions
46 class Color4;
47 class Color4f;
48 class Vec2;
49 class Vec4;
50 
61 class Vec3 {
62 
63 #pragma mark Values
64 public:
66  float x;
68  float y;
70  float z;
71 
73  static const Vec3 ZERO;
75  static const Vec3 ONE;
77  static const Vec3 UNIT_X;
79  static const Vec3 UNIT_Y;
81  static const Vec3 UNIT_Z;
82 
83 
84 #pragma mark -
85 #pragma mark Constructors
86 public:
90  Vec3() : x(0), y(0), z(0) {}
91 
99  Vec3(float x, float y, float z) { this->x = x; this->y = y; this->z = z; }
100 
106  Vec3(const float* array) { x = array[0]; y = array[1]; z = array[2]; }
107 
114  Vec3(const Vec3& p1, const Vec3& p2) {
115  x = p2.x-p1.x; y = p2.y-p1.y; z = p2.z-p1.z;
116  }
117 
118 
119 #pragma mark -
120 #pragma mark Setters
121 
128  Vec3& operator=(const float* array) {
129  return set(array);
130  }
131 
141  Vec3& set(float x, float y, float z) {
142  this->x = x; this->y = y; this->z = z;
143  return *this;
144  }
145 
153  Vec3& set(const float* array) {
154  x = array[0]; y = array[1]; z = array[2];
155  return *this;
156  }
157 
165  Vec3& set(const Vec3& v) {
166  x = v.x; y = v.y; z = v.z;
167  return *this;
168  }
169 
178  Vec3& set(const Vec3& p1, const Vec3& p2) {
179  x = p2.x-p1.x; y = p2.y-p1.y; z = p2.z-p1.z;
180  return *this;
181  }
182 
189  x = y = z = 0;
190  return *this;
191  }
192 
193 
194 #pragma mark -
195 #pragma mark Static Arithmetic
196 
206  static Vec3* clamp(const Vec3& v, const Vec3& min, const Vec3& max, Vec3* dst);
207 
221  static float angle(const Vec3& v1, const Vec3& v2, const Vec3& up = Vec3::UNIT_Z);
222 
232  static Vec3* add(const Vec3& v1, const Vec3& v2, Vec3* dst);
233 
244  static Vec3* subtract(const Vec3& v1, const Vec3& v2, Vec3* dst);
245 
257  static Vec3* scale(const Vec3& v, float s, Vec3* dst);
258 
270  static Vec3* scale(const Vec3& v1, const Vec3& v2, Vec3* dst);
271 
283  static Vec3* divide(const Vec3& v, float s, Vec3* dst);
284 
296  static Vec3* divide(const Vec3& v1, const Vec3& v2, Vec3* dst);
297 
310  static Vec3* reciprocate(const Vec3& v, Vec3* dst);
311 
320  static Vec3* negate(const Vec3& v, Vec3* dst);
321 
322 #pragma mark -
323 #pragma mark Arithmetic
324 
332  Vec3& clamp(const Vec3& min, const Vec3& max) {
333  x = clampf(x, min.x, max.x);
334  y = clampf(y, min.y, max.y);
335  z = clampf(z, min.z, max.z);
336  return *this;
337  }
338 
349  Vec3 getClamp(const Vec3& min, const Vec3& max) const {
350  return Vec3(clampf(x,min.x,max.x), clampf(y, min.y, max.y),clampf(z, min.z, max.z));
351  }
352 
360  Vec3& add(const Vec3& v) {
361  x += v.x; y += v.y; z += v.z;
362  return *this;
363  }
364 
374  Vec3& add(float x, float y, float z) {
375  this->x += x; this->y += y; this->z += z;
376  return *this;
377  }
378 
386  Vec3& subtract(const Vec3& v) {
387  x -= v.x; y -= v.y; z -= v.z;
388  return *this;
389  }
390 
400  Vec3& subtract(float x, float y, float z) {
401  this->x -= x; this->y -= y; this->z -= z;
402  return *this;
403  }
404 
412  Vec3& scale(float s) {
413  x *= s; y *= s; z *= s;
414  return *this;
415  }
416 
426  Vec3& scale(float sx, float sy, float sz) {
427  x *= sx; y *= sy; z *= sz;
428  return *this;
429  }
430 
438  Vec3& scale(const Vec3& v) {
439  x *= v.x; y *= v.y; z *= v.z;
440  return *this;
441  }
442 
450  Vec3& divide(float s);
451 
461  Vec3& divide(float sx, float sy, float sz);
462 
472  Vec3& divide(const Vec3& v);
473 
480  x = -x; y = -y; z = -z;
481  return *this;
482  }
483 
494  x = 1.0f/x; y = 1.0f/y; z = 1.0f/z;
495  return *this;
496  }
497 
505  Vec3 getNegation() const {
506  Vec3 result(*this);
507  return result.negate();
508  }
509 
521  Vec3 getReciprocal() const {
522  Vec3 result(*this);
523  return result.reciprocate();
524  }
525 
536  Vec3& map(std::function<float(float)> func) {
537  x = func(x); y = func(y); z = func(z);
538  return *this;
539  }
540 
551  Vec3 getMap(std::function<float(float)> func) const {
552  return Vec3(func(x), func(y), func(z));
553  }
554 
555 
556 #pragma mark -
557 #pragma mark Operators
558 
565  Vec3& operator+=(const Vec3& v) {
566  return add(v);
567  }
568 
576  Vec3& operator-=(const Vec3& v) {
577  return subtract(v);
578  }
579 
587  Vec3& operator*=(float s) {
588  return scale(s);
589  }
590 
598  Vec3& operator*=(const Vec3& v) {
599  return scale(v);
600  }
601 
609  Vec3& operator/=(float s) {
610  return divide(s);
611  }
612 
622  Vec3& operator/=(const Vec3& v) {
623  return divide(v);
624  }
625 
635  const Vec3 operator+(const Vec3& v) const {
636  Vec3 result(*this);
637  return result.add(v);
638  }
639 
649  const Vec3 operator-(const Vec3& v) const {
650  Vec3 result(*this);
651  return result.subtract(v);
652  }
653 
661  const Vec3 operator-() const {
662  Vec3 result(*this);
663  return result.negate();
664  }
665 
675  const Vec3 operator*(float s) const {
676  Vec3 result(*this);
677  return result.scale(s);
678  }
679 
690  const Vec3 operator*(const Vec3& v) const {
691  Vec3 result(*this);
692  return result.scale(v);
693  }
694 
704  const Vec3 operator/(float s) const {
705  Vec3 result(*this);
706  return result.divide(s);
707  }
708 
719  const Vec3 operator/(const Vec3& v) const {
720  Vec3 result(*this);
721  return result.divide(v);
722  }
723 
724 
725 #pragma mark -
726 #pragma mark Comparisons
727 
738  bool operator<(const Vec3& v) const {
739  return (x == v.x ? (y == v.y ? z < v.z : y < v.y) : x < v.x);
740  }
741 
753  bool operator<=(const Vec3& v) const {
754  return (x == v.x ? (y == v.y ? z <= v.z : y <= v.y) : x <= v.x);
755  }
756 
768  bool operator>(const Vec3& v) const {
769  return (x == v.x ? (y == v.y ? z > v.z : y > v.y) : x > v.x);
770  }
771 
783  bool operator>=(const Vec3& v) const {
784  return (x == v.x ? (y == v.y ? z >= v.z : y >= v.y) : x >= v.x);
785  }
786 
797  bool operator==(const Vec3& v) const {
798  return x == v.x && y == v.y && z == v.z;
799  }
800 
811  bool operator!=(const Vec3& v) const {
812  return x != v.x || y != v.y || z != v.z;
813  }
814 
825  bool under(const Vec3& v) const {
826  return x <= v.x && y <= v.y && z <= v.z;
827  }
828 
839  bool over(const Vec3& v) const {
840  return x >= v.x && y >= v.y && z >= v.z;
841  }
842 
854  bool equals(const Vec3& v, float variance=CU_MATH_EPSILON) const {
855  return distanceSquared(v) <= variance*variance;
856  }
857 
858 
859 #pragma mark -
860 #pragma mark Linear Attributes
861 
866  bool isZero() const {
867  return x == 0.0f && y == 0.0f && z == 0.0f;
868  }
869 
877  bool isNearZero(float variance=CU_MATH_EPSILON) const {
878  return lengthSquared() < variance*variance;
879  }
880 
886  bool isOne() const {
887  return x == 1.0f && y == 1.0f && z == 1.0f;
888  }
889 
895  bool isInvertible() const {
896  return x != 0.0f && y != 0.0f && z != 0.0f;
897  }
898 
906  bool isUnit(float variance=CU_MATH_EPSILON) const {
907  float dot = lengthSquared()-1;
908  return dot < variance && dot > -variance;
909  }
910 
923  float getAngle(const Vec3& other, const Vec3& up = Vec3::UNIT_Z) const;
924 
934  float distance(const Vec3& v) const {
935  return sqrt(distanceSquared(v));
936  }
937 
952  float distanceSquared(const Vec3& v) const {
953  return (x-v.x)*(x-v.x)+(y-v.y)*(y-v.y)+(z-v.z)*(z-v.z);
954  }
955 
963  float length() const {
964  return sqrt(lengthSquared());
965  }
966 
979  float lengthSquared() const {
980  return x*x+y*y+z*z;
981  }
982 
983 
984 #pragma mark -
985 #pragma mark Linear Algebra
986 
993  float dot(const Vec3& v) const { return (x * v.x + y * v.y + z * v.z); }
994 
1002  Vec3& cross(const Vec3& v) {
1003  return *(cross(*this,v,this));
1004  }
1005 
1015  Vec3 getCross(const Vec3& other) const {
1016  Vec3 result(*this);
1017  return result.cross(other);
1018  }
1019 
1030  Vec3& normalize();
1031 
1044  Vec3 result(*this);
1045  return result.normalize();
1046  }
1047 
1057  Vec3 getMidpoint(const Vec3& other) const {
1058  return Vec3((x + other.x) / 2.0f, (y + other.y) / 2.0f, (z + other.z) / 2.0f);
1059  }
1060 
1068  Vec3& project(const Vec3& other) {
1069  *this = getProjection(other);
1070  return *this;
1071  }
1072 
1082  Vec3 getProjection(const Vec3& other) const {
1083  return other * (dot(other)/other.dot(other));
1084  }
1085 
1098  Vec3& lerp(const Vec3& other, float alpha) {
1099  *this *= (1.f - alpha);
1100  return *this += other * alpha;
1101  }
1102 
1115  Vec3 getLerp(const Vec3& other, float alpha) {
1116  return *this * (1.f - alpha) + other * alpha;
1117  }
1118 
1133  Vec3& smooth(const Vec3& target, float elapsed, float response);
1134 
1135 
1136 #pragma mark -
1137 #pragma mark Static Linear Algebra
1138 
1146  static float dot(const Vec3& v1, const Vec3& v2);
1147 
1157  static Vec3* cross(const Vec3& v1, const Vec3& v2, Vec3* dst);
1158 
1170  static Vec3* normalize(const Vec3& v, Vec3* dst);
1171 
1181  static Vec3* midpoint(const Vec3& v1, const Vec3& v2, Vec3* dst);
1182 
1192  static Vec3* project(const Vec3& v1, const Vec3& v2, Vec3* dst);
1193 
1208  static Vec3* lerp(const Vec3& v1, const Vec3& v2, float alpha, Vec3* dst);
1209 
1210 
1211 #pragma mark -
1212 #pragma mark Conversion Methods
1213 public:
1224  std::string toString(bool verbose = false) const;
1225 
1227  operator std::string() const { return toString(); }
1228 
1230  operator Color4() const;
1231 
1239  explicit Vec3(Color4 color);
1240 
1250  Vec3& operator= (Color4 color);
1251 
1253  operator Color4f() const;
1254 
1262  explicit Vec3(const Color4f& color);
1263 
1273  Vec3& operator= (const Color4f& color);
1274 
1280  operator Vec2() const;
1281 
1289  explicit Vec3(const Vec2& v);
1290 
1300  Vec3& operator= (const Vec2& v);
1301 
1308  operator Vec4() const;
1309 
1318  explicit Vec3(const Vec4& v);
1319 
1330  Vec3& operator= (const Vec4& size);
1331 
1332 };
1333 
1334 #pragma mark -
1335 #pragma mark Friend Operations
1336 
1345 inline const Vec3 operator*(float x, const Vec3& v) {
1346  Vec3 result(v);
1347  return result.scale(x);
1348 }
1349 
1351 typedef Vec3 Point3;
1352 
1353 }
1354 #endif /* __CU_VEC3_H__ */
cugl::Vec3::getProjection
Vec3 getProjection(const Vec3 &other) const
Definition: CUVec3.h:1082
cugl::Vec3::set
Vec3 & set(float x, float y, float z)
Definition: CUVec3.h:141
cugl::Vec3::isInvertible
bool isInvertible() const
Definition: CUVec3.h:895
cugl::Vec3::subtract
Vec3 & subtract(float x, float y, float z)
Definition: CUVec3.h:400
cugl::Vec3::subtract
Vec3 & subtract(const Vec3 &v)
Definition: CUVec3.h:386
cugl::Vec3::operator/=
Vec3 & operator/=(const Vec3 &v)
Definition: CUVec3.h:622
cugl::Vec3::scale
Vec3 & scale(float sx, float sy, float sz)
Definition: CUVec3.h:426
cugl::Vec3::x
float x
Definition: CUVec3.h:66
cugl::Vec4
Definition: CUVec4.h:64
cugl::Vec3::z
float z
Definition: CUVec3.h:70
cugl::Vec3::clamp
static Vec3 * clamp(const Vec3 &v, const Vec3 &min, const Vec3 &max, Vec3 *dst)
cugl::Vec3::distance
float distance(const Vec3 &v) const
Definition: CUVec3.h:934
cugl::Vec3::getNormalization
Vec3 getNormalization() const
Definition: CUVec3.h:1043
cugl::Vec3::add
Vec3 & add(const Vec3 &v)
Definition: CUVec3.h:360
cugl::Vec3::getCross
Vec3 getCross(const Vec3 &other) const
Definition: CUVec3.h:1015
cugl::Vec3::isNearZero
bool isNearZero(float variance=CU_MATH_EPSILON) const
Definition: CUVec3.h:877
cugl::Vec3::scale
Vec3 & scale(const Vec3 &v)
Definition: CUVec3.h:438
cugl::Vec3::Vec3
Vec3()
Definition: CUVec3.h:90
cugl::Vec3::operator+=
Vec3 & operator+=(const Vec3 &v)
Definition: CUVec3.h:565
cugl::Vec3::negate
static Vec3 * negate(const Vec3 &v, Vec3 *dst)
cugl::Vec3::scale
static Vec3 * scale(const Vec3 &v, float s, Vec3 *dst)
cugl::Vec3::operator/=
Vec3 & operator/=(float s)
Definition: CUVec3.h:609
cugl::Vec3::UNIT_X
static const Vec3 UNIT_X
Definition: CUVec3.h:77
cugl::Vec3::operator*=
Vec3 & operator*=(float s)
Definition: CUVec3.h:587
cugl::Vec3::getMap
Vec3 getMap(std::function< float(float)> func) const
Definition: CUVec3.h:551
cugl::Vec3::operator>
bool operator>(const Vec3 &v) const
Definition: CUVec3.h:768
cugl::Vec3::scale
Vec3 & scale(float s)
Definition: CUVec3.h:412
cugl::Color4
Definition: CUColor4.h:1084
cugl::Vec3::Vec3
Vec3(const float *array)
Definition: CUVec3.h:106
cugl::Vec3::getLerp
Vec3 getLerp(const Vec3 &other, float alpha)
Definition: CUVec3.h:1115
cugl::Vec3::reciprocate
static Vec3 * reciprocate(const Vec3 &v, Vec3 *dst)
cugl::Vec3::Vec3
Vec3(const Vec3 &p1, const Vec3 &p2)
Definition: CUVec3.h:114
cugl::Vec3::operator<=
bool operator<=(const Vec3 &v) const
Definition: CUVec3.h:753
cugl::Vec3::add
Vec3 & add(float x, float y, float z)
Definition: CUVec3.h:374
cugl::Vec3::equals
bool equals(const Vec3 &v, float variance=CU_MATH_EPSILON) const
Definition: CUVec3.h:854
cugl::Vec3::getMidpoint
Vec3 getMidpoint(const Vec3 &other) const
Definition: CUVec3.h:1057
cugl::Vec3::lengthSquared
float lengthSquared() const
Definition: CUVec3.h:979
cugl::Vec3::operator!=
bool operator!=(const Vec3 &v) const
Definition: CUVec3.h:811
cugl::Vec3::map
Vec3 & map(std::function< float(float)> func)
Definition: CUVec3.h:536
cugl::Vec3::getNegation
Vec3 getNegation() const
Definition: CUVec3.h:505
cugl::Vec3::isZero
bool isZero() const
Definition: CUVec3.h:866
cugl::Vec3::operator/
const Vec3 operator/(const Vec3 &v) const
Definition: CUVec3.h:719
cugl::Vec3::set
Vec3 & set(const float *array)
Definition: CUVec3.h:153
cugl::Vec3::angle
static float angle(const Vec3 &v1, const Vec3 &v2, const Vec3 &up=Vec3::UNIT_Z)
cugl::Vec3::ZERO
static const Vec3 ZERO
Definition: CUVec3.h:73
cugl::Vec3::lerp
Vec3 & lerp(const Vec3 &other, float alpha)
Definition: CUVec3.h:1098
cugl::Vec3::operator-
const Vec3 operator-(const Vec3 &v) const
Definition: CUVec3.h:649
cugl::Vec3::reciprocate
Vec3 & reciprocate()
Definition: CUVec3.h:493
cugl::Vec3::set
Vec3 & set(const Vec3 &v)
Definition: CUVec3.h:165
cugl::Vec3::UNIT_Y
static const Vec3 UNIT_Y
Definition: CUVec3.h:79
cugl::Vec3::operator/
const Vec3 operator/(float s) const
Definition: CUVec3.h:704
cugl::Vec3::operator+
const Vec3 operator+(const Vec3 &v) const
Definition: CUVec3.h:635
cugl::Vec3::getAngle
float getAngle(const Vec3 &other, const Vec3 &up=Vec3::UNIT_Z) const
cugl::Vec3::operator<
bool operator<(const Vec3 &v) const
Definition: CUVec3.h:738
cugl::Vec3::operator-=
Vec3 & operator-=(const Vec3 &v)
Definition: CUVec3.h:576
cugl::Color4f
Definition: CUColor4.h:73
cugl::Vec3::operator*=
Vec3 & operator*=(const Vec3 &v)
Definition: CUVec3.h:598
cugl::Vec3::operator*
const Vec3 operator*(const Vec3 &v) const
Definition: CUVec3.h:690
cugl::Vec3::add
static Vec3 * add(const Vec3 &v1, const Vec3 &v2, Vec3 *dst)
cugl::Vec3::y
float y
Definition: CUVec3.h:68
cugl::Vec3::getReciprocal
Vec3 getReciprocal() const
Definition: CUVec3.h:521
cugl::Vec2
Definition: CUVec2.h:61
cugl::Vec3::toString
std::string toString(bool verbose=false) const
cugl::Vec3::set
Vec3 & set(const Vec3 &p1, const Vec3 &p2)
Definition: CUVec3.h:178
cugl::Vec3::setZero
Vec3 & setZero()
Definition: CUVec3.h:188
cugl::Vec3::subtract
static Vec3 * subtract(const Vec3 &v1, const Vec3 &v2, Vec3 *dst)
cugl::Vec3::ONE
static const Vec3 ONE
Definition: CUVec3.h:75
cugl::Vec3::isOne
bool isOne() const
Definition: CUVec3.h:886
cugl::Vec3::length
float length() const
Definition: CUVec3.h:963
cugl::Vec3::negate
Vec3 & negate()
Definition: CUVec3.h:479
cugl::Vec3::operator-
const Vec3 operator-() const
Definition: CUVec3.h:661
cugl::Vec3::distanceSquared
float distanceSquared(const Vec3 &v) const
Definition: CUVec3.h:952
cugl::Vec3::midpoint
static Vec3 * midpoint(const Vec3 &v1, const Vec3 &v2, Vec3 *dst)
cugl::Vec3::UNIT_Z
static const Vec3 UNIT_Z
Definition: CUVec3.h:81
cugl::Vec3::isUnit
bool isUnit(float variance=CU_MATH_EPSILON) const
Definition: CUVec3.h:906
cugl::Vec3::divide
static Vec3 * divide(const Vec3 &v, float s, Vec3 *dst)
cugl::Vec3::operator=
Vec3 & operator=(const float *array)
Definition: CUVec3.h:128
cugl::Vec3::smooth
Vec3 & smooth(const Vec3 &target, float elapsed, float response)
cugl::Vec3
Definition: CUVec3.h:61
cugl::Vec3::cross
Vec3 & cross(const Vec3 &v)
Definition: CUVec3.h:1002
cugl::Vec3::getClamp
Vec3 getClamp(const Vec3 &min, const Vec3 &max) const
Definition: CUVec3.h:349
cugl::Vec3::clamp
Vec3 & clamp(const Vec3 &min, const Vec3 &max)
Definition: CUVec3.h:332
cugl::Vec3::operator>=
bool operator>=(const Vec3 &v) const
Definition: CUVec3.h:783
cugl::Vec3::project
Vec3 & project(const Vec3 &other)
Definition: CUVec3.h:1068
cugl::Vec3::Vec3
Vec3(float x, float y, float z)
Definition: CUVec3.h:99
cugl::Vec3::operator*
const Vec3 operator*(float s) const
Definition: CUVec3.h:675
cugl::Vec3::under
bool under(const Vec3 &v) const
Definition: CUVec3.h:825
cugl::Vec3::normalize
Vec3 & normalize()
cugl::Vec3::over
bool over(const Vec3 &v) const
Definition: CUVec3.h:839
cugl::Vec3::dot
float dot(const Vec3 &v) const
Definition: CUVec3.h:993
cugl::Vec3::operator==
bool operator==(const Vec3 &v) const
Definition: CUVec3.h:797