Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(modeling): geom2 mirroring transform #1302

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/modeling/src/geometries/geom2/transform.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as mat4 from '../../maths/mat4/index.js'

import { reverse } from './reverse.js'

/**
* Transform the given geometry using the given matrix.
* This is a lazy transform of the outlines, as this function only adjusts the transforms.
Expand All @@ -14,5 +16,11 @@ import * as mat4 from '../../maths/mat4/index.js'
*/
export const transform = (matrix, geometry) => {
const transforms = mat4.multiply(mat4.create(), matrix, geometry.transforms)
return Object.assign({}, geometry, { transforms })
const transformed = Object.assign({}, geometry, { transforms })
// 2D determinant
platypii marked this conversation as resolved.
Show resolved Hide resolved
if (matrix[0] * matrix[5] - matrix[4] * matrix[1] < 0) {
// reverse the order to preserve the orientation
return reverse(transformed)
}
return transformed
}
41 changes: 41 additions & 0 deletions packages/modeling/src/geometries/geom2/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import test from 'ava'

import { mat4 } from '../../maths/index.js'

import { measureArea } from '../../measurements/index.js'

import { mirrorX, mirrorZ } from '../../operations/transforms/index.js'

import { square } from '../../primitives/index.js'

import { create, transform, toOutlines, toSides } from './index.js'

import { comparePoints, compareVectors } from '../../../test/helpers/index.js'
Expand Down Expand Up @@ -50,3 +56,38 @@ test('transform: adjusts the transforms of geom2', (t) => {
t.true(comparePoints(another.outlines[0], expected.outlines[0]))
t.true(compareVectors(another.transforms, expected.transforms))
})

test('transform: mirroring transform geom2', (t) => {
const geometry = square()
const matrix = mat4.fromScaling(mat4.create(), [-1, -1, -1])
platypii marked this conversation as resolved.
Show resolved Hide resolved
const transformed = mirrorX(geometry)
t.is(measureArea(geometry), 4)
// area will be negative unless we reversed the points
t.is(measureArea(transformed), 4)
const pts = toOutlines(transformed)[0]
const exp = [[1, 1], [-1, 1], [-1, -1], [1, -1]]
t.true(comparePoints(pts, exp))
t.deepEqual(toSides(transformed), [
[[1, 1], [-1, 1]],
[[-1, 1], [-1, -1]],
[[-1, -1], [1, -1]],
[[1, -1], [1, 1]]
])
})

platypii marked this conversation as resolved.
Show resolved Hide resolved
test('transform: don\'t reverse mirrorZ', (t) => {
const geometry = square()
const transformed = mirrorZ(geometry)
t.is(measureArea(geometry), 4)
// area will be negative unless we DIDN'T reverse the points
t.is(measureArea(transformed), 4)
const pts = toOutlines(transformed)[0]
const exp = [[-1, -1], [1, -1], [1, 1], [-1, 1]]
t.true(comparePoints(pts, exp))
t.deepEqual(toSides(transformed), [
[[-1, -1], [1, -1]],
[[1, -1], [1, 1]],
[[1, 1], [-1, 1]],
[[-1, 1], [-1, -1]]
])
})
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const extrudeRotate = (options, geometry) => {
return [point0, point1]
})
// recreate the geometry from the (-) capped points
sliceGeometry = geom2.reverse(geom2.fromSides(shapeSides))
sliceGeometry = geom2.fromSides(shapeSides)
sliceGeometry = mirrorX(sliceGeometry)
} else if (pointsWithPositiveX.length >= pointsWithNegativeX.length) {
shapeSides = shapeSides.map((side) => {
Expand Down
14 changes: 7 additions & 7 deletions packages/modeling/src/operations/transforms/mirror.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ test('mirror: mirroring of geom2 about X/Y produces expected changes to points',
// mirror about X
let mirrored = mirror({ normal: [1, 0, 0] }, geometry)
let obs = geom2.toPoints(mirrored)
let exp = [[5, -5], [0, 5], [-10, -5]]
let exp = [[-10, -5], [0, 5], [5, -5]]
t.notThrows(() => geom2.validate(mirrored))
t.is(measureArea(mirrored), -measureArea(geometry))
t.is(measureArea(mirrored), measureArea(geometry))
t.true(comparePoints(obs, exp))

mirrored = mirrorX(geometry)
obs = geom2.toPoints(mirrored)
t.notThrows(() => geom2.validate(mirrored))
t.is(measureArea(mirrored), -measureArea(geometry))
t.is(measureArea(mirrored), measureArea(geometry))
t.true(comparePoints(obs, exp))

// mirror about Y
mirrored = mirror({ normal: [0, 1, 0] }, geometry)
obs = geom2.toPoints(mirrored)
exp = [[-5, 5], [0, -5], [10, 5]]
exp = [[10, 5], [0, -5], [-5, 5]]
t.notThrows(() => geom2.validate(mirrored))
t.is(measureArea(mirrored), -measureArea(geometry))
t.is(measureArea(mirrored), measureArea(geometry))
t.true(comparePoints(obs, exp))

mirrored = mirrorY(geometry)
obs = geom2.toPoints(mirrored)
t.notThrows(() => geom2.validate(mirrored))
t.is(measureArea(mirrored), -measureArea(geometry))
t.is(measureArea(mirrored), measureArea(geometry))
t.true(comparePoints(obs, exp))
})

Expand Down Expand Up @@ -158,7 +158,7 @@ test('mirror: mirroring of multiple objects produces an array of mirrored object
t.true(comparePoints(obs, exp))

obs = geom2.toPoints(mirrored[2])
exp = [[-5, 5], [0, -5], [10, 5]]
exp = [[10, 5], [0, -5], [-5, 5]]
t.notThrows(() => geom2.validate(mirrored[2]))
t.true(comparePoints(obs, exp))
})
Loading