1 /******************************************************************************
2 * Copyright (c) 2014, Howard Butler (howard@hobu.co)
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in
14 *       the documentation and/or other materials provided
15 *       with the distribution.
16 *     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
17 *       names of its contributors may be used to endorse or promote
18 *       products derived from this software without specific prior
19 *       written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34 
35 #include <pdal/pdal_test_main.hpp>
36 
37 #include <iostream>
38 #include <string>
39 #include <random>
40 
41 #include "Support.hpp"
42 #include <pdal/Options.hpp>
43 #include <pdal/PointView.hpp>
44 #include <pdal/compression/LazPerfCompression.hpp>
45 #include <io/LasReader.hpp>
46 
47 using namespace pdal;
48 
getBytes(PointViewPtr view)49 std::vector<char> getBytes(PointViewPtr view)
50 {
51     std::vector<char> bytes(view->pointSize() * view->size());
52     DimTypeList dimTypes = view->dimTypes();
53 
54     char *p = bytes.data();
55     for (PointId idx = 0; idx < view->size(); ++idx)
56     {
57         view->getPackedPoint(dimTypes, idx, p);
58         p += view->pointSize();
59     }
60     return bytes;
61 }
62 
63 
TEST(Compression,simple)64 TEST(Compression, simple)
65 {
66     const std::string file(Support::datapath("las/1.2-with-color.las"));
67 
68     const pdal::Option opt_filename("filename", file);
69     pdal::Options opts;
70     opts.add(opt_filename);
71 
72     LasReader reader;
73     reader.setOptions(opts);
74 
75     PointTable table;
76     PointLayoutPtr layout(table.layout());
77 
78     reader.prepare(table);
79     PointViewSet viewSet = reader.execute(table);
80     PointViewPtr view = *viewSet.begin();
81 
82     EXPECT_EQ(layout->pointSize(), 52U);
83 
84     std::vector<unsigned char> rawBuf;
85 
86     DimTypeList dimTypes = layout->dimTypes();
87     auto cb = [&rawBuf](char *buf, size_t bufsize)
88     {
89         unsigned char *ubuf = reinterpret_cast<unsigned char *>(buf);
90         rawBuf.insert(rawBuf.end(), ubuf, ubuf + bufsize);
91     };
92 
93     LazPerfCompressor compressor(cb, dimTypes);
94 
95     std::vector<char> tmpbuf(layout->pointSize());
96     for (PointId idx = 0; idx < view->size(); ++idx)
97     {
98         view->getPackedPoint(dimTypes, idx, tmpbuf.data());
99         compressor.compress(tmpbuf.data(), layout->pointSize());
100     }
101     compressor.done();
102 
103     EXPECT_EQ(view->size() * layout->pointSize(), (size_t)55380);
104     EXPECT_EQ(rawBuf.size(), (size_t)30945);
105 
106     PointViewPtr otherView(new PointView(table));
107     PointId nextId(0);
108     auto cb2 = [&otherView, &dimTypes, &nextId](char *buf, size_t bufsize)
109     {
110 
111         otherView->setPackedPoint(dimTypes, nextId, buf);
112         nextId++;
113     };
114 
115     LazPerfDecompressor(cb2, dimTypes, view->size()).
116         decompress(reinterpret_cast<const char *>(rawBuf.data()),
117             rawBuf.size());
118 
119     EXPECT_EQ(otherView->size(), 1065U);
120     EXPECT_EQ(getBytes(otherView).size(), (size_t)(52 * 1065));
121 
122     uint16_t r = otherView->getFieldAs<uint16_t>(Dimension::Id::Red, 10);
123     EXPECT_EQ(r, 64U);
124     int32_t x = otherView->getFieldAs<int32_t>(Dimension::Id::X, 10);
125     EXPECT_EQ(x, 636038);
126     double xd = otherView->getFieldAs<double>(Dimension::Id::X, 10);
127     EXPECT_FLOAT_EQ(xd, 636037.53);
128     int32_t y = otherView->getFieldAs<int32_t>(Dimension::Id::Y, 10);
129     EXPECT_EQ(y, 849338);
130 }
131 
132 
TEST(Compression,types)133 TEST(Compression, types)
134 {
135     using namespace Dimension;
136     Type types[] = {
137         Type::Unsigned8, Type::Unsigned16, Type::Unsigned32, Type::Unsigned64,
138         Type::Signed8, Type::Signed16, Type::Signed32, Type::Signed64,
139         Type::Float, Type::Double
140     };
141     // Size is 42.
142 
143     std::default_random_engine generator;
144     std::uniform_int_distribution<int> dist((std::numeric_limits<int>::min)());
145     char pts[3][42];
146 
147     // Fill three "points" with some random data.
148     char *c = &pts[0][0];
149     for (size_t i = 0; i < 3 * 42; ++i)
150     {
151         int v = dist(generator);
152         memcpy(c++, &v, sizeof(char));
153     }
154 
155     DimTypeList dimTypes;
156     for (auto ti = std::begin(types); ti != std::end(types); ++ti)
157         dimTypes.push_back(DimType(Dimension::Id::Unknown, *ti));
158 
159     std::vector<unsigned char> rawBuf;
160     auto cb = [&rawBuf](char *buf, size_t bufsize)
161     {
162         unsigned char *ubuf = reinterpret_cast<unsigned char *>(buf);
163         rawBuf.insert(rawBuf.begin(), ubuf, ubuf + bufsize);
164     };
165 
166     LazPerfCompressor compressor(cb, dimTypes);
167     for (size_t i = 0; i < 50; i++)
168     {
169         compressor.compress(pts[0], 42);
170         compressor.compress(pts[1], 42);
171         compressor.compress(pts[2], 42);
172     }
173     compressor.done();
174 
175     char oPts[3][42];
176     PointId id = 0;
177     auto cb2 = [&pts, &oPts, &id](char *buf, size_t bufsize)
178     {
179         memcpy(oPts[id++], buf, bufsize);
180         if (id == 3)
181         {
182             EXPECT_EQ(memcmp(pts[0], oPts[0], 42), 0);
183             EXPECT_EQ(memcmp(pts[0], oPts[0], 42), 0);
184             EXPECT_EQ(memcmp(pts[2], oPts[2], 42), 0);
185             memset(oPts[0], 0, 42);
186             memset(oPts[1], 0, 42);
187             memset(oPts[2], 0, 42);
188             id = 0;
189         }
190     };
191     LazPerfDecompressor(cb2, dimTypes, 50 * 3).
192            decompress(reinterpret_cast<const char *>(rawBuf.data()),
193                        rawBuf.size());
194 }
195 
196