project mouse into view when clicking w/ mouse unlocked

This commit is contained in:
2024-08-28 02:28:24 +10:00
parent 160c9c8a68
commit 503c48404c
12 changed files with 278 additions and 111 deletions

View File

@ -88,6 +88,19 @@ public extension simd_float4x4 {
.init(0, 0, z, -1),
.init(0, 0, w, 0))
}
func project(_ v: SIMD3<Float>) -> SIMD3<Float> {
let t = self.transpose, v = SIMD4(v, 1)
return .init(simd_dot(v, t.columns.0), simd_dot(v, t.columns.1), simd_dot(v, t.columns.2))
* (1 / simd_dot(v, t.columns.3))
}
static func * (lhs: Self, rhs: SIMD3<Float>) -> SIMD3<Float> {
.init(
simd_dot(lhs.columns.0.xyz, rhs),
simd_dot(lhs.columns.1.xyz, rhs),
simd_dot(lhs.columns.2.xyz, rhs))
}
}
extension simd_quatf {

View File

@ -43,12 +43,32 @@ public struct Size<T: AdditiveArithmetic>: Equatable {
extension Size where T: BinaryInteger {
static var one: Self { .init(T(1), T(1)) }
init<O>(_ other: Size<O>) where O: BinaryInteger {
init<O: BinaryInteger>(_ other: Size<O>) {
self.init(T(other.w), T(other.h))
}
init<O: BinaryFloatingPoint>(_ other: Size<O>) {
self.init(T(other.w), T(other.h))
}
}
struct Rect<T: AdditiveArithmetic>: Equatable {
extension Size where T: BinaryFloatingPoint {
init<O: BinaryInteger>(_ other: Size<O>) {
self.init(T(other.w), T(other.h))
}
init<O: BinaryFloatingPoint>(_ other: Size<O>) {
self.init(T(other.w), T(other.h))
}
@inline(__always) public static func / (lhs: Self, rhs: Self) -> Self { .init(lhs.w / rhs.w, lhs.h / rhs.h) }
}
extension SIMD2 where Scalar: AdditiveArithmetic {
init(_ size: Size<Scalar>) {
self.init(size.w, size.h)
}
}
public struct Rect<T: AdditiveArithmetic>: Equatable {
var x: T, y: T, w: T, h: T
var origin: Point<T> {
@ -76,12 +96,12 @@ struct Rect<T: AdditiveArithmetic>: Equatable {
self.h = size.h
}
@inline(__always) static func == (lhs: Self, rhs: Self) -> Bool {
@inline(__always) public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.x == rhs.x && lhs.y == rhs.y && lhs.w == rhs.w && lhs.h == rhs.h
}
}
extension Rect where T: AdditiveArithmetic {
public extension Rect where T: AdditiveArithmetic {
var left: T { x }
var right: T { x + w }
var up: T { y }

View File

@ -1,3 +1,23 @@
import simd
extension SIMD3 {
var xy: SIMD2<Scalar> {
get { .init(self.x, self.y) }
set {
self.x = newValue.x
self.y = newValue.y
}
}
var xz: SIMD2<Scalar> {
get { .init(self.x, self.z) }
set {
self.x = newValue.x
self.z = newValue.y
}
}
}
extension SIMD3 where Scalar: FloatingPoint {
@inline(__always) static var X: Self { Self(1, 0, 0) }
@inline(__always) static var Y: Self { Self(0, 1, 0) }
@ -10,3 +30,42 @@ extension SIMD3 where Scalar: FloatingPoint {
@inline(__always) static var forward: Self { -Z }
@inline(__always) static var back: Self { Z }
}
extension SIMD3 where Scalar == Float {
static func * (q: simd_quatf, v: Self) -> Self {
#if true
let q = simd_inverse(q)
#else
let q = simd_quatf(real: q.real, imag: -q.imag)
#endif
#if true
var out = q.imag * 2 * simd_dot(q.imag, v)
out += v * (q.real * q.real - simd_dot(q.imag, q.imag))
return out + simd_cross(q.imag, v) * 2 * q.real
#else
let uv = simd_cross(q.imag, v)
let uuv = simd_cross(q.imag, uv)
return v + ((uv * q.real) + uuv) * 2
#endif
}
}
extension SIMD4 {
var xy: SIMD2<Scalar> {
get { .init(self.x, self.y) }
set {
self.x = newValue.x
self.y = newValue.y
}
}
var xyz: SIMD3<Scalar> {
get { .init(self.x, self.y, self.z) }
set {
self.x = newValue.x
self.y = newValue.y
self.z = newValue.z
}
}
}