-
Notifications
You must be signed in to change notification settings - Fork 109
/
object.go
62 lines (54 loc) · 1.78 KB
/
object.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
package vision
import (
"errors"
"math"
commonpb "go.viam.com/api/common/v1"
pc "go.viam.com/rdk/pointcloud"
"go.viam.com/rdk/spatialmath"
)
// Object extends PointCloud with respective metadata, like the center coordinate.
// NOTE(bh):Can potentially add category or pose information to this struct.
type Object struct {
pc.PointCloud
Geometry spatialmath.Geometry
}
// NewObject creates a new vision.Object from a point cloud with an empty label.
func NewObject(cloud pc.PointCloud) (*Object, error) {
return NewObjectWithLabel(cloud, "", nil)
}
// NewObjectWithLabel creates a new vision.Object from a point cloud with the given label.
func NewObjectWithLabel(cloud pc.PointCloud, label string, geometry *commonpb.Geometry) (*Object, error) {
if cloud == nil {
return NewEmptyObject(), nil
}
if geometry == nil {
box, err := pc.BoundingBoxFromPointCloudWithLabel(cloud, label)
if err != nil {
return nil, err
}
return &Object{cloud, box}, nil
}
if label != "" { // will override geometry proto label with given label (unless empty)
geometry.Label = label
}
geom, err := spatialmath.NewGeometryFromProto(geometry)
if err != nil {
return nil, err
}
return &Object{PointCloud: cloud, Geometry: geom}, nil
}
// NewEmptyObject creates a new empty point cloud with metadata.
func NewEmptyObject() *Object {
cloud := pc.New()
return &Object{PointCloud: cloud}
}
// Distance calculates and returns the distance from the center point of the object to the origin.
func (o *Object) Distance() (float64, error) {
if o.Geometry == nil {
return -1, errors.New("no geometry object defined for distance formula to be applied")
}
point := o.Geometry.Pose().Point()
dist := math.Pow(point.X, 2) + math.Pow(point.Y, 2) + math.Pow(point.Z, 2)
dist = math.Sqrt(dist)
return dist, nil
}