summaryrefslogtreecommitdiff
path: root/honey/vec3.lua
blob: af9ac4bb2c201fbc49ea8d7fdde7866fdb403dc9 (plain)
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
local glm = honey.glm

local vec3 = {}
setmetatable(vec3, {__index=_G})
setfenv(1, vec3)


Vec3 = {}

function Vec3.new(_, values)
	local self = {}
	self.data = glm.vec3_create()
	setmetatable(self, Vec3)
	if values then
		self[1] = values[1]
		self[2] = values[2]
		self[3] = values[3]
	end
	return self
end
setmetatable(Vec3, {__call=Vec3.new})


function Vec3.__index(self, key)
	if type(key) == 'number' then
		return glm.vec3_get(self.data, key-1)
	else
		return Vec3[key]
	end
end


function Vec3.__newindex(self, key, value)
	glm.vec3_set(self.data, key-1, value)
end


function Vec3.__tostring(self)
	return string.format("[%.4f, %.4f, %.4f]", self[1], self[2], self[3])
end


--===== arithmetic =====--

local function swapIfNumber(self, other)
	if type(self) == "number" and type(other) == "table" then
		return other, self
	else
		return self, other
	end
end


function Vec3.__add(self, other)
	local self, other = swapIfNumber(self, other)

	local dest = Vec3()
	if type(other) == "number" then
		glm.vec3_adds(self.data, other, dest.data)
	elseif type(other) == "table" then
		glm.vec3_add(self.data, other.data, dest.data)
	else
		error(string.format("cannot add %s to Vec3", type(other)))
	end
	return dest
end


function Vec3.__sub(self, other)
	local dest = Vec3()
	if type(other) == "number" then
		glm.vec3_subs(self.data, other, dest.data)
	elseif type(other) == "table" then
		glm.vec3_sub(self.data, other.data, dest.data)
	else
		error(string.format("cannot subtract %s from Vec3", type(other)))
	end
	return dest
end


function Vec3.__mul(self, other)
	local self, other = swapIfNumber(self, other)
	local dest = Vec3()
	if type(other) == "number" then
		glm.vec3_scale(self.data, other, dest.data)
	elseif type(other) == "table" then
		glm.vec3_mul(self.data, other.data, dest.data)
	else
		error(string.format("cannot multiply %s and Vec3", type(other)))
	end
	return dest
end


function Vec3.__div(self, other)
	local dest = Vec3()
	if type(other) == "number" then
		glm.vec3_divs(self.data, other, dest.data)
	elseif type(other) == "table" then
		glm.vec3_div(self.data, other.data, dest.data)
	else
		error(string.format("cannot divide Vec3 by %s", type(other)))
	end
	return dest
end




function Vec3.copyTo(self, dest)
	glm.vec3_copy(self.data, dest.data)
end


function Vec3.zero(self)
	glm.vec3_zero(self.data)
end


function Vec3.zero(self)
	glm.vec3_zero(self.data)
end
function Vec3.one(self)
	glm.vec3_one(self.data)
end


function Vec3.dot(self, other)
	return glm.vec3_dot(self.data, other.data)
end


function Vec3.crossTo(self, other, dest)
	glm.vec3_cross(self.data, other.data, dest.data)
end
function Vec3.cross(self, other)
	local dest = Vec3()
	self:crossTo(other, dest)
	return dest
end


function Vec3.crossnTo(self, other, dest)
	glm.vec3_crossn(self.data, other.data, dest.data)
end
function Vec3.crossn(self, other)
	local dest = Vec3()
	self:crossTo(other, dest)
	return dest
end


function Vec3.norm2(self)
	return glm.vec3_norm2(self.data)
end
function Vec3.norm(self)
	return glm.vec3_norm(self.data)
end


function Vec3.normalize(self)
	glm.vec3_normalize(self.data)
end
function Vec3.normalizeTo(self, dest)
	glm.vec3_normalize_to(self.data, dest.data)
end


return vec3.Vec3