/
contour.go
88 lines (70 loc) · 2.27 KB
/
contour.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
package border
import (
"fmt"
"image"
"strings"
)
const (
Outer = 0
Hole = 1
)
// Contour represents a single contour/border extracted from an image.
// It also tracks its parents and children.
type Contour struct {
// Points making up the contour
Points []image.Point
Id int
// Outer or Hole.
BorderType int
// Id of parent
ParentId int
// ParentCollision indicates if colliding with parent. Just an optimisation for quick removal later on.
ParentCollision bool
// Parent links to contours parent
Parent *Contour
// Children links to contours children
Children []*Contour
// ConflictingContours is a map of contours that we KNOW we conflict with. This may be the parent or other
// siblings
ConflictingContours map[int]bool // hate to use maps here... but want uniqueness
// usable or not. Not filtering out but marking that we may not use it. (say if we're conflicting with another contour)
Usable bool
}
// NewContour create new contour
func NewContour(id int) *Contour {
c := Contour{}
c.Id = id
c.BorderType = Hole
c.ConflictingContours = make(map[int]bool)
c.Usable = true
return &c
}
// AddPoint adds a point (image.Point) to the contour
func (c *Contour) AddPoint(p image.Point) error {
c.Points = append(c.Points, p)
return nil
}
// ContourStats generates writes to stdout stats about the contour and all children.
// Primarily used for debugging
func ContourStats(c *Contour, offset int) {
if len(c.Points) > 0 {
pad := strings.Repeat(" ", offset)
fmt.Printf("%s%d : len %d : no kids %d : no col %d : col with parent %+v\n", pad, c.Id, len(c.Points), len(c.Children), len(c.ConflictingContours), c.ParentCollision)
}
for _, ch := range c.Children {
ContourStats(ch, offset+2)
}
}
// ContourStatsWithCollisions generates writes to stdout stats about the contour and all children that have collisions
// Primarily used for debugging.
func ContourStatsWithCollisions(c *Contour, offset int) {
if len(c.Points) > 0 {
if len(c.ConflictingContours) > 0 {
pad := strings.Repeat(" ", offset)
fmt.Printf("%s%d : len %d : no kids %d : no col %d : col with parent %+v\n", pad, c.Id, len(c.Points), len(c.Children), len(c.ConflictingContours), c.ParentCollision)
}
}
for _, ch := range c.Children {
ContourStatsWithCollisions(ch, offset+2)
}
}