A hell of a lot cleaning

This commit is contained in:
Thomas FORGIONE
2015-07-01 15:03:11 +02:00
parent bb938e5342
commit 86f2094af6
45 changed files with 56 additions and 193 deletions
+201
View File
@@ -0,0 +1,201 @@
var Hermite = {};
Hermite.Polynom = function(t, f, fp) {
this.times = t;
this.evals = f;
this.primes = fp;
this.baseFunctions = [];
for (var i in this.times) {
this.baseFunctions.push(new Hermite.BaseFunction(i, this.times));
}
// Let's do something at least a little reusable
this.tools = {};
if (f[0] instanceof THREE.Vector3) {
this.tools.whatType = 'THREE.Vector3';
this.tools.sum = Tools.sum;
this.tools.prod = Tools.mul;
} else {
this.tools.whatType = 'number';
this.tools.sum = function(a, b) { return a + b; };
this.tools.prod = function(a, b) { return a * b; };
}
};
Hermite.Polynom.prototype.eval = function(t) {
var ret;
if (this.tools.whatType === 'THREE.Vector3') {
ret = new THREE.Vector3();
} else {
ret = 0;
}
for (var i in this.times) {
var ti = this.times[i];
var qi_t = this.baseFunctions[i].eval(t);
// var qi_ti = this.baseFunctions[i].eval(ti);
var qip_ti = this.baseFunctions[i].prime(ti);
var f_ti = this.evals[i];
var fp_ti = this.primes[i];
// This is the wikipedia formula
// ret += (qi_t / qi_ti) * ((1 - (t - ti) * (qip_ti / qi_ti)) * f_ti + (t - ti) * fp_ti);
// Let's not forget that qi_ti = 1
// This is the final formula
// ret += (qi_t) * ((1 - (t - ti) * (qip_ti)) * f_ti + (t - ti) * fp_ti);
// This is the implementation working with THREE.Vector3
// In terms of disgusting code, we're quite good there
ret =
this.tools.sum(
ret,
this.tools.prod(
this.tools.sum(
this.tools.prod(f_ti, 1 - (t - ti) * (qip_ti)),
this.tools.prod(fp_ti, t - ti)
),
qi_t
)
);
}
return ret;
};
Hermite.Polynom.prototype.prime = function(t) {
var ret;
if (this.tools.whatType === 'THREE.Vector3') {
ret = new THREE.Vector3();
} else {
ret = 0;
}
for (var i in this.times) {
var ti = this.times[i];
var qi_t = this.baseFunctions[i].eval(t);
// var qi_ti = this.baseFunctions[i].eval(ti);
var qip_t = this.baseFunctions[i].prime(t );
var qip_ti = this.baseFunctions[i].prime(ti);
var f_ti = this.evals[i];
var fp_ti = this.primes[i];
// The return of the disgusting code...
// First part is the same that the eval function, but changing qi_t by qip_t
// (first part of the derivative)
ret =
this.tools.sum(
ret,
this.tools.prod(
this.tools.sum(
this.tools.prod(f_ti, 1 - (t - ti) * (qip_ti)),
this.tools.prod(fp_ti, t - ti)
),
qip_t
)
);
// Here, we just add
// ret += qi_t * (-qip_t * f_ti + fp_ti);
ret =
this.tools.sum(
ret,
this.tools.prod(
this.tools.sum(
this.tools.prod(
f_ti,
-qip_t
),
fp_ti
),
qi_t
)
);
// Now the following code is the same as the precedent affectation
// However it doesn't work, and I can't see the difference between
// this and the previous one... so I keep it here, to find the
// mistate later
// ret =
// this.tools.sum(
// ret,
// this.tools.prod(
// this.tools.sum(
// fp_ti,
// this.tools.prod(
// f_ti,
// -qip_ti
// )
// ),
// qi_t
// )
// );
}
return ret;
};
Hermite.BaseFunction = function(index, times) {
this.index = index;
this.times = times;
};
Hermite.BaseFunction.prototype.eval = function(t) {
var ret = 1;
for (var i in this.times) {
if (i !== this.index) {
ret *= (t - this.times[i]) / (this.times[this.index] - this.times[i]);
}
}
return ret * ret;
};
Hermite.BaseFunction.prototype.prime = function(t) {
var ret = 0;
for (var i in this.times) {
if (i !== this.index) {
ret += 2 / (t - this.times[i]);
}
}
return this.eval(t) * ret;
};
Hermite.special = {};
// This polynom interpolates with two coords and one derivative
// t = [0,1]
Hermite.special.Polynom = function(P0, P1, PP1) {
this.tools = {};
if (P0 instanceof THREE.Vector3) {
this.tools.sum = Tools.sum;
this.tools.mul = Tools.mul;
this.tools.diff = Tools.diff;
this.c = P0.clone();
} else {
this.tools.sum = function(a,b) { return a+b; };
this.tools.mul = function(a,b) { return a*b; };
this.tools.diff = function(a,b) { return a-b; };
this.c = P0;
}
this.a = this.tools.sum(PP1, this.tools.diff(P0, P1));
this.b = this.tools.diff(this.tools.mul(this.tools.diff(P1,P0), 2), PP1);
};
Hermite.special.Polynom.prototype.eval = function(t) {
return this.tools.sum(this.tools.mul(this.a, t*t), this.tools.sum(this.tools.mul(this.b, t), this.c));
};
Hermite.special.Polynom.prototype.prime = function(t) {
return this.tools.sum(this.tools.mul(this.a,2*t), this.b);
};
@@ -0,0 +1,44 @@
var container = document.getElementById('content');
function print(text) {
var content = document.createTextNode(text);
var new_line = document.createElement('br');
container.appendChild(content);
container.appendChild(new_line);
}
function toString(variable) {
if (variable instanceof THREE.Vector3) {
return variable.x + ', ' + variable.y + ', ' + variable.z;
} else {
return variable;
}
}
// Test with THREE.Vector3
// t = [0,1];
// f = [new THREE.Vector3(0,0,0), new THREE.Vector3(1,1,1)];
// fp = [new THREE.Vector3(0,1,2), new THREE.Vector3(0,0,0)];
// Test with doubles
t = [0,1];
f = [0,1];
fp = [-1,-1];
var hermite = new Hermite.special.Polynom(0, 1, -1);
print('M = [');
for (var t = 0; t < 1; t += 0.01) {
var res = hermite.eval(t);
print("\t" + t + ',' + toString(res) + ';');
}
print('];');
print('MP = [');
for (var t = 0; t < 1; t += 0.01) {
var res = hermite.prime(t);
print("\t" + t + ',' + toString(res) + ';');
}
print('];');
+37
View File
@@ -0,0 +1,37 @@
var Tools = {version : "1.0" };
Tools.sum = function(v1, v2) {
return new THREE.Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
};
Tools.diff = function(v1, v2) {
return new THREE.Vector3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
};
Tools.dot = function(v1, v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
};
Tools.cross = function(v1, v2) {
return new THREE.Vector3(
v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x
);
};
Tools.mul = function(v1, lambda) {
return new THREE.Vector3(v1.x * lambda, v1.y * lambda, v1.z * lambda);
};
Tools.equals = function(v1, v2) {
return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;
};
Tools.norm2 = function(v) {
return v.x * v.x + v.y * v.y + v.z * v.z;
};
Tools.norm = function(v) {
return Math.sqrt(Tools.norm2(v));
};
+81
View File
@@ -0,0 +1,81 @@
// We will be doing a lot of document.write, so let's remove jshint warnings
/* jshint evil:true */
function test(b) {
if (b)
document.write("<li style='color: #008800'>Success !</li>");
else
document.write("<li style='color: red'>Failure !</li>");
}
function main() {
document.write("<h1>Starting test !</h1>");
var v1 = new THREE.Vector3(1,2,3);
var v2 = new THREE.Vector3(2,3,4);
var v1Bak = v1.clone();
var v2Bak = v2.clone();
// First tests
document.write("<ol>");
var v3 = Tools.sum(v1,v2);
test(v3.x == v1.x + v2.x && v3.y == v1.y + v2.y && v3.z == v1.z + v2.z);
test(Tools.equals(v1, v1Bak));
test(Tools.equals(v2, v2Bak));
document.write('</ol>');
// Clear v1, v2
v1 = v1Bak.clone();
v2 = v2Bak.clone();
document.write("<ol>");
var v4 = Tools.diff(v1,v2);
test(v4.x == v1.x - v2.x && v4.y == v1.y - v2.y && v4.z == v1.z - v2.z);
test(Tools.equals(v1, v1Bak));
test(Tools.equals(v2, v2Bak));
document.write('</ol>');
v1 = v1Bak.clone();
v2 = v2Bak.clone();
document.write("<ol>");
var v5 = Tools.dot(v1,v2);
test(v5 == v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
test(Tools.equals(v1, v1Bak));
test(Tools.equals(v2, v2Bak));
document.write('</ol>');
v1 = v1Bak.clone();
v2 = v2Bak.clone();
document.write("<ol>");
var v6 = Tools.cross(new THREE.Vector3(1,0,0), new THREE.Vector3(0,1,0));
test(Tools.equals(v6, new THREE.Vector3(0,0,1)));
test(Tools.equals(v1, v1Bak));
test(Tools.equals(v2, v2Bak));
document.write('</ol>');
v1 = v1Bak.clone();
v2 = v2Bak.clone();
document.write("<ol>");
for (var lambda = 0; lambda < 5; lambda += 0.5)
{
var v7 = Tools.mul(v1, lambda);
test(Tools.equals(v7, new THREE.Vector3(v1Bak.x*lambda, v1Bak.y*lambda, v1Bak.z*lambda)));
v1 = v1Bak.clone();
v2 = v2Bak.clone();
var v8 = Tools.mul(v1, lambda);
test(Tools.equals(v8, new THREE.Vector3(v1Bak.x*lambda, v1Bak.y*lambda, v1Bak.z*lambda)));
v1 = v1Bak.clone();
v2 = v2Bak.clone();
// Try into v1
v1 = Tools.mul(v1, lambda);
test(Tools.equals(v1, new THREE.Vector3(v1Bak.x*lambda, v1Bak.y*lambda, v1Bak.z*lambda)));
v1 = v1Bak.clone();
v2 = v2Bak.clone();
}
}
main();