Skip to content

Commit

Permalink
add skew methods
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Feb 7, 2024
1 parent 55d86d7 commit 7d33171
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
136 changes: 136 additions & 0 deletions cli/tests/unit/geometry_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,142 @@ Deno.test(function matrixRotateAxisAngleSelf() {
}
});

Deno.test(function matrixSkewX() {
// 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,
};
// deno-fmt-ignore
const expect = {
m11: 1, m21: 2.5773502691896257, m31: 3, m41: 4,
m12: 5, m22: 8.8867513459481270, m32: 7, m42: 8,
m13: 9, m23: 15.1961524227066300, m33: 11, m43: 12,
m14: 13, m24: 21.5055534994651330, m34: 15, m44: 16,
};
const matrix = DOMMatrix.fromMatrix(init);
const matrix2 = matrix.skewX(30);
assertEquals(
matrix,
DOMMatrix.fromMatrix(init),
);
for (
const [key, value] of Object.entries(expect) as [
keyof typeof expect,
number,
][]
) {
assertAlmostEquals(
matrix2[key],
value,
);
}
});

Deno.test(function matrixSkewXSelf() {
// 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,
};
// deno-fmt-ignore
const expect = {
m11: 1, m21: 2.5773502691896257, m31: 3, m41: 4,
m12: 5, m22: 8.8867513459481270, m32: 7, m42: 8,
m13: 9, m23: 15.1961524227066300, m33: 11, m43: 12,
m14: 13, m24: 21.5055534994651330, m34: 15, m44: 16,
};
const matrix = DOMMatrix.fromMatrix(init);
const matrix2 = matrix.skewXSelf(30);
assertStrictEquals(
matrix,
matrix2,
);
for (
const [key, value] of Object.entries(expect) as [
keyof typeof expect,
number,
][]
) {
assertAlmostEquals(
matrix[key],
value,
);
}
});

Deno.test(function matrixSkewY() {
// 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,
};
// deno-fmt-ignore
const expect = {
m11: 2.1547005383792515, m21: 2, m31: 3, m41: 4,
m12: 8.4641016151377530, m22: 6, m32: 7, m42: 8,
m13: 14.7735026918962560, m23: 10, m33: 11, m43: 12,
m14: 21.0829037686547600, m24: 14, m34: 15, m44: 16,
};
const matrix = DOMMatrix.fromMatrix(init);
const matrix2 = matrix.skewY(30);
assertEquals(
matrix,
DOMMatrix.fromMatrix(init),
);
for (
const [key, value] of Object.entries(expect) as [
keyof typeof expect,
number,
][]
) {
assertAlmostEquals(
matrix2[key],
value,
);
}
});

Deno.test(function matrixSkewYSelf() {
// 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,
};
// deno-fmt-ignore
const expect = {
m11: 2.1547005383792515, m21: 2, m31: 3, m41: 4,
m12: 8.4641016151377530, m22: 6, m32: 7, m42: 8,
m13: 14.7735026918962560, m23: 10, m33: 11, m43: 12,
m14: 21.0829037686547600, m24: 14, m34: 15, m44: 16,
};
const matrix = DOMMatrix.fromMatrix(init);
const matrix2 = matrix.skewYSelf(30);
assertStrictEquals(
matrix,
matrix2,
);
for (
const [key, value] of Object.entries(expect) as [
keyof typeof expect,
number,
][]
) {
assertAlmostEquals(
matrix[key],
value,
);
}
});

Deno.test(function matrixMultiply() {
// deno-fmt-ignore
const init = {
Expand Down
47 changes: 47 additions & 0 deletions ext/geometry/01_geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
op_geometry_rotate_self,
op_geometry_scale_self,
op_geometry_scale_with_origin_self,
op_geometry_skew_self,
op_geometry_translate_self,
} from "ext:core/ops";
const {
Expand Down Expand Up @@ -1026,6 +1027,32 @@ class DOMMatrixReadOnly {
return matrix;
}

skewX(sx = 0) {
webidl.assertBranded(this, DOMMatrixReadOnlyPrototype);
const matrix = webidl.createBranded(DOMMatrix);
matrix[_raw] = new Float64Array(this[_raw]);
op_geometry_skew_self(
webidl.converters["unrestricted double"](sx),
0,
matrix[_raw],
);
matrix[_is2D] = this[_is2D];
return matrix;
}

skewY(sy = 0) {
webidl.assertBranded(this, DOMMatrixReadOnlyPrototype);
const matrix = webidl.createBranded(DOMMatrix);
matrix[_raw] = new Float64Array(this[_raw]);
op_geometry_skew_self(
0,
webidl.converters["unrestricted double"](sy),
matrix[_raw],
);
matrix[_is2D] = this[_is2D];
return matrix;
}

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

skewXSelf(sx = 0) {
webidl.assertBranded(this, DOMMatrixReadOnlyPrototype);
op_geometry_skew_self(
webidl.converters["unrestricted double"](sx),
0,
this[_raw],
);
return this;
}

skewYSelf(sy = 0) {
webidl.assertBranded(this, DOMMatrixReadOnlyPrototype);
op_geometry_skew_self(
0,
webidl.converters["unrestricted double"](sy),
this[_raw],
);
return this;
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
Expand Down
36 changes: 36 additions & 0 deletions ext/geometry/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ deno_core::extension!(
op_geometry_rotate_self,
op_geometry_rotate_from_vector_self,
op_geometry_rotate_axis_angle_self,
op_geometry_skew_self,
op_geometry_multiply,
op_geometry_multiply_self,
op_geometry_premultiply_self,
Expand Down Expand Up @@ -129,6 +130,41 @@ pub fn op_geometry_rotate_axis_angle_self(
inout.copy_from(&result);
}

#[op2(fast)]
pub fn op_geometry_skew_self(
x_degrees: f64,
y_degrees: f64,
#[buffer] inout: &mut [f64],
) -> () {
let skew: nalgebra::Matrix<
f64,
nalgebra::Const<4>,
nalgebra::Const<4>,
nalgebra::ArrayStorage<f64, 4, 4>,
> = Matrix::new(
1.0,
x_degrees.to_radians().tan(),
0.0,
0.0,
y_degrees.to_radians().tan(),
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
);
let mut inout = MatrixViewMut::from_slice(inout);
let mut result = Matrix::zeros();
inout.mul_to(&skew, &mut result);
inout.copy_from(&result);
}

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

0 comments on commit 7d33171

Please sign in to comment.