@@ -1,25 +1,100 @@ + +// requires matrix_vector_lib.js +// TODO we could make some libraryless way of checking dependencies and versions. + // global namespace object var WireframeModel; (function(){ + + // A wireframe model and all the controls needed to move the camera and draw. // despite the name, it may also include polygon surfaces. - -var origin = [0, 0, 0]; - - - -var points = []; //global -var solo_points = {}; -var lines = []; +function __WireframeModel(){ + + var WFMObj = this; + + var view_origin = [0, 0, 0]; + var view_transform = [[1, 0, 0], + [0, 1, 0], + [0, 0, 1]]; + + + + var points = []; //global + var solo_points = {}; + var lines = []; + + + // just because these actual objects is returned + // doesn't mean you should modify it externally if you can help it. + + WFMObj.getPoints = function getPoints(){ + return points; } + + WFMObj.getLines = function getLines(){ + return lines; } + + WFMObj.getViewTransform = function getViewTransform(){ + return view_transform; } + + + + WFMObj.addPoint = function addPoint(pt){ + + // Check type of passed object. + if(Array.isArray && !Array.isArray(pt)){ // only check if Array.isArray function is available + throw "pt object is not an array"; } + + if(pt.length != 3){ + throw "pt array object not length 3"; } + + for(var i = 0; i < 3; ++i){ + if(isNaN(pt[i])) throw "pt object array does not store a number at index " + i; } + + points.push(pt); + return points.length - 1; } + + + + WFMObj.addLine = function addLine(line){ + if(Array.isArray && !Array.isArray(line)){ + throw "line object is not an array" } + + if(line.length != 2){ + throw "line object must be between 2 points. line object has length: " + line.length; } + + for(var i = 0; i < 2; ++i){ + var ptindex = line[i]; + if(isNaN(ptindex) || Math.floor(ptindex) != ptindex){ + throw "pt ref at index " + i + " of line was not an integer: " + ptindex; } + + if(ptindex < 0 || ptindex >= points.length){ + throw "pt ref at index " + i + " of line was not in bounds: " + ptindex; }} + + lines.push(line); + return lines.length - 1; } + + +} + +// use constructor as global namespace object. +WireframeModel = __WireframeModel; + + +})(); + +/* var point_projections = []; + + var line_midpoint_projections = []; var highlight_object = null; var mouse_dragging = false; @@ -337,9 +412,7 @@ } ctx.fillStyle = "rgb(0,0,0)"; writeMsg(canvas, msg); } - +*/ - -})();