Skip to content

Commit 52e792d

Browse files
z3devkaosat-dev
authored andcommitted
fix(center): Correcting center() functionality (#97)
* Corrected center() and updated tests suites * Removed centering mutators (UG) * Added center() proper, which was added by mutators previously * Cleaned up, and added options.center to center upon a given point * Updated to determine arrays using Array.isArray() * Corrected argument passing to centerV2() * JSDOC changes
1 parent caa7241 commit 52e792d

File tree

6 files changed

+47
-23
lines changed

6 files changed

+47
-23
lines changed

csg.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,6 @@ addTransformationMethodsToPrototype(CAG.prototype)
192192
addTransformationMethodsToPrototype(CAG.Side.prototype)
193193
addTransformationMethodsToPrototype(CAG.Vertex.prototype)
194194

195-
addCenteringToPrototype(CSG.prototype, ['x', 'y', 'z'])
196-
addCenteringToPrototype(CAG.prototype, ['x', 'y'])
197-
198195
CSG.parseOptionAs2DVector = optionsParsers.parseOptionAs3DVector
199196
CSG.parseOptionAs3DVector = optionsParsers.parseOptionAs3DVector
200197
CSG.parseOptionAs3DVectorList = optionsParsers.parseOptionAs3DVectorList

src/api/center.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
const toArray = require('../core/utils/toArray')
22

3-
/** NOTE: this is not functional YET !!
4-
* centers the given object(s) on the given axis
5-
* @param {Object|Array} object(s) the shapes to center
6-
* @param {Object} options
3+
/**
4+
* Centers the given object(s) using the given options (if any)
5+
* @param {Object} [options] - options for centering
6+
* @param {Array} [options.axes=[true,true,true]] - axis of which to center, true or false
7+
* @param {Array} [options.center=[0,0,0]] - point of which to center the object upon
8+
* @param {Object|Array} objects - the shape(s) to center
9+
* @return {Object|Array} objects
10+
*
11+
* @example
12+
* let csg = center({axes: [true,false,false]}, sphere()) // center about the X axis
713
*/
8-
const center = (objects, options) => {
14+
const center = function (options, objects) {
915
const defaults = {
10-
axes: [1, 1, 1]
16+
axes: [true, true, true],
17+
center: [0, 0, 0]
18+
// TODO : Add addition 'methods' of centering; midpoint, centeriod
1119
}
1220
options = Object.assign({}, defaults, options)
13-
const {axes} = options
21+
const {axes,center} = options
1422
objects = toArray(objects)
1523

1624
const results = objects.map(function (object) {
17-
let b = object.getBounds()
18-
return object.translate(axes.map(function (a) {
19-
return cAxes.indexOf(a) > -1 ? -(b[0][a] + b[1][a]) / 2 : 0
20-
}))
25+
let bounds = object.getBounds()
26+
let offset = [0,0,0]
27+
if (axes[0]) offset[0] = center[0] - (bounds[0].x + ((bounds[1].x - bounds[0].x) / 2))
28+
if (axes[1]) offset[1] = center[1] - (bounds[0].y + ((bounds[1].y - bounds[0].y) / 2))
29+
if (axes[2]) offset[2] = center[2] - (bounds[0].z + ((bounds[1].y - bounds[0].y) / 2))
30+
return object.translate(offset)
2131
})
2232
// if there is more than one result, return them all , otherwise a single one
2333
return results.length === 1 ? results[0] : results

src/api/ops-transformations.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,17 @@ function transform (matrix, ...objects) { // v, obj or array
118118
return object.transform(transformationMatrix)
119119
}
120120

121-
/** center an object in 2D/3D space
122-
* @param {Boolean|Array} axis - either an array or single boolean to indicate which axis you want to center on
123-
* @param {Object(s)|Array} objects either a single or multiple CSG/CAG objects to translate
121+
122+
/**
123+
* Center the given object(s) about the given axes
124+
* @param {Array|Boolean} axes=[true,true,true]|true - an array of boolean values that indicate the axes (X,Y,Z) to center upon. A single boolean is also allowed.
125+
* @param {...Object} object one or more objects to center, i.e. objects are CSG or CAG
124126
* @returns {CSG} new CSG object , translated by the given amount
125127
*
126128
* @example
127-
* let movedSphere = center(false, sphere())
129+
* let csg = center([true,false,false], sphere()) // center about the X axis
128130
*/
129-
function center (axis, ...objects) { // v, obj or array
131+
function center (axes, ...objects) {
130132
const _objects = (objects.length >= 1 && objects[0].length) ? objects[0] : objects
131133
let object = _objects[0]
132134

@@ -135,7 +137,10 @@ function center (axis, ...objects) { // v, obj or array
135137
object = object.union(_objects[i])
136138
}
137139
}
138-
return object.center(axis)
140+
if (! Array.isArray(axes)) {
141+
axes = [axes,axes,axes]
142+
}
143+
return object.center(axes)
139144
}
140145

141146
/** mirror an object in 2D/3D space

src/api/ops-transformations.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ test('transform (multiple items, 2d , translation)', t => {
185185
test('center (single item)', t => {
186186
const op1 = cube()
187187
const obs = center(true, op1)
188-
t.deepEqual(obs.properties.cube.center, {_x: 0.5, _y: 0.5, _z: 0.5})
188+
t.deepEqual(obs.properties.cube.center, {_x: 0, _y: 0, _z: 0})
189189
})
190190

191191
test('center (multiple item)', t => {
@@ -201,8 +201,8 @@ test('center (multiple items, 2d)', t => {
201201
const op2 = circle()
202202
const obs = center(true, op1, op2)
203203

204-
sideEquals(t, obs.sides[0], [[1.9807852804032304, 0.8049096779838713], [2, 1]])
205-
sideEquals(t, obs.sides[obs.sides.length - 1], [[0, 0], [0.9999999999999998, 0]])
204+
sideEquals(t, obs.sides[0], [[0.9807852804032304, -0.19509032201612875], [1, 0]])
205+
sideEquals(t, obs.sides[obs.sides.length - 1], [[-1, -1], [-2.220446049250313e-16, -1]])
206206
})
207207

208208
test('mirror (single item)', t => {

src/core/CAG.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {area, getBounds} = require('./utils/cagMeasurements')
1616
const {overCutInsideCorners} = require('../api/ops-cnc')
1717
const {extrudeInOrthonormalBasis, extrudeInPlane, extrude, rotateExtrude} = require('../api/ops-extrusions')
1818
const cagoutlinePaths = require('../api/cagOutlinePaths')
19+
const center = require('../api/center')
1920
const {expand, contract, expandedShellOfCAG} = require('../api/ops-expandContract')
2021
/**
2122
* Class CAG
@@ -101,6 +102,11 @@ CAG.prototype = {
101102
return fromSides(newsides)
102103
},
103104

105+
// ALIAS !
106+
center: function (axes) {
107+
return center({axes: axes}, [this])
108+
},
109+
104110
// ALIAS !
105111
expandedShell: function (radius, resolution) {
106112
return expandedShellOfCAG(this, radius, resolution)

src/core/CSG.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {projectToOrthoNormalBasis} = require('./utils/csgProjections')
1616

1717
const {lieFlat, getTransformationToFlatLying, getTransformationAndInverseTransformationToFlatLying} = require('../api/ops-cnc')
1818
const {sectionCut, cutByPlane} = require('../api/ops-cuts')
19+
const center = require('../api/center')
1920
const {expand, contract, expandedShellOfCCSG} = require('../api/ops-expandContract')
2021

2122
/** Class CSG
@@ -267,6 +268,11 @@ CSG.prototype = {
267268
return result
268269
},
269270

271+
// ALIAS !
272+
center: function (axes) {
273+
return center({axes: axes},[this])
274+
},
275+
270276
// ALIAS !
271277
expand: function (radius, resolution) {
272278
return expand(this, radius, resolution)

0 commit comments

Comments
 (0)