Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1372 Rev 1422
1
/*
1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
3
	All Rights Reserved.
4
 
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
9
*/
-
 
10
 
-
 
11
 
10
 
12
 
11
dojo.provide("dojo.math.curves");
13
dojo.provide("dojo.math.curves");
12
dojo.require("dojo.math");
14
dojo.require("dojo.math");
13
dojo.math.curves = {Line:function (start, end) {
15
dojo.math.curves = {Line:function (start, end) {
14
	this.start = start;
16
	this.start = start;
15
	this.end = end;
17
	this.end = end;
16
	this.dimensions = start.length;
18
	this.dimensions = start.length;
17
	for (var i = 0; i < start.length; i++) {
19
	for (var i = 0; i < start.length; i++) {
18
		start[i] = Number(start[i]);
20
		start[i] = Number(start[i]);
19
	}
21
	}
20
	for (var i = 0; i < end.length; i++) {
22
	for (var i = 0; i < end.length; i++) {
21
		end[i] = Number(end[i]);
23
		end[i] = Number(end[i]);
22
	}
24
	}
23
	this.getValue = function (n) {
25
	this.getValue = function (n) {
24
		var retVal = new Array(this.dimensions);
26
		var retVal = new Array(this.dimensions);
25
		for (var i = 0; i < this.dimensions; i++) {
27
		for (var i = 0; i < this.dimensions; i++) {
26
			retVal[i] = ((this.end[i] - this.start[i]) * n) + this.start[i];
28
			retVal[i] = ((this.end[i] - this.start[i]) * n) + this.start[i];
27
		}
29
		}
28
		return retVal;
30
		return retVal;
29
	};
31
	};
30
	return this;
32
	return this;
31
}, Bezier:function (pnts) {
33
}, Bezier:function (pnts) {
32
	this.getValue = function (step) {
34
	this.getValue = function (step) {
33
		if (step >= 1) {
35
		if (step >= 1) {
34
			return this.p[this.p.length - 1];
36
			return this.p[this.p.length - 1];
35
		}
37
		}
36
		if (step <= 0) {
38
		if (step <= 0) {
37
			return this.p[0];
39
			return this.p[0];
38
		}
40
		}
39
		var retVal = new Array(this.p[0].length);
41
		var retVal = new Array(this.p[0].length);
40
		for (var k = 0; j < this.p[0].length; k++) {
42
		for (var k = 0; j < this.p[0].length; k++) {
41
			retVal[k] = 0;
43
			retVal[k] = 0;
42
		}
44
		}
43
		for (var j = 0; j < this.p[0].length; j++) {
45
		for (var j = 0; j < this.p[0].length; j++) {
44
			var C = 0;
46
			var C = 0;
45
			var D = 0;
47
			var D = 0;
46
			for (var i = 0; i < this.p.length; i++) {
48
			for (var i = 0; i < this.p.length; i++) {
47
				C += this.p[i][j] * this.p[this.p.length - 1][0] * dojo.math.bernstein(step, this.p.length, i);
49
				C += this.p[i][j] * this.p[this.p.length - 1][0] * dojo.math.bernstein(step, this.p.length, i);
48
			}
50
			}
49
			for (var l = 0; l < this.p.length; l++) {
51
			for (var l = 0; l < this.p.length; l++) {
50
				D += this.p[this.p.length - 1][0] * dojo.math.bernstein(step, this.p.length, l);
52
				D += this.p[this.p.length - 1][0] * dojo.math.bernstein(step, this.p.length, l);
51
			}
53
			}
52
			retVal[j] = C / D;
54
			retVal[j] = C / D;
53
		}
55
		}
54
		return retVal;
56
		return retVal;
55
	};
57
	};
56
	this.p = pnts;
58
	this.p = pnts;
57
	return this;
59
	return this;
58
}, CatmullRom:function (pnts, c) {
60
}, CatmullRom:function (pnts, c) {
59
	this.getValue = function (step) {
61
	this.getValue = function (step) {
60
		var percent = step * (this.p.length - 1);
62
		var percent = step * (this.p.length - 1);
61
		var node = Math.floor(percent);
63
		var node = Math.floor(percent);
62
		var progress = percent - node;
64
		var progress = percent - node;
63
		var i0 = node - 1;
65
		var i0 = node - 1;
64
		if (i0 < 0) {
66
		if (i0 < 0) {
65
			i0 = 0;
67
			i0 = 0;
66
		}
68
		}
67
		var i = node;
69
		var i = node;
68
		var i1 = node + 1;
70
		var i1 = node + 1;
69
		if (i1 >= this.p.length) {
71
		if (i1 >= this.p.length) {
70
			i1 = this.p.length - 1;
72
			i1 = this.p.length - 1;
71
		}
73
		}
72
		var i2 = node + 2;
74
		var i2 = node + 2;
73
		if (i2 >= this.p.length) {
75
		if (i2 >= this.p.length) {
74
			i2 = this.p.length - 1;
76
			i2 = this.p.length - 1;
75
		}
77
		}
76
		var u = progress;
78
		var u = progress;
77
		var u2 = progress * progress;
79
		var u2 = progress * progress;
78
		var u3 = progress * progress * progress;
80
		var u3 = progress * progress * progress;
79
		var retVal = new Array(this.p[0].length);
81
		var retVal = new Array(this.p[0].length);
80
		for (var k = 0; k < this.p[0].length; k++) {
82
		for (var k = 0; k < this.p[0].length; k++) {
81
			var x1 = (-this.c * this.p[i0][k]) + ((2 - this.c) * this.p[i][k]) + ((this.c - 2) * this.p[i1][k]) + (this.c * this.p[i2][k]);
83
			var x1 = (-this.c * this.p[i0][k]) + ((2 - this.c) * this.p[i][k]) + ((this.c - 2) * this.p[i1][k]) + (this.c * this.p[i2][k]);
82
			var x2 = (2 * this.c * this.p[i0][k]) + ((this.c - 3) * this.p[i][k]) + ((3 - 2 * this.c) * this.p[i1][k]) + (-this.c * this.p[i2][k]);
84
			var x2 = (2 * this.c * this.p[i0][k]) + ((this.c - 3) * this.p[i][k]) + ((3 - 2 * this.c) * this.p[i1][k]) + (-this.c * this.p[i2][k]);
83
			var x3 = (-this.c * this.p[i0][k]) + (this.c * this.p[i1][k]);
85
			var x3 = (-this.c * this.p[i0][k]) + (this.c * this.p[i1][k]);
84
			var x4 = this.p[i][k];
86
			var x4 = this.p[i][k];
85
			retVal[k] = x1 * u3 + x2 * u2 + x3 * u + x4;
87
			retVal[k] = x1 * u3 + x2 * u2 + x3 * u + x4;
86
		}
88
		}
87
		return retVal;
89
		return retVal;
88
	};
90
	};
89
	if (!c) {
91
	if (!c) {
90
		this.c = 0.7;
92
		this.c = 0.7;
91
	} else {
93
	} else {
92
		this.c = c;
94
		this.c = c;
93
	}
95
	}
94
	this.p = pnts;
96
	this.p = pnts;
95
	return this;
97
	return this;
96
}, Arc:function (start, end, ccw) {
98
}, Arc:function (start, end, ccw) {
97
	var center = dojo.math.points.midpoint(start, end);
99
	var center = dojo.math.points.midpoint(start, end);
98
	var sides = dojo.math.points.translate(dojo.math.points.invert(center), start);
100
	var sides = dojo.math.points.translate(dojo.math.points.invert(center), start);
99
	var rad = Math.sqrt(Math.pow(sides[0], 2) + Math.pow(sides[1], 2));
101
	var rad = Math.sqrt(Math.pow(sides[0], 2) + Math.pow(sides[1], 2));
100
	var theta = dojo.math.radToDeg(Math.atan(sides[1] / sides[0]));
102
	var theta = dojo.math.radToDeg(Math.atan(sides[1] / sides[0]));
101
	if (sides[0] < 0) {
103
	if (sides[0] < 0) {
102
		theta -= 90;
104
		theta -= 90;
103
	} else {
105
	} else {
104
		theta += 90;
106
		theta += 90;
105
	}
107
	}
106
	dojo.math.curves.CenteredArc.call(this, center, rad, theta, theta + (ccw ? -180 : 180));
108
	dojo.math.curves.CenteredArc.call(this, center, rad, theta, theta + (ccw ? -180 : 180));
107
}, CenteredArc:function (center, radius, start, end) {
109
}, CenteredArc:function (center, radius, start, end) {
108
	this.center = center;
110
	this.center = center;
109
	this.radius = radius;
111
	this.radius = radius;
110
	this.start = start || 0;
112
	this.start = start || 0;
111
	this.end = end;
113
	this.end = end;
112
	this.getValue = function (n) {
114
	this.getValue = function (n) {
113
		var retVal = new Array(2);
115
		var retVal = new Array(2);
114
		var theta = dojo.math.degToRad(this.start + ((this.end - this.start) * n));
116
		var theta = dojo.math.degToRad(this.start + ((this.end - this.start) * n));
115
		retVal[0] = this.center[0] + this.radius * Math.sin(theta);
117
		retVal[0] = this.center[0] + this.radius * Math.sin(theta);
116
		retVal[1] = this.center[1] - this.radius * Math.cos(theta);
118
		retVal[1] = this.center[1] - this.radius * Math.cos(theta);
117
		return retVal;
119
		return retVal;
118
	};
120
	};
119
	return this;
121
	return this;
120
}, Circle:function (center, radius) {
122
}, Circle:function (center, radius) {
121
	dojo.math.curves.CenteredArc.call(this, center, radius, 0, 360);
123
	dojo.math.curves.CenteredArc.call(this, center, radius, 0, 360);
122
	return this;
124
	return this;
123
}, Path:function () {
125
}, Path:function () {
124
	var curves = [];
126
	var curves = [];
125
	var weights = [];
127
	var weights = [];
126
	var ranges = [];
128
	var ranges = [];
127
	var totalWeight = 0;
129
	var totalWeight = 0;
128
	this.add = function (curve, weight) {
130
	this.add = function (curve, weight) {
129
		if (weight < 0) {
131
		if (weight < 0) {
130
			dojo.raise("dojo.math.curves.Path.add: weight cannot be less than 0");
132
			dojo.raise("dojo.math.curves.Path.add: weight cannot be less than 0");
131
		}
133
		}
132
		curves.push(curve);
134
		curves.push(curve);
133
		weights.push(weight);
135
		weights.push(weight);
134
		totalWeight += weight;
136
		totalWeight += weight;
135
		computeRanges();
137
		computeRanges();
136
	};
138
	};
137
	this.remove = function (curve) {
139
	this.remove = function (curve) {
138
		for (var i = 0; i < curves.length; i++) {
140
		for (var i = 0; i < curves.length; i++) {
139
			if (curves[i] == curve) {
141
			if (curves[i] == curve) {
140
				curves.splice(i, 1);
142
				curves.splice(i, 1);
141
				totalWeight -= weights.splice(i, 1)[0];
143
				totalWeight -= weights.splice(i, 1)[0];
142
				break;
144
				break;
143
			}
145
			}
144
		}
146
		}
145
		computeRanges();
147
		computeRanges();
146
	};
148
	};
147
	this.removeAll = function () {
149
	this.removeAll = function () {
148
		curves = [];
150
		curves = [];
149
		weights = [];
151
		weights = [];
150
		totalWeight = 0;
152
		totalWeight = 0;
151
	};
153
	};
152
	this.getValue = function (n) {
154
	this.getValue = function (n) {
153
		var found = false, value = 0;
155
		var found = false, value = 0;
154
		for (var i = 0; i < ranges.length; i++) {
156
		for (var i = 0; i < ranges.length; i++) {
155
			var r = ranges[i];
157
			var r = ranges[i];
156
			if (n >= r[0] && n < r[1]) {
158
			if (n >= r[0] && n < r[1]) {
157
				var subN = (n - r[0]) / r[2];
159
				var subN = (n - r[0]) / r[2];
158
				value = curves[i].getValue(subN);
160
				value = curves[i].getValue(subN);
159
				found = true;
161
				found = true;
160
				break;
162
				break;
161
			}
163
			}
162
		}
164
		}
163
		if (!found) {
165
		if (!found) {
164
			value = curves[curves.length - 1].getValue(1);
166
			value = curves[curves.length - 1].getValue(1);
165
		}
167
		}
166
		for (var j = 0; j < i; j++) {
168
		for (var j = 0; j < i; j++) {
167
			value = dojo.math.points.translate(value, curves[j].getValue(1));
169
			value = dojo.math.points.translate(value, curves[j].getValue(1));
168
		}
170
		}
169
		return value;
171
		return value;
170
	};
172
	};
171
	function computeRanges() {
173
	function computeRanges() {
172
		var start = 0;
174
		var start = 0;
173
		for (var i = 0; i < weights.length; i++) {
175
		for (var i = 0; i < weights.length; i++) {
174
			var end = start + weights[i] / totalWeight;
176
			var end = start + weights[i] / totalWeight;
175
			var len = end - start;
177
			var len = end - start;
176
			ranges[i] = [start, end, len];
178
			ranges[i] = [start, end, len];
177
			start = end;
179
			start = end;
178
		}
180
		}
179
	}
181
	}
180
	return this;
182
	return this;
181
}};
183
}};
182
 
184