Skip to content

Instantly share code, notes, and snippets.

@whatsim
Created November 12, 2013 06:02
Show Gist options
  • Save whatsim/7426212 to your computer and use it in GitHub Desktop.
Save whatsim/7426212 to your computer and use it in GitHub Desktop.
matrix from points
var sylvester = require('sylvester'), glMatrix = require('gl-matrix');
var points = {
topLeft : sylvester.Vector.create([-3.5530766369047617, 1.3324590773809524, 2.1444642857142853]),
topRight : sylvester.Vector.create([0.15434523809523798, -0.19136904761904774, 0.048714285714285266]),
bottomLeft : sylvester.Vector.create([-2.5932031249999996, 1.403546875, 4.597250000000001])
};
var mat = mapping(82,46,points.topLeft,points.topRight,points.bottomLeft);
for(var i = 0; i < 16; i++){
var r = Math.floor(i/4)+1;
var c = i%4+1
console.log('transformMat.M'+r+c+" = "+matl[i]+';');
}
console.log(applyMatrixByHand([1.265,-.157,2.451]));
function fromColumns(a,b,c,d){
var matrixArray = [[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]];
for(var i=0;i<3;i++){
matrixArray[0][i] = a.elements[i];
matrixArray[1][i] = b.elements[i];
matrixArray[2][i] = c.elements[i];
matrixArray[3][i] = d.elements[i];
}
var out = sylvester.Matrix.create(matrixArray);
return out;
}
function scaleMatrix(m,x,y,z) {
for(i = 0; i <= 3; i++) {
m[i][0] *= x;
m[i][1] *= y;
m[i][2] *= z;
}
}
function mapping(wd, ht, a, b, c) {
var dx = b.subtract(a);
var dy = c.subtract(a);
var dz = dy.cross(dx);
var ma = a.dup();
var byExample= fromColumns(dx,dy,dz,ma);
byExample = byExample.inverse();
console.log(byExample);
scaleMatrix(byExample.elements,wd,ht,wd*ht);
var temp = new Array();
for (var i = 0; byExample.elements[i]; i++){
temp.push(byExample.elements[i][0]);
temp.push(byExample.elements[i][1]);
temp.push(byExample.elements[i][2]);
temp.push(byExample.elements[i][3]);
}
byExample = glMatrix.mat4.create(temp);
return byExample;
}
function applyMatrix(vec){
var inV;
if(vec.length){
inV = glMatrix.vec3.create([vec[0],vec[1],vec[2],1]);
} else {
inV = glMatrix.vec3.create([vec.x,vec.y,vec.z,1]);
}
var outV = glMatrix.vec3.create();
return glMatrix.mat4.multiplyVec3(mat,inV,outV);
}
function applyMatrixByHand(vec){
var x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
var y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
var z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
//var w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
return [x,y,z];
}
function Vector(a){
this.x = a[0];
this.y = a[1];
this.z = a[2];
this.add = function(v){
var ox = this.x + v.x;
var oy = this.y + v.y;
var oz = this.z + v.z;
return new Vector([ox,oy,oz]);
}
this.subtract = function(v){
var ox = this.x - v.x;
var oy = this.y - v.y;
var oz = this.z - v.z;
return new Vector([ox,oy,oz]);
}
this.scale = function(s){
var ox = this.x * s;
var oy = this.y * s;
var oz = this.z * s;
return new Vector([ox,oy,oz]);
}
this.normalize = function(){
var length = Math.sqrt(Math.pow(this.x,2) + Math.pow(this.y,2) + Math.pow(this.z,2))
var ox = this.x/length;
var oy = this.y/length;
var oz = this.z/length;
return new Vector([ox,oy,oz]);
}
return this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment