1 /******************************************************************************
2  * Copyright (c) 2019, Helix Re Inc.
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 Helix Re Inc. 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 "Support.hpp"
36 #include <pdal/pdal_test_main.hpp>
37 #include <stdio.h>
38 
39 #include "io/LasReader.hpp"
40 #include "plugins/e57/io/E57Reader.hpp"
41 #include "plugins/e57/io/E57Writer.hpp"
42 #include "plugins/e57/io/Utils.hpp"
43 
44 namespace pdal
45 {
46 
TEST(E57Writer,testCtr)47 TEST(E57Writer, testCtr)
48 {
49     Options ops;
50     std::string outfile = Support::datapath("e57/test.e57");
51     ops.add("filename", outfile);
52     E57Writer writer;
53     writer.setOptions(ops);
54     PointTable table;
55     writer.prepare(table);
56     remove(outfile.c_str());
57 }
58 
writertest_readE57(std::string filename,PointTableRef table)59 PointViewSet writertest_readE57(std::string filename, PointTableRef table)
60 {
61     Options ops;
62     ops.add("filename", filename);
63     E57Reader reader;
64     reader.setOptions(ops);
65     reader.prepare(table);
66     return reader.execute(table);
67 }
68 
TEST(E57Writer,testWrite)69 TEST(E57Writer, testWrite)
70 {
71     std::string outfile(Support::datapath("e57/test.e57"));
72     std::string infile(Support::datapath("e57/A4.e57"));
73 
74     remove(outfile.c_str());
75 
76     E57Reader r;
77     Options ro;
78 
79     ro.add("filename", infile);
80     r.setOptions(ro);
81 
82     {
83         E57Writer w;
84         Options wo;
85 
86         wo.add("filename", outfile);
87         w.setOptions(wo);
88         w.setInput(r);
89 
90         PointTable t;
91 
92         w.prepare(t);
93         w.execute(t);
94     }
95     PointTable tablein;
96     auto viewin = writertest_readE57(infile, tablein);
97     auto cloudin = *viewin.begin();
98     PointTable tableout;
99     auto viewout = writertest_readE57(outfile, tableout);
100     auto cloudout = *viewout.begin();
101 
102     auto expectedDimensions = {Dimension::Id::X,        Dimension::Id::Y,
103                                Dimension::Id::Z,        Dimension::Id::Red,
104                                Dimension::Id::Green,    Dimension::Id::Blue,
105                                Dimension::Id::Intensity
106                               };
107     for (point_count_t i = 0; i < cloudout->size(); i++)
108     {
109         auto ptB = cloudin->point(i);
110         auto pt = cloudout->point(i);
111         for (auto& dim : expectedDimensions)
112         {
113             ASSERT_TRUE(pt.hasDim(dim));
114             ASSERT_FLOAT_EQ(pt.getFieldAs<float>(dim),
115                             ptB.getFieldAs<float>(dim));
116         }
117     }
118 
119     remove(outfile.c_str());
120 }
121 
writerTest_testColorRanges(pdal::Reader * r,std::string infile,int min,int max)122 void writerTest_testColorRanges(pdal::Reader* r, std::string infile, int min, int max)
123 {
124     std::string outfile(Support::datapath("e57/test.e57"));
125     remove(outfile.c_str());
126 
127     Options ro;
128 
129     ro.add("filename", infile);
130     r->setOptions(ro);
131 
132     {
133         E57Writer w;
134         Options wo;
135 
136         wo.add("filename", outfile);
137         w.setOptions(wo);
138         w.setInput(*r);
139 
140         PointTable t;
141 
142         w.prepare(t);
143         w.execute(t);
144     }
145     e57::ImageFile imf(outfile, "r");
146 
147     e57::VectorNode data3D(imf.root().get("/data3D"));
148     auto colorLimits = (e57::StructureNode)((e57::StructureNode)data3D.get(0))
149                        .get("colorLimits");
150     std::vector<std::string> minDims{"colorRedMinimum", "colorGreenMinimum",
151                                      "colorBlueMinimum"};
152     std::vector<std::string> maxDims{"colorRedMaximum", "colorGreenMaximum",
153                                      "colorBlueMaximum"};
154 
155     for (auto& dim : minDims)
156     {
157         ASSERT_EQ(((e57::IntegerNode)colorLimits.get(dim)).value(), min);
158     }
159 
160     for (auto& dim : maxDims)
161     {
162         ASSERT_EQ(((e57::IntegerNode)colorLimits.get(dim)).value(), max);
163     }
164 
165     remove(outfile.c_str());
166 }
167 
TEST(E57Writer,testWriteRanges)168 TEST(E57Writer, testWriteRanges)
169 {
170     writerTest_testColorRanges(new LasReader(), Support::datapath("las/autzen_trim.las"), 0, 255);
171     writerTest_testColorRanges(new E57Reader(), Support::datapath("e57/A4.e57"), 0, 65535);
172 }
173 
TEST(E57Writer,testExtraDims)174 TEST(E57Writer, testExtraDims)
175 {
176     std::string infile = Support::datapath("las/autzen_trim.las");
177     std::string outfile(Support::datapath("e57/test.e57"));
178     remove(outfile.c_str());
179 
180     Options ro;
181 
182     LasReader r;
183     ro.add("filename", infile);
184     r.setOptions(ro);
185 
186     {
187         E57Writer w;
188         Options wo;
189 
190         wo.add("filename", outfile);
191         wo.add("extra_dims", "PointSourceId=int,testDim=double");
192         w.setOptions(wo);
193         w.setInput(r);
194 
195         PointTable t;
196 
197         w.prepare(t);
198         w.execute(t);
199     }
200     e57::ImageFile imf(outfile, "r");
201 
202     e57::VectorNode data3D(imf.root().get("/data3D"));
203 
204     // Dimension which is present in input pointcloud
205     auto limits = (e57::StructureNode)((e57::StructureNode)data3D.get(0))
206                   .get("PointSourceIdLimits");
207     ASSERT_EQ(((e57::FloatNode)limits.get("PointSourceIdMinimum")).value(), 0);
208     ASSERT_EQ(((e57::FloatNode)limits.get("PointSourceIdMaximum")).value(), 7326);
209 
210     // Dimension which is not present in input point cloud.
211     // This dimension should not be there in output E57.
212     ASSERT_THROW((e57::StructureNode)((e57::StructureNode)data3D.get(0))
213                  .get("testDimLimits"), e57::E57Exception);
214 
215     // Classification dimension, This will be written if available in input otherwise ignored. Not configurable through extra_dims.
216     limits = (e57::StructureNode)((e57::StructureNode)data3D.get(0))
217              .get("classificationLimits");
218     ASSERT_EQ(((e57::IntegerNode)limits.get("classificationMinimum")).value(), 0);
219     ASSERT_EQ(((e57::IntegerNode)limits.get("classificationMaximum")).value(), 255);
220 
221     remove(outfile.c_str());
222 }
223 } // namespace pdal
224