Skip to content

Commit

Permalink
Merge branch 'pr-94'
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardolundgren committed Aug 9, 2014
2 parents 1c45031 + fb69976 commit bd9a089
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build/tracking-min.js
100755 → 100644

Large diffs are not rendered by default.

38 changes: 34 additions & 4 deletions build/tracking.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,14 @@
*/
tracking.ColorTracker.prototype.minDimension = 20;

/**
* Holds the maximum dimension to classify a rectangle.
* @default Infinity
* @type {number}
*/
tracking.ColorTracker.prototype.maxDimension = Infinity;


/**
* Holds the minimum group size to be classified as a rectangle.
* @default 30
Expand Down Expand Up @@ -1985,13 +1993,21 @@
};

/**
* Sets the minimum dimension to classify a rectangle.
* @param {number} minDimension
* Gets the minimum dimension to classify a rectangle.
* @return {number}
*/
tracking.ColorTracker.prototype.getMinDimension = function() {
return this.minDimension;
};

/**
* Gets the maximum dimension to classify a rectangle.
* @return {number}
*/
tracking.ColorTracker.prototype.getMaxDimension = function() {
return this.maxDimension;
};

/**
* Gets the minimum group size to be classified as a rectangle.
* @return {number}
Expand Down Expand Up @@ -2037,6 +2053,8 @@
var intersects;
var results = [];
var minDimension = this.getMinDimension();
var maxDimension = this.getMaxDimension();

for (var r = 0; r < rects.length; r++) {
var r1 = rects[r];
intersects = true;
Expand All @@ -2055,12 +2073,16 @@
break;
}
}

if (intersects) {
if (r1.width >= minDimension && r1.height >= minDimension) {
results.push(r1);
if (r1.width <= maxDimension && r1.height <= maxDimension) {
results.push(r1);
}
}
}
}

return results;
};

Expand All @@ -2074,12 +2096,20 @@

/**
* Sets the minimum dimension to classify a rectangle.
* @return {number}
* @param {number} minDimension
*/
tracking.ColorTracker.prototype.setMinDimension = function(minDimension) {
this.minDimension = minDimension;
};

/**
* Sets the maximum dimension to classify a rectangle.
* @param {number} maxDimension
*/
tracking.ColorTracker.prototype.setMaxDimension = function(maxDimension) {
this.maxDimension = maxDimension;
};

/**
* Sets the minimum group size to be classified as a rectangle.
* @param {number} minGroupSize
Expand Down
38 changes: 34 additions & 4 deletions src/trackers/ColorTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@
*/
tracking.ColorTracker.prototype.minDimension = 20;

/**
* Holds the maximum dimension to classify a rectangle.
* @default Infinity
* @type {number}
*/
tracking.ColorTracker.prototype.maxDimension = Infinity;


/**
* Holds the minimum group size to be classified as a rectangle.
* @default 30
Expand Down Expand Up @@ -135,13 +143,21 @@
};

/**
* Sets the minimum dimension to classify a rectangle.
* @param {number} minDimension
* Gets the minimum dimension to classify a rectangle.
* @return {number}
*/
tracking.ColorTracker.prototype.getMinDimension = function() {
return this.minDimension;
};

/**
* Gets the maximum dimension to classify a rectangle.
* @return {number}
*/
tracking.ColorTracker.prototype.getMaxDimension = function() {
return this.maxDimension;
};

/**
* Gets the minimum group size to be classified as a rectangle.
* @return {number}
Expand Down Expand Up @@ -187,6 +203,8 @@
var intersects;
var results = [];
var minDimension = this.getMinDimension();
var maxDimension = this.getMaxDimension();

for (var r = 0; r < rects.length; r++) {
var r1 = rects[r];
intersects = true;
Expand All @@ -205,12 +223,16 @@
break;
}
}

if (intersects) {
if (r1.width >= minDimension && r1.height >= minDimension) {
results.push(r1);
if (r1.width <= maxDimension && r1.height <= maxDimension) {
results.push(r1);
}
}
}
}

return results;
};

Expand All @@ -224,12 +246,20 @@

/**
* Sets the minimum dimension to classify a rectangle.
* @return {number}
* @param {number} minDimension
*/
tracking.ColorTracker.prototype.setMinDimension = function(minDimension) {
this.minDimension = minDimension;
};

