Skip to content
Snippets Groups Projects
Commit 3912ba19 authored by Yuriy Maksymets's avatar Yuriy Maksymets
Browse files

Length based matrix weights

parent 0c2de929
Branches
Tags
No related merge requests found
......@@ -13,7 +13,7 @@ const numAngleCells = 180 / angleStep
const rhoMax = 1000
const getDistance = (a, b) => {
if (!(a & b)) return 0
if (!(a && b)) return 0
return Math.sqrt(
(a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]),
)
......@@ -73,6 +73,14 @@ function boundingCoords(points) {
}
}
const LINE_Q = 10
function couldBeLine(points) {
const { maxX, minX, maxY, minY } = boundingCoords(points)
const [dx, dy] = [maxX - minX, maxY - minY].map((x) => x + 0.00001)
return dy / dx > LINE_Q || dx / dy > LINE_Q
}
const MATRIX_SIZE = 3
const MATRIX_CENTER_RATIO = 0.65
......@@ -97,24 +105,27 @@ function computeClusters(points, xBounds, yBounds) {
.map(() =>
Array(MATRIX_SIZE)
.fill()
.map(() => []),
.map(() => ({ arr: [], sum: 0 })),
)
const intervals = points.map((point, i) => ({
point,
dist: getDistance(point, points[i + 1]),
}))
let totalSum = 0
intervals.forEach((interval) => {
const { x, y } = getCluster(interval.point, xBounds, yBounds)
clusters[x][y].push(interval)
clusters[x][y].arr.push(interval)
clusters[x][y].sum += interval.dist
totalSum += interval.dist
})
return clusters
return { arr: clusters, totalSum }
}
function clusterCoefficients(clusters, points) {
return clusters.map((rowCluster) =>
rowCluster.map((cluster) => cluster.length / points.length),
function clusterCoefficients(clusters) {
return clusters.arr.map((rowCluster) =>
rowCluster.map((cluster) => cluster.sum / clusters.totalSum),
)
}
......@@ -127,16 +138,8 @@ export function computeMatrixCoefficients(points, boundingRect) {
return coefficients
}
const LINE_Q = 10
function couldBeLine(points) {
const { maxX, minX, maxY, minY } = boundingCoords(points)
const [dx, dy] = [maxX - minX, maxY - minY].map((x) => x + 0.00001)
return dy / dx > LINE_Q || dx / dy > LINE_Q
}
const RECT_THRESHOLD_CENTER = 0.05
const RECT_THRESHOLD_SIDE_VARIANCE = 0.12
const RECT_THRESHOLD_CENTER = 0
const RECT_THRESHOLD_SIDE_VARIANCE = 0.25
function couldBeRect(points) {
if (points.length < 4) return false
......@@ -145,8 +148,8 @@ function couldBeRect(points) {
const matrixCoefficients = computeMatrixCoefficients(points, boundingRect)
let [maxC, minC] = [0, 1]
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
for (let i = 0; i < MATRIX_SIZE; i++) {
for (let j = 0; j < MATRIX_SIZE; j++) {
if (!(i === j && j === 1)) {
maxC = Math.max(maxC, matrixCoefficients[i][j])
minC = Math.min(minC, matrixCoefficients[i][j])
......@@ -154,13 +157,9 @@ function couldBeRect(points) {
}
}
console.log(matrixCoefficients)
if (
(matrixCoefficients[1][1] < RECT_THRESHOLD_CENTER &&
maxC - minC < RECT_THRESHOLD_SIDE_VARIANCE) ||
(maxC - minC < RECT_THRESHOLD_SIDE_VARIANCE * 2 &&
matrixCoefficients[1][1] === 0)
matrixCoefficients[1][1] <= RECT_THRESHOLD_CENTER &&
maxC - minC < RECT_THRESHOLD_SIDE_VARIANCE
) {
return { coefficients: matrixCoefficients, boundingRect }
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment