-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectortypes.lua
More file actions
176 lines (140 loc) · 4.2 KB
/
vectortypes.lua
File metadata and controls
176 lines (140 loc) · 4.2 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
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
function instancevec3(v1, v2, v3)
local vec3 = {}
if v1 ~= nil then
vec3.v1 = v1
else
vec3.v1 = nil
end
if v2 ~= nil then
vec3.v2 = v2
else
vec3.v2 = nil
end
if v3 ~= nil then
vec3.v3 = v3
else
vec3.v3 = nil
end
vec3.x = function () return vec3.v1 end
vec3.y = function () return vec3.v2 end
vec3.z = function () return vec3.v3 end
vec3.pos = function (num)
if num == 1 then
return vec3.x()
elseif num == 2 then
return vec3.y()
elseif num == 3 then
return vec3.z()
else
return 0.0
end
end
vec3.negative = function ()
return instancevec3(-vec3.x(), -vec3.y(), -vec3.z())
end
vec3.addvec = function (vec)
return instancevec3(vec3.x() + vec.x(), vec3.y() + vec.y(), vec3.z() + vec.z())
end
vec3.addnum = function (num)
return instancevec3(vec3.x() + num, vec3.y() + num, vec3.z() + num)
end
vec3.subvec = function (vec)
return instancevec3(vec3.x() - vec.x(), vec3.y() - vec.y(), vec3.z() - vec.z())
end
vec3.subnum = function (num)
return instancevec3(vec3.x() - num, vec3.y() - num, vec3.z() - num)
end
vec3.multnum = function (num)
return instancevec3(vec3.x() * num, vec3.y() * num, vec3.z() * num)
end
vec3.multvec = function (vec)
return instancevec3(vec3.x() * vec.x(), vec3.y() * vec.y(), vec3.z() * vec.z())
end
vec3.divbynum = function (num)
return instancevec3(vec3.x() / num, vec3.y() / num, vec3.z() / num)
end
vec3.divbyvec = function (vec)
return instancevec3(vec3.x() / vec.x(), vec3.y() / vec.y(), vec3.z() / vec.z())
end
vec3.lensq = function ()
return vec3.x()*vec3.x() + vec3.y()*vec3.y() + vec3.z()*vec3.z()
end
vec3.len = function ()
return math.sqrt(vec3.lensq())
end
vec3.dot = function (vec)
return vec3.x()*vec.x() + vec3.y()*vec.y() + vec3.z()*vec.z()
end
vec3.cross = function(vec)
return instancevec3(
vec3.y()*vec.z() - vec3.z()*vec.y(),
vec3.z()*vec.x() - vec3.x()*vec.z(),
vec3.x()*vec.y() - vec3.y()*vec.x()
)
end
vec3.unit = function ()
return vec3.divbynum(vec3.len())
end
vec3.printinfo = function ()
print(vec3.x(),vec3.y(),vec3.z())
end
vec3.returnvals = function ()
return vec3.x(),vec3.y(),vec3.z()
end
vec3.nearzero = function ()
local threshold = 1e-8
return vec3.x() < threshold and vec3.y() < threshold and vec3.z() < threshold
end
return vec3
end
function randomvec3(min, max)
if min ~= nil and max ~= nil then
return instancevec3(randomrange(min, max),randomrange(min, max),randomrange(min, max))
end
return instancevec3(random(),random(),random())
end
function randomunitvec3()
while true do
local unit_vector = randomvec3(-1,1)
local lensq = unit_vector.lensq()
if lensq > 1e-160 and lensq < 1 then
return unit_vector.divbynum(math.sqrt(lensq))
end
end
end
function randomonhemisphere(normal)
local onunitsphere = randomunitvec3()
if normal.dot(onunitsphere) > 0 then
return onunitsphere
else
return onunitsphere.negative()
end
end
function instancepoint3(v1,v2,v3)
return instancevec3(v1,v2,v3)
end
function reflectvector(vec,normal)
return vec.subvec(normal.multnum(vec.dot(normal)).multnum(2))
end
function refractvector(vec, normal, etaioveretat, costheta)
local routperp = vec.addvec(normal.multnum(costheta)).multnum(etaioveretat)
local routparallel = normal.multnum(-math.sqrt(math.abs(1 - (routperp.lensq()))))
return routperp.addvec(routparallel)
end
function instanceray(origin, direction)
local ray = {}
if origin ~= nil then
ray.origin = origin
else
ray.origin = nil
end
if direction ~= nil then
ray.direction = direction
else
ray.direction = nil
end
ray.at = function (distance)
return ray.origin.addvec(ray.direction.multnum(distance))
end
return ray
end