!function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.dz=e():"undefined"!=typeof global?global.dz=e():"undefined"!=typeof self&&(self.dz=e())}(function(){var define,module,exports; return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o a line // n = 2 -. a triangle // etc... points.circle = function(n){ var t = Math.PI * 2 // tau return d3.range(n + 1).map(function(i){ return [sin(i / n * t), cos(i / n * t), 0] }) } // construct a grid or matrix box of points where `n` is the "resolution", ie., // the number of inner points // n = 2 -> a simple 8 point cube points.grid = function(nx, ny, nz){ // optional arguments if(!nx) nx = 2 if(!ny) ny = nx if(!nz) nz = ny var points = [] , sx, tx = 0.5 , sy, ty = 0.5 , sz, tz = 0.5 if(nx < 2){ sx = 1; tx = 0 } else sx = 1 / (nx - 1) if(ny < 2){ sy = 1; ty = 0 } else sy = 1 / (ny - 1) if(nz < 2){ sz = 1; tz = 0 } else sz = 1 / (nz - 1) for(var x = 0; x < nx; x++){ for(var y = 0; y < ny; y++){ for(var z = 0; z < nz; z++){ points.push([x * sx - tx, y * sy - ty, z * sz - tz]) } } } return points } points.plane = function(n){ return points.grid(1, n, n) } },{}],4:[function(require,module,exports){ var projection = module.exports = {} var matrix = require('./matrix') var vector = require('./vector') // perspective creator projection.perspective = function(){ // the perspective object var perspective = function(p){ camera.transform() // recalculate the transform // `ip` is the vector of the poin in "camera space" aka, as if the camera // was at (0, 0, 0) var ip = i.multiVector(p) /*copy `p`*/, target = [0, 0, -f] , Az = ip[2], Bz = target[2], scale if(Az === 0) Az = 0.00001 scale = Bz / Az if(scale < 0) scale = 0 // return [ x, y, distance-to-the-camera, scale ] return [ip[0] * scale, ip[1] * scale, (ip[2] - f), scale] } perspective.x = function(p){ return perspective(p)[0] } perspective.y = function(p){ return perspective(p)[1] } // how far the point is from the camera perspective.depth = function(p){ return i.multiVector(p)[2] /*copy `p`*/ } perspective.scale = function(p){ return perspective(p)[3] } // does the camera transform matrix need to be recomputed? var dirty = true , t = matrix() , i = matrix() , f = 1 // focal length var camera = (function(){ // defaults var position = [0, 0, 1], lookAt = [0, 0, 0], up = [0, 1, 0] , camera = {} camera.position = function(array3){ if(!array3) return position.slice(0) // copy dirty = true position = array3.slice(0) /*copy*/ return camera } camera.lookAt = function(array3){ if(!array3) return lookAt.slice(0) // copy dirty = true lookAt = array3 return camera } camera.up = function(array3){ if(!array3) return up.slice(0) // copy dirty = true up = array3.slice(0); return camera } camera.focalLength = function(scalar){ if(scalar === undefined) return f dirty = true f = scalar; return camera } // the transformation matrix that transforms the unit camera // (at loc (0,0,0), looking down the -z axis) to the position of this // camera. the inverse of this matrix transform will take world coordinates // and translate them to "film" coordinates // TODO: do we never need to expose this externally? camera.transform = function(){ // unit camera points down the -z axis and is positioned at (0,0,0) if(dirty) { t.lookAt(position, lookAt, up) i = matrix(t).inverse() } dirty = false return t } return camera })() perspective.camera = function(){ return camera } // create a new perspective return perspective } },{"./matrix":2,"./vector":5}],5:[function(require,module,exports){ var vector = module.exports = function(array3){ // vector argument if(!array3) array3 = [0, 0, 0] array3 = array(array3).slice(0) // copy var x = array3[0], y = array3[1], z = array3[2] , v = {} // ensure array function array(v){ if(v instanceof Array) return v else return v.array() } // subtract vectors v.minus = function(v){ v = array(v) x = x - v[0] y = y - v[1] z = z - v[2] return this } v.cross = function(v1, v2){ // allow single argument form if(arguments.length === 1){ v2 = v1; v1 = v } v1 = array(v1); v2 = array(v2) var tx, ty, tz // temp tx = v1[1] * v2[2] - v1[2] * v2[1] ty = v1[2] * v2[0] - v1[0] * v2[2] tz = v1[0] * v2[1] - v1[1] * v2[0] x = tx, y = ty, z = tz return this } v.normalize = function(){ var l = v.length() if(l){ x = x / l y = y / l z = z / l } return this } v.x = function(s){ if(!arguments.length) return x x = s; return this } v.y = function(s){ if(!arguments.length) return y y = s; return this } v.z = function(s){ if(!arguments.length) return z z =s ; return this } v.scale = function(s){ x = x * s; y = y * s; z = z * s return this } v.length = function(){ return Math.sqrt( x * x + y * y + z * z) } v.array = function(vec){ if(!arguments.length) return [x, y, z] x = vec[0]; y = vec[1]; z = vec[2] return this } return v } },{}]},{},[1]) (1) }); ;