Repository navigation
Expand file tree
/
Copy pathtag_family.go
More file actions
151 lines (139 loc) · 4.35 KB
/
Copy pathtag_family.go
File metadata and controls
151 lines (139 loc) · 4.35 KB
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
package main
/*
#include "apriltag/apriltag.h"
#include "apriltag/tag16h5.h"
#include "apriltag/tag25h9.h"
#include "apriltag/tag36h11.h"
#include "apriltag/tagCircle21h7.h"
#include "apriltag/tagCircle49h12.h"
#include "apriltag/tagCustom48h12.h"
#include "apriltag/tagStandard41h12.h"
#include "apriltag/tagStandard52h13.h"
#include "oldtags/tag36h10.h"
#cgo LDFLAGS: apriltag/libapriltag.a oldtags/liboldtags.a -lm
*/
import "C"
import (
"fmt"
"reflect"
"strings"
"unsafe"
)
type TagFamily struct {
Codes []uint64
NBits int
LocationX []int
LocationY []int
Inside []bool
Name string
Hamming int
TotalWidth int
WidthAtBorder int
ReversedBorder bool
}
func newTagFamily(tf *C.apriltag_family_t, name string) *TagFamily {
ncodes := int(tf.ncodes)
nbits := int(tf.nbits)
var codes []uint64
var bitX, bitY []int32
sliceHeader := (*reflect.SliceHeader)((unsafe.Pointer(&codes)))
sliceHeader.Cap = ncodes
sliceHeader.Len = ncodes
sliceHeader.Data = uintptr(unsafe.Pointer(tf.codes))
sliceHeaderX := (*reflect.SliceHeader)((unsafe.Pointer(&bitX)))
sliceHeaderX.Cap = nbits
sliceHeaderX.Len = nbits
sliceHeaderX.Data = uintptr(unsafe.Pointer(tf.bit_x))
sliceHeaderY := (*reflect.SliceHeader)((unsafe.Pointer(&bitY)))
sliceHeaderY.Cap = nbits
sliceHeaderY.Len = nbits
sliceHeaderY.Data = uintptr(unsafe.Pointer(tf.bit_y))
res := &TagFamily{
Codes: append([]uint64{}, codes...),
Name: name,
NBits: nbits,
TotalWidth: int(tf.total_width),
WidthAtBorder: int(tf.width_at_border),
ReversedBorder: bool(tf.reversed_border),
Hamming: int(tf.h),
LocationX: nil,
LocationY: nil,
Inside: nil,
}
for i := 0; i < res.NBits; i++ {
x := int(bitX[i])
y := int(bitY[i])
res.LocationX = append(res.LocationX, x)
res.LocationY = append(res.LocationY, y)
inside := true
if x < 0 || x >= res.WidthAtBorder {
inside = false
}
if y < 0 || y >= res.WidthAtBorder {
inside = false
}
res.Inside = append(res.Inside, inside)
}
return res
}
func (tf *TagFamily) CodeSize() int {
return tf.NBits
}
func (tf *TagFamily) SizeInPX() int {
return tf.TotalWidth
}
type cAprilTagFamily struct {
Constructor func() *C.apriltag_family_t
Destructor func(*C.apriltag_family_t)
}
var familyFactory map[string]cAprilTagFamily
func init() {
familyFactory = map[string]cAprilTagFamily{
"36h10": cAprilTagFamily{
Constructor: func() *C.apriltag_family_t { return C.tag36h10_create() },
Destructor: func(f *C.apriltag_family_t) { C.tag36h10_destroy(f) },
},
"36h11": cAprilTagFamily{
Constructor: func() *C.apriltag_family_t { return C.tag36h11_create() },
Destructor: func(f *C.apriltag_family_t) { C.tag36h11_destroy(f) },
},
"16h5": cAprilTagFamily{
Constructor: func() *C.apriltag_family_t { return C.tag16h5_create() },
Destructor: func(f *C.apriltag_family_t) { C.tag16h5_destroy(f) },
},
"25h9": cAprilTagFamily{
Constructor: func() *C.apriltag_family_t { return C.tag25h9_create() },
Destructor: func(f *C.apriltag_family_t) { C.tag25h9_destroy(f) },
},
"Circle21h7": cAprilTagFamily{
Constructor: func() *C.apriltag_family_t { return C.tagCircle21h7_create() },
Destructor: func(f *C.apriltag_family_t) { C.tagCircle21h7_destroy(f) },
},
"Circle49h12": cAprilTagFamily{
Constructor: func() *C.apriltag_family_t { return C.tagCircle49h12_create() },
Destructor: func(f *C.apriltag_family_t) { C.tagCircle49h12_destroy(f) },
},
"Custom48h12": cAprilTagFamily{
Constructor: func() *C.apriltag_family_t { return C.tagCustom48h12_create() },
Destructor: func(f *C.apriltag_family_t) { C.tagCustom48h12_destroy(f) },
},
"Standard41h12": cAprilTagFamily{
Constructor: func() *C.apriltag_family_t { return C.tagStandard41h12_create() },
Destructor: func(f *C.apriltag_family_t) { C.tagStandard41h12_destroy(f) },
},
"Standard52h13": cAprilTagFamily{
Constructor: func() *C.apriltag_family_t { return C.tagStandard52h13_create() },
Destructor: func(f *C.apriltag_family_t) { C.tagStandard52h13_destroy(f) },
},
}
}
func GetFamily(name string) (*TagFamily, error) {
ff, ok := familyFactory[name]
if ok == false {
return nil, fmt.Errorf("Unknown famnily '%s'", name)
}
name = strings.ToUpper(name)
tf := ff.Constructor()
defer ff.Destructor(tf)
return newTagFamily(tf, name), nil
}