Skip to content

Commit

Permalink
add translate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Feb 1, 2024
1 parent 3a0116b commit 4206e6c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cli/tests/unit/geometry_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals, assertStrictEquals } from "./test_util.ts";

Deno.test(function matrixTranslate() {
// deno-fmt-ignore
const init = {
m11: 1, m21: 2, m31: 3, m41: 4,
m12: 5, m22: 6, m32: 7, m42: 8,
m13: 9, m23: 10, m33: 11, m43: 12,
m14: 13, m24: 14, m34: 15, m44: 16,
};
const matrix = DOMMatrix.fromMatrix(init);
const matrix2 = matrix.translate(1, 2, 3);
assertEquals(
matrix,
DOMMatrix.fromMatrix(init),
);
assertEquals(
matrix2,
// deno-fmt-ignore
DOMMatrix.fromMatrix({
m11: 1, m21: 2, m31: 3, m41: 1 * 1 + 2 * 2 + 3 * 3 + 4 * 1,
m12: 5, m22: 6, m32: 7, m42: 5 * 1 + 6 * 2 + 7 * 3 + 8 * 1,
m13: 9, m23: 10, m33: 11, m43: 9 * 1 + 10 * 2 + 11 * 3 + 12 * 1,
m14: 13, m24: 14, m34: 15, m44: 13 * 1 + 14 * 2 + 15 * 3 + 16 * 1,
}),
);
});

Deno.test(function matrixTranslateSelf() {
// deno-fmt-ignore
const init = {
m11: 1, m21: 2, m31: 3, m41: 4,
m12: 5, m22: 6, m32: 7, m42: 8,
m13: 9, m23: 10, m33: 11, m43: 12,
m14: 13, m24: 14, m34: 15, m44: 16,
};
const matrix = DOMMatrix.fromMatrix(init);
const matrix2 = matrix.translateSelf(1, 2, 3);
assertStrictEquals(
matrix,
matrix2,
);
assertEquals(
matrix,
// deno-fmt-ignore
DOMMatrix.fromMatrix({
m11: 1, m21: 2, m31: 3, m41: 1 * 1 + 2 * 2 + 3 * 3 + 4 * 1,
m12: 5, m22: 6, m32: 7, m42: 5 * 1 + 6 * 2 + 7 * 3 + 8 * 1,
m13: 9, m23: 10, m33: 11, m43: 9 * 1 + 10 * 2 + 11 * 3 + 12 * 1,
m14: 13, m24: 14, m34: 15, m44: 13 * 1 + 14 * 2 + 15 * 3 + 16 * 1,
}),
);
});

Deno.test(function matrixMultiply() {
// deno-fmt-ignore
const init = {
Expand Down
29 changes: 29 additions & 0 deletions ext/geometry/01_geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
op_geometry_multiply,
op_geometry_multiply_self,
op_geometry_premultiply_self,
op_geometry_translate,
op_geometry_translate_self,
} from "ext:core/ops";
const {
ArrayPrototypeJoin,
Expand Down Expand Up @@ -866,6 +868,21 @@ class DOMMatrixReadOnly {
return isMatrixIdentity(this);
}

translate(tx = 0, ty = 0, tz = 0) {
webidl.assertBranded(this, DOMMatrixReadOnlyPrototype);
const matrix = webidl.createBranded(DOMMatrix);
matrix[_raw] = new Float64Array(16);
op_geometry_translate(
this[_raw],
webidl.converters["unrestricted double"](tx),
webidl.converters["unrestricted double"](ty),
webidl.converters["unrestricted double"](tz),
matrix[_raw],
);
matrix[_is2D] = this[_is2D] && tz === 0;
return matrix;
}

multiply(other = {}) {
webidl.assertBranded(this, DOMMatrixReadOnlyPrototype);
const prefix = "Failed to call 'DOMMatrixReadOnly.prototype.multiply'";
Expand Down Expand Up @@ -1254,6 +1271,18 @@ class DOMMatrix extends DOMMatrixReadOnly {
return this;
}

translateSelf(tx = 0, ty = 0, tz = 0) {
webidl.assertBranded(this, DOMMatrixReadOnlyPrototype);
op_geometry_translate_self(
webidl.converters["unrestricted double"](tx),
webidl.converters["unrestricted double"](ty),
webidl.converters["unrestricted double"](tz),
this[_raw],
);
this[_is2D] &&= tz === 0;
return this;
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
Expand Down
29 changes: 29 additions & 0 deletions ext/geometry/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use deno_core::op2;
use nalgebra::Matrix4;
use nalgebra::MatrixView4;
use nalgebra::MatrixViewMut4;
use nalgebra::Vector3;
use std::path::PathBuf;

type Matrix = Matrix4<f64>;
Expand All @@ -14,6 +15,8 @@ deno_core::extension!(
deno_geometry,
deps = [deno_webidl, deno_web, deno_console],
ops = [
op_geometry_translate,
op_geometry_translate_self,
op_geometry_multiply,
op_geometry_multiply_self,
op_geometry_premultiply_self,
Expand All @@ -25,6 +28,32 @@ pub fn get_declaration() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lib.deno_geometry.d.ts")
}

#[op2(fast)]
pub fn op_geometry_translate(
#[buffer] input: &[f64],
x: f64,
y: f64,
z: f64,
#[buffer] out: &mut [f64],
) -> () {
let shift = Vector3::new(x, y, z);
let mut out = MatrixViewMut::from_slice(out);
out.copy_from_slice(input);
out.prepend_translation_mut(&shift);
}

#[op2(fast)]
pub fn op_geometry_translate_self(
x: f64,
y: f64,
z: f64,
#[buffer] out: &mut [f64],
) -> () {
let shift = Vector3::new(x, y, z);
let mut out = MatrixViewMut::from_slice(out);
out.prepend_translation_mut(&shift);
}

#[op2(fast)]
pub fn op_geometry_multiply(
#[buffer] lhs: &[f64],
Expand Down

0 comments on commit 4206e6c

Please sign in to comment.