This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
0acfd71
commit 4e009ba
Showing
4 changed files
with
291 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Package dataprocess manages code related to the data-saving process | ||
package dataprocess | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"os" | ||
|
||
pc "go.viam.com/rdk/pointcloud" | ||
) | ||
|
||
// WritePCDToFile encodes the pointcloud and then saves it to the passed filename. | ||
func WritePCDToFile(pointcloud pc.PointCloud, filename string) error { | ||
buf := new(bytes.Buffer) | ||
err := pc.ToPCD(pointcloud, buf, 1) | ||
if err != nil { | ||
return err | ||
} | ||
return WriteBytesToFile(buf.Bytes(), filename) | ||
} | ||
|
||
// WriteBytesToFile writes the passed bytes to the passed filename. | ||
func WriteBytesToFile(bytes []byte, filename string) error { | ||
//nolint:gosec | ||
f, err := os.Create(filename) | ||
if err != nil { | ||
return err | ||
} | ||
w := bufio.NewWriter(f) | ||
if _, err := w.Write(bytes); err != nil { | ||
return err | ||
} | ||
if err := w.Flush(); err != nil { | ||
return err | ||
} | ||
return f.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package dataprocess | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
pc "go.viam.com/rdk/pointcloud" | ||
"go.viam.com/test" | ||
) | ||
|
||
func TestWriteBytesToFile(t *testing.T) { | ||
t.Run("Write bytes to file", func(t *testing.T) { | ||
tempDir, err := os.MkdirTemp("", "*") | ||
defer os.RemoveAll(tempDir) | ||
test.That(t, err, test.ShouldBeNil) | ||
// Save a set of bytes to a file | ||
actualBytes := []byte{1, 5, 8} | ||
fileDest := tempDir + "test_bytes" | ||
err = WriteBytesToFile(actualBytes, fileDest) | ||
test.That(t, err, test.ShouldBeNil) | ||
// Test that the file was actually written | ||
readBytes, err := os.ReadFile(fileDest) | ||
test.That(t, err, test.ShouldBeNil) | ||
test.That(t, readBytes, test.ShouldResemble, actualBytes) | ||
}) | ||
} | ||
|
||
func TestWritePCDToFile(t *testing.T) { | ||
t.Run("Write PCD to file", func(t *testing.T) { | ||
tempDir, err := os.MkdirTemp("", "*") | ||
defer os.RemoveAll(tempDir) | ||
test.That(t, err, test.ShouldBeNil) | ||
fileDest := tempDir + "test_pcd.pcd" | ||
pointcloud := pc.New() | ||
err = WritePCDToFile(pointcloud, fileDest) | ||
test.That(t, err, test.ShouldBeNil) | ||
// Test that the file was actually written | ||
_, err = os.Stat(fileDest) | ||
test.That(t, err, test.ShouldBeNil) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.