Skip to content

Commit

Permalink
refactor(modeling): reworked slice to use vertices in parameters, etc
Browse files Browse the repository at this point in the history
* refactor(modeling): reworked slice to use vertices in parameters, etc.

* refactor(modeling): adjusted usage of slice in extrusions
  • Loading branch information
z3dev authored Jun 12, 2023
1 parent d388fab commit 6e50bb6
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 51 deletions.
8 changes: 4 additions & 4 deletions packages/modeling/src/geometries/slice/calculatePlane.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import test from 'ava'
import { TAU } from '../../maths/constants.js'
import { mat4 } from '../../maths/index.js'

import { calculatePlane, create, fromPoints, transform } from './index.js'
import { calculatePlane, create, fromVertices, transform } from './index.js'

import { compareVectors } from '../../../test/helpers/index.js'

Expand All @@ -12,7 +12,7 @@ test('slice: calculatePlane() returns correct plans for various slices', (t) =>
// const slice1 = create()
// const plane1 = calculatePlane(slice1)

const slice2 = fromPoints([[0, 0], [1, 0], [1, 1]])
const slice2 = fromVertices([[0, 0], [1, 0], [1, 1]])
const plane2 = calculatePlane(slice2)
t.true(compareVectors(plane2, [0, 0, 1, 0]))

Expand All @@ -25,11 +25,11 @@ test('slice: calculatePlane() returns correct plans for various slices', (t) =>
t.true(compareVectors(plane4, [1, 0, 0, 0]))

// Issue #749
const slice5 = fromPoints([[-4, 0, 2], [4, 0, 2], [4, 5, 2], [6, 5, 2], [4, 7, 2], [-4, 7, 2], [-6, 5, 2], [-4, 5, 2]])
const slice5 = fromVertices([[-4, 0, 2], [4, 0, 2], [4, 5, 2], [6, 5, 2], [4, 7, 2], [-4, 7, 2], [-6, 5, 2], [-4, 5, 2]])
const plane5 = calculatePlane(slice5)
t.true(compareVectors(plane5, [0, 0, 1, 2]))

const slice6 = fromPoints([[4, 0, 0], [-4, 0, 0], [-4, 5, 0], [-6, 5, 0], [-4, 7, 0], [4, 7, 0], [6, 5, 0], [4, 5, 0]])
const slice6 = fromVertices([[4, 0, 0], [-4, 0, 0], [-4, 5, 0], [-6, 5, 0], [-4, 7, 0], [4, 7, 0], [6, 5, 0], [4, 5, 0]])
const plane6 = calculatePlane(slice6)
t.true(compareVectors(plane6, [0, 0, -1, 0]))

Expand Down
6 changes: 3 additions & 3 deletions packages/modeling/src/geometries/slice/clone.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import test from 'ava'

import { create, clone, fromPoints, toPoints } from './index.js'
import { create, clone, fromVertices, toVertices } from './index.js'

test('slice: clone() should return a new slice with same values', (t) => {
const org1 = create()
const ret1 = clone(org1)
t.not(ret1, org1)

const org2 = fromPoints([[1, 1], [-1, 1], [-1, -1], [1, -1]])
const org2 = fromVertices([[1, 1], [-1, 1], [-1, -1], [1, -1]])
const ret2 = clone(org2)
t.not(ret2, org2)
t.deepEqual(toPoints(ret2), toPoints(org2))
t.deepEqual(toVertices(ret2), toVertices(org2))
})
8 changes: 4 additions & 4 deletions packages/modeling/src/geometries/slice/equals.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import test from 'ava'

import { equals, fromPoints } from './index.js'
import { equals, fromVertices } from './index.js'

test('slice: equals() should return proper value', (t) => {
const sliceA = fromPoints([[0, 0], [1, 0], [1, 1]])
const sliceB = fromPoints([[0, 1], [1, 0], [1, 1]])
const sliceC = fromPoints([[0, 0], [1, 0], [1, 1], [0, 0]])
const sliceA = fromVertices([[0, 0], [1, 0], [1, 1]])
const sliceB = fromVertices([[0, 1], [1, 0], [1, 1]])
const sliceC = fromVertices([[0, 0], [1, 0], [1, 1], [0, 0]])

t.true(equals(sliceA, sliceA))

Expand Down
7 changes: 0 additions & 7 deletions packages/modeling/src/geometries/slice/fromPoints.d.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/modeling/src/geometries/slice/fromPoints.test.js

This file was deleted.

7 changes: 7 additions & 0 deletions packages/modeling/src/geometries/slice/fromVertices.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Vec2, Vec3 } from '../../maths/types'

import { Slice } from './type'

type Vertex = Vec2 | Vec3

export function fromVertices(points: Array<Vertex>): Slice
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import { create } from './create.js'
*
* @param {Array} vertices - list of vertices, where each vertex is either 2D or 3D
* @returns {slice} a new slice
* @alias module:modeling/geometries/slice.fromPoints
* @alias module:modeling/geometries/slice.fromVertices
*
* @example
* const vertices = [
* [0, 0, 3],
* [0, 10, 3],
* [0, 10, 6]
* ]
* const slice = fromPoints(vertices)
* const slice = fromVertices(vertices)
*/
export const fromPoints = (vertices) => {
export const fromVertices = (vertices) => {
if (!Array.isArray(vertices)) throw new Error('the given vertices must be an array')
if (vertices.length < 3) throw new Error('the given vertices must contain THREE or more vertices')

Expand Down
11 changes: 11 additions & 0 deletions packages/modeling/src/geometries/slice/fromVertices.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from 'ava'

import { fromVertices } from './index.js'

test('slice: fromVertices() should return a new slice with correct values', (t) => {
const exp1 = {
contours: [[[0, 0, 0], [1, 0, 0], [1, 1, 0]]]
}
const obs1 = fromVertices([[0, 0], [1, 0], [1, 1]])
t.deepEqual(obs1, exp1)
})
4 changes: 2 additions & 2 deletions packages/modeling/src/geometries/slice/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ export { clone } from './clone'
export { create } from './create'
export { equals } from './equals'
export { fromGeom2 } from './fromGeom2'
export { fromPoints } from './fromPoints'
export { fromVertices } from './fromVertices'
export { isA } from './isA'
export { reverse } from './reverse'
export { toEdges } from './toEdges'
export { toPoints } from './toPoints'
export { toVertices } from './toVertices'
export { toPolygons } from './toPolygons'
export { toString } from './toString'
export { transform } from './transform'
Expand Down
4 changes: 2 additions & 2 deletions packages/modeling/src/geometries/slice/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export { clone } from './clone.js'
export { create } from './create.js'
export { equals } from './equals.js'
export { fromGeom2 } from './fromGeom2.js'
export { fromPoints } from './fromPoints.js'
export { fromVertices } from './fromVertices.js'
export { isA } from './isA.js'
export { reverse } from './reverse.js'
export { toEdges } from './toEdges.js'
export { toPoints } from './toPoints.js'
export { toVertices } from './toVertices.js'
export { toPolygons } from './toPolygons.js'
export { toString } from './toString.js'
export { transform } from './transform.js'
4 changes: 2 additions & 2 deletions packages/modeling/src/geometries/slice/isA.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import test from 'ava'

import { isA, create, fromPoints } from './index.js'
import { isA, create, fromVertices } from './index.js'

test('isA: identifies created slice', (t) => {
const p1 = create()
const p2 = fromPoints([[0, 0], [1, 0], [1, 1]])
const p2 = fromVertices([[0, 0], [1, 0], [1, 1]])
t.true(isA(p1))
t.true(isA(p2))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* The returned array should not be modified as the data is shared with the slice.
* @param {slice} slice - the slice
* @returns {Array} an array of 3D vertices
* @alias module:modeling/geometries/slice.toPoints
* @alias module:modeling/geometries/slice.toVertices
*
* @example
* let sharedPoints = toPoints(slice)
* let sharedVertices = toVertices(slice)
*/
export const toPoints = (slice) => {
export const toVertices = (slice) => {
const vertices = []
slice.contours.forEach((contour) => {
contour.forEach((vertex) => {
Expand Down
14 changes: 7 additions & 7 deletions packages/modeling/src/geometries/slice/transform.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'

import { transform, fromPoints, toPoints } from './index.js'
import { transform, fromVertices, toVertices } from './index.js'

test('slice: transform() should return a new slice with correct values', (t) => {
const identityMatrix = [
Expand All @@ -10,11 +10,11 @@ test('slice: transform() should return a new slice with correct values', (t) =>
0, 0, 0, 1
]

const org1 = fromPoints([[0, 0], [1, 0], [1, 1]])
const org1 = fromVertices([[0, 0], [1, 0], [1, 1]])
const ret1 = transform(identityMatrix, org1)
t.not(org1, ret1)

const edges1 = toPoints(ret1)
const edges1 = toVertices(ret1)
const exp1 = [[0, 0, 0], [1, 0, 0], [1, 1, 0]]
t.deepEqual(edges1, exp1)

Expand All @@ -28,11 +28,11 @@ test('slice: transform() should return a new slice with correct values', (t) =>
x, y, z, 1
]

const org2 = fromPoints([[0, 0], [1, 0], [1, 1]])
const org2 = fromVertices([[0, 0], [1, 0], [1, 1]])
const ret2 = transform(translationMatrix, org2)
t.not(org2, ret2)

const edges2 = toPoints(ret2)
const edges2 = toVertices(ret2)
const exp2 = [[1, 5, 7], [2, 5, 7], [2, 6, 7]]
t.deepEqual(edges2, exp2)

Expand All @@ -44,11 +44,11 @@ test('slice: transform() should return a new slice with correct values', (t) =>
0, 0, 0, 1
]

const org3 = fromPoints([[0, 0], [1, 0], [1, 1]])
const org3 = fromVertices([[0, 0], [1, 0], [1, 1]])
const ret3 = transform(rotateZMatrix, org3)
t.not(org3, ret3)

const edges3 = toPoints(ret3)
const edges3 = toVertices(ret3)
const exp3 = [
[0, 0, 0], [6.123233995736766e-17, -1, 0], [1, -0.9999999999999999, 0]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { extrudeWalls } from './extrudeWalls.js'
const defaultCallback = (progress, index, base) => {
let baseSlice = null
if (geom2.isA(base)) baseSlice = slice.fromGeom2(base)
if (poly3.isA(base)) baseSlice = slice.fromPoints(poly3.toVertices(base))
if (poly3.isA(base)) baseSlice = slice.fromVertices(poly3.toVertices(base))

return progress === 0 || progress === 1 ? slice.transform(mat4.fromTranslation(mat4.create(), [0, 0, progress]), baseSlice) : null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test('extrudeFromSlices (torus)', (t) => {
[radius / 2, -radius * sqrt3, 0]
])
hex = poly3.transform(mat4.fromTranslation(mat4.create(), [0, 20, 0]), hex)
hex = slice.fromPoints(poly3.toVertices(hex))
hex = slice.fromVertices(poly3.toVertices(hex))

const angle = TAU / 8
const geometry3 = extrudeFromSlices(
Expand All @@ -79,7 +79,7 @@ test('extrudeFromSlices (torus)', (t) => {
})

test('extrudeFromSlices (same shape, changing dimensions)', (t) => {
const base = slice.fromPoints([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]])
const base = slice.fromVertices([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]])
const geometry3 = extrudeFromSlices(
{
numberOfSlices: 4,
Expand Down

0 comments on commit 6e50bb6

Please sign in to comment.