CUGL
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 zlib 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 
123  Vec3(const Vec3& copy) { x = copy.x; y = copy.y; z = copy.z; }
124 
128  ~Vec3() {}
129 
130 
131 #pragma mark -
132 #pragma mark Setters
133 
140  Vec3& operator=(const Vec3& v) {
141  return set(v);
142  }
143 
151  Vec3& operator=(const float* array) {
152  return set(array);
153  }
154 
164  Vec3& set(float x, float y, float z) {
165  this->x = x; this->y = y; this->z = z;
166  return *this;
167  }
168 
176  Vec3& set(const float* array) {
177  x = array[0]; y = array[1]; z = array[2];
178  return *this;
179  }
180 
188  Vec3& set(const Vec3& v) {
189  x = v.x; y = v.y; z = v.z;
190  return *this;
191  }
192 
201  Vec3& set(const Vec3& p1, const Vec3& p2) {
202  x = p2.x-p1.x; y = p2.y-p1.y; z = p2.z-p1.z;
203  return *this;
204  }
205 
212  x = y = z = 0;
213  return *this;
214  }
215 
216 
217 #pragma mark -
218 #pragma mark Static Arithmetic
219 
229  static Vec3* clamp(const Vec3& v, const Vec3& min, const Vec3& max, Vec3* dst);
230 
244  static float angle(const Vec3& v1, const Vec3& v2, const Vec3& up = Vec3::UNIT_Z);
245 
255  static Vec3* add(const Vec3& v1, const Vec3& v2, Vec3* dst);
256 
267  static Vec3* subtract(const Vec3& v1, const Vec3& v2, Vec3* dst);
268 
280  static Vec3* scale(const Vec3& v, float s, Vec3* dst);
281 
293  static Vec3* scale(const Vec3& v1, const Vec3& v2, Vec3* dst);
294 
306  static Vec3* divide(const Vec3& v, float s, Vec3* dst);
307 
319  static Vec3* divide(const Vec3& v1, const Vec3& v2, Vec3* dst);
320 
333  static Vec3* reciprocate(const Vec3& v, Vec3* dst);
334 
343  static Vec3* negate(const Vec3& v, Vec3* dst);
344 
345 #pragma mark -
346 #pragma mark Arithmetic
347 
355  Vec3& clamp(const Vec3& min, const Vec3& max) {
356  x = clampf(x, min.x, max.x);
357  y = clampf(y, min.y, max.y);
358  z = clampf(z, min.z, max.z);
359  return *this;
360  }
361 
372  Vec3 getClamp(const Vec3& min, const Vec3& max) const {
373  return Vec3(clampf(x,min.x,max.x), clampf(y, min.y, max.y),clampf(z, min.z, max.z));
374  }
375 
383  Vec3& add(const Vec3& v) {
384  x += v.x; y += v.y; z += v.z;
385  return *this;
386  }
387 
397  Vec3& add(float x, float y, float z) {
398  this->x += x; this->y += y; this->z += z;
399  return *this;
400  }
401 
409  Vec3& subtract(const Vec3& v) {
410  x -= v.x; y -= v.y; z -= v.z;
411  return *this;
412  }
413 
423  Vec3& subtract(float x, float y, float z) {
424  this->x -= x; this->y -= y; this->z -= z;
425  return *this;
426  }
427 
435  Vec3& scale(float s) {
436  x *= s; y *= s; z *= s;
437  return *this;
438  }
439 
449  Vec3& scale(float sx, float sy, float sz) {
450  x *= sx; y *= sy; z *= sz;
451  return *this;
452  }
453 
461  Vec3& scale(const Vec3& v) {
462  x *= v.x; y *= v.y; z *= v.z;
463  return *this;
464  }
465 
473  Vec3& divide(float s);
474 
484  Vec3& divide(float sx, float sy, float sz);
485 
495  Vec3& divide(const Vec3& v);
496 
503  x = -x; y = -y; z = -z;
504  return *this;
505  }
506 
517  x = 1.0f/x; y = 1.0f/y; z = 1.0f/z;
518  return *this;
519  }
520 
528  Vec3 getNegation() const {
529  Vec3 result(*this);
530  return result.negate();
531  }
532 
544  Vec3 getReciprocal() const {
545  Vec3 result(*this);
546  return result.reciprocate();
547  }
548 
559  Vec3& map(std::function<float(float)> func) {
560  x = func(x); y = func(y); z = func(z);
561  return *this;
562  }
563 
574  Vec3 getMap(std::function<float(float)> func) const {
575  return Vec3(func(x), func(y), func(z));
576  }
577 
578 
579 #pragma mark -
580 #pragma mark Operators
581 
588  Vec3& operator+=(const Vec3& v) {
589  return add(v);
590  }
591 
599  Vec3& operator-=(const Vec3& v) {
600  return subtract(v);
601  }
602 
610  Vec3& operator*=(float s) {
611  return scale(s);
612  }
613 
621  Vec3& operator*=(const Vec3& v) {
622  return scale(v);
623  }
624 
632  Vec3& operator/=(float s) {
633  return divide(s);
634  }
635 
645  Vec3& operator/=(const Vec3& v) {
646  return divide(v);
647  }
648 
658  const Vec3 operator+(const Vec3& v) const {
659  Vec3 result(*this);
660  return result.add(v);
661  }
662 
672  const Vec3 operator-(const Vec3& v) const {
673  Vec3 result(*this);
674  return result.subtract(v);
675  }
676 
684  const Vec3 operator-() const {
685  Vec3 result(*this);
686  return result.negate();
687  }
688 
698  const Vec3 operator*(float s) const {
699  Vec3 result(*this);
700  return result.scale(s);
701  }
702 
713  const Vec3 operator*(const Vec3& v) const {
714  Vec3 result(*this);
715  return result.scale(v);
716  }
717 
727  const Vec3 operator/(float s) const {
728  Vec3 result(*this);
729  return result.divide(s);
730  }
731 
742  const Vec3 operator/(const Vec3& v) const {
743  Vec3 result(*this);
744  return result.divide(v);
745  }
746 
747 
748 #pragma mark -
749 #pragma mark Comparisons
750 
761  bool operator<(const Vec3& v) const {
762  return (x == v.x ? (y == v.y ? z < v.z : y < v.y) : x < v.x);
763  }
764 
776  bool operator<=(const Vec3& v) const {
777  return (x == v.x ? (y == v.y ? z <= v.z : y <= v.y) : x <= v.x);
778  }
779 
791  bool operator>(const Vec3& v) const {
792  return (x == v.x ? (y == v.y ? z > v.z : y > v.y) : x > v.x);
793  }
794 
806  bool operator>=(const Vec3& v) const {
807  return (x == v.x ? (y == v.y ? z >= v.z : y >= v.y) : x >= v.x);
808  }
809 
820  bool operator==(const Vec3& v) const {
821  return x == v.x && y == v.y && z == v.z;
822  }
823 
834  bool operator!=(const Vec3& v) const {
835  return x != v.x || y != v.y || z != v.z;
836  }
837 
848  bool under(const Vec3& v) const {
849  return x <= v.x && y <= v.y && z <= v.z;
850  }
851 
862  bool over(const Vec3& v) const {
863  return x >= v.x && y >= v.y && z >= v.z;
864  }
865 
877  bool equals(const Vec3& v, float variance=CU_MATH_EPSILON) const {
878  return distanceSquared(v) <= variance*variance;
879  }
880 
881 
882 #pragma mark -
883 #pragma mark Linear Attributes
884 
889  bool isZero() const {
890  return x == 0.0f && y == 0.0f && z == 0.0f;
891  }
892 
900  bool isNearZero(float variance=CU_MATH_EPSILON) const {
901  return lengthSquared() < variance*variance;
902  }
903 
909  bool isOne() const {
910  return x == 1.0f && y == 1.0f && z == 1.0f;
911  }
912 
918  bool isInvertible() const {
919  return x != 0.0f && y != 0.0f && z != 0.0f;
920  }
921 
929  bool isUnit(float variance=CU_MATH_EPSILON) const {
930  float dot = lengthSquared()-1;
931  return dot < variance && dot > -variance;
932  }
933 
946  float getAngle(const Vec3& other, const Vec3& up = Vec3::UNIT_Z) const;
947 
957  float distance(const Vec3& v) const {
958  return sqrt(distanceSquared(v));
959  }
960 
975  float distanceSquared(const Vec3& v) const {
976  return (x-v.x)*(x-v.x)+(y-v.y)*(y-v.y)+(z-v.z)*(z-v.z);
977  }
978 
986  float length() const {
987  return sqrt(lengthSquared());
988  }
989 
1002  float lengthSquared() const {
1003  return x*x+y*y+z*z;
1004  }
1005 
1006 
1007 #pragma mark -
1008 #pragma mark Linear Algebra
1009 
1016  float dot(const Vec3& v) const { return (x * v.x + y * v.y + z * v.z); }
1017 
1025  Vec3& cross(const Vec3& v) {
1026  return *(cross(*this,v,this));
1027  }
1028 
1038  Vec3 getCross(const Vec3& other) const {
1039  Vec3 result(*this);
1040  return result.cross(other);
1041  }
1042 
1053  Vec3& normalize();
1054 
1067  Vec3 result(*this);
1068  return result.normalize();
1069  }
1070 
1080  Vec3 getMidpoint(const Vec3& other) const {
1081  return Vec3((x + other.x) / 2.0f, (y + other.y) / 2.0f, (z + other.z) / 2.0f);
1082  }
1083 
1091  Vec3& project(const Vec3& other) {
1092  *this = getProjection(other);
1093  return *this;
1094  }
1095 
1105  Vec3 getProjection(const Vec3& other) const {
1106  return other * (dot(other)/other.dot(other));
1107  }
1108 
1121  Vec3& lerp(const Vec3& other, float alpha) {
1122  *this *= (1.f - alpha);
1123  return *this += other * alpha;
1124  }
1125 
1138  Vec3 getLerp(const Vec3& other, float alpha) {
1139  return *this * (1.f - alpha) + other * alpha;
1140  }
1141 
1156  Vec3& smooth(const Vec3& target, float elapsed, float response);
1157 
1158 
1159 #pragma mark -
1160 #pragma mark Static Linear Algebra
1161 
1169  static float dot(const Vec3& v1, const Vec3& v2);
1170 
1180  static Vec3* cross(const Vec3& v1, const Vec3& v2, Vec3* dst);
1181 
1193  static Vec3* normalize(const Vec3& v, Vec3* dst);
1194 
1204  static Vec3* midpoint(const Vec3& v1, const Vec3& v2, Vec3* dst);
1205 
1215  static Vec3* project(const Vec3& v1, const Vec3& v2, Vec3* dst);
1216 
1231  static Vec3* lerp(const Vec3& v1, const Vec3& v2, float alpha, Vec3* dst);
1232 
1233 
1234 #pragma mark -
1235 #pragma mark Conversion Methods
1236 public:
1247  std::string toString(bool verbose = false) const;
1248 
1250  operator std::string() const { return toString(); }
1251 
1253  operator Color4() const;
1254 
1262  explicit Vec3(Color4 color);
1263 
1273  Vec3& operator= (Color4 color);
1274 
1276  operator Color4f() const;
1277 
1285  explicit Vec3(const Color4f& color);
1286 
1296  Vec3& operator= (const Color4f& color);
1297 
1303  operator Vec2() const;
1304 
1312  explicit Vec3(const Vec2& v);
1313 
1323  Vec3& operator= (const Vec2& v);
1324 
1331  operator Vec4() const;
1332 
1341  explicit Vec3(const Vec4& v);
1342 
1353  Vec3& operator= (const Vec4& size);
1354 
1355 };
1356 
1357 #pragma mark -
1358 #pragma mark Friend Operations
1359 
1368 inline const Vec3 operator*(float x, const Vec3& v) {
1369  Vec3 result(v);
1370  return result.scale(x);
1371 }
1372 
1374 typedef Vec3 Point3;
1375 
1376 }
1377 #endif /* __CU_VEC3_H__ */
Vec3 & scale(float s)
Definition: CUVec3.h:435
float dot(const Vec3 &v) const
Definition: CUVec3.h:1016
Vec3 getClamp(const Vec3 &min, const Vec3 &max) const
Definition: CUVec3.h:372
Vec3 & setZero()
Definition: CUVec3.h:211
Vec3 getLerp(const Vec3 &other, float alpha)
Definition: CUVec3.h:1138
Vec3 getNormalization() const
Definition: CUVec3.h:1066
bool isNearZero(float variance=CU_MATH_EPSILON) const
Definition: CUVec3.h:900
Vec3 & cross(const Vec3 &v)
Definition: CUVec3.h:1025
float x
Definition: CUVec3.h:66
Vec3(float x, float y, float z)
Definition: CUVec3.h:99
const Vec3 operator-() const
Definition: CUVec3.h:684
Vec3 & operator*=(const Vec3 &v)
Definition: CUVec3.h:621
Vec3 & operator/=(float s)
Definition: CUVec3.h:632
Vec3 & lerp(const Vec3 &other, float alpha)
Definition: CUVec3.h:1121
Definition: CUVec2.h:61
Vec3()
Definition: CUVec3.h:90
bool operator<(const Vec3 &v) const
Definition: CUVec3.h:761
Vec3 & normalize()
static const Vec3 UNIT_Z
Definition: CUVec3.h:81
Vec3 & operator/=(const Vec3 &v)
Definition: CUVec3.h:645
bool isOne() const
Definition: CUVec3.h:909
float z
Definition: CUVec3.h:70
Vec3 & set(float x, float y, float z)
Definition: CUVec3.h:164
const Vec3 operator*(float s) const
Definition: CUVec3.h:698
Vec3 & add(float x, float y, float z)
Definition: CUVec3.h:397
const Vec3 operator-(const Vec3 &v) const
Definition: CUVec3.h:672
bool under(const Vec3 &v) const
Definition: CUVec3.h:848
float distanceSquared(const Vec3 &v) const
Definition: CUVec3.h:975
Vec3 getCross(const Vec3 &other) const
Definition: CUVec3.h:1038
float length() const
Definition: CUVec3.h:986
Vec3 & set(const Vec3 &p1, const Vec3 &p2)
Definition: CUVec3.h:201
Vec3 & reciprocate()
Definition: CUVec3.h:516
static Vec3 * subtract(const Vec3 &v1, const Vec3 &v2, Vec3 *dst)
Vec3 & map(std::function< float(float)> func)
Definition: CUVec3.h:559
bool operator<=(const Vec3 &v) const
Definition: CUVec3.h:776
Definition: CUColor4.h:73
Vec3 getProjection(const Vec3 &other) const
Definition: CUVec3.h:1105
const Vec3 operator*(const Vec3 &v) const
Definition: CUVec3.h:713
float getAngle(const Vec3 &other, const Vec3 &up=Vec3::UNIT_Z) const
const Vec3 operator/(const Vec3 &v) const
Definition: CUVec3.h:742
bool isInvertible() const
Definition: CUVec3.h:918
static float angle(const Vec3 &v1, const Vec3 &v2, const Vec3 &up=Vec3::UNIT_Z)
const Vec3 operator/(float s) const
Definition: CUVec3.h:727
bool isUnit(float variance=CU_MATH_EPSILON) const
Definition: CUVec3.h:929
static const Vec3 UNIT_Y
Definition: CUVec3.h:79
Vec3 & operator+=(const Vec3 &v)
Definition: CUVec3.h:588
bool over(const Vec3 &v) const
Definition: CUVec3.h:862
static Vec3 * negate(const Vec3 &v, Vec3 *dst)
static const Vec3 ONE
Definition: CUVec3.h:75
~Vec3()
Definition: CUVec3.h:128
Vec3 & set(const Vec3 &v)
Definition: CUVec3.h:188
bool operator!=(const Vec3 &v) const
Definition: CUVec3.h:834
Vec3 & operator=(const Vec3 &v)
Definition: CUVec3.h:140
Vec3 & smooth(const Vec3 &target, float elapsed, float response)
Vec3 & set(const float *array)
Definition: CUVec3.h:176
Definition: CUVec4.h:73
float distance(const Vec3 &v) const
Definition: CUVec3.h:957
Vec3 & subtract(const Vec3 &v)
Definition: CUVec3.h:409
Vec3 & add(const Vec3 &v)
Definition: CUVec3.h:383
Vec3 & scale(float sx, float sy, float sz)
Definition: CUVec3.h:449
Vec3 & clamp(const Vec3 &min, const Vec3 &max)
Definition: CUVec3.h:355
bool operator>(const Vec3 &v) const
Definition: CUVec3.h:791
Vec3 getNegation() const
Definition: CUVec3.h:528
Vec3(const Vec3 &copy)
Definition: CUVec3.h:123
static Vec3 * add(const Vec3 &v1, const Vec3 &v2, Vec3 *dst)
static Vec3 * midpoint(const Vec3 &v1, const Vec3 &v2, Vec3 *dst)
Vec3 & operator*=(float s)
Definition: CUVec3.h:610
static Vec3 * scale(const Vec3 &v, float s, Vec3 *dst)
const Vec3 operator+(const Vec3 &v) const
Definition: CUVec3.h:658
Vec3 & subtract(float x, float y, float z)
Definition: CUVec3.h:423
bool operator==(const Vec3 &v) const
Definition: CUVec3.h:820
static Vec3 * reciprocate(const Vec3 &v, Vec3 *dst)
Vec3 getMidpoint(const Vec3 &other) const
Definition: CUVec3.h:1080
Vec3 & operator=(const float *array)
Definition: CUVec3.h:151
int operator*(Font::Style value)
Definition: CUFont.h:1503
Vec3(const float *array)
Definition: CUVec3.h:106
static Vec3 * divide(const Vec3 &v, float s, Vec3 *dst)
Vec3 & operator-=(const Vec3 &v)
Definition: CUVec3.h:599
Definition: CUVec3.h:61
Vec3 & project(const Vec3 &other)
Definition: CUVec3.h:1091
std::string toString(bool verbose=false) const
Definition: CUColor4.h:1104
float y
Definition: CUVec3.h:68
Definition: CUAnimationNode.h:52
float lengthSquared() const
Definition: CUVec3.h:1002
bool isZero() const
Definition: CUVec3.h:889
static const Vec3 ZERO
Definition: CUVec3.h:73
static const Vec3 UNIT_X
Definition: CUVec3.h:77
static Vec3 * clamp(const Vec3 &v, const Vec3 &min, const Vec3 &max, Vec3 *dst)
bool operator>=(const Vec3 &v) const
Definition: CUVec3.h:806
Vec3 getReciprocal() const
Definition: CUVec3.h:544
Vec3 getMap(std::function< float(float)> func) const
Definition: CUVec3.h:574
Vec3 & negate()
Definition: CUVec3.h:502
Vec3 & scale(const Vec3 &v)
Definition: CUVec3.h:461
Vec3(const Vec3 &p1, const Vec3 &p2)
Definition: CUVec3.h:114
bool equals(const Vec3 &v, float variance=CU_MATH_EPSILON) const
Definition: CUVec3.h:877
Vec3 Point3
Definition: CUVec3.h:1374