summaryrefslogtreecommitdiffstats
path: root/dev/MinGfx/src/vector3.cc
blob: 19262c2d3ea9f33e42acd528c18b561ff6de7ea8 (plain) (blame)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 Copyright (c) 2017,2018 Regents of the University of Minnesota.
 All Rights Reserved.
 See corresponding header file for details.
 */

#include "vector3.h"

#include <math.h>

namespace mingfx {

static const Vector3 s_zerov3d = Vector3(0,0,0);
static const Vector3 s_onev3d = Vector3(1,1,1);
static const Vector3 s_unitxv3d = Vector3(1,0,0);
static const Vector3 s_unityv3d = Vector3(0,1,0);
static const Vector3 s_unitzv3d = Vector3(0,0,1);

const Vector3& Vector3::Zero() { return s_zerov3d; }
const Vector3& Vector3::One() { return s_onev3d; }
const Vector3& Vector3::UnitX() { return s_unitxv3d; }
const Vector3& Vector3::UnitY() { return s_unityv3d; }
const Vector3& Vector3::UnitZ() { return s_unitzv3d; }
    
    
Vector3::Vector3() {
    v[0] = 0.0;
    v[1] = 0.0;
    v[2] = 0.0;
}
  
Vector3::Vector3(float x, float y, float z) {
    v[0] = x;
    v[1] = y;
    v[2] = z;
}
  
Vector3::Vector3(float *ptr) {
    v[0] = ptr[0];
    v[1] = ptr[1];
    v[2] = ptr[2];
}

Vector3::Vector3(const Vector3& other) {
    v[0] = other[0];
    v[1] = other[1];
    v[2] = other[2];
}
  
Vector3::~Vector3() {
}

bool Vector3::operator==(const Vector3& other) const {
  return (fabs(other[0] - v[0]) < MINGFX_MATH_EPSILON &&
          fabs(other[1] - v[1]) < MINGFX_MATH_EPSILON &&
          fabs(other[2] - v[2]) < MINGFX_MATH_EPSILON);
}
  
bool Vector3::operator!=(const Vector3& other) const {
    return (fabs(other[0] - v[0]) >= MINGFX_MATH_EPSILON &&
            fabs(other[1] - v[1]) >= MINGFX_MATH_EPSILON &&
            fabs(other[2] - v[2]) >= MINGFX_MATH_EPSILON);
}

Vector3& Vector3::operator=(const Vector3& other) {
    v[0] = other[0];
    v[1] = other[1];
    v[2] = other[2];
    return *this;
}
  
float Vector3::operator[](const int i) const {
    if ((i>=0) && (i<=2)) {
        return v[i];
    }
    else {
        // w component of a vector is 0 so return the constant 0.0
        return 0.0;
    }
}

float& Vector3::operator[](const int i) {
    return v[i];
}
  
float Vector3::Dot(const Vector3& other) const {
    return v[0]*other[0] + v[1]*other[1] + v[2]*other[2];
}

Vector3 Vector3::Cross(const Vector3& other) const {
    return Vector3(v[1] * other[2] - v[2] * other[1],
                   v[2] * other[0] - v[0] * other[2],
                   v[0] * other[1] - v[1] * other[0]);
}

float Vector3::Length() const {
    return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
}

void Vector3::Normalize() {
    // Hill & Kelley provide this:
    float sizeSq =  + v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
    if (sizeSq < MINGFX_MATH_EPSILON) {
        return; // do nothing to zero vectors;
    } 
    float scaleFactor = (float)1.0/(float)sqrt(sizeSq);
    v[0] *= scaleFactor;
    v[1] *= scaleFactor;
    v[2] *= scaleFactor;
}


Vector3 Vector3::ToUnit() const {
    Vector3 v(*this);
    v.Normalize();
    return v;
}

const float * Vector3::value_ptr() const {
    return v;
}

    
    
Vector3 Vector3::Normalize(const Vector3 &v) {
    return v.ToUnit();
}

Vector3 Vector3::Cross(const Vector3 &v1, const Vector3 &v2) {
    return v1.Cross(v2);
}

float Vector3::Dot(const Vector3 &v1, const Vector3 &v2) {
    return v1.Dot(v2);
}
    
Vector3 Vector3::Lerp(const Vector3 &b, float alpha) const {
    float x = (1.0f-alpha)*(*this)[0] + alpha*b[0];
    float y = (1.0f-alpha)*(*this)[1] + alpha*b[1];
    float z = (1.0f-alpha)*(*this)[2] + alpha*b[2];
    return Vector3(x,y,z);
}

Vector3 Vector3::Lerp(const Vector3 &a, const Vector3 &b, float alpha) {
    float x = (1.0f-alpha)*a[0] + alpha*b[0];
    float y = (1.0f-alpha)*a[1] + alpha*b[1];
    float z = (1.0f-alpha)*a[2] + alpha*b[2];
    return Vector3(x,y,z);
}

    

Vector3 operator/(const Vector3& v, const float s) {
    const float invS = 1 / s;
    return Vector3(v[0]*invS, v[1]*invS, v[2]*invS);
}

Vector3 operator*(const float s, const Vector3& v) {
    return Vector3(v[0]*s, v[1]*s, v[2]*s);
}

Vector3 operator*(const Vector3& v, const float s) {
    return Vector3(v[0]*s, v[1]*s, v[2]*s);
}

Vector3 operator-(const Vector3& v) {
    return Vector3(-v[0], -v[1], -v[2]);
}

Point3 operator+(const Vector3& v, const Point3& p) {
    return Point3(p[0] + v[0], p[1] + v[1], p[2] + v[2]);
};

Point3 operator+(const Point3& p, const Vector3& v) {
    return Point3(p[0] + v[0], p[1] + v[1], p[2] + v[2]);
}

Vector3 operator+(const Vector3& v1, const Vector3& v2) {
    return Vector3(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]);
}

Point3 operator-(const Point3& p, const Vector3& v) {
    return Point3(p[0] - v[0], p[1] - v[1], p[2] - v[2]);
}

Vector3 operator-(const Vector3& v1, const Vector3& v2) {
    return Vector3(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]);
}

Vector3 operator-(const Point3& p1, const Point3& p2) {
  return Vector3(p1[0] - p2[0], p1[1] - p2[1], p1[2] - p2[2]);
}


std::ostream & operator<< ( std::ostream &os, const Vector3 &v) {
  return os << "<" << v[0] << ", " << v[1] << ", " << v[2] << ">";
}

std::istream & operator>> ( std::istream &is, Vector3 &v) {
  // format:  <x, y, z>
  char dummy;
  return is >> dummy >> v[0] >> dummy >> v[1] >> dummy >> v[2] >> dummy;
}


} // end namespace