Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/homog.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var Homog = function Homog (rot, trans) {
}

if (rot.cols !== rot.rows || trans.v.length !== rot.cols) {
return new Error('Rotation matrix must be square and transform must be of same order as matrix');
throw new Error('Rotation matrix must be square and transform must be of same order as matrix');
}

var H = new m(4);
Expand Down Expand Up @@ -46,4 +46,4 @@ var Homog = function Homog (rot, trans) {
return H;
};

exports = module.exports = Homog;
exports = module.exports = Homog;
6 changes: 6 additions & 0 deletions tests/homog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ var should = require('should');
var Matrix = require('../').matrix;

describe('Create a homogenous matrix', function () {
it('detects bad rotation matrix', function () {
var Rz = new Matrix(3, 4);
(function () {
var H1 = new Homog(Rz, 0);
}).should.throw();
});
it('rotating about the z-axis by pi/2 radians', function () {
var Rz = new Rotate.RotZ(Math.PI/2);
var H1 = new Homog(Rz, 0);
Expand Down
31 changes: 31 additions & 0 deletions tests/vector-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ var v = require('../').vector;
var should = require('should');

describe('Creating vectors: ', function () {
it('truly creates a vector', function () {
var a = new v(1);
a.isVector.should.eql(true);
});

it('Should pass with 3 arguments', function () {
var a = new v(1,0,0);
a.v.length.should.eql(3);
Expand Down Expand Up @@ -29,28 +34,54 @@ describe('Creating vectors: ', function () {

var a = new v(1,0,0);
var b = new v(1,1,0);
var d = new v(0,1,1);

describe('Between two vectors: ', function () {
it('should add two vectors easily', function () {
var c = a.add(b);
c.v.length.should.eql(3);
c.v.should.eql([ 2, 1, 0 ]);

var c = a.add(d);
c.v.length.should.eql(3);
c.v.should.eql([ 1, 1, 1 ]);
});

it('should calculate the dot product', function () {
var c = a.dot(b);
c.should.eql(1);

var c = a.dot(d);
c.should.eql(0);
});

it('should calculate the cross product', function () {
var c = a.cross(b);
c.v.length.should.eql(3);
c.v.should.eql([0,0,1]);

var c = b.cross(a);
c.v.length.should.eql(3);
c.v.should.eql([0,0,-1]);

var c = a.cross(d);
c.v.length.should.eql(3);
c.v.should.eql([0,-1,1]);

var c = d.cross(b);
c.v.length.should.eql(3);
c.v.should.eql([-1,1,-1]);
});

it('should calculate the distance between the two', function () {
var c = a.distanceFrom(b);
c.should.eql(1);

var c = a.distanceFrom(a);
c.should.eql(0);

var c = b.distanceFrom(d);
c.should.eql(Math.sqrt(2));
});

it('should calculate the length of the vector', function () {
Expand Down