-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgebra.h
126 lines (108 loc) · 2.54 KB
/
Algebra.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
//
// Created by Selin Yıldırım on 13.11.2021.
//
#pragma once
#include <iostream>
#include "parser.h"
#include "ppm.h"
#include <cmath> // for sqrt in Algebra.cpp
#ifndef GIT_ALGEBRA_H
#define GIT_ALGEBRA_H
#define LOG_ERR(message) cout << endl << message << endl;
using namespace parser;
using namespace std;
inline Vec3f operator+(Vec3f lhs, const Vec3f& rhs)
{
// TODO: insert return statement here
Vec3f r;
r.x = lhs.x + rhs.x;
r.y = lhs.y + rhs.y;
r.z = lhs.z + rhs.z;
return r;
}
inline Vec3f operator-(Vec3f lhs, const Vec3f& rhs)
{
Vec3f r;
r.x = lhs.x - rhs.x;
r.y = lhs.y - rhs.y;
r.z = lhs.z - rhs.z;
return r;
}
inline Vec3f operator*(Vec3f lhs, float rhs)
{
Vec3f r;
r.x = lhs.x * rhs;
r.y = lhs.y * rhs;
r.z = lhs.z * rhs;
return r;
}
inline Vec3f operator/(Vec3f lhs, float rhs)
{
Vec3f r;
r.x = lhs.x / rhs;
r.y = lhs.y / rhs;
r.z = lhs.z / rhs;
return r;
}
inline Vec3f operator*(float lhs, const Vec3f& rhs)
{
Vec3f r;
r.x = lhs * rhs.x;
r.y = lhs * rhs.y;
r.z = lhs * rhs.z;
return r;
}
/*
* calculates distance between two coordinates
*/
inline float calculateDistance(const Vec3f& source, const Vec3f& destination)
{
Vec3f r = source - destination;
return sqrt(r.x * r.x + r.y * r.y + r.z * r.z);
}
inline float dotProduct(const Vec3f& lhs, const Vec3f& rhs)
{
float r = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
return r;
}
inline Vec3f crossProduct(const Vec3f& lhs, const Vec3f& rhs)
{
Vec3f r;
r.x = lhs.y * rhs.z - lhs.z * rhs.y;
r.y = lhs.z * rhs.x - lhs.x * rhs.z;
r.z = lhs.x * rhs.y - lhs.y * rhs.x;
return r;
}
inline Vec3f hadamardProduct(const Vec3f& lhs, const Vec3f& rhs)
{
Vec3f r;
r.x = lhs.x * rhs.x;
r.y = lhs.y * rhs.y;
r.z = lhs.z * rhs.z;
return r;
}
inline float determinant(const Vec3f& v1, const Vec3f& v2, const Vec3f& v3)
{
float r = v1.x * v2.y * v3.z - v1.x * v2.z * v3.y - v1.y * v2.x * v3.z + v1.y * v2.z * v3.x + v1.z * v2.x * v3.y - v1.z * v2.y * v3.z;
return r;
}
inline Vec3f& normalize(Vec3f& v)
{
float norm = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
if (norm < epsilon) {
//LOG_ERR("Norm is zero while normalizing. Quitting here.")
return v;
}
v.x = v.x / norm;
v.y = v.y / norm;
v.z = v.z / norm;
return v;
}
inline Vec3f& limitColorRange(Vec3f& v)
{
v.x = min(255, max(0, (int)v.x));
v.y = min(255, max(0, (int)v.y));
v.y = min(255, max(0, (int)v.y));
return v;
}
#endif //GIT_ALGEBRA_H