Diff
Not logged in

Differences From Artifact [ba994e1850]:

To Artifact [52461a961c]:


7
8
9
10
11
12
13





























































































































14
15
16
17
18
19
20
..
22
23
24
25
26
27
28

29
30
31


32
33
34
35
36
37



38
39
40



41
42
43
44
45
46
47
..
74
75
76
77
78
79
80
81
82
















83
84
85
86
87
88
89
...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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.
function __WireframeModel(){

	var WFMObj = this;
	
................................................................................
	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){
................................................................................
				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;


})();
................................................................................
    if(cameracentric) offset = matrix_mult(view_transform, [offset])[0];
    //alert("offset: " + offset);
    vector_add(origin, offset, origin); }
	
	
	
	
    

// converts a point from three space to the canvas plane.
// Note: because of depth perspective, this conversion is not
// easy to define if the point lies behind the camera.
// There are two options:
// When drawing a line, another colinear point in front of the camera may be provided
// to help find an alertnate point.
// if both points lie behind the camera or the colinear_point is not provided,
// this function will return null.
 
function project(canvas, xyz, view_transform, origin, colinear_point){
    if(!xyz) return null;  // point has been deleted or does not exist
    
    var w = canvas.width;
    var h = canvas.height;
    
    var scale = Math.min(w, h);
    
    var v = xyz.slice(0);
    if(origin) v = vector_minus(v, origin, v);
    
    var z = vector_dot(view_transform[2], v);
    
    if(z <= -zoom_dist){
        if(!colinear_point) return null;
        
        var v2 = colinear_point.slice(0);
        if(origin) vector_minus(v2, origin, v2);
        
        var z2 = vector_dot(view_transform[2], v2);
        
        if(z2 < 0) return null;
        
        // get the coefficients for a complex combination.
        // t*z + (1-t)*z2 = 0.0002  -- z of new point is just barely infront of the camera.
        
        var t = (0.0002 - z2)/(z - z2);  // no division by zero, z is negative, z2 is positive
        
        vector_add(vector_scale(v, t, v),
                   vector_scale(v2, 1-t, v2),
                   v);
                   
        z = vector_dot(view_transform[2], v); }

        
    var scale2 = zoom_scale * scale / (zoom_dist + z);
    
    return [ scale2 * vector_dot(view_transform[0], v) + 0.5 * w,
             scale2 * vector_dot(view_transform[1], v) + 0.5 * h ]; }
   

   
//  removes deleted points and lines
//  works with global objects.