/**
* Sets the maximum dimension to classify a rectangle.
* @param {number} maxDimension
*/
tracking.ColorTracker.prototype.setMaxDimension = function(maxDimension) {
this.maxDimension = maxDimension;
};

/**
* Sets the minimum group size to be classified as a rectangle.
* @param {number} minGroupSize
Expand Down
88 changes: 57 additions & 31 deletions test/ColorTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ module.exports = {
},

testConstructorEmpty: function(test) {
var colors;
var colors;
var tracker;

test.doesNotThrow(
function() {
tracker = new tracking.ColorTracker();
}
);
test.doesNotThrow(function() {
tracker = new tracking.ColorTracker();
});

colors = tracker.getColors();
test.equal(1, colors.length, 'Colors array should have a single value');
Expand All @@ -32,21 +30,17 @@ module.exports = {
var colors;
var tracker;

test.doesNotThrow(
function() {
tracker = new tracking.ColorTracker('yellow');
}
);
test.doesNotThrow(function() {
tracker = new tracking.ColorTracker('yellow');
});

colors = tracker.getColors();
test.equal(1, colors.length, 'Colors array should have a single value');
test.equal('yellow', colors[0], 'The colors array should be set to value in the constructor');

test.throws(
function() {
tracker = new tracking.ColorTracker('notvalid');
}
);
test.throws(function() {
tracker = new tracking.ColorTracker('notvalid');
});

test.done();
},
Expand All @@ -55,32 +49,26 @@ module.exports = {
var colors;
var tracker;

test.doesNotThrow(
function() {
tracker = new tracking.ColorTracker([]);
}
);
test.doesNotThrow(function() {
tracker = new tracking.ColorTracker([]);
});

colors = tracker.getColors();
test.equal(0, colors.length, 'Colors array should be empty');

test.doesNotThrow(
function() {
tracker = new tracking.ColorTracker(['magenta', 'cyan', 'yellow']);
}
);
test.doesNotThrow(function() {
tracker = new tracking.ColorTracker(['magenta', 'cyan', 'yellow']);
});

colors = tracker.getColors();
test.equal(3, colors.length, 'Colors array have 3 values');
test.equal('magenta', colors[0], 'The colors array should be set to values in the constructor');
test.equal('cyan', colors[1], 'The colors array should be set to values in the constructor');
test.equal('yellow', colors[2], 'The colors array should be set to values in the constructor');

test.throws(
function() {
tracker = new tracking.ColorTracker(['magenta', null, 'yellow']);
}
);
test.throws(function() {
tracker = new tracking.ColorTracker(['magenta', null, 'yellow']);
});

test.done();
},
Expand Down Expand Up @@ -163,6 +151,44 @@ module.exports = {
test.done();
});

tracker.track(pixels, 6, 11);
},

testDimensionConstraints: function(test) {
var pixels;
var tracker;

tracking.ColorTracker.registerColor('black', function(r, g, b) {
return r === 0 && g === 0 && b === 0;
});

tracker = new tracking.ColorTracker('black');
tracker.setMinDimension(1);
tracker.setMaxDimension(2);
tracker.setMinGroupSize(6);

pixels = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0
];

tracker.on('track', function(event) {
test.equal(1, event.data.length, 'There should be 1 result rectangle');
test.equal(1, event.data[0].width, 'The rectangle\'s width should be 1');
test.equal(2, event.data[0].height, 'The rectangle\'s height should be 2');

test.done();
});

tracker.track(pixels, 6, 11);
}
};

0 comments on commit bd9a089

Please sign in to comment.