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

feat(modeling): input array structure #1299

Merged
merged 7 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 1 addition & 5 deletions packages/modeling/src/colors/colorize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { flatten } from '../utils/flatten.js'

import * as geom2 from '../geometries/geom2/index.js'
import * as geom3 from '../geometries/geom3/index.js'
import * as path2 from '../geometries/path2/index.js'
Expand Down Expand Up @@ -47,14 +45,12 @@ export const colorize = (color, ...objects) => {
if (color.length < 3) throw new Error('color must contain R, G and B values')
if (color.length === 3) color = [color[0], color[1], color[2], 1.0] // add alpha

objects = flatten(objects)
if (objects.length === 0) throw new Error('wrong number of arguments')

const results = objects.map((object) => {
if (geom2.isA(object)) return colorGeom2(color, object)
if (geom3.isA(object)) return colorGeom3(color, object)
if (path2.isA(object)) return colorPath2(color, object)
if (poly3.isA(object)) return colorPoly3(color, object)
if (Array.isArray(object)) return colorize(color, ...object)

object.color = color
return object
Expand Down
16 changes: 8 additions & 8 deletions packages/modeling/src/colors/colorize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ test('color (rgb on objects)', (t) => {
const obs = colorize([1, 0, 0], obj1, obj2)
const exp1 = { color: [1, 0, 0, 1] }
const exp2 = { id: 'a', color: [1, 0, 0, 1] }
const exp3 = { id: 'b', color: [1, 0, 0, 1] }

t.is(obs.length, 3)
t.is(obs.length, 2)
t.deepEqual(obs[0], exp1)
t.deepEqual(obs[1], exp2)

const obs3 = colorize([1, 0, 0], obj1)
const exp3 = { color: [1, 0, 0, 1] }
t.deepEqual(obs3, exp3)
t.deepEqual(obs[1][0], exp2)
t.deepEqual(obs[1][1], exp3)
})

test('color (rgba on objects)', (t) => {
Expand All @@ -28,10 +26,12 @@ test('color (rgba on objects)', (t) => {
const obs = colorize([1, 1, 0.5, 0.8], obj1, obj2)
const exp1 = { color: [1, 1, 0.5, 0.8] }
const exp2 = { id: 'a', color: [1, 1, 0.5, 0.8] }
const exp3 = { id: 'b', color: [1, 1, 0.5, 0.8] }

t.is(obs.length, 3)
t.is(obs.length, 2)
t.deepEqual(obs[0], exp1)
t.deepEqual(obs[1], exp2)
t.deepEqual(obs[1][0], exp2)
t.deepEqual(obs[1][1], exp3)
})

test('color (rgba on geometry)', (t) => {
Expand Down
1 change: 0 additions & 1 deletion packages/modeling/src/measurements/measureAggregateArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { measureArea } from './measureArea.js'
*/
export const measureAggregateArea = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('measureAggregateArea: no geometries supplied')
const areas = measureArea(geometries)
if (geometries.length === 1) {
return areas
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { measureBoundingBox } from './measureBoundingBox.js'
*/
export const measureAggregateBoundingBox = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('measureAggregateBoundingBox: no geometries supplied')
const bounds = measureBoundingBox(geometries)
if (geometries.length === 1) {
return bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { calculateEpsilonFromBounds } from './calculateEpsilonFromBounds.js'
*/
export const measureAggregateEpsilon = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('measureAggregateEpsilon: no geometries supplied')
const bounds = measureAggregateBoundingBox(geometries)

let dimensions = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { measureVolume } from './measureVolume.js'
*/
export const measureAggregateVolume = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('measureAggregateVolume: no geometries supplied')
const volumes = measureVolume(geometries)
if (geometries.length === 1) {
return volumes
Expand Down
1 change: 0 additions & 1 deletion packages/modeling/src/measurements/measureArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ const measureAreaOfSlice = (geometry) => {
*/
export const measureArea = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('wrong number of arguments')

const results = geometries.map((geometry) => {
if (path2.isA(geometry)) return measureAreaOfPath2(geometry)
Expand Down
1 change: 0 additions & 1 deletion packages/modeling/src/measurements/measureBoundingBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ const measureBoundingBoxOfSlice = (geometry) => {
*/
export const measureBoundingBox = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('wrong number of arguments')

const results = geometries.map((geometry) => {
if (path2.isA(geometry)) return measureCached(geometry, measureBoundingBoxOfPath2)
Expand Down
1 change: 0 additions & 1 deletion packages/modeling/src/measurements/measureEpsilon.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { measureBoundingBox } from './measureBoundingBox.js'
*/
export const measureEpsilon = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('wrong number of arguments')

const results = geometries.map((geometry) => {
if (path2.isA(geometry)) return calculateEpsilonFromBounds(measureBoundingBox(geometry), 2)
Expand Down
1 change: 0 additions & 1 deletion packages/modeling/src/measurements/measureVolume.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const measureVolumeOfGeom3 = (geometry) => {
*/
export const measureVolume = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('wrong number of arguments')

const results = geometries.map((geometry) => {
if (geom3.isA(geometry)) return measureVolumeOfGeom3(geometry)
Expand Down
8 changes: 4 additions & 4 deletions packages/modeling/src/operations/booleans/intersect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { areAllShapesTheSameType } from '../../utils/areAllShapesTheSameType.js'
import { flatten } from '../../utils/flatten.js'
import { coalesce } from '../../utils/coalesce.js'

import * as geom2 from '../../geometries/geom2/index.js'
import * as geom3 from '../../geometries/geom3/index.js'
Expand All @@ -13,7 +13,7 @@ import { intersectGeom3 } from './intersectGeom3.js'
* The given geometries should be of the same type, either geom2 or geom3.
*
* @param {...Object} geometries - list of geometries
* @returns {Geom2|geom3} a new geometry
* @returns {Geom2|Geom3} a new geometry
* @alias module:modeling/booleans.intersect
*
* @example
Expand All @@ -30,9 +30,9 @@ import { intersectGeom3 } from './intersectGeom3.js'
* +-------+
*/
export const intersect = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('intersect wrong number of arguments')
geometries = coalesce(geometries)

if (geometries.length === 0) return undefined
if (!areAllShapesTheSameType(geometries)) {
throw new Error('intersect arguments must be the same geometry type')
}
Expand Down
13 changes: 6 additions & 7 deletions packages/modeling/src/operations/booleans/intersect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { geom2, geom3 } from '../../geometries/index.js'

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

test('intersect error wrong number of arguments', (t) => {
const message = 'intersect wrong number of arguments'
t.throws(() => intersect(), { message })
t.throws(() => intersect([]), { message })
t.throws(() => intersect([[], []]), { message })
test('intersect empty arguments', (t) => {
t.is(intersect(), undefined)
t.is(intersect([]), undefined)
t.is(intersect([[], []]), undefined)
t.is(intersect(null, null), undefined)
})

test('intersect error different geometry types', (t) => {
Expand All @@ -20,6 +20,5 @@ test('intersect error non-geometries', (t) => {
const message = 'intersect unsupported geometry type'
t.throws(() => intersect([1, 2, 3], [4, 5, 6]), { message })
t.throws(() => intersect([], [123]), { message })
t.throws(() => intersect("one", "two"), { message })
t.throws(() => intersect(null, null), { message })
t.throws(() => intersect('one', 'two'), { message })
})
8 changes: 2 additions & 6 deletions packages/modeling/src/operations/booleans/intersectGeom2.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { flatten } from '../../utils/flatten.js'

import { INTERSECTION } from './martinez/operation.js'
import { boolean } from './martinez/index.js'

/*
* Return a new 2D geometry representing space in both the first geometry and
* in the subsequent geometries. None of the given geometries are modified.
* @param {...geom2} geometries - list of 2D geometries
* @param {Geom2[]} geometries - a flat list of 2D geometries
* @returns {Geom2} new 2D geometry
*/
export const intersectGeom2 = (...geometries) => {
geometries = flatten(geometries)

export const intersectGeom2 = (geometries) => {
let newGeometry = geometries.shift()
geometries.forEach((geometry) => {
newGeometry = boolean(newGeometry, geometry, INTERSECTION)
Expand Down
26 changes: 25 additions & 1 deletion packages/modeling/src/operations/booleans/intersectGeom2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { geom2 } from '../../geometries/index.js'

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

import { circle, rectangle } from '../../primitives/index.js'
import { circle, rectangle, square } from '../../primitives/index.js'

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

Expand Down Expand Up @@ -73,3 +73,27 @@ test('intersect: intersect of one or more geom2 objects produces expected geomet
t.is(obs.length, 8)
t.true(comparePoints(obs, exp))
})

test('intersect with undefined/null values', (t) => {
const square1 = square({ size: 8 })
const square2 = square({ size: 6 })
const square3 = square({ size: 4 })
const geometries = [square1, undefined, square2, null, square3]

let obs = intersect(...geometries)
let pts = geom2.toPoints(obs)
t.notThrows(() => geom2.validate(obs))
t.is(pts.length, 4)
})

test('intersect of nested arrays', (t) => {
const square1 = square({ size: 8 })
const square2 = square({ size: 6 })
const square3 = square({ size: 4 })
const geometries = [square1, [square2, [square3]]]

const obs = intersect(...geometries)
const pts = geom2.toPoints(obs)
t.notThrows(() => geom2.validate(obs))
t.is(pts.length, 4)
})
8 changes: 2 additions & 6 deletions packages/modeling/src/operations/booleans/intersectGeom3.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { flatten } from '../../utils/flatten.js'

import { retessellate } from '../modifiers/retessellate.js'

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

/*
* Return a new 3D geometry representing space in both the first geometry and
* in the subsequent geometries. None of the given geometries are modified.
* @param {...geom3} geometries - list of 3D geometries
* @param {Geom3[]} geometries - a flat list of 3D geometries
* @returns {Geom3} new 3D geometry
*/
export const intersectGeom3 = (...geometries) => {
geometries = flatten(geometries)

export const intersectGeom3 = (geometries) => {
let newGeometry = geometries.shift()
geometries.forEach((geometry) => {
newGeometry = intersectGeom3Sub(newGeometry, geometry)
Expand Down
6 changes: 1 addition & 5 deletions packages/modeling/src/operations/booleans/scission.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { flatten } from '../../utils/flatten.js'

import * as geom3 from '../../geometries/geom3/index.js'

import { scissionGeom3 } from './scissionGeom3.js'
Expand All @@ -26,13 +24,11 @@ import { scissionGeom3 } from './scissionGeom3.js'
* +-------+ +-------+
*/
export const scission = (...objects) => {
objects = flatten(objects)
if (objects.length === 0) throw new Error('wrong number of arguments')

const results = objects.map((object) => {
// if (path2.isA(object)) return path2.transform(matrix, object)
// if (geom2.isA(object)) return geom2.transform(matrix, object)
if (geom3.isA(object)) return scissionGeom3(object)
if (Array.isArray(object)) return scission(...object)
return object
})
return results.length === 1 ? results[0] : results
Expand Down
8 changes: 4 additions & 4 deletions packages/modeling/src/operations/booleans/subtract.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { areAllShapesTheSameType } from '../../utils/areAllShapesTheSameType.js'
import { flatten } from '../../utils/flatten.js'
import { coalesce } from '../../utils/coalesce.js'

import * as geom2 from '../../geometries/geom2/index.js'
import * as geom3 from '../../geometries/geom3/index.js'
Expand All @@ -13,7 +13,7 @@ import { subtractGeom3 } from './subtractGeom3.js'
* The given geometries should be of the same type, either geom2 or geom3.
*
* @param {...Object} geometries - list of geometries
* @returns {Geom2|geom3} a new geometry
* @returns {Geom2|Geom3} a new geometry
* @alias module:modeling/booleans.subtract
*
* @example
Expand All @@ -30,9 +30,9 @@ import { subtractGeom3 } from './subtractGeom3.js'
* +-------+
*/
export const subtract = (...geometries) => {
geometries = flatten(geometries)
if (geometries.length === 0) throw new Error('subtract wrong number of arguments')
geometries = coalesce(geometries)

if (geometries.length === 0) return undefined
if (!areAllShapesTheSameType(geometries)) {
throw new Error('subtract arguments must be the same geometry type')
}
Expand Down
13 changes: 6 additions & 7 deletions packages/modeling/src/operations/booleans/subtract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { geom2, geom3 } from '../../geometries/index.js'

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

test('subtract error wrong number of arguments', (t) => {
const message = 'subtract wrong number of arguments'
t.throws(() => subtract(), { message })
t.throws(() => subtract([]), { message })
t.throws(() => subtract([[], []]), { message })
test('subtract empty arguments', (t) => {
t.is(subtract(), undefined)
t.is(subtract([]), undefined)
t.is(subtract([[], []]), undefined)
t.is(subtract(null, null), undefined)
})

test('subtract error different geometry types', (t) => {
Expand All @@ -20,6 +20,5 @@ test('subtract error non-geometries', (t) => {
const message = 'subtract unsupported geometry type'
t.throws(() => subtract([1, 2, 3], [4, 5, 6]), { message })
t.throws(() => subtract([], [123]), { message })
t.throws(() => subtract("one", "two"), { message })
t.throws(() => subtract(null, null), { message })
t.throws(() => subtract('one', 'two'), { message })
})
8 changes: 2 additions & 6 deletions packages/modeling/src/operations/booleans/subtractGeom2.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { flatten } from '../../utils/flatten.js'

import { DIFFERENCE } from './martinez/operation.js'
import { boolean } from './martinez/index.js'

/*
* Return a new 2D geometry representing space in the first geometry but
* not in the subsequent geometries. None of the given geometries are modified.
* @param {...geom2} geometries - list of geometries
* @param {Geom2[]} geometries - a flat list of 2D geometries
* @returns {Geom2} new 2D geometry
*/
export const subtractGeom2 = (...geometries) => {
geometries = flatten(geometries)

export const subtractGeom2 = (geometries) => {
let newGeometry = geometries.shift()
geometries.forEach((geometry) => {
newGeometry = boolean(newGeometry, geometry, DIFFERENCE)
Expand Down
26 changes: 25 additions & 1 deletion packages/modeling/src/operations/booleans/subtractGeom2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { geom2 } from '../../geometries/index.js'

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

import { circle, rectangle } from '../../primitives/index.js'
import { circle, rectangle, square } from '../../primitives/index.js'

import { center } from '../transforms/index.js'

Expand Down Expand Up @@ -75,3 +75,27 @@ test('subtract: subtract of one or more geom2 objects produces expected geometry
t.is(obs.length, 0)
t.deepEqual(obs, exp)
})

test('subtract with undefined/null values', (t) => {
const square1 = square({ size: 8 })
const square2 = square({ size: 6 })
const square3 = square({ size: 4 })
const geometries = [square1, undefined, square2, null, square3]

let obs = subtract(...geometries)
let pts = geom2.toPoints(obs)
t.notThrows(() => geom2.validate(obs))
t.is(pts.length, 8)
})

test('subtract of nested arrays', (t) => {
const square1 = square({ size: 8 })
const square2 = square({ size: 6 })
const square3 = square({ size: 4 })
const geometries = [square1, [square2, [square3]]]

const obs = subtract(...geometries)
const pts = geom2.toPoints(obs)
t.notThrows(() => geom2.validate(obs))
t.is(pts.length, 8)
})
Loading
Loading