Skip to content

Commit

Permalink
Added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbany committed Nov 16, 2016
1 parent 03ed03b commit 2b1cf3a
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 9 deletions.
14 changes: 12 additions & 2 deletions dist/CommentCoreLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ var CommentUtils;
Matrix3D.identity = function () {
return new Matrix3D([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
};
Matrix3D.createScaleMatrix = function (xscale, yscale, zscale) {
return new Matrix3D([xscale, 0, 0, 0, 0, yscale, 0, 0, 0, 0, zscale, 0, 0, 0, 0, 1]);
};
Matrix3D.createRotationMatrix = function (xrot, yrot, zrot) {
var DEG2RAD = Math.PI / 180;
var yr = yrot * DEG2RAD;
Expand All @@ -277,7 +280,9 @@ var CommentUtils;
(-SIN(yr) * COS(zr)), (-SIN(yr) * SIN(zr)), COS(yr), 0,
0, 0, 0, 1
];
return new Matrix3D(matrix);
return new Matrix3D(matrix.map(function (v) {
return Math.round(v * 1e10) * 1e-10;
}));
};
return Matrix3D;
}());
Expand Down Expand Up @@ -805,6 +810,9 @@ var CommentUtils;
Matrix3D.identity = function () {
return new Matrix3D([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
};
Matrix3D.createScaleMatrix = function (xscale, yscale, zscale) {
return new Matrix3D([xscale, 0, 0, 0, 0, yscale, 0, 0, 0, 0, zscale, 0, 0, 0, 0, 1]);
};
Matrix3D.createRotationMatrix = function (xrot, yrot, zrot) {
var DEG2RAD = Math.PI / 180;
var yr = yrot * DEG2RAD;
Expand All @@ -817,7 +825,9 @@ var CommentUtils;
(-SIN(yr) * COS(zr)), (-SIN(yr) * SIN(zr)), COS(yr), 0,
0, 0, 0, 1
];
return new Matrix3D(matrix);
return new Matrix3D(matrix.map(function (v) {
return Math.round(v * 1e10) * 1e-10;
}));
};
return Matrix3D;
}());
Expand Down
4 changes: 2 additions & 2 deletions dist/CommentCoreLibrary.min.js

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions spec/core/Utils_spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'
describe 'CommentUtils', ->
describe 'Matrix3D', ->
describe 'identity', ->
it 'returns identity matrix', ->
expect(CommentUtils.Matrix3D.identity().isIdentity()).toBe true

describe 'createScaleMatrix', ->
it 'returns identity matrix', ->
expect(CommentUtils.Matrix3D.createScaleMatrix(1, 1, 1).isIdentity()).toBe true

it 'returns proper scale matrix', ->
expect(CommentUtils.Matrix3D.createScaleMatrix(1, 2, 3).flatArray).toEqual [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1]

describe 'createRotationMatrix', ->
it 'returns identity matrix if no rotation', ->
expect(CommentUtils.Matrix3D.createRotationMatrix(0, 0, 0).isIdentity()).toBe true

it 'returns identity matrix if rotating 360 degrees', ->
expect(CommentUtils.Matrix3D.createRotationMatrix(360, 0, 0).isIdentity()).toBe true
expect(CommentUtils.Matrix3D.createRotationMatrix(0, 360, 0).isIdentity()).toBe true
expect(CommentUtils.Matrix3D.createRotationMatrix(0, 0, 360).isIdentity()).toBe true

describe 'constructor', ->
it 'throws error if constructed improper dimensions', ->
expect( => new CommentUtils.Matrix3D([1, 2, 3])).toThrow()

describe 'instance API', ->
m = null
n = null
mdotn = null
beforeEach ->
m = new CommentUtils.Matrix3D [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
n = CommentUtils.Matrix3D.createScaleMatrix 6, 7, 8
mdotn = new CommentUtils.Matrix3D [6, 14, 24, 4, 30, 42, 56, 8, 54, 0, 8, 2, 18, 28, 40, 6]

it 'gets flatArray', ->
expect(m.flatArray).toEqual [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]

it 'cannot set flatArray', ->
expect( => m.flatArray = []).toThrow()

it 'checks identity', ->
expect(m.isIdentity()).toBe false

it 'dot product', ->
expect(m.dot(n).equals mdotn).toBe true

it 'toCss', ->
expect(n.toCss()).toBe 'matrix3d(6,0,0,0,0,7,0,0,0,0,8,0,0,0,0,1)'
7 changes: 6 additions & 1 deletion src/core/Comment.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/Comment.js.map

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/core/CommentFactory.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/core/CommentFactory.js.map

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/core/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module CommentUtils {
return new Matrix3D([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
};

public static createScaleMatrix:Function = function (xscale:number, yscale:number, zscale:number):Matrix3D {
return new Matrix3D([xscale, 0, 0, 0, 0, yscale, 0, 0, 0, 0, zscale, 0, 0, 0, 0, 1]);
};

public static createRotationMatrix:Function = function (xrot:number, yrot:number, zrot:number):Matrix3D {
// Courtesy of @StarBrilliant, re-adapted for general case
var DEG2RAD = Math.PI/180;
Expand All @@ -26,7 +30,10 @@ module CommentUtils {
(-SIN(yr) * COS(zr)) , (-SIN(yr) * SIN(zr)) , COS(yr) , 0,
0 , 0 , 0 , 1
];
return new Matrix3D(matrix);
// Do some rounding
return new Matrix3D(matrix.map(function (v) {
return Math.round(v * 1e10) * 1e-10;
}));
};

/**
Expand Down

0 comments on commit 2b1cf3a

Please sign in to comment.