-
Notifications
You must be signed in to change notification settings - Fork 110
/
frame_json.go
133 lines (116 loc) · 3.35 KB
/
frame_json.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
package referenceframe
import (
"encoding/json"
"fmt"
"github.com/golang/geo/r3"
"github.com/mitchellh/mapstructure"
spatial "go.viam.com/rdk/spatialmath"
"go.viam.com/rdk/utils"
)
// FrameMapConfig represents the format for configuring a Frame object.
type FrameMapConfig map[string]interface{}
// UnmarshalFrameJSON deserialized json into a reference referenceframe.
func UnmarshalFrameJSON(data []byte) (Frame, error) {
config := FrameMapConfig{}
err := json.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return config.ParseConfig()
}
// ParseConfig converts a FrameMapConfig to a Frame object.
func (config FrameMapConfig) ParseConfig() (Frame, error) {
var err error
switch config["type"] {
case "static":
f := staticFrame{}
var ok bool
f.name, ok = config["name"].(string)
if !ok {
return nil, utils.NewUnexpectedTypeError(f.name, config["name"])
}
pose, ok := config["transform"].(map[string]interface{})
if !ok {
return nil, utils.NewUnexpectedTypeError(pose, config["transform"])
}
f.transform, err = decodePose(pose)
if err != nil {
return nil, fmt.Errorf("error decoding transform (%v) %w", config["transform"], err)
}
return &f, nil
case "translational":
f := translationalFrame{}
var ok bool
f.name, ok = config["name"].(string)
if !ok {
return nil, utils.NewUnexpectedTypeError(f.name, config["name"])
}
err := mapstructure.Decode(config["transAxis"], &f.transAxis)
if err != nil {
return nil, err
}
err = mapstructure.Decode(config["limit"], &f.limit)
if err != nil {
return nil, err
}
return &f, nil
case "rotational":
f := rotationalFrame{}
var ok bool
f.name, ok = config["name"].(string)
if !ok {
return nil, utils.NewUnexpectedTypeError(f.name, config["name"])
}
rotAxis, ok := config["rotAxis"].(map[string]interface{})
if !ok {
return nil, utils.NewUnexpectedTypeError(rotAxis, config["rotAxis"])
}
f.rotAxis.X, ok = rotAxis["X"].(float64)
if !ok {
return nil, utils.NewUnexpectedTypeError(f.rotAxis.X, rotAxis["X"])
}
f.rotAxis.Y, ok = rotAxis["Y"].(float64)
if !ok {
return nil, utils.NewUnexpectedTypeError(f.rotAxis.Y, rotAxis["Y"])
}
f.rotAxis.Z, ok = rotAxis["Z"].(float64)
if !ok {
return nil, utils.NewUnexpectedTypeError(f.rotAxis.Z, rotAxis["Z"])
}
err = mapstructure.Decode(config["limit"], &f.limit)
if err != nil {
return nil, err
}
return &f, nil
default:
return nil, fmt.Errorf("no frame type: [%v]", config["type"])
}
}
func decodePose(config FrameMapConfig) (spatial.Pose, error) {
var point r3.Vector
err := mapstructure.Decode(config["point"], &point)
if err != nil {
return nil, err
}
orientationMap, ok := config["orientation"].(map[string]interface{})
if !ok {
return nil, utils.NewUnexpectedTypeError(orientationMap, config["orientation"])
}
oType, ok := orientationMap["type"].(string)
if !ok {
return nil, utils.NewUnexpectedTypeError(oType, orientationMap["type"])
}
oValue, ok := orientationMap["value"].(map[string]interface{})
if !ok {
return nil, utils.NewUnexpectedTypeError(oValue, orientationMap["value"])
}
jsonValue, err := json.Marshal(oValue)
if err != nil {
return nil, err
}
orientation, err := (&spatial.OrientationConfig{oType, jsonValue}).ParseConfig()
if err != nil {
return nil, err
}
return spatial.NewPoseFromOrientation(point, orientation), nil
}