Skip to content

Commit 231ba02

Browse files
authored
Point cloud to points (#5248)
1 parent 9efa915 commit 231ba02

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

pointcloud/pointcloud.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ func CloudCentroid(pc PointCloud) r3.Vector {
170170
}
171171
}
172172

173+
// CloudToPoints converts a point cloud to a list of points (a list of vectors).
174+
func CloudToPoints(pc PointCloud) []r3.Vector {
175+
points := make([]r3.Vector, 0, pc.Size())
176+
pc.Iterate(0, 0, func(point r3.Vector, _ Data) bool {
177+
points = append(points, point)
178+
return true // Keep going through all points
179+
})
180+
return points
181+
}
182+
173183
// CloudMatrixCol is a type that represents the columns of a CloudMatrix.
174184
type CloudMatrixCol int
175185

pointcloud/pointcloud_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,35 @@ func TestPointCloudCentroid(t *testing.T) {
113113
test.That(t, CloudCentroid(pc), test.ShouldResemble, r3.Vector{20, 200, 2000})
114114
}
115115

116+
func TestPointCloudPoints(t *testing.T) {
117+
var data Data
118+
pc := NewBasicPointCloud(3)
119+
test.That(t, pc.Set(r3.Vector{1, 2, 3}, data), test.ShouldBeNil)
120+
test.That(t, pc.Set(r3.Vector{4, 5, 6}, data), test.ShouldBeNil)
121+
test.That(t, pc.Set(r3.Vector{7, 8, 9}, data), test.ShouldBeNil)
122+
points := CloudToPoints(pc)
123+
test.That(t, len(points), test.ShouldEqual, 3)
124+
// The points can come in an unexpected order. So, just check that the 3 points we expect are
125+
// in there somewhere.
126+
has1 := false
127+
has2 := false
128+
has3 := false
129+
for _, point := range points {
130+
if point.ApproxEqual(r3.Vector{1, 2, 3}) {
131+
has1 = true
132+
}
133+
if point.ApproxEqual(r3.Vector{4, 5, 6}) {
134+
has2 = true
135+
}
136+
if point.ApproxEqual(r3.Vector{7, 8, 9}) {
137+
has3 = true
138+
}
139+
}
140+
test.That(t, has1, test.ShouldBeTrue)
141+
test.That(t, has2, test.ShouldBeTrue)
142+
test.That(t, has3, test.ShouldBeTrue)
143+
}
144+
116145
func TestPointCloudMatrix(t *testing.T) {
117146
pc := NewBasicPointCloud(0)
118147

0 commit comments

Comments
 (0)