Skip to content

Commit

Permalink
style(modeling): fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii authored Jun 14, 2023
1 parent 12157ac commit e1e20d1
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 127 deletions.
12 changes: 6 additions & 6 deletions packages/modeling/src/curves/bezier/arcLengthToT.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const lengths = require('./lengths')
/**
* Convert a given arc length along a bezier curve to a t value.
* Useful for generating equally spaced points along a bezier curve.
*
*
* @example
* const points = [];
* const segments = 9; // this will generate 10 equally spaced points
Expand All @@ -14,7 +14,7 @@ const lengths = require('./lengths')
* points.push(point);
* }
* return points;
*
*
* @param {Object} [options] options for construction
* @param {Number} [options.distance=0] the distance along the bezier curve for which we want to find the corresponding t value.
* @param {Number} [options.segments=100] the number of segments to use when approximating the curve length.
Expand All @@ -27,8 +27,8 @@ const arcLengthToT = (options, bezier) => {
distance: 0,
segments: 100
}
const {distance, segments} = Object.assign({}, defaults, options)
const { distance, segments } = Object.assign({}, defaults, options)

const arcLengths = lengths(segments, bezier)
// binary search for the index with largest value smaller than target arcLength
let startIndex = 0
Expand Down Expand Up @@ -58,6 +58,6 @@ const arcLengthToT = (options, bezier) => {
const segmentFraction = (distance - lengthBefore) / segmentLength
// add that fractional amount and return
return (targetIndex + segmentFraction) / segments
};
}

module.exports = arcLengthToT
module.exports = arcLengthToT
50 changes: 25 additions & 25 deletions packages/modeling/src/curves/bezier/arcLengthToT.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,71 @@ const { nearlyEqual } = require('../../../test/helpers/index')
test('calculate arcLengthToT for an 1D linear bezier with numeric control points', (t) => {
const bezierCurve = bezier.create([0, 10])
const len = length(100, bezierCurve)
nearlyEqual(t, arcLengthToT({distance: 0}, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len / 2}, bezierCurve), 0.5, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len}, bezierCurve), 1, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: 0 }, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len / 2 }, bezierCurve), 0.5, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len }, bezierCurve), 1, 0.0001)
t.true(true)
})

test('calculate arcLengthToT for an 1D linear bezier with array control points', (t) => {
const bezierCurve = bezier.create([[0], [10]])
const len = length(100, bezierCurve)
nearlyEqual(t, arcLengthToT({distance: 0}, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len / 2}, bezierCurve), 0.5, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len}, bezierCurve), 1, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: 0 }, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len / 2 }, bezierCurve), 0.5, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len }, bezierCurve), 1, 0.0001)
t.true(true)
})

test('calculate arcLengthToT for a 2D linear bezier', (t) => {
const bezierCurve = bezier.create([[0, 0], [10, 10]])
const len = length(100, bezierCurve)
nearlyEqual(t, arcLengthToT({distance: 0}, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len / 2}, bezierCurve), 0.5, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len}, bezierCurve), 1, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: 0 }, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len / 2 }, bezierCurve), 0.5, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len }, bezierCurve), 1, 0.0001)
t.true(true)
})

test('calculate arcLengthToT for a 2D quadratic (3 control points) bezier', (t) => {
const bezierCurve = bezier.create([[0, 0], [0, 10], [10, 10]])
const len = length(100, bezierCurve)
nearlyEqual(t, arcLengthToT({distance: 0}, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len / 2}, bezierCurve), 0.50001, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len}, bezierCurve), 1, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: 0 }, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len / 2 }, bezierCurve), 0.50001, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len }, bezierCurve), 1, 0.0001)
t.true(true)
})

test('calculate arcLengthToT for a 2D cubic (4 control points) bezier', (t) => {
const bezierCurve = bezier.create([[0, 0], [0, 10], [10, 10], [10, 0]])
const len = length(100, bezierCurve)
nearlyEqual(t, arcLengthToT({distance: 0}, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len / 2}, bezierCurve), 0.49999, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len}, bezierCurve), 1, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: 0 }, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len / 2 }, bezierCurve), 0.49999, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len }, bezierCurve), 1, 0.0001)
t.true(true)
})

test('calculate arcLengthToT for a 3D linear bezier', (t) => {
const bezierCurve = bezier.create([[0, 0, 0], [10, 10, 10]])
const len = length(100, bezierCurve)
nearlyEqual(t, arcLengthToT({distance: 0}, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len / 2}, bezierCurve), 0.49999, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len}, bezierCurve), 1, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: 0 }, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len / 2 }, bezierCurve), 0.49999, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len }, bezierCurve), 1, 0.0001)
t.true(true)
})

test('calculate arcLengthToT for a 3D quadratic (3 control points) bezier', (t) => {
const bezierCurve = bezier.create([[0, 0, 0], [5, 5, 5], [0, 0, 10]])
const len = length(100, bezierCurve)
nearlyEqual(t, arcLengthToT({distance: 0}, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len / 2}, bezierCurve), 0.49999, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len}, bezierCurve), 1, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: 0 }, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len / 2 }, bezierCurve), 0.49999, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len }, bezierCurve), 1, 0.0001)
t.true(true)
})

test('calculate arcLengthToT for a 3D cubic (4 control points) bezier', (t) => {
const bezierCurve = bezier.create([[0, 0, 0], [5, 5, 5], [0, 0, 10], [-5, -5, 5]])
const len = length(100, bezierCurve)
nearlyEqual(t, arcLengthToT({distance: 0}, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len / 2}, bezierCurve), 0.5621, 0.0001)
nearlyEqual(t, arcLengthToT({distance: len}, bezierCurve), 1, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: 0 }, bezierCurve), 0, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len / 2 }, bezierCurve), 0.5621, 0.0001)
nearlyEqual(t, arcLengthToT({ distance: len }, bezierCurve), 1, 0.0001)
t.true(true)
})
})
8 changes: 3 additions & 5 deletions packages/modeling/src/curves/bezier/length.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ const lengths = require('./lengths')
* @example
* const b = bezier.create([[0, 0], [0, 10]]);
* console.log(length(100, b)) // output 10
*
*
* @param {Number} segments the number of segments to use when approximating the curve length.
* @param {Object} bezier a bezier curve.
* @returns an approximation of the curve's length.
* @alias module:modeling/curves/bezier.length
*/
const length = (segments, bezier) => {
return lengths(segments, bezier)[segments]
};
const length = (segments, bezier) => lengths(segments, bezier)[segments]

