/
data_shape.go
98 lines (85 loc) · 1.95 KB
/
data_shape.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
package skyhook
import (
"encoding/json"
)
type ShapeMetadata struct {
CanvasDims [2]int `json:",omitempty"`
Categories []string `json:",omitempty"`
}
func (m ShapeMetadata) Update(other DataMetadata) DataMetadata {
other_ := other.(ShapeMetadata)
if other_.CanvasDims[0] > 0 {
m.CanvasDims = other_.CanvasDims
}
if len(other_.Categories) > 0 {
m.Categories = other_.Categories
}
return m
}
// Shape types.
type TypeOfShape string
const (
PointShape TypeOfShape = "point"
LineShape = "line"
PolyLineShape = "polyline"
BoxShape = "box"
PolygonShape = "polygon"
)
type Shape struct {
Type TypeOfShape
Points [][2]int
// Optional metadata
Category string `json:",omitempty"`
TrackID int `json:",omitempty"`
Metadata map[string]string `json:",omitempty"`
}
func (shp Shape) Bounds() [4]int {
sx := shp.Points[0][0]
sy := shp.Points[0][1]
ex := shp.Points[0][0]
ey := shp.Points[0][1]
for _, p := range shp.Points {
if p[0] < sx {
sx = p[0]
}
if p[0] > ex {
ex = p[0]
}
if p[1] < sy {
sy = p[1]
}
if p[1] > ey {
ey = p[1]
}
}
return [4]int{sx, sy, ex, ey}
}
type ShapeJsonSpec struct {}
func (s ShapeJsonSpec) DecodeMetadata(rawMetadata string) DataMetadata {
if rawMetadata == "" {
return ShapeMetadata{}
}
var m ShapeMetadata
JsonUnmarshal([]byte(rawMetadata), &m)
return m
}
func (s ShapeJsonSpec) DecodeData(bytes []byte) (interface{}, error) {
var data [][]Shape
err := json.Unmarshal(bytes, &data)
return data, err
}
func (s ShapeJsonSpec) GetEmptyMetadata() (metadata DataMetadata) {
return ShapeMetadata{}
}
func (s ShapeJsonSpec) Length(data interface{}) int {
return len(data.([][]Shape))
}
func (s ShapeJsonSpec) Append(data interface{}, more interface{}) interface{} {
return append(data.([][]Shape), more.([][]Shape)...)
}
func (s ShapeJsonSpec) Slice(data interface{}, i int, j int) interface{} {
return data.([][]Shape)[i:j]
}
func init() {
DataSpecs[ShapeType] = SequenceJsonDataImpl{ShapeJsonSpec{}}
}