modeling/src/primitives/cylinder.js

  1. const geom3 = require('../geometries/geom3')
  2. const cylinderElliptic = require('./cylinderElliptic')
  3. const { isGTE } = require('./commonChecks')
  4. /**
  5. * Construct a Z axis-aligned cylinder in three dimensional space.
  6. * @see [cylinderElliptic]{@link module:modeling/primitives.cylinderElliptic} for more options
  7. * @param {Object} [options] - options for construction
  8. * @param {Array} [options.center=[0,0,0]] - center of cylinder
  9. * @param {Number} [options.height=2] - height of cylinder
  10. * @param {Number} [options.radius=1] - radius of cylinder (at both start and end)
  11. * @param {Number} [options.segments=32] - number of segments to create per full rotation
  12. * @returns {geom3} new geometry
  13. * @alias module:modeling/primitives.cylinder
  14. *
  15. * @example
  16. * let myshape = cylinder({height: 2, radius: 10})
  17. */
  18. const cylinder = (options) => {
  19. const defaults = {
  20. center: [0, 0, 0],
  21. height: 2,
  22. radius: 1,
  23. segments: 32
  24. }
  25. const { center, height, radius, segments } = Object.assign({}, defaults, options)
  26. if (!isGTE(radius, 0)) throw new Error('radius must be positive')
  27. // if size is zero return empty geometry
  28. if (height === 0 || radius === 0) return geom3.create()
  29. const newoptions = {
  30. center,
  31. height,
  32. startRadius: [radius, radius],
  33. endRadius: [radius, radius],
  34. segments
  35. }
  36. return cylinderElliptic(newoptions)
  37. }
  38. module.exports = cylinder