function cleanupDeletedPoints(){







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
|

<
>
>






>
>
>



>
>
>







 







|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
...
147
148
149
150
151
152
153
154
155
156

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
...
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
...
261
262
263
264
265
266
267
268
269
















































270
271
272
273
274
275
276
// global namespace object
var WireframeModel;


(function(){





// converts a point from three space to the canvas plane.
// Note: because of depth perspective, this conversion is not
// easy to define if the point lies behind the camera.
// There are two options:
// When drawing a line, another colinear point in front of the camera may be provided
// to help find an alternate point.
// if both points lie behind the camera or the colinear_point is not provided,
// this function will return null.
 
function project(canvas, xyz, view, colinear_point){
    if(!xyz) return null;  // point has been deleted or does not exist
    
	var view_transform = view.transform;
	var origin = view.origin;
	var zoom_scale = view.zoom_scale;
	var zoom_dist = view.zoom_dist;
	
	if(!zoom_scale) zoom_scale = 1;
	if(!zoom_dist) zoom_dist = 0;
	if(!origin) origin = [0,0,0];
	
	
    var w = canvas.width;
    var h = canvas.height;
    
    var scale = Math.min(w, h);
    
    var v = xyz.slice(0);
    if(origin) v = vector_minus(v, origin, v);
    
    var z = vector_dot(view_transform[2], v);
    
    if(z <= -zoom_dist){  // this point is on the wrong side of the viewer, find the closest point on the correct side if we can.
        if(!colinear_point) return null;
        
        var v2 = colinear_point.slice(0);
        if(origin) vector_minus(v2, origin, v2);
        
        var z2 = vector_dot(view_transform[2], v2);
        
        if(z2 < 0) return null;
        
        // get the coefficients for a complex combination.
        // t*z + (1-t)*z2 = 0.0002  -- z of new point is just barely infront of the camera.
        
        var t = (0.0002 - z2)/(z - z2);  // no division by zero, z is negative, z2 is positive
        
        vector_add(vector_scale(v, t, v),
                   vector_scale(v2, 1-t, v2),
                   v);
                   
        z = vector_dot(view_transform[2], v); }

        
    var scale2 = zoom_scale * scale / (zoom_dist + z);
    
    return [ scale2 * vector_dot(view_transform[0], v) + 0.5 * w,
             scale2 * vector_dot(view_transform[1], v) + 0.5 * h ]; }



//  Just like the project function above but for a set of points.
//  It's put into a loop a little better.
function project_all(canvas, points, point_projections, view){

	var view_transform = view.transform;
	var origin = view.origin;
	var zoom_scale = view.zoom_scale;
	var zoom_dist = view.zoom_dist;
	
	if(!zoom_scale) zoom_scale = 1;
	if(!zoom_dist) zoom_dist = 0;
	if(!origin) origin = [0,0,0];
	

    if(!point_projections) point_projections = [];
	point_projections.length = points.length;
    
    var w = canvas.width;
    var h = canvas.height;
	var w_2 = w/2;
	var h_2 = h/2;
    
    var scale = zoom_scale * Math.min(w, h);
	
	var v00 = view_transform[0][0];
	var v01 = view_transform[0][1];
	var v02 = view_transform[0][2];
	var v10 = view_transform[1][0];
	var v11 = view_transform[1][1];
	var v12 = view_transform[1][2];
	var v20 = view_transform[2][0];
	var v21 = view_transform[2][1];
	var v22 = view_transform[2][2];
	
    
	for(var i = 0; i < points.length; ++i){
		var pt = points[i];
		
		var x = pt[0];
		var y = pt[1];
		var z = pt[2];
		
		if(origin){
	         x -= origin[0];
			 y -= origin[1];
			 z -= origin[2]; }
		
		var depth = x*v20 + y*v21 + z*v22;
		var scale2 = scale / (zoom_dist + depth);
		
		if(!point_projections[i]) point_projections[i] = [0, 0];
		
		var point_projection = point_projections[i];
		
		point_projection[0] = scale2 * (x*v00 + y*v01 + z*v02) + w_2;
		point_projection[1] = scale2 * (x*v10 + y*v11 + z*v22) + h_2; }
	
	return point_projections; }




//  A wireframe model and all the controls needed to move the camera and draw.
//  despite the name, it may also include polygon surfaces.
function __WireframeModel(){

	var WFMObj = this;
	
................................................................................
	var view_transform = [[1, 0, 0],
	                      [0, 1, 0],
						  [0, 0, 1]];



	var points = []; //global
	var point_projections = [];
	//var solo_points = {};
	var lines = [];

	var zoom_scale = 1;
	var zoom_dist = 1;
	
	// 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.getPoint = function getPoint(index){
		return points[index].slice(0); }
	
	WFMObj.getLines = function getLines(){
		return lines; }
		
	WFMObj.getLine = function getLine(index){
		return lines[index].slice(0); }
	
	WFMObj.getViewTransform = function getViewTransform(){
		return view_transform; }
	
	
	
	WFMObj.addPoint = function addPoint(pt){
................................................................................
				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; }

		
	WFMObj.draw = function draw(canvas){
		var ctx = canvas.getContext("2d");
		
		var view = {};
		view.transform = view_transform;
		view.origin = view_origin;
		view.zoom_scale = zoom_scale;
		view.zoom_dist = zoom_dist;
		
		project_all(canvas, points, point_projections, view);
		// project_all()
		
		for(var i = 0; i < point_projections.length; ++i){
			var point_projection = point_projections[i];
			if(point_projection) ctx.fillRect(point_projection[0] - 1, point_projection[1] - 1, 2, 2); }
	}
}

//  use constructor as global namespace object.
WireframeModel = __WireframeModel;


})();
................................................................................
    if(cameracentric) offset = matrix_mult(view_transform, [offset])[0];
    //alert("offset: " + offset);
    vector_add(origin, offset, origin); }
	
	
	
	
   

















































   

   
//  removes deleted points and lines
//  works with global objects.

function cleanupDeletedPoints(){