|
1 | 1 | const toArray = require('../core/utils/toArray')
|
2 | 2 |
|
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 |
7 | 13 | */
|
8 |
| -const center = (objects, options) => { |
| 14 | +const center = function (options, objects) { |
9 | 15 | 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 |
11 | 19 | }
|
12 | 20 | options = Object.assign({}, defaults, options)
|
13 |
| - const {axes} = options |
| 21 | + const {axes,center} = options |
14 | 22 | objects = toArray(objects)
|
15 | 23 |
|
16 | 24 | 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) |
21 | 31 | })
|
22 | 32 | // if there is more than one result, return them all , otherwise a single one
|
23 | 33 | return results.length === 1 ? results[0] : results
|
|
0 commit comments