dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / external / lib_gfx / vector_n.lucb
#==============================================================================================
#
# vector_n - Gfx::VectorN<3, float> and VectorN<4, float>: the arithmetic CSS transforms
# and their interpolation use
#
# DESCRIPTION:
# Port of the members of Libraries/LibGfx/VectorN.h that LibWeb calls on FloatVector3
# and FloatVector4 (luce-browser-render's gfx declares the two structs; its region r08
# left their methods for the users of CSS transform interpolation, phase 4). luce-
# browser-render is pinned, so region p4a ports them here (DESIGN.md ยง4.4: a later
# phase's declarations for a lower package go to web): construction, + - and * by a
# scalar, dot, cross, length, normalize and normalized. TransformationStyleValue's
# private length/normalized helpers (region r51a) moved here.
#
# Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
# Copyright (c) 2022, Jelle Raaijmakers <jelle@ladybird.org>
# Copyright (c) 2022, the SerenityOS developers.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: FloatVector3 ============================================================================
## Ported from Libraries/LibGfx/VectorN.h:40 Gfx::VectorN<3, float>::VectorN(x, y, z).
pub func gfx_float_vector3_new(x: f32, y: f32, z: f32) -> gfx.FloatVector3:
return gfx.FloatVector3(m_data = [x, y, z])
## Ported from Libraries/LibGfx/VectorN.h:115 Gfx::VectorN<3, float>::operator+(VectorN).
pub func gfx_float_vector3_add(this: gfx.FloatVector3, other: gfx.FloatVector3) -> gfx.FloatVector3:
var result = gfx.FloatVector3(m_data = [0.0, 0.0, 0.0])
for i in 0..<3:
result.m_data[i] = this.m_data[i] + other.m_data[i]
return result
## Ported from Libraries/LibGfx/VectorN.h:124 Gfx::VectorN<3, float>::operator-(VectorN).
pub func gfx_float_vector3_sub(this: gfx.FloatVector3, other: gfx.FloatVector3) -> gfx.FloatVector3:
var result = gfx.FloatVector3(m_data = [0.0, 0.0, 0.0])
for i in 0..<3:
result.m_data[i] = this.m_data[i] - other.m_data[i]
return result
## Ported from Libraries/LibGfx/VectorN.h:181 Gfx::VectorN<3, float>::operator*(U) with a float.
pub func gfx_float_vector3_mul(this: gfx.FloatVector3, f: f32) -> gfx.FloatVector3:
var result = gfx.FloatVector3(m_data = [0.0, 0.0, 0.0])
for i in 0..<3:
result.m_data[i] = this.m_data[i] * f
return result
## Ported from Libraries/LibGfx/VectorN.h:211 Gfx::VectorN<3, float>::dot.
pub func gfx_float_vector3_dot(this: gfx.FloatVector3, other: gfx.FloatVector3) -> f32:
var result: f32 = 0.0
for i in 0..<3:
result += this.m_data[i] * other.m_data[i]
return result
## Ported from Libraries/LibGfx/VectorN.h:220 Gfx::VectorN<3, float>::cross.
pub func gfx_float_vector3_cross(this: gfx.FloatVector3, other: gfx.FloatVector3) -> gfx.FloatVector3:
let x = this.m_data[0]
let y = this.m_data[1]
let z = this.m_data[2]
return gfx_float_vector3_new(y * other.m_data[2] - z * other.m_data[1], z * other.m_data[0] - x * other.m_data[2], x * other.m_data[1] - y * other.m_data[0])
## Ported from Libraries/LibGfx/VectorN.h:229 Gfx::VectorN<3, float>::normalized.
pub func gfx_float_vector3_normalized(this: gfx.FloatVector3) -> gfx.FloatVector3:
var copy = this
gfx_float_vector3_normalize(©)
return copy
## Ported from Libraries/LibGfx/VectorN.h:252 Gfx::VectorN<3, float>::normalize.
pub func gfx_float_vector3_normalize(this: gfx.FloatVector3*):
let inv_length: f32 = 1.0 / gfx_float_vector3_length(*this)
for i in 0..<3:
this.m_data[i] *= inv_length
## Ported from Libraries/LibGfx/VectorN.h:259 Gfx::VectorN<3, float>::length.
pub func gfx_float_vector3_length(this: gfx.FloatVector3) -> f32:
return math32.sqrt(gfx_float_vector3_dot(this, this))
# mark: FloatVector4 ============================================================================
## Ported from Libraries/LibGfx/VectorN.h:40 Gfx::VectorN<4, float>::VectorN(x, y, z, w).
pub func gfx_float_vector4_new(x: f32, y: f32, z: f32, w: f32) -> gfx.FloatVector4:
return gfx.FloatVector4(m_data = [x, y, z, w])
## Ported from Libraries/LibGfx/VectorN.h:115 Gfx::VectorN<4, float>::operator+(VectorN).
pub func gfx_float_vector4_add(this: gfx.FloatVector4, other: gfx.FloatVector4) -> gfx.FloatVector4:
var result = gfx_float_vector4_new(0.0, 0.0, 0.0, 0.0)
for i in 0..<4:
result.m_data[i] = this.m_data[i] + other.m_data[i]
return result
## Ported from Libraries/LibGfx/VectorN.h:124 Gfx::VectorN<4, float>::operator-(VectorN).
pub func gfx_float_vector4_sub(this: gfx.FloatVector4, other: gfx.FloatVector4) -> gfx.FloatVector4:
var result = gfx_float_vector4_new(0.0, 0.0, 0.0, 0.0)
for i in 0..<4:
result.m_data[i] = this.m_data[i] - other.m_data[i]
return result
## Ported from Libraries/LibGfx/VectorN.h:181 Gfx::VectorN<4, float>::operator*(U) with a float.
pub func gfx_float_vector4_mul(this: gfx.FloatVector4, f: f32) -> gfx.FloatVector4:
var result = gfx_float_vector4_new(0.0, 0.0, 0.0, 0.0)
for i in 0..<4:
result.m_data[i] = this.m_data[i] * f
return result
## Ported from Libraries/LibGfx/VectorN.h:211 Gfx::VectorN<4, float>::dot.
pub func gfx_float_vector4_dot(this: gfx.FloatVector4, other: gfx.FloatVector4) -> f32:
var result: f32 = 0.0
for i in 0..<4:
result += this.m_data[i] * other.m_data[i]
return result