You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
![]()
10 years ago
|
import {vec2} from 'gl-matrix';
|
||
![]()
11 years ago
|
/**
|
||
![]()
10 years ago
|
* Creates a cluster for grouping similar orientations of datapoints
|
||
![]()
11 years ago
|
*/
|
||
![]()
10 years ago
|
export default {
|
||
![]()
10 years ago
|
create: function(point, threshold) {
|
||
|
var points = [],
|
||
|
center = {
|
||
|
rad: 0,
|
||
|
vec: vec2.clone([0, 0])
|
||
|
},
|
||
|
pointMap = {};
|
||
![]()
10 years ago
|
|
||
|
function init() {
|
||
|
add(point);
|
||
|
updateCenter();
|
||
|
}
|
||
![]()
11 years ago
|
|
||
![]()
10 years ago
|
function add(pointToAdd) {
|
||
|
pointMap[pointToAdd.id] = pointToAdd;
|
||
|
points.push(pointToAdd);
|
||
![]()
10 years ago
|
}
|
||
![]()
11 years ago
|
|
||
![]()
10 years ago
|
function updateCenter() {
|
||
|
var i, sum = 0;
|
||
|
for ( i = 0; i < points.length; i++) {
|
||
|
sum += points[i].rad;
|
||
![]()
11 years ago
|
}
|
||
![]()
10 years ago
|
center.rad = sum / points.length;
|
||
|
center.vec = vec2.clone([Math.cos(center.rad), Math.sin(center.rad)]);
|
||
|
}
|
||
![]()
11 years ago
|
|
||
![]()
10 years ago
|
init();
|
||
![]()
11 years ago
|
|
||
![]()
10 years ago
|
return {
|
||
![]()
10 years ago
|
add: function(pointToAdd) {
|
||
|
if (!pointMap[pointToAdd.id]) {
|
||
|
add(pointToAdd);
|
||
![]()
10 years ago
|
updateCenter();
|
||
![]()
11 years ago
|
}
|
||
![]()
10 years ago
|
},
|
||
![]()
10 years ago
|
fits: function(otherPoint) {
|
||
![]()
10 years ago
|
// check cosine similarity to center-angle
|
||
![]()
10 years ago
|
var similarity = Math.abs(vec2.dot(otherPoint.point.vec, center.vec));
|
||
![]()
10 years ago
|
if (similarity > threshold) {
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
},
|
||
![]()
10 years ago
|
getPoints: function() {
|
||
![]()
10 years ago
|
return points;
|
||
|
},
|
||
![]()
10 years ago
|
getCenter: function() {
|
||
![]()
10 years ago
|
return center;
|
||
|
}
|
||
|
};
|
||
|
},
|
||
![]()
10 years ago
|
createPoint: function(newPoint, id, property) {
|
||
![]()
10 years ago
|
return {
|
||
![]()
10 years ago
|
rad: newPoint[property],
|
||
|
point: newPoint,
|
||
![]()
10 years ago
|
id: id
|
||
![]()
10 years ago
|
};
|
||
|
}
|
||
|
};
|