Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drawing-app
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hlgr
drawing-app
Commits
0c2de929
Commit
0c2de929
authored
5 years ago
by
Yuriy Maksymets
Browse files
Options
Downloads
Patches
Plain Diff
Rectangle position and recognition
parent
05429a34
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
__tests__/shape.test.js
+3
-3
3 additions, 3 deletions
__tests__/shape.test.js
src/app.js
+9
-2
9 additions, 2 deletions
src/app.js
src/shapes.js
+59
-21
59 additions, 21 deletions
src/shapes.js
with
71 additions
and
26 deletions
__tests__/shape.test.js
+
3
−
3
View file @
0c2de929
...
...
@@ -244,15 +244,15 @@ describe("shape recognition", () => {
})
test
(
"
should recognize almost-closed rectangle
"
,
()
=>
{
const
points
=
[[
-
10
,
-
10
],
[
10
,
-
10
],
[
10
,
10
],
[
-
9
,
-
9
]]
const
points
=
[[
-
10
,
-
10
],
[
10
,
-
10
],
[
10
,
10
],
[
-
9
,
9
]]
const
result
=
recognizeFromPoints
(
points
)
expect
(
result
.
shape
).
toBe
(
Shapes
.
rectangle
)
})
test
(
"
should
not
recognize half-closed rectangle
"
,
()
=>
{
test
(
"
should recognize half-closed rectangle
"
,
()
=>
{
const
points
=
[[
-
10
,
-
10
],
[
10
,
-
10
],
[
10
,
10
],
[
-
1
,
-
9
]]
const
result
=
recognizeFromPoints
(
points
)
expect
(
result
.
shape
).
not
.
toBe
(
Shapes
.
rectangle
)
expect
(
result
.
shape
).
toBe
(
Shapes
.
rectangle
)
})
})
})
This diff is collapsed.
Click to expand it.
src/app.js
+
9
−
2
View file @
0c2de929
...
...
@@ -7,7 +7,7 @@ import * as canvas from "./canvas.js"
import
*
as
HTML
from
"
./elements.js
"
import
{
connect
}
from
"
./room.js
"
import
*
as
toolSelection
from
"
./tool-selection.js
"
import
recognizeFromPoints
from
"
./shapes.js
"
import
recognizeFromPoints
,
{
Shapes
}
from
"
./shapes.js
"
const
TEST_ROOM
=
"
imperial
"
...
...
@@ -107,15 +107,22 @@ const onRoomConnect = (room_) => {
}
const
mp
=
(
x
,
y
)
=>
[
x
,
y
,
1
,
"
black
"
,
true
]
function
drawRecognized
(
points
)
{
const
recognizedShape
=
recognizeFromPoints
(
points
)
if
(
recognizedShape
.
shape
)
{
if
(
recognizedShape
.
shape
===
Shapes
.
line
)
{
console
.
log
(
recognizedShape
)
const
[
x
,
y
]
=
points
[
0
]
const
a
=
(
recognizedShape
.
angle
*
Math
.
PI
)
/
180
const
[
x0
,
y0
]
=
[
x
-
2000
*
Math
.
cos
(
a
),
y
+
2000
*
Math
.
sin
(
a
)]
const
[
x1
,
y1
]
=
[
x
+
2000
*
Math
.
cos
(
a
),
y
-
2000
*
Math
.
sin
(
a
)]
canvas
.
renderPath
(
"
lastRecognizedLine
"
,
[
mp
(
x0
,
y0
),
mp
(
x1
,
y1
)])
}
else
if
(
recognizedShape
.
shape
===
Shapes
.
rectangle
)
{
console
.
log
(
recognizedShape
)
canvas
.
renderPath
(
"
lastRecognizedLine
"
,
recognizedShape
.
boundingPoints
.
map
((
x
)
=>
mp
(...
x
)),
)
}
else
{
canvas
.
renderPath
(
"
lastRecognizedLine
"
,
[])
}
...
...
This diff is collapsed.
Click to expand it.
src/shapes.js
+
59
−
21
View file @
0c2de929
...
...
@@ -12,6 +12,13 @@ const angleStep = 10
const
numAngleCells
=
180
/
angleStep
const
rhoMax
=
1000
const
getDistance
=
(
a
,
b
)
=>
{
if
(
!
(
a
&
b
))
return
0
return
Math
.
sqrt
(
(
a
[
0
]
-
b
[
0
])
*
(
a
[
0
]
-
b
[
0
])
+
(
a
[
1
]
-
b
[
1
])
*
(
a
[
1
]
-
b
[
1
]),
)
}
function
findMaxInHough
(
accum
,
threshold
)
{
let
max
=
0
// let bestRho = 0
...
...
@@ -67,12 +74,14 @@ function boundingCoords(points) {
}
const
MATRIX_SIZE
=
3
const
MATRIX_CENTER_RATIO
=
0.65
function
mArray
(
min
,
max
)
{
const
step
=
(
max
-
min
)
/
MATRIX_SIZE
return
Array
(
MATRIX_SIZE
)
.
fill
(
min
)
.
map
((
x
,
i
)
=>
x
+
step
*
(
i
+
1
))
const
d
=
max
-
min
const
centerSegmentSize
=
d
*
MATRIX_CENTER_RATIO
const
smallStep
=
(
d
-
centerSegmentSize
)
/
2
const
p
=
[
min
+
smallStep
,
min
+
smallStep
+
centerSegmentSize
,
max
]
return
p
}
function
getCluster
([
x
,
y
],
xBounds
,
yBounds
)
{
...
...
@@ -90,10 +99,16 @@ function computeClusters(points, xBounds, yBounds) {
.
fill
()
.
map
(()
=>
[]),
)
points
.
forEach
((
point
)
=>
{
const
{
x
,
y
}
=
getCluster
(
point
,
xBounds
,
yBounds
)
clusters
[
x
][
y
].
push
(
point
)
const
intervals
=
points
.
map
((
point
,
i
)
=>
({
point
,
dist
:
getDistance
(
point
,
points
[
i
+
1
]),
}))
intervals
.
forEach
((
interval
)
=>
{
const
{
x
,
y
}
=
getCluster
(
interval
.
point
,
xBounds
,
yBounds
)
clusters
[
x
][
y
].
push
(
interval
)
})
return
clusters
}
...
...
@@ -103,8 +118,8 @@ function clusterCoefficients(clusters, points) {
)
}
export
function
computeMatrixCoefficients
(
points
)
{
const
{
maxX
,
minX
,
maxY
,
minY
}
=
bounding
Coords
(
points
)
export
function
computeMatrixCoefficients
(
points
,
boundingRect
)
{
const
{
maxX
,
minX
,
maxY
,
minY
}
=
bounding
Rect
const
xBounds
=
mArray
(
minX
,
maxX
)
const
yBounds
=
mArray
(
minY
,
maxY
)
const
clusters
=
computeClusters
(
points
,
xBounds
,
yBounds
)
...
...
@@ -112,16 +127,22 @@ export function computeMatrixCoefficients(points) {
return
coefficients
}
const
LINE_Q
=
10
function
couldBeLine
(
points
)
{
return
points
.
length
>=
2
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.2
const
RECT_THRESHOLD_SIDE_VARIANCE
=
0.
1
2
function
couldBeRect
(
points
)
{
if
(
points
.
length
<
4
)
return
false
const
matrixCoefficients
=
computeMatrixCoefficients
(
points
)
const
boundingRect
=
boundingCoords
(
points
)
const
matrixCoefficients
=
computeMatrixCoefficients
(
points
,
boundingRect
)
let
[
maxC
,
minC
]
=
[
0
,
1
]
for
(
let
i
=
0
;
i
<
3
;
i
++
)
{
...
...
@@ -133,24 +154,40 @@ function couldBeRect(points) {
}
}
console
.
log
(
matrixCoefficients
)
if
(
matrixCoefficients
[
1
][
1
]
<
RECT_THRESHOLD_CENTER
&&
maxC
-
minC
<
RECT_THRESHOLD_SIDE_VARIANCE
(
matrixCoefficients
[
1
][
1
]
<
RECT_THRESHOLD_CENTER
&&
maxC
-
minC
<
RECT_THRESHOLD_SIDE_VARIANCE
)
||
(
maxC
-
minC
<
RECT_THRESHOLD_SIDE_VARIANCE
*
2
&&
matrixCoefficients
[
1
][
1
]
===
0
)
)
{
return
true
return
{
coefficients
:
matrixCoefficients
,
boundingRect
}
}
return
false
return
undefined
}
function
recognizeRect
(
points
)
{
return
{
points
}
function
recognizeRect
(
points
,
rectDetectionData
)
{
const
{
minX
,
minY
,
maxX
,
maxY
}
=
rectDetectionData
.
boundingRect
return
{
boundingRect
:
rectDetectionData
.
boundingRect
,
boundingPoints
:
[
[
minX
,
minY
],
[
minX
,
maxY
],
[
maxX
,
maxY
],
[
maxX
,
minY
],
[
minX
,
minY
],
],
shape
:
Shapes
.
rectangle
,
points
,
}
}
function
recognizeLine
(
points
)
{
if
(
!
(
points
&&
points
.
length
))
return
{}
const
accum
=
Array
(
numAngleCells
)
const
houghConfig
=
{
rhoStep
:
points
.
length
>
10
0
?
50
:
5
,
rhoStep
:
points
.
length
>
5
0
?
50
:
5
,
}
points
.
forEach
((
x
)
=>
constructHoughAccumulator
(
houghConfig
,
accum
,
...
x
))
const
angle
=
findMaxInHough
(
accum
,
points
.
length
-
1
)
...
...
@@ -168,8 +205,9 @@ function recognizeLine(points) {
}
function
recognizeFromPoints
(
points
)
{
if
(
couldBeRect
(
points
))
{
return
recognizeRect
(
points
)
const
rectDetectData
=
couldBeRect
(
points
)
if
(
rectDetectData
)
{
return
recognizeRect
(
points
,
rectDetectData
)
}
else
if
(
couldBeLine
(
points
))
{
return
recognizeLine
(
points
)
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment