-
Notifications
You must be signed in to change notification settings - Fork 17
/
mathUtils.h
182 lines (127 loc) · 4.85 KB
/
mathUtils.h
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
173
174
175
176
177
178
179
180
181
182
#pragma once
#include <limits>
#include <cmath>
#include <iostream>
namespace tc
{
class Vector
{
public:
//-------------------
// Access to elements
//-------------------
double x, y, z;
double & operator [] (int i);
const double & operator [] (int i) const;
//-------------
// Constructors
//-------------
Vector(); // no initialization
explicit Vector(double a); // (a a a)
Vector(double a, double b, double c); // (a b c)
//---------------------------------
// Copy constructors and assignment
//---------------------------------
Vector(const Vector &v);
const Vector & operator = (const Vector &v);
//---------
// Equality
//---------
bool operator == (const Vector &v) const;
bool operator != (const Vector &v) const;
//-----------------------------------------------------------------------
// Compare two vectors and test if they are "approximately equal":
//
// equalWithAbsError (v, e)
//
// Returns true if the coefficients of this and v are the same with
// an absolute error of no more than e, i.e., for all i
//
// abs (this[i] - v[i]) <= e
//
// equalWithRelError (v, e)
//
// Returns true if the coefficients of this and v are the same with
// a relative error of no more than e, i.e., for all i
//
// abs (this[i] - v[i]) <= e * abs (this[i])
//-----------------------------------------------------------------------
bool equalWithAbsError(const Vector &v, double e) const;
//------------
// Dot product
//------------
double dot(const Vector &v) const;
double operator ^ (const Vector &v) const;
//---------------------------
// Right-handed cross product
//---------------------------
Vector cross(const Vector &v) const;
const Vector & operator %= (const Vector &v);
Vector operator % (const Vector &v) const;
//------------------------
// Component-wise addition
//------------------------
const Vector & operator += (const Vector &v);
Vector operator + (const Vector &v) const;
//---------------------------
// Component-wise subtraction
//---------------------------
const Vector & operator -= (const Vector &v);
Vector operator - (const Vector &v) const;
//------------------------------------
// Component-wise multiplication by -1
//------------------------------------
Vector operator - () const;
const Vector & negate();
//------------------------------
// Component-wise multiplication
//------------------------------
const Vector & operator *= (const Vector &v);
const Vector & operator *= (double a);
Vector operator * (const Vector &v) const;
Vector operator * (double a) const;
//------------------------
// Component-wise division
//------------------------
const Vector & operator /= (const Vector &v);
const Vector & operator /= (double a);
Vector operator / (const Vector &v) const;
Vector operator / (double a) const;
//----------------------------------------------------------------
// Length and normalization: If v.length() is 0.0, v.normalize()
// and v.normalized() produce a null vector; v.normalizeExc() and
// v.normalizedExc() throw a NullVecExc.
// v.normalizeNonNull() and v.normalizedNonNull() are slightly
// faster than the other normalization routines, but if v.length()
// is 0.0, the result is undefined.
//----------------------------------------------------------------
double length() const;
double length2() const;
const Vector & normalize(); // modifies *this
const Vector & normalizeNonNull();
Vector normalized() const; // does not modify *this
Vector normalizedNonNull() const;
//--------------------------------------------------------
// Number of dimensions, i.e. number of elements in a Vector
//--------------------------------------------------------
static unsigned int dimensions() { return 3; }
//-------------------------------------------------
// Limitations of type double (see also class limits<double>)
//-------------------------------------------------
/*
static double baseTypeMin() { return std::numeric_limits<double>::min(); }
static double baseTypeMax() { return std::numeric_limits<double>::max(); }
static double baseTypeSmallest() { return std::numeric_limits<double>::min(); }
static double baseTypeEpsilon() { return std::numeric_limits<double>::epsilon(); }
*/
//--------------------------------------------------------------
// Base type -- in templates, which accept a parameter, V, which
// could be either a Vec2<double>, a Vector<double>, or a Vec4<double> you can
// refer to double as V::BaseType
//--------------------------------------------------------------
typedef double BaseType;
private:
double lengthTiny() const;
};
std::ostream & operator << (std::ostream &s, const Vector &v);
}