1
|
|
|
import os |
2
|
|
|
from tempfile import NamedTemporaryFile |
3
|
|
|
|
4
|
|
|
import pcl |
5
|
|
|
import numpy as np |
6
|
|
|
from patty import utils |
7
|
|
|
|
8
|
|
|
from numpy.testing import assert_array_almost_equal |
9
|
|
|
from nose.tools import assert_equal, assert_raises |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
def _compare( pcA, pcB ): |
13
|
|
|
''' compare two pointclouds point-by-point''' |
14
|
|
|
|
15
|
|
|
pcA_arr = np.asarray(pcA) |
16
|
|
|
pcB_arr = np.asarray(pcB) |
17
|
|
|
|
18
|
|
|
# dont use set_srs function, they will be tested later |
19
|
|
|
if hasattr(pcA, 'offset' ): |
20
|
|
|
pcA_arr += pcA.offset |
21
|
|
|
if hasattr(pcB, 'offset' ): |
22
|
|
|
pcB_arr += pcB.offset |
23
|
|
|
|
24
|
|
|
assert_array_almost_equal(pcA_arr, pcB_arr, 2, |
25
|
|
|
"Written/read point clouds are different!") |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
def test_read_write(): |
29
|
|
|
''' Test read and write LAS files functionality''' |
30
|
|
|
filename = './testIO.las' |
31
|
|
|
|
32
|
|
|
# make and save a pointcloud |
33
|
|
|
pc1 = pcl.PointCloud(10) |
34
|
|
|
pc1_arr = np.asarray(pc1) |
35
|
|
|
pc1_arr[:] = np.random.randn(*pc1_arr.shape) |
36
|
|
|
utils.save(pc1, filename) |
37
|
|
|
|
38
|
|
|
# reload it |
39
|
|
|
pc2 = utils.load(filename) |
40
|
|
|
|
41
|
|
|
_compare( pc1, pc2 ) |
42
|
|
|
|
43
|
|
|
os.remove(filename) |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def test_auto_file_format(): |
47
|
|
|
"""Test saving and loading pointclouds via the pcl loader""" |
48
|
|
|
|
49
|
|
|
# make and save a pointcloud |
50
|
|
|
pc = pcl.PointCloud(10) |
51
|
|
|
pc_arr = np.asarray(pc) |
52
|
|
|
pc_arr[:] = np.random.randn(*pc_arr.shape) |
53
|
|
|
|
54
|
|
|
with NamedTemporaryFile(suffix='.ply') as f: |
55
|
|
|
utils.save(pc, f.name) |
56
|
|
|
pc2 = utils.load(f.name) |
57
|
|
|
_compare( pc, pc2 ) |
58
|
|
|
|
59
|
|
|
with NamedTemporaryFile(suffix='.pcd') as f: |
60
|
|
|
utils.save(pc, f.name) |
61
|
|
|
pc2 = utils.load(f.name) |
62
|
|
|
_compare( pc, pc2 ) |
63
|
|
|
|
64
|
|
|
with NamedTemporaryFile(suffix='.las') as f: |
65
|
|
|
utils.save(pc, f.name, format="PLY") |
66
|
|
|
pc2 = utils.load(f.name, format="PLY") |
67
|
|
|
_compare( pc, pc2 ) |
68
|
|
|
|
69
|
|
|
with NamedTemporaryFile(suffix='.las') as f: |
70
|
|
|
utils.save(pc, f.name, format="PCD") |
71
|
|
|
pc2 = utils.load(f.name, format="PCD") |
72
|
|
|
_compare( pc, pc2 ) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
def test_downsample_random(): |
76
|
|
|
pc = pcl.PointCloud(10) |
77
|
|
|
a = np.asarray(pc) |
78
|
|
|
a[:] = np.random.randn(*a.shape) |
79
|
|
|
|
80
|
|
|
assert_raises(ValueError, utils.downsample_random, pc, 0) |
81
|
|
|
assert_raises(ValueError, utils.downsample_random, pc, 2) |
82
|
|
|
|
83
|
|
|
assert_equal(len(utils.downsample_random(pc, .39)), 4) |
84
|
|
|
|