quat
This commit is contained in:
parent
7a9bca4236
commit
a307f89589
1 changed files with 107 additions and 97 deletions
150
src/vmath.nim
150
src/vmath.nim
|
@ -806,52 +806,52 @@ proc `*`*(a: Mat4, b: Vec4): Vec4 =
|
||||||
result.z = a[2] * b.x + a[6] * b.y + a[10] * b.z + a[14] * b.w
|
result.z = a[2] * b.x + a[6] * b.y + a[10] * b.z + a[14] * b.w
|
||||||
result.w = a[3] * b.x + a[7] * b.y + a[11] * b.z + a[15] * b.w
|
result.w = a[3] * b.x + a[7] * b.y + a[11] * b.z + a[15] * b.w
|
||||||
|
|
||||||
proc right*(a: Mat4): Vec3 =
|
proc right*(a: Mat4): Vec3 {.inline.} =
|
||||||
result.x = a[0]
|
result.x = a[0]
|
||||||
result.y = a[1]
|
result.y = a[1]
|
||||||
result.z = a[2]
|
result.z = a[2]
|
||||||
|
|
||||||
proc `right=`*(a: var Mat4, b: Vec3) =
|
proc `right=`*(a: var Mat4, b: Vec3) {.inline.} =
|
||||||
a[0] = b.x
|
a[0] = b.x
|
||||||
a[1] = b.y
|
a[1] = b.y
|
||||||
a[2] = b.z
|
a[2] = b.z
|
||||||
|
|
||||||
proc up*(a: Mat4): Vec3 =
|
proc up*(a: Mat4): Vec3 {.inline.} =
|
||||||
result.x = a[4]
|
result.x = a[4]
|
||||||
result.y = a[5]
|
result.y = a[5]
|
||||||
result.z = a[6]
|
result.z = a[6]
|
||||||
|
|
||||||
proc `up=`*(a: var Mat4, b: Vec3) =
|
proc `up=`*(a: var Mat4, b: Vec3) {.inline.} =
|
||||||
a[4] = b.x
|
a[4] = b.x
|
||||||
a[5] = b.y
|
a[5] = b.y
|
||||||
a[6] = b.z
|
a[6] = b.z
|
||||||
|
|
||||||
proc forward*(a: Mat4): Vec3 =
|
proc forward*(a: Mat4): Vec3 {.inline.} =
|
||||||
result.x = a[8]
|
result.x = a[8]
|
||||||
result.y = a[9]
|
result.y = a[9]
|
||||||
result.z = a[10]
|
result.z = a[10]
|
||||||
|
|
||||||
proc `forward=`*(a: var Mat4, b: Vec3) =
|
proc `forward=`*(a: var Mat4, b: Vec3) {.inline.} =
|
||||||
a[8] = b.x
|
a[8] = b.x
|
||||||
a[9] = b.y
|
a[9] = b.y
|
||||||
a[10] = b.z
|
a[10] = b.z
|
||||||
|
|
||||||
proc pos*(a: Mat4): Vec3 =
|
proc pos*(a: Mat4): Vec3 {.inline.} =
|
||||||
result.x = a[12]
|
result.x = a[12]
|
||||||
result.y = a[13]
|
result.y = a[13]
|
||||||
result.z = a[14]
|
result.z = a[14]
|
||||||
|
|
||||||
proc `pos=`*(a: var Mat4, b: Vec3) =
|
proc `pos=`*(a: var Mat4, b: Vec3) {.inline.} =
|
||||||
a[12] = b.x
|
a[12] = b.x
|
||||||
a[13] = b.y
|
a[13] = b.y
|
||||||
a[14] = b.z
|
a[14] = b.z
|
||||||
|
|
||||||
proc rotationOnly*(a: Mat4): Mat4 =
|
proc rotationOnly*(a: Mat4): Mat4 {.inline.} =
|
||||||
result = a
|
result = a
|
||||||
result.pos = vec3(0, 0, 0)
|
result.pos = vec3(0, 0, 0)
|
||||||
|
|
||||||
proc dist*(a, b: Mat4): float32 =
|
proc dist*(a, b: Mat4): float32 =
|
||||||
var
|
let
|
||||||
x = a[12] - b[12]
|
x = a[12] - b[12]
|
||||||
y = a[13] - b[13]
|
y = a[13] - b[13]
|
||||||
z = a[14] - b[14]
|
z = a[14] - b[14]
|
||||||
|
@ -932,7 +932,7 @@ proc hrp*(m: Mat4): Vec3 =
|
||||||
result.z = roll
|
result.z = roll
|
||||||
|
|
||||||
proc frustum*(left, right, bottom, top, near, far: float32): Mat4 =
|
proc frustum*(left, right, bottom, top, near, far: float32): Mat4 =
|
||||||
var
|
let
|
||||||
rl = (right - left)
|
rl = (right - left)
|
||||||
tb = (top - bottom)
|
tb = (top - bottom)
|
||||||
fn = (far - near)
|
fn = (far - near)
|
||||||
|
@ -954,13 +954,13 @@ proc frustum*(left, right, bottom, top, near, far: float32): Mat4 =
|
||||||
result[15] = 0
|
result[15] = 0
|
||||||
|
|
||||||
proc perspective*(fovy, aspect, near, far: float32): Mat4 =
|
proc perspective*(fovy, aspect, near, far: float32): Mat4 =
|
||||||
var
|
let
|
||||||
top = near * tan(fovy * PI / 360.0)
|
top = near * tan(fovy * PI / 360.0)
|
||||||
right = top * aspect
|
right = top * aspect
|
||||||
frustum(-right, right, -top, top, near, far)
|
frustum(-right, right, -top, top, near, far)
|
||||||
|
|
||||||
proc ortho*(left, right, bottom, top, near, far: float32): Mat4 =
|
proc ortho*(left, right, bottom, top, near, far: float32): Mat4 =
|
||||||
var
|
let
|
||||||
rl = (right - left)
|
rl = (right - left)
|
||||||
tb = (top - bottom)
|
tb = (top - bottom)
|
||||||
fn = (far - near)
|
fn = (far - near)
|
||||||
|
@ -982,7 +982,7 @@ proc ortho*(left, right, bottom, top, near, far: float32): Mat4 =
|
||||||
result[15] = 1
|
result[15] = 1
|
||||||
|
|
||||||
proc lookAt*(eye, center, up: Vec3): Mat4 =
|
proc lookAt*(eye, center, up: Vec3): Mat4 =
|
||||||
var
|
let
|
||||||
eyex = eye[0]
|
eyex = eye[0]
|
||||||
eyey = eye[1]
|
eyey = eye[1]
|
||||||
eyez = eye[2]
|
eyez = eye[2]
|
||||||
|
@ -1128,19 +1128,19 @@ type Quat* = object
|
||||||
z*: float32
|
z*: float32
|
||||||
w*: float32
|
w*: float32
|
||||||
|
|
||||||
proc quat*(x, y, z, w: float32): Quat =
|
proc quat*(x, y, z, w: float32): Quat {.inline.} =
|
||||||
result.x = x
|
result.x = x
|
||||||
result.y = y
|
result.y = y
|
||||||
result.z = z
|
result.z = z
|
||||||
result.w = w
|
result.w = w
|
||||||
|
|
||||||
proc conjugate*(q: Quat): Quat =
|
proc conjugate*(q: Quat): Quat {.inline.} =
|
||||||
result.w = +q.w
|
result.w = +q.w
|
||||||
result.x = -q.x
|
result.x = -q.x
|
||||||
result.y = -q.y
|
result.y = -q.y
|
||||||
result.z = -q.z
|
result.z = -q.z
|
||||||
|
|
||||||
proc length*(q: Quat): float32 =
|
proc length*(q: Quat): float32 {.inline.} =
|
||||||
sqrt(
|
sqrt(
|
||||||
q.w * q.w +
|
q.w * q.w +
|
||||||
q.x * q.x +
|
q.x * q.x +
|
||||||
|
@ -1155,23 +1155,23 @@ proc normalize*(q: Quat): Quat =
|
||||||
result.z = q.z / m
|
result.z = q.z / m
|
||||||
result.w = q.w / m
|
result.w = q.w / m
|
||||||
|
|
||||||
proc xyz*(q: Quat): Vec3 =
|
proc xyz*(q: Quat): Vec3 {.inline.} =
|
||||||
result.x = q.x
|
result.x = q.x
|
||||||
result.y = q.y
|
result.y = q.y
|
||||||
result.z = q.z
|
result.z = q.z
|
||||||
|
|
||||||
proc `xyz=`*(q: var Quat, v: Vec3) =
|
proc `xyz=`*(q: var Quat, v: Vec3) {.inline.} =
|
||||||
q.x = v.x
|
q.x = v.x
|
||||||
q.y = v.y
|
q.y = v.y
|
||||||
q.z = v.z
|
q.z = v.z
|
||||||
|
|
||||||
proc `-`*(a: var Quat): Quat =
|
proc `-`*(a: var Quat): Quat {.inline.} =
|
||||||
result.x = -a.x
|
result.x = -a.x
|
||||||
result.y = -a.y
|
result.y = -a.y
|
||||||
result.z = -a.z
|
result.z = -a.z
|
||||||
result.w = -a.w
|
result.w = -a.w
|
||||||
|
|
||||||
proc `+`*(a: Quat, b: Quat): Quat =
|
proc `+`*(a: Quat, b: Quat): Quat {.inline.} =
|
||||||
result.x = a.x + b.x
|
result.x = a.x + b.x
|
||||||
result.y = a.y + b.y
|
result.y = a.y + b.y
|
||||||
result.z = a.z + b.z
|
result.z = a.z + b.z
|
||||||
|
@ -1195,7 +1195,7 @@ proc `*`*(a, b: Quat): Quat =
|
||||||
result.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x
|
result.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x
|
||||||
result.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
|
result.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
|
||||||
|
|
||||||
proc `*`*(q: Quat, v: float32): Quat =
|
proc `*`*(q: Quat, v: float32): Quat {.inline.} =
|
||||||
## Multiply the quaternion by a float32.
|
## Multiply the quaternion by a float32.
|
||||||
result.x = q.x * v
|
result.x = q.x * v
|
||||||
result.y = q.y * v
|
result.y = q.y * v
|
||||||
|
@ -1204,7 +1204,7 @@ proc `*`*(q: Quat, v: float32): Quat =
|
||||||
|
|
||||||
proc `*`*(q: Quat, v: Vec3): Vec3 =
|
proc `*`*(q: Quat, v: Vec3): Vec3 =
|
||||||
## Multiply the quaternion by a vector.
|
## Multiply the quaternion by a vector.
|
||||||
var
|
let
|
||||||
x = v.x
|
x = v.x
|
||||||
y = v.y
|
y = v.y
|
||||||
z = v.z
|
z = v.z
|
||||||
|
@ -1223,29 +1223,35 @@ proc `*`*(q: Quat, v: Vec3): Vec3 =
|
||||||
result.y = iy * qw + iw * -qy + iz * -qx - ix * -qz
|
result.y = iy * qw + iw * -qy + iz * -qx - ix * -qz
|
||||||
result.z = iz * qw + iw * -qz + ix * -qy - iy * -qx
|
result.z = iz * qw + iw * -qz + ix * -qy - iy * -qx
|
||||||
|
|
||||||
|
proc `[]`*(a: var Quat, i: int, b: float32) =
|
||||||
|
case i
|
||||||
|
of 0: a.x
|
||||||
|
of 1: a.y
|
||||||
|
of 2: a.z
|
||||||
|
of 3: a.w
|
||||||
|
else: raise newException(IndexDefect, "Index not in 0 .. 3")
|
||||||
|
|
||||||
proc `[]=`*(a: var Quat, i: int, b: float32) =
|
proc `[]=`*(a: var Quat, i: int, b: float32) =
|
||||||
assert(i == 0 or i == 1 or i == 2 or i == 3)
|
case i
|
||||||
if i == 0:
|
of 0: a.x = b
|
||||||
a.x = b
|
of 1: a.y = b
|
||||||
elif i == 1:
|
of 2: a.z = b
|
||||||
a.y = b
|
of 3: a.w = b
|
||||||
elif i == 2:
|
else: raise newException(IndexDefect, "Index not in 0 .. 3")
|
||||||
a.z = b
|
|
||||||
elif i == 3:
|
|
||||||
a.w = b
|
|
||||||
|
|
||||||
proc mat3*(q: Quat): Mat3 =
|
proc mat3*(q: Quat): Mat3 =
|
||||||
var xx = q.x * q.x
|
let
|
||||||
var xy = q.x * q.y
|
xx = q.x * q.x
|
||||||
var xz = q.x * q.z
|
xy = q.x * q.y
|
||||||
var xw = q.x * q.w
|
xz = q.x * q.z
|
||||||
|
xw = q.x * q.w
|
||||||
|
|
||||||
var yy = q.y * q.y
|
yy = q.y * q.y
|
||||||
var yz = q.y * q.z
|
yz = q.y * q.z
|
||||||
var yw = q.y * q.w
|
yw = q.y * q.w
|
||||||
|
|
||||||
var zz = q.z * q.z
|
zz = q.z * q.z
|
||||||
var zw = q.z * q.w
|
zw = q.z * q.w
|
||||||
|
|
||||||
result[0] = 1 - 2 * (yy + zz)
|
result[0] = 1 - 2 * (yy + zz)
|
||||||
result[1] = 0 + 2 * (xy - zw)
|
result[1] = 0 + 2 * (xy - zw)
|
||||||
|
@ -1258,17 +1264,18 @@ proc mat3*(q: Quat): Mat3 =
|
||||||
result[8] = 1 - 2 * (xx + yy)
|
result[8] = 1 - 2 * (xx + yy)
|
||||||
|
|
||||||
proc mat4*(q: Quat): Mat4 =
|
proc mat4*(q: Quat): Mat4 =
|
||||||
var xx = q.x * q.x
|
let
|
||||||
var xy = q.x * q.y
|
xx = q.x * q.x
|
||||||
var xz = q.x * q.z
|
xy = q.x * q.y
|
||||||
var xw = q.x * q.w
|
xz = q.x * q.z
|
||||||
|
xw = q.x * q.w
|
||||||
|
|
||||||
var yy = q.y * q.y
|
yy = q.y * q.y
|
||||||
var yz = q.y * q.z
|
yz = q.y * q.z
|
||||||
var yw = q.y * q.w
|
yw = q.y * q.w
|
||||||
|
|
||||||
var zz = q.z * q.z
|
zz = q.z * q.z
|
||||||
var zw = q.z * q.w
|
zw = q.z * q.w
|
||||||
|
|
||||||
result[00] = 1 - 2 * (yy + zz)
|
result[00] = 1 - 2 * (yy + zz)
|
||||||
result[01] = 0 + 2 * (xy - zw)
|
result[01] = 0 + 2 * (xy - zw)
|
||||||
|
@ -1288,11 +1295,11 @@ proc mat4*(q: Quat): Mat4 =
|
||||||
result[14] = 0
|
result[14] = 0
|
||||||
result[15] = 1.0
|
result[15] = 1.0
|
||||||
|
|
||||||
proc recifuncalSqrt*(x: float32): float32 =
|
proc recifuncalSqrt*(x: float32): float32 {.inline.} =
|
||||||
1.0 / sqrt(x)
|
1.0 / sqrt(x)
|
||||||
|
|
||||||
proc quat*(m: Mat4): Quat =
|
proc quat*(m: Mat4): Quat =
|
||||||
var
|
let
|
||||||
m00 = m[0]
|
m00 = m[0]
|
||||||
m01 = m[4]
|
m01 = m[4]
|
||||||
m02 = m[8]
|
m02 = m[8]
|
||||||
|
@ -1305,8 +1312,9 @@ proc quat*(m: Mat4): Quat =
|
||||||
m21 = m[6]
|
m21 = m[6]
|
||||||
m22 = m[10]
|
m22 = m[10]
|
||||||
|
|
||||||
var q: Quat
|
var
|
||||||
var t: float32
|
q: Quat
|
||||||
|
t: float32
|
||||||
|
|
||||||
if m22 < 0:
|
if m22 < 0:
|
||||||
if m00 > m11:
|
if m00 > m11:
|
||||||
|
@ -1328,8 +1336,9 @@ proc quat*(m: Mat4): Quat =
|
||||||
q
|
q
|
||||||
|
|
||||||
proc fromAxisAngle*(axis: Vec3, angle: float32): Quat =
|
proc fromAxisAngle*(axis: Vec3, angle: float32): Quat =
|
||||||
var a = axis.normalize()
|
let
|
||||||
var s = sin(angle / 2)
|
a = axis.normalize()
|
||||||
|
s = sin(angle / 2)
|
||||||
result.x = a.x * s
|
result.x = a.x * s
|
||||||
result.y = a.y * s
|
result.y = a.y * s
|
||||||
result.z = a.z * s
|
result.z = a.z * s
|
||||||
|
@ -1348,18 +1357,19 @@ proc toAxisAngle*(q: Quat, axis: var Vec3, angle: var float32) =
|
||||||
axis.z = q.z / sinAngle
|
axis.z = q.z / sinAngle
|
||||||
|
|
||||||
proc quat*(heading, pitch, roll: float32): Quat =
|
proc quat*(heading, pitch, roll: float32): Quat =
|
||||||
var t0 = cos(heading * 0.5)
|
let
|
||||||
var t1 = sin(heading * 0.5)
|
t0 = cos(heading * 0.5)
|
||||||
var t2 = cos(roll * 0.5)
|
t1 = sin(heading * 0.5)
|
||||||
var t3 = sin(roll * 0.5)
|
t2 = cos(roll * 0.5)
|
||||||
var t4 = cos(pitch * 0.5)
|
t3 = sin(roll * 0.5)
|
||||||
var t5 = sin(pitch * 0.5)
|
t4 = cos(pitch * 0.5)
|
||||||
|
t5 = sin(pitch * 0.5)
|
||||||
result.w = t0 * t2 * t4 + t1 * t3 * t5
|
result.w = t0 * t2 * t4 + t1 * t3 * t5
|
||||||
result.x = t0 * t3 * t4 - t1 * t2 * t5
|
result.x = t0 * t3 * t4 - t1 * t2 * t5
|
||||||
result.y = t0 * t2 * t5 + t1 * t3 * t4
|
result.y = t0 * t2 * t5 + t1 * t3 * t4
|
||||||
result.z = t1 * t2 * t4 - t0 * t3 * t5
|
result.z = t1 * t2 * t4 - t0 * t3 * t5
|
||||||
|
|
||||||
proc quat*(hpr: Vec3): Quat =
|
proc quat*(hpr: Vec3): Quat {.inline.} =
|
||||||
quat(hpr.x, hpr.y, hpr.z)
|
quat(hpr.x, hpr.y, hpr.z)
|
||||||
|
|
||||||
proc hrp*(q: Quat): Vec3 =
|
proc hrp*(q: Quat): Vec3 =
|
||||||
|
@ -1380,7 +1390,7 @@ proc hrp*(q: Quat): Vec3 =
|
||||||
var t4 = +1.0 - 2.0 * (ysqr + q.z * q.z)
|
var t4 = +1.0 - 2.0 * (ysqr + q.z * q.z)
|
||||||
result.x = arctan2(t3, t4)
|
result.x = arctan2(t3, t4)
|
||||||
|
|
||||||
proc dot*(a: Quat, b: Quat): float32 =
|
proc dot*(a: Quat, b: Quat): float32 {.inline.} =
|
||||||
a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
|
a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
|
||||||
|
|
||||||
proc nlerp*(a: Quat, b: Quat, v: float32): Quat =
|
proc nlerp*(a: Quat, b: Quat, v: float32): Quat =
|
||||||
|
@ -1393,25 +1403,25 @@ proc nlerp*(a: Quat, b: Quat, v: float32): Quat =
|
||||||
proc `$`*(a: Quat): string =
|
proc `$`*(a: Quat): string =
|
||||||
&"q({a.x:.8f}, {a.y:.8f}, {a.z:.8f}, {a.w:.8f})"
|
&"q({a.x:.8f}, {a.y:.8f}, {a.z:.8f}, {a.w:.8f})"
|
||||||
|
|
||||||
proc rotate*(angle: float32, axis: Vec3): Mat4 =
|
proc rotate*(angle: float32, axis: Vec3): Mat4 {.inline.} =
|
||||||
fromAxisAngle(axis, angle).mat4()
|
fromAxisAngle(axis, angle).mat4()
|
||||||
|
|
||||||
proc rotateX*(angle: float32): Mat4 =
|
proc rotateX*(angle: float32): Mat4 {.inline.} =
|
||||||
rotate(angle, vec3(1, 0, 0))
|
rotate(angle, vec3(1, 0, 0))
|
||||||
|
|
||||||
proc rotateY*(angle: float32): Mat4 =
|
proc rotateY*(angle: float32): Mat4 {.inline.} =
|
||||||
rotate(angle, vec3(0, 1, 0))
|
rotate(angle, vec3(0, 1, 0))
|
||||||
|
|
||||||
proc rotateZ*(angle: float32): Mat4 =
|
proc rotateZ*(angle: float32): Mat4 {.inline.} =
|
||||||
rotate(angle, vec3(0, 0, 1))
|
rotate(angle, vec3(0, 0, 1))
|
||||||
|
|
||||||
proc scaleMat*(scale: Vec3): Mat4 =
|
proc scaleMat*(scale: Vec3): Mat4 {.inline.} =
|
||||||
result[0] = scale.x
|
result[0] = scale.x
|
||||||
result[5] = scale.y
|
result[5] = scale.y
|
||||||
result[10] = scale.z
|
result[10] = scale.z
|
||||||
result[15] = 1.0
|
result[15] = 1.0
|
||||||
|
|
||||||
proc scaleMat*(scale: float32): Mat4 =
|
proc scaleMat*(scale: float32): Mat4 {.inline.} =
|
||||||
scaleMat(vec3(scale, scale, scale))
|
scaleMat(vec3(scale, scale, scale))
|
||||||
|
|
||||||
type Rect* = object
|
type Rect* = object
|
||||||
|
|
Loading…
Reference in a new issue