File size: 4,806 Bytes
898c672 |
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 |
local math3d = {}
math3d.projection_constant = 0.7071067811865
function math3d.project_vec3(vec3)
return
{
vec3[1],
(vec3[2] + vec3[3]) * math3d.projection_constant
}
end
math3d.vector4 = {}
math3d.vector4.zero = {0, 0, 0, 0}
function math3d.vector4.dot_product(u, v)
return u[1] * v[1] + u[2] * v[2] + u[3] * v[3] + u[4] * v[4]
end
function math3d.vector4.add(u, v)
return { u[1]+v[1], u[2]+v[2], u[3]+v[3], u[4]+v[4] }
end
function math3d.vector4.sub(u, v)
return { u[1]-v[1], u[2]-v[2], u[3]-v[3], u[4]-v[4] }
end
function math3d.vector4.from_vec3(u)
return { u[1], u[2], u[3], 1 }
end
math3d.vector3 = {}
math3d.vector3.zero = {0, 0, 0}
function math3d.vector3.dot_product(u, v)
return u[1] * v[1] + u[2] * v[2] + u[3] * v[3]
end
function math3d.vector3.add(u, v)
return { u[1]+v[1], u[2]+v[2], u[3]+v[3] }
end
function math3d.vector3.sub(u, v)
return { u[1]-v[1], u[2]-v[2], u[3]-v[3] }
end
function math3d.vector3.mul(u, k)
return { u[1]*k, u[2]*k, u[3]*k }
end
function math3d.vector3.cross_product(u, v)
return {u[2]*v[3] - u[3]*v[2],u[3]*v[1] - u[1]*v[3],u[1]*v[2] - u[2]*v[1]}
end
function math3d.vector3.angle(u, v)
local len = math.sqrt(math3d.vector3.dot_product(u,u) * math3d.vector3.dot_product(v,v))
local cos_phi = math3d.vector3.dot_product(u, v) / len
return math.acos(cos_phi)
end
math3d.vector2 = {}
math3d.vector2.zero = {0, 0}
function math3d.vector2.dot_product(u, v)
return u[1] * v[1] + u[2] * v[2]
end
function math3d.vector2.add(u, v)
return { u[1]+v[1], u[2]+v[2] }
end
function math3d.vector2.sub(u, v)
return { u[1]-v[1], u[2]-v[2] }
end
function math3d.vector2.mul(u, k)
return { u[1]*k, u[2]*k }
end
function math3d.vector2.rotate(v, phi)
local sin_phi = math.sin(phi)
local cos_phi = math.cos(phi)
return
{
v[1] * cos_phi - v[2] * sin_phi,
v[1] * sin_phi + v[2] * cos_phi
}
end
math3d.matrix4x4 = {}
math3d.matrix4x4.identity =
{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
}
function math3d.matrix4x4.rotation_x(phi)
local sin_phi = math.sin(phi)
local cos_phi = math.cos(phi)
return
{
{ 1, 0, 0, 0 },
{ 0, cos_phi, -sin_phi, 0 },
{ 0, sin_phi, cos_phi, 0 },
{ 0, 0, 0, 1 }
}
end
function math3d.matrix4x4.rotation_y(phi)
local sin_phi = math.sin(phi)
local cos_phi = math.cos(phi)
return
{
{ cos_phi, 0, sin_phi, 0 },
{ 0, 1, 0, 0 },
{-sin_phi, 0, cos_phi, 0 },
{ 0, 0, 0, 1 }
}
end
function math3d.matrix4x4.rotation_z(phi)
local sin_phi = math.sin(phi)
local cos_phi = math.cos(phi)
return
{
{ cos_phi, -sin_phi, 0, 0 },
{ sin_phi, cos_phi, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
}
end
function math3d.matrix4x4.translation(x, y, z)
return
{
{ 1, 0, 0, x },
{ 0, 1, 0, y },
{ 0, 0, 1, z },
{ 0, 0, 0, 1 }
}
end
function math3d.matrix4x4.translation_vec3(vec3)
return math3d.matrix4x4.translation(vec3[1], vec3[2], vec3[3])
end
function math3d.matrix4x4.scale(x, y, z)
return
{
{ x, 0, 0, 0 },
{ 0, y, 0, 0 },
{ 0, 0, z, 0 },
{ 0, 0, 0, 1 }
}
end
function math3d.matrix4x4.column(mat, index)
return { mat[1][index], mat[2][index], mat[3][index], mat[4][index] }
end
function math3d.matrix4x4.transpose(mat)
return
{
math3d.matrix4x4.column(mat, 1),
math3d.matrix4x4.column(mat, 2),
math3d.matrix4x4.column(mat, 3),
math3d.matrix4x4.column(mat, 4)
}
end
function math3d.matrix4x4.mul_mat(m1, m2)
local dot = math3d.vector4.dot_product
local t = math3d.matrix4x4.transpose(m2)
return
{
{ dot(m1[1], t[1]), dot(m1[1], t[2]), dot(m1[1], t[3]), dot(m1[1], t[4]) },
{ dot(m1[2], t[1]), dot(m1[2], t[2]), dot(m1[2], t[3]), dot(m1[2], t[4]) },
{ dot(m1[3], t[1]), dot(m1[3], t[2]), dot(m1[3], t[3]), dot(m1[3], t[4]) },
{ dot(m1[4], t[1]), dot(m1[4], t[2]), dot(m1[4], t[3]), dot(m1[4], t[4]) }
}
end
function math3d.matrix4x4.mul_vec3(mat, vec3)
return
{
math3d.vector3.dot_product(vec3, mat[1]) + mat[1][4],
math3d.vector3.dot_product(vec3, mat[2]) + mat[2][4],
math3d.vector3.dot_product(vec3, mat[3]) + mat[3][4]
}
end
function math3d.matrix4x4.compose(list)
local retval = math3d.matrix4x4.identity
for i,m in ipairs(list) do
retval = math3d.matrix4x4.mul_mat(m, retval)
end
return retval
end
return math3d |