-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVec3.cpp
More file actions
57 lines (41 loc) · 1.79 KB
/
Vec3.cpp
File metadata and controls
57 lines (41 loc) · 1.79 KB
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
/**
* \author Marie DARRIGOL & Anthony LEONARD & Ophélie PELLOUX-PRAYER & Olivier SOLDANO
* \project Ray-Tracing
* \file Vec3.cpp
* \brief Represent a vector a 3 float, either a true vector, a position or a color.
*/
#include "Vec3.h"
#include <cmath>
Vec3::Vec3() : Vec3(0.0, 0.0, 0.0) {}
Vec3::Vec3(const float x, const float y, const float z) : x(x), y(y), z(z) {}
Vec3::Vec3(const Vec3& v) : x(v.x), y(v.y), z(v.z) {}
Vec3::Vec3(const Vec3 & v1, const Vec3 & v2): x(v2.x - v1.x), y(v2.y - v1.y), z(v2.z - v1.z){}
Vec3 Vec3::operator*(const float a) const { return Vec3(a*x, a*y, a*z); }
Vec3 Vec3::operator/(const float a) const { return Vec3(x/a, y/a, z/a); }
float Vec3::operator*(const Vec3& v) const { return x*v.x + y*v.y + z*v.z; }
Vec3 Vec3::operator+(const Vec3& v) const { return Vec3(x + v.x, y + v.y, z + v.z); }
Vec3 Vec3::operator+(const float a) const { return Vec3(a+x, a+y, a+z); }
Vec3 Vec3::operator-(const Vec3& v) const { return Vec3(x - v.x, y - v.y, z - v.z); }
/**
*\fn operator-()
*\brief unary opposite operator for vector
*/
Vec3 Vec3::operator-() const{
return Vec3(-this->x,-this->y, -this->z);
}
/**
*\fn operator^(const Vec3& v)
* \brief cross product operator
*/
Vec3 Vec3::operator^(const Vec3& v) const { return Vec3(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x); }
float Vec3::length() const { return sqrtf(x*x + y*y + z*z); }
Vec3 Vec3::unit() const { float len = this->length(); return Vec3(x / len, y / len, z / len); }
Vec3 Vec3::reflect(Vec3 &norm){
return (((2.f * (*this * norm)) * norm) - *this ).unit();
}
Vec3 Vec3::refract(Vec3 & norm, float index_incom, float index_outgo){
float ratio = index_incom / index_outgo;
Vec3 outgoing;
outgoing = ratio * (norm ^ (-norm ^ *this)) - norm * sqrtf(1.f - ratio * ratio * (norm ^ *this) * (norm ^ *this));
return outgoing;
}