HopefulLlama/UnitTestSCAD

View on GitHub
docs/api_ThreeDModule.js.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: api/ThreeDModule.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: api/ThreeDModule.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>const {EOL} = require('os');

const AbstractModule = require('./AbstractModule');
const ThreeDModuleFile = require('../file/ThreeDModuleFile');

function getDimensionSize(vertices, index) {
  const range = vertices.reduce((accumulator, vertex) => {
    return {
      min: vertex[index] &lt; accumulator.min ? vertex[index] : accumulator.min,
      max: vertex[index] > accumulator.max ? vertex[index] : accumulator.max,
    };
  }, {
    min: Number.POSITIVE_INFINITY,
    max: Number.NEGATIVE_INFINITY
  });

  return range.max - range.min;
}

function getVertex(content) {
  return content
    .split(' ')
    // Last three elements should be the co-ordinates, as a string
    .slice(-3)
    .map(vertex => parseFloat(vertex, 10));
}

function getVertices(contents) {
  const vertexRegex = new RegExp(/vertex([ ][0-9]+[.]*[0-9]*){3}/, 'gm');

  return contents
    .match(vertexRegex)
    .filter((value, index, self) => self.indexOf(value) === index)
    .map(vertexString => getVertex(vertexString));
}

function getTriangles(contents) {
  const triangleRegex = new RegExp(/outer loop[\s\S]+?endloop/, 'g');

  return contents
    .match(triangleRegex)
    .map(triangleString => triangleString.split(EOL))
    .map(triangleStrings => triangleStrings.splice(1, 3))
    .map(verticesOfTriangleStrings => {
      return verticesOfTriangleStrings.map(vertexString => getVertex(vertexString));
    });
}

/** @class */
class ThreeDModule extends AbstractModule {
  /** @param {Options} options */
  constructor(options) {
    super(options, ThreeDModuleFile);
    /**
     * @memberof ThreeDModule
     * @instance
     * @member {string} output The extracted output from execution of the .scad file.
     */

    /**
     * @memberof ThreeDModule
     * @instance
     * @function
     * @name isWithinBoundingBox
     * @param boundingBox {BoundingBox} The 3D box which the model should fit inside. It is considered 'within' if any coordinate is equal to, or within the box.
     * @returns {boolean} True if the model fits within the bounding box.
     */

    /**
     * @memberof ThreeDModule
     * @instance
     * @member {Vertex[]} vertices A list of 3D vertices returned from the .scad file execution.
     */
    this.vertices = getVertices(this.output);

    /**
     * @memberof ThreeDModule
     * @instance
     * @member {number} width The width of the model.
     */
    this.width = getDimensionSize(this.vertices, 0);

    /**
     * @memberof ThreeDModule
     * @instance
     * @member {number} height The height of the model.
     */
    this.height = getDimensionSize(this.vertices, 1);

    /**
     * @memberof ThreeDModule
     * @instance
     * @member {number} depth The depth of the model.
     */
    this.depth = getDimensionSize(this.vertices, 2);

    /**
     * @typedef {Vertex[]} Triangle An array of three 3D vertices which describe a triangle. See {@link https://en.wikipedia.org/wiki/Triangle_mesh}.
     */

    /**
     * @memberof ThreeDModule
     * @instance
     * @member {Triangle[]} triangles The triangles which make up the model. See {@link https://en.wikipedia.org/wiki/Triangle_mesh}.
     */
    this.triangles = getTriangles(this.output);
  }
}

module.exports = ThreeDModule;</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Function.html">Function</a></li><li><a href="ThreeDModule.html">ThreeDModule</a></li><li><a href="TwoDModule.html">TwoDModule</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a>
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>