module.exports = length
module.exports = length
4 changes: 2 additions & 2 deletions packages/modeling/src/curves/bezier/length.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { nearlyEqual } = require('../../../test/helpers/index')

test('calculate the length of an 1D linear bezier with numeric control points', (t) => {
const bezierCurve = bezier.create([0, 10])
nearlyEqual(t, length(100, bezierCurve), 10, 0.0001)
nearlyEqual(t, length(100, bezierCurve), 10, 0.0001)
t.true(true)
})

Expand Down Expand Up @@ -51,4 +51,4 @@ test('calculate the length of a 3D cubic (4 control points) bezier', (t) => {
const bezierCurve = bezier.create([[0, 0, 0], [5, 5, 5], [0, 0, 10], [-5, -5, 5]])
nearlyEqual(t, length(100, bezierCurve), 17.2116, 0.0001)
t.true(true)
})
})
20 changes: 10 additions & 10 deletions packages/modeling/src/curves/bezier/lengths.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const valueAt = require("./valueAt")
const valueAt = require('./valueAt')

/**
* Divides the bezier curve into line segments and returns the cumulative length of those segments as an array.
* Utility function used to calculate the curve's approximate length and determine the equivalence between arc length and time.
*
*
* @example
* const b = bezier.create([[0, 0], [0, 10]]);
* const totalLength = lengths(100, b).pop(); // the last element of the array is the curve's approximate length
*
*
* @param {Number} segments the number of segments to use when approximating the curve length.
* @param {Object} bezier a bezier curve.
* @returns an array containing the cumulative length of the segments.
*/
const lengths = (segments, bezier) => {
let sum = 0
let lengths = [0]
const lengths = [0]
let previous = valueAt(0, bezier)
for (let index = 1; index <= segments; index++) {
const current = valueAt(index / segments, bezier)
Expand All @@ -23,15 +23,15 @@ const lengths = (segments, bezier) => {
previous = current
}
return lengths
};
}

/**
* Calculates the Euclidean distance between two n-dimensional points.
*
* @example
* const distance = distanceBetween([0, 0], [0, 10]); // calculate distance between 2D points
* console.log(distance); // output 10
*
*
* @param {Array} a - first operand.
* @param {Array} b - second operand.
* @returns {Number} - distance.
Expand All @@ -41,16 +41,16 @@ const distanceBetween = (a, b) => {
return Math.abs(a - b)
} else if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) {
throw new Error("The operands must have the same number of dimensions.")
throw new Error('The operands must have the same number of dimensions.')
}
let sum = 0
for (let i = 0; i < a.length; i++) {
sum += (b[i] - a[i]) * (b[i] - a[i])
}
return Math.sqrt(sum)
} else {
throw new Error("The operands must be of the same type, either number or array.")
throw new Error('The operands must be of the same type, either number or array.')
}
};
}

module.exports = lengths
module.exports = lengths
6 changes: 3 additions & 3 deletions packages/modeling/src/curves/bezier/lengths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('calculate lengths for a 1D linear bezier with numeric control points', (t)
test('calculate lengths for a 1D linear bezier with array control points', (t) => {
const bezierCurve = bezier.create([[0], [10]])
const result = lengths(100, bezierCurve)
t.is(result.length, 101)
t.is(result.length, 101)
nearlyEqual(t, result[0], 0, 0.0001)
nearlyEqual(t, result[50], 5, 0.0001)
nearlyEqual(t, result[100], 10, 0.0001)
Expand All @@ -26,7 +26,7 @@ test('calculate lengths for a 1D linear bezier with array control points', (t) =
test('calculate lengths for a 2D linear bezier', (t) => {
const bezierCurve = bezier.create([[0, 0], [10, 10]])
const result = lengths(100, bezierCurve)
t.is(result.length, 101)
t.is(result.length, 101)
nearlyEqual(t, result[0], 0, 0.0001)
nearlyEqual(t, result[50], 7.0710, 0.0001)
nearlyEqual(t, result[100], 14.1421, 0.0001)
Expand All @@ -53,7 +53,7 @@ test('calculate lengths for a 2D cubic (4 control points) bezier', (t) => {
test('calculate lengths for a 3D linear bezier', (t) => {
const bezierCurve = bezier.create([[0, 0, 0], [10, 10, 10]])
const result = lengths(100, bezierCurve)
t.is(result.length, 101)
t.is(result.length, 101)
nearlyEqual(t, result[0], 0, 0.0001)
nearlyEqual(t, result[50], 8.6602, 0.0001)
nearlyEqual(t, result[100], 17.3205, 0.0001)
Expand Down
7 changes: 0 additions & 7 deletions packages/modeling/src/geometries/poly2/arePointsInside.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,4 @@ const isPointInside = (point, polygon) => {
return insideFlag
}

/*
* > 0 : p2 is left of the line p0 -> p1
* = 0 : p2 is on the line p0 -> p1
* < 0 : p2 is right of the line p0 -> p1
*/
const isLeft = (p0, p1, p2) => (p1[0] - p0[0]) * (p2[1] - p0[1]) - (p2[0] - p0[0]) * (p1[1] - p0[1])

module.exports = arePointsInside
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const vec3 = require('../../maths/vec3')
const vec4 = require('../../maths/vec4')

const cache = new WeakMap()
Expand All @@ -10,7 +9,7 @@ const cache = new WeakMap()
* @alias module:modeling/geometries/poly3.measureBoundingSphere
*/
const measureBoundingSphere = (polygon) => {
let boundingSphere = cache.get(polygon)
const boundingSphere = cache.get(polygon)
if (boundingSphere) return boundingSphere

const vertices = polygon.vertices
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PolygonTreeNode {
this.parent = parent
this.children = []
this.polygon = polygon
this.removed = false // state of branch or leaf
this.removed = false // state of branch or leaf
}

// fill the tree with polygons. Should be called on the root node only; child nodes must
Expand Down
2 changes: 1 addition & 1 deletion packages/modeling/src/operations/expansions/expand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test('expand: round-expanding a bent line produces expected geometry', (t) => {
const expandedPoints = geom2.toPoints(expandedPathGeom2)

t.notThrows(() => geom2.validate(expandedPathGeom2))
const expectedArea = 56 + TAU * delta * 1.25 // shape will have 1 and 1/4 circles
const expectedArea = 56 + TAU * delta * 1.25 // shape will have 1 and 1/4 circles
nearlyEqual(t, area(expandedPoints), expectedArea, 0.01, 'Measured area should be pretty close')
const boundingBox = measureBoundingBox(expandedPathGeom2)
t.true(comparePoints(boundingBox, [[-7, -2, 0], [2, 12, 0]]), 'Unexpected bounding box: ' + JSON.stringify(boundingBox))
Expand Down
13 changes: 6 additions & 7 deletions packages/modeling/src/operations/extrusions/extrudeHelical.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const extrudeHelical = (options, geometry) => {

let pitch
// ignore height if pitch is set
if(!options.pitch && options.height) {
if (!options.pitch && options.height) {
pitch = options.height / (angle / TAU)
} else {
pitch = options.pitch ? options.pitch : defaults.pitch
Expand All @@ -49,18 +49,17 @@ const extrudeHelical = (options, geometry) => {
// needs at least 3 segments for each revolution
const minNumberOfSegments = 3

if (segmentsPerRotation < minNumberOfSegments)
throw new Error(`The number of segments per rotation needs to be at least 3.`)
if (segmentsPerRotation < minNumberOfSegments) { throw new Error('The number of segments per rotation needs to be at least 3.') }

let shapeSides = geom2.toSides(geometry)
const shapeSides = geom2.toSides(geometry)
if (shapeSides.length === 0) throw new Error('the given geometry cannot be empty')

// const pointsWithNegativeX = shapeSides.filter((s) => (s[0][0] < 0))
const pointsWithPositiveX = shapeSides.filter((s) => (s[0][0] >= 0))

let baseSlice = slice.fromSides(shapeSides)
if(pointsWithPositiveX.length === 0) {

if (pointsWithPositiveX.length === 0) {
// only points in negative x plane, reverse
baseSlice = slice.reverse(baseSlice)
}
Expand Down
Loading

0 comments on commit e1e20d1

Please sign in to comment.