forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint_utils.go
169 lines (139 loc) · 3.3 KB
/
point_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package rimage
import (
"image"
"math"
"github.com/golang/geo/r2"
"github.com/pkg/errors"
"go.viam.com/rdk/utils"
)
// NoPoints TODO.
var NoPoints = image.Point{-1, -1}
// Center TODO.
func Center(contour []image.Point, maxDiff int) image.Point {
if len(contour) == 0 {
return NoPoints
}
x := 0
y := 0
for _, p := range contour {
x += p.X
y += p.Y
}
weightedMiddle := image.Point{x / len(contour), y / len(contour)}
numPoints := 0
box := image.Rectangle{image.Point{1000000, 100000}, image.Point{0, 0}}
for _, p := range contour {
if utils.AbsInt(p.X-weightedMiddle.X) > maxDiff || utils.AbsInt(p.Y-weightedMiddle.Y) > maxDiff {
continue
}
numPoints++
if p.X < box.Min.X {
box.Min.X = p.X
}
if p.Y < box.Min.Y {
box.Min.Y = p.Y
}
if p.X > box.Max.X {
box.Max.X = p.X
}
if p.Y > box.Max.Y {
box.Max.Y = p.Y
}
}
if numPoints == 0 {
return NoPoints
}
avgMiddle := image.Point{(box.Min.X + box.Max.X) / 2, (box.Min.Y + box.Max.Y) / 2}
// fmt.Printf("%v -> %v box: %v\n", weightedMiddle, avgMiddle, box)
return avgMiddle
}
// PointDistance TODO.
func PointDistance(a, b image.Point) float64 {
x := utils.SquareInt(b.X - a.X)
x += utils.SquareInt(b.Y - a.Y)
return math.Sqrt(float64(x))
}
// ArrayToPoints TODO.
func ArrayToPoints(pts []image.Point) []image.Point {
if len(pts) == 4 {
return pts
}
if len(pts) == 2 {
r := image.Rectangle{pts[0], pts[1]}
return []image.Point{
r.Min,
{r.Max.X, r.Min.Y},
r.Max,
{r.Min.X, r.Max.Y},
}
}
panic(errors.Errorf("invalid number of points passed to ArrayToPoints %d", len(pts)))
}
// PointAngle TODO.
func PointAngle(a, b image.Point) float64 {
x := b.X - a.X
y := b.Y - a.Y
return math.Atan2(float64(y), float64(x))
}
// BoundingBox TODO.
func BoundingBox(pts []image.Point) image.Rectangle {
min := image.Point{math.MaxInt32, math.MaxInt32}
max := image.Point{0, 0}
for _, p := range pts {
if p.X < min.X {
min.X = p.X
}
if p.Y < min.Y {
min.Y = p.Y
}
if p.X > max.X {
max.X = p.X
}
if p.Y > max.Y {
max.Y = p.Y
}
}
return image.Rectangle{min, max}
}
// AllPointsIn TODO.
func AllPointsIn(size image.Point, pts []image.Point) bool {
for _, p := range pts {
if p.X < 0 || p.Y < 0 {
return false
}
if p.X >= size.X {
return false
}
if p.Y >= size.Y {
return false
}
}
return true
}
// R2PointToImagePoint TODO.
func R2PointToImagePoint(pt r2.Point) image.Point {
return image.Point{int(math.Round(pt.X)), int(math.Round(pt.Y))}
}
// R2RectToImageRect TODO.
func R2RectToImageRect(rec r2.Rect) image.Rectangle {
return image.Rectangle{R2PointToImagePoint(rec.Lo()), R2PointToImagePoint(rec.Hi())}
}
// TranslateR2Rect TODO.
func TranslateR2Rect(rect r2.Rect, pt r2.Point) r2.Rect {
return r2.RectFromCenterSize(rect.Center().Add(pt), rect.Size())
}
// SliceVecsToXsYs converts a slice of r2.Point to 2 slices floats containing x and y coordinates.
func SliceVecsToXsYs(pts []r2.Point) ([]float64, []float64) {
xs := make([]float64, len(pts))
ys := make([]float64, len(pts))
for i, pt := range pts {
xs[i] = pt.X
ys[i] = pt.Y
}
return xs, ys
}
// AreCollinear returns true if the 3 points a, b and c are collinear.
func AreCollinear(a, b, c r2.Point, eps float64) bool {
val := math.Abs((b.X-a.X)*(c.Y-a.Y) - (c.X-a.X)*(b.Y-a.Y))
return val < eps
}