Skip to content
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
2 changes: 2 additions & 0 deletions packages/modeling/src/primitives/torus.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Vec3 from '../maths/vec3/type'
import Geom3 from '../geometries/geom3/type'

export default torus

export interface TorusOptions {
center?: Vec3
innerRadius?: number
outerRadius?: number
innerSegments?: number
Expand Down
13 changes: 10 additions & 3 deletions packages/modeling/src/primitives/torus.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ const { translate } = require('../operations/transforms/translate')

const circle = require('./circle')

const { isGT, isGTE } = require('./commonChecks')
const { isGT, isGTE, isNumberArray } = require('./commonChecks')

/**
* Construct a torus by revolving a small circle (inner) about the circumference of a large (outer) circle.
* @param {Object} [options] - options for construction
* @param {Array} [options.center=[0,0,0]] - center of the torus
* @param {Number} [options.innerRadius=1] - radius of small (inner) circle
* @param {Number} [options.outerRadius=4] - radius of large (outer) circle
* @param {Integer} [options.innerSegments=32] - number of segments to create per rotation
Expand All @@ -26,6 +27,7 @@ const { isGT, isGTE } = require('./commonChecks')
*/
const torus = (options) => {
const defaults = {
center: [0, 0, 0],
innerRadius: 1,
innerSegments: 32,
outerRadius: 4,
Expand All @@ -34,8 +36,9 @@ const torus = (options) => {
startAngle: 0,
outerRotation: TAU
}
const { innerRadius, innerSegments, outerRadius, outerSegments, innerRotation, startAngle, outerRotation } = Object.assign({}, defaults, options)
const { center, innerRadius, innerSegments, outerRadius, outerSegments, innerRotation, startAngle, outerRotation } = Object.assign({}, defaults, options)

if (!isNumberArray(center, 3)) throw new Error('center must be an array of X, Y and Z values')
if (!isGT(innerRadius, 0)) throw new Error('innerRadius must be greater than zero')
if (!isGTE(innerSegments, 3)) throw new Error('innerSegments must be three or more')
if (!isGT(outerRadius, 0)) throw new Error('outerRadius must be greater than zero')
Expand All @@ -58,7 +61,11 @@ const torus = (options) => {
angle: outerRotation,
segments: outerSegments
}
return extrudeRotate(extrudeOptions, innerCircle)
let result = extrudeRotate(extrudeOptions, innerCircle);
if ((center[0] !== 0) || (center[1] !== 0) || (center[2] !== 0)) {
result = translate(center, result)
}
return result;
}

module.exports = torus
Loading