Check-in [e9599cdacc]
Not logged in
Overview
SHA1:e9599cdacc73364bfca699076b4884b2ae6169ce
Date: 2013-01-03 20:26:41
User: Derek
Comment:starting basic work on wireframe_model.js wrote some test cases in seperate page
Timelines: family | ancestors | descendants | both | trunk
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2013-01-05
03:08
[4ceaad09ba] Leaf: got the draw function working. (user: Derek, tags: trunk)
2013-01-03
20:26
[e9599cdacc] starting basic work on wireframe_model.js wrote some test cases in seperate page (user: Derek, tags: trunk)
2013-01-02
17:22
[1514d83c29] started moving around code in files to factor stuff. (user: Derek, tags: trunk)
Changes

Added test_wireframe_model.html version [d11af1c4b7].

            1  +
            2  +<html>
            3  +<head>
            4  +
            5  +<script src="wireframe_model.js"></script>
            6  +
            7  +
            8  +
            9  +<script>
           10  +
           11  +////
           12  +//  Some helper stuff for running tests.
           13  +////
           14  +
           15  +
           16  +var testcount = 0;
           17  +
           18  +function mustThrowError(func, testname){
           19  +	++testcount;
           20  +	if(!testname) testname = "Test_" + testcount;
           21  +	var threwsomething = false;
           22  +	try{ func(); }
           23  +	catch(excpt){ threwsomething = true; }
           24  +	if(!threwsomething)
           25  +		throw "'" + testname + "' did not throw an error, it was supposed to throw an error!";
           26  +}
           27  +
           28  +function mustNotThrowError(func, testname){
           29  +	++testcount;
           30  +	if(!testname) testname = "Test_" + testcount;
           31  +	try{ func(); }
           32  +	catch(excpt){
           33  +		throw "'" + testname + "' failed and threw an error: " + excpt; }}
           34  +		
           35  +
           36  +function assert(value, testname){
           37  +	++testcount;
           38  +	if(!testname) testname = "Test_" + testcount;
           39  +	
           40  +	if(!value) throw "Assertion " + testname + " failed"; }
           41  +	
           42  +	
           43  +	
           44  +////
           45  +//
           46  +//  WireframeModel test cases.
           47  +//
           48  +//  You should play around with these to convince yourself that they work the way they are supposed to.
           49  +//
           50  +////
           51  +	
           52  +	
           53  +var model = new WireframeModel();
           54  +
           55  +mustThrowError(function(){ model.addPoint("hippo"); }, "addpoint hippo");
           56  +mustThrowError(function(){ model.addPoint([1, 2, 3, 0]); }, "addpoint too long");
           57  +mustThrowError(function(){ model.addPoint(7); });
           58  +mustThrowError(function(){ model.addPoint([1, 2, "a"]); });
           59  +
           60  +assert(0 == model.addPoint([1.3845, -0.110, 7]));
           61  +assert(1 == model.addPoint([1.3845, -0.110, -7]));
           62  +
           63  +mustThrowError(function(){ model.addLine("dolphin"); }, "addline dolphin");
           64  +mustThrowError(function(){ model.addLine([1]); }, "addline too short");
           65  +mustThrowError(function(){ model.addLine([-1, 0]); }, "addline out of bounds negative");
           66  +mustThrowError(function(){ model.addLine([0, 7]); }, "addline out of bounds");
           67  +
           68  +assert(0 == model.addLine([0, 1]));
           69  +assert(1 == model.addLine([1, model.addPoint([-1.3845, -0.110, 7])]));
           70  +
           71  +
           72  +
           73  +
           74  +
           75  +alert("Tests completed.");
           76  +
           77  +// uncomment when you want to prove must throw error actually does something
           78  +// mustThrowError(function(){ }, "check mustthrowerror works. have it do nothing.");
           79  +
           80  +
           81  +
           82  +
           83  +
           84  +
           85  +</script>
           86  +
           87  +
           88  +</head>
           89  +
           90  +<body>
           91  +<p>
           92  +This is a test page that encapsulates scripts for testing wireframe_model.js
           93  +</p>
           94  +<p>
           95  +If tests are successful, a message will say so and no errors will be thrown.
           96  +</p>
           97  +</body>
           98  +
           99  +</html>

Modified wireframe_model.js from [0b9640f7fc] to [ba994e1850].

     1      1   
            2  +
            3  +// requires matrix_vector_lib.js
            4  +// TODO we could make some libraryless way of checking dependencies and versions.
            5  +
     2      6   
     3      7   // global namespace object
     4      8   var WireframeModel;
     5      9   
     6     10   
     7     11   (function(){
           12  +
           13  +
     8     14   
     9     15   //  A wireframe model and all the controls needed to move the camera and draw.
    10     16   //  despite the name, it may also include polygon surfaces.
    11         -
    12         -var origin = [0, 0, 0];
    13         -
    14         -
    15         -
    16         -var points = []; //global
    17         -var solo_points = {};
    18         -var lines = [];
           17  +function __WireframeModel(){
           18  +
           19  +	var WFMObj = this;
           20  +	
           21  +	var view_origin = [0, 0, 0];
           22  +	var view_transform = [[1, 0, 0],
           23  +	                      [0, 1, 0],
           24  +						  [0, 0, 1]];
           25  +
           26  +
           27  +
           28  +	var points = []; //global
           29  +	var solo_points = {};
           30  +	var lines = [];
           31  +	
           32  +	
           33  +	// just because these actual objects is returned
           34  +	// doesn't mean you should modify it externally if you can help it.
           35  +	
           36  +	WFMObj.getPoints = function getPoints(){
           37  +		return points; }
           38  +	
           39  +	WFMObj.getLines = function getLines(){
           40  +		return lines; }
           41  +	
           42  +	WFMObj.getViewTransform = function getViewTransform(){
           43  +		return view_transform; }
           44  +	
           45  +	
           46  +	
           47  +	WFMObj.addPoint = function addPoint(pt){
           48  +		
           49  +		//  Check type of passed object.
           50  +		if(Array.isArray && !Array.isArray(pt)){  // only check if Array.isArray function is available
           51  +			throw "pt object is not an array"; }
           52  +			
           53  +		if(pt.length != 3){
           54  +			throw "pt array object not length 3"; }
           55  +		
           56  +		for(var i = 0; i < 3; ++i){
           57  +			if(isNaN(pt[i])) throw "pt object array does not store a number at index " + i; }
           58  +			
           59  +		points.push(pt);
           60  +		return points.length - 1; }
           61  +	
           62  +	
           63  +	
           64  +	WFMObj.addLine = function addLine(line){
           65  +		if(Array.isArray && !Array.isArray(line)){
           66  +			throw "line object is not an array" }
           67  +		
           68  +		if(line.length != 2){
           69  +			throw "line object must be between 2 points.  line object has length: " + line.length; }
           70  +		
           71  +		for(var i = 0; i < 2; ++i){
           72  +			var ptindex = line[i];
           73  +			if(isNaN(ptindex) || Math.floor(ptindex) != ptindex){
           74  +				throw "pt ref at index " + i + " of line was not an integer: " + ptindex; }
           75  +			
           76  +			if(ptindex < 0 || ptindex >= points.length){
           77  +				throw "pt ref at index " + i + " of line was not in bounds: " + ptindex; }}
           78  +		
           79  +		lines.push(line);
           80  +		return lines.length - 1; }
           81  +				
           82  +	
           83  +}
           84  +
           85  +//  use constructor as global namespace object.
           86  +WireframeModel = __WireframeModel;
           87  +
           88  +
           89  +})();
           90  +
           91  +/*
    19     92   
    20     93   var point_projections = [];
           94  +
           95  +
    21     96   var line_midpoint_projections = [];
    22     97   
    23     98   
    24     99   var highlight_object = null;
    25    100   var mouse_dragging = false;
    26    101   var last_mouse_down = null;
    27    102   var mouse_loc = null;
................................................................................
   335    410   				ctx.rect(minx, miny, maxx - minx, maxy - miny);
   336    411   				ctx.stroke(); }}
   337    412   	}
   338    413       
   339    414       ctx.fillStyle = "rgb(0,0,0)";
   340    415   	writeMsg(canvas, msg); }
   341    416   	
   342         -	
          417  +*/
   343    418   
   344         -
   345         -})();