~ubuntu-branches/ubuntu/trusty/liblas/trusty-proposed

« back to all changes in this revision

Viewing changes to test/unit/writer_test.cpp

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2014-01-05 17:00:29 UTC
  • mfrom: (7.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140105170029-ddtp0j63x5jvck2u
Tags: 1.7.0+dfsg-2
Fixed missing linking of system boost component.
(closes: #733282)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id$
 
2
//
 
3
// (C) Copyright Mateusz Loskot 2008, mateusz@loskot.net
 
4
// Distributed under the BSD License
 
5
// (See accompanying file LICENSE.txt or copy at
 
6
// http://www.opensource.org/licenses/bsd-license.php)
 
7
//
 
8
 
 
9
#include <liblas/liblas.hpp>
 
10
#include <tut/tut.hpp>
 
11
#include <cstdio>
 
12
#include <bitset>
 
13
#include <fstream>
 
14
#include <string>
 
15
#include "liblas_test.hpp"
 
16
#include "common.hpp"
 
17
 
 
18
namespace tut
 
19
 
20
    struct laswriter_data
 
21
    {
 
22
        std::string tmpfile_;
 
23
        std::string file10_;
 
24
 
 
25
        laswriter_data() :
 
26
            tmpfile_(g_test_data_path + "//tmp.las"),
 
27
            file10_(g_test_data_path + "//TO_core_last_clip.las")
 
28
        {}
 
29
 
 
30
        ~laswriter_data()
 
31
        {
 
32
            // remove temporary file after each test case
 
33
            int const ret = std::remove(tmpfile_.c_str());
 
34
            if (0 != ret)
 
35
            {
 
36
                ; // ignore, file may not exist
 
37
            }
 
38
        }
 
39
    };
 
40
 
 
41
    typedef test_group<laswriter_data> tg;
 
42
    typedef tg::object to;
 
43
 
 
44
    tg test_group_laswriter("liblas::Writer");
 
45
 
 
46
    // Test user-declared constructor
 
47
    template<>
 
48
    template<>
 
49
    void to::test<1>()
 
50
    {
 
51
        // Create new LAS file using default header block
 
52
        {
 
53
            std::ofstream ofs;
 
54
            ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
 
55
 
 
56
            // LAS 1.2, Point Format 0
 
57
            liblas::Header header;
 
58
            liblas::Writer writer(ofs, header);
 
59
 
 
60
            ensure_equals(writer.GetHeader().GetVersionMinor(), 2);
 
61
 
 
62
            liblas::Header const& hdr_default = writer.GetHeader();
 
63
            test_default_header(hdr_default);
 
64
        }
 
65
 
 
66
        // Read previously created LAS file and check its header block
 
67
        {
 
68
            std::ifstream ifs;
 
69
            ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
 
70
            ensure(ifs.is_open());
 
71
 
 
72
            liblas::Reader reader(ifs);
 
73
 
 
74
            ensure_equals(reader.GetHeader().GetVersionMinor(), 2);
 
75
            
 
76
            liblas::Header const& hdr_default = reader.GetHeader();
 
77
            test_default_header(hdr_default);
 
78
        }
 
79
    }
 
80
 
 
81
    // Test WritePoint method
 
82
    template<>
 
83
    template<>
 
84
    void to::test<2>()
 
85
    {
 
86
        {
 
87
            std::ofstream ofs;
 
88
            ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
 
89
 
 
90
            // LAS 1.1, Point Format 0
 
91
            liblas::Header header;
 
92
            liblas::Writer writer(ofs, header);
 
93
 
 
94
            liblas::Point point;
 
95
 
 
96
            // Write 1st point
 
97
            point.SetCoordinates(10, 20, 30);
 
98
            point.SetIntensity(5);
 
99
            point.SetReturnNumber(1);
 
100
            point.SetNumberOfReturns(1);
 
101
            point.SetScanDirection(1);
 
102
            point.SetFlightLineEdge(1);
 
103
            point.SetClassification(7);
 
104
            point.SetScanAngleRank(90);
 
105
            point.SetUserData(0);
 
106
            point.SetPointSourceID(1);
 
107
 
 
108
            writer.WritePoint(point);
 
109
 
 
110
            // write 2nd point
 
111
            point.SetCoordinates(40, 50, 60);
 
112
            point.SetPointSourceID(2);
 
113
            writer.WritePoint(point);
 
114
 
 
115
            // write 3rd point
 
116
            point.SetCoordinates(70, 80, 90);
 
117
            point.SetPointSourceID(3);
 
118
            writer.WritePoint(point);
 
119
        }
 
120
 
 
121
        // Read previously create LAS file with 3 point records
 
122
        {
 
123
            std::ifstream ifs;
 
124
            ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
 
125
            ensure(ifs.is_open());
 
126
            liblas::Reader reader(ifs);
 
127
 
 
128
            liblas::Point point; // reusable cache
 
129
 
 
130
            // read 1st point
 
131
            reader.ReadNextPoint();
 
132
            point = reader.GetPoint();
 
133
            ensure_distance(point.GetX(), 10.0, 0.1);
 
134
            ensure_distance(point.GetY(), 20.0, 0.1);
 
135
            ensure_distance(point.GetZ(), 30.0, 0.1);
 
136
            ensure_equals(point.GetIntensity(), 5);
 
137
            ensure_equals(point.GetReturnNumber(), 1);
 
138
            ensure_equals(point.GetNumberOfReturns(), 1);
 
139
            ensure_equals(point.GetScanDirection(), 1);
 
140
            ensure_equals(point.GetFlightLineEdge(), 1);
 
141
            ensure_equals(point.GetScanAngleRank(), 90);
 
142
            ensure_equals(point.GetUserData(), 0);
 
143
            ensure_equals(point.GetPointSourceID(), 1);
 
144
 
 
145
            typedef liblas::Classification::bitset_type bitset_type;
 
146
            ensure_equals(bitset_type(point.GetClassification()), bitset_type(7));
 
147
 
 
148
            // read 2nd point
 
149
            reader.ReadNextPoint();
 
150
            point = reader.GetPoint();
 
151
            ensure_distance(point.GetX(), 40.0, 0.1);
 
152
            ensure_distance(point.GetY(), 50.0, 0.1);
 
153
            ensure_distance(point.GetZ(), 60.0, 0.1);
 
154
            ensure_equals(point.GetIntensity(), 5);
 
155
            ensure_equals(point.GetReturnNumber(), 1);
 
156
            ensure_equals(point.GetNumberOfReturns(), 1);
 
157
            ensure_equals(point.GetScanDirection(), 1);
 
158
            ensure_equals(point.GetFlightLineEdge(), 1);
 
159
            ensure_equals(point.GetScanAngleRank(), 90);
 
160
            ensure_equals(point.GetUserData(), 0);
 
161
            ensure_equals(point.GetPointSourceID(), 2);
 
162
 
 
163
            // read 3rd point
 
164
            reader.ReadNextPoint();
 
165
            point = reader.GetPoint();
 
166
            ensure_distance(point.GetX(), 70.0, 0.1);
 
167
            ensure_distance(point.GetY(), 80.0, 0.1);
 
168
            ensure_distance(point.GetZ(), 90.0, 0.1);
 
169
            ensure_equals(point.GetIntensity(), 5);
 
170
            ensure_equals(point.GetReturnNumber(), 1);
 
171
            ensure_equals(point.GetNumberOfReturns(), 1);
 
172
            ensure_equals(point.GetScanDirection(), 1);
 
173
            ensure_equals(point.GetFlightLineEdge(), 1);
 
174
            ensure_equals(point.GetScanAngleRank(), 90);
 
175
            ensure_equals(point.GetUserData(), 0);
 
176
            ensure_equals(point.GetPointSourceID(), 3);
 
177
 
 
178
            typedef liblas::Classification::bitset_type bitset_type;
 
179
            ensure_equals(bitset_type(point.GetClassification()), bitset_type(7));
 
180
        }
 
181
    }
 
182
 
 
183
    
 
184
    // Test WriteHeader method
 
185
    template<>
 
186
    template<>
 
187
    void to::test<3>()
 
188
    {
 
189
        {
 
190
            std::ofstream ofs;
 
191
            ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
 
192
 
 
193
            liblas::Header header;
 
194
            liblas::Writer writer(ofs, header);
 
195
 
 
196
            // test initially written header
 
197
            liblas::Header const& hdr_default = writer.GetHeader();
 
198
            test_default_header(hdr_default);
 
199
 
 
200
            // update some header data and overwrite header block
 
201
            header.SetReserved(1);
 
202
            header.SetFileSourceId(65535);
 
203
            header.SetSystemId("Unit Test libLAS System");
 
204
            header.SetSoftwareId("Unit Test libLAS Software");
 
205
            header.SetCreationDOY(100);
 
206
            header.SetCreationYear(2008);
 
207
            header.SetScale(1.123, 2.123, 3.123);
 
208
            header.SetOffset(4.321, 5.321, 6.321);
 
209
 
 
210
            writer.SetHeader(header);
 
211
            writer.WriteHeader();
 
212
            liblas::Header const& written_header = writer.GetHeader();
 
213
            ensure_equals("written_header fileSource id", written_header.GetFileSourceId(), 65535);
 
214
            ensure_equals("written_header SystemId", written_header.GetSystemId(), "Unit Test libLAS System");
 
215
            ensure_equals("written_header SoftwareId", written_header.GetSoftwareId(), "Unit Test libLAS Software");
 
216
            ensure_equals("written_header CreationDOY", written_header.GetCreationDOY(), 100);
 
217
            ensure_equals("written_header CreationYear", written_header.GetCreationYear(), 2008);
 
218
 
 
219
        }
 
220
 
 
221
        // read and check updated header block
 
222
        {
 
223
            std::ifstream ifs;
 
224
            ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
 
225
            ensure(ifs.is_open());
 
226
            liblas::Reader reader(ifs);
 
227
 
 
228
            liblas::Header const& header = reader.GetHeader();
 
229
            ensure_equals(header.GetReserved(), 1);
 
230
            ensure_equals(header.GetFileSourceId(), 65535);
 
231
            ensure_equals(header.GetSystemId(), std::string("Unit Test libLAS System"));
 
232
            ensure_equals(header.GetSoftwareId(), std::string("Unit Test libLAS Software"));
 
233
            ensure_equals(header.GetCreationDOY(), 100);
 
234
            ensure_equals(header.GetCreationYear(), 2008);
 
235
            ensure_equals(header.GetScaleX(), 1.123);
 
236
            ensure_equals(header.GetScaleY(), 2.123);
 
237
            ensure_equals(header.GetScaleZ(), 3.123);
 
238
            ensure_equals(header.GetOffsetX(), 4.321);
 
239
            ensure_equals(header.GetOffsetY(), 5.321);
 
240
            ensure_equals(header.GetOffsetZ(), 6.321);
 
241
        }
 
242
    }
 
243
 
 
244
    // Test appending to an existing file
 
245
    template<>
 
246
    template<>
 
247
    void to::test<4>()
 
248
    {
 
249
        std::ofstream ofs;
 
250
        ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
 
251
 
 
252
        liblas::Header header;
 
253
        header.SetDataOffset(759);//Toggle to see the differences
 
254
        header.SetDataFormatId( liblas::ePointFormat1 );
 
255
        {
 
256
            liblas::Writer testWriter( ofs, header);
 
257
        }
 
258
        
 
259
        ofs.close();
 
260
        
 
261
        ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
 
262
        {
 
263
            
 
264
            liblas::Writer test2Writer( ofs, header);
 
265
 
 
266
            size_t count = 500;
 
267
            for ( size_t i = 0; i < count ; i++ )
 
268
            {
 
269
                liblas::Point point;
 
270
                point.SetCoordinates( 10.0 + i, 20.0 + i, 30.0 + i );
 
271
                test2Writer.WritePoint( point );
 
272
            }
 
273
        }
 
274
        
 
275
        ofs.close();
 
276
        
 
277
        std::ifstream ifs;
 
278
        ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
 
279
        liblas::Reader reader(ifs);
 
280
        
 
281
        liblas::Header const& h = reader.GetHeader();
 
282
        ensure_equals("Appended point count does not match", h.GetPointRecordsCount(), static_cast<boost::uint32_t>(500));
 
283
 
 
284
    }
 
285
 
 
286
 
 
287
    // Test padding application for 1.0 files
 
288
    template<>
 
289
    template<>
 
290
    void to::test<5>()
 
291
    {
 
292
        {            
 
293
            std::ofstream ofs;
 
294
            ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
 
295
 
 
296
            liblas::Header header;
 
297
            header.SetDataOffset(227);
 
298
            header.SetDataFormatId( liblas::ePointFormat0 );
 
299
            header.SetVersionMinor(0);
 
300
            liblas::Writer testWriter( ofs, header);
 
301
            ofs.close();
 
302
        }
 
303
        
 
304
        {
 
305
            std::ifstream ifs;
 
306
            ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
 
307
            liblas::Reader reader( ifs);
 
308
            liblas::Header const& h = reader.GetHeader();
 
309
            ensure_equals("DataOffset is not expected value", h.GetDataOffset(), static_cast<boost::uint32_t>(229));
 
310
 
 
311
        }
 
312
 
 
313
 
 
314
    }
 
315
 
 
316
    // Test using a bogus record count in the header
 
317
    template<>
 
318
    template<>
 
319
    void to::test<6>()
 
320
    {
 
321
        size_t count = 10;
 
322
        
 
323
        // Create new LAS file using default header block
 
324
        {
 
325
            std::ofstream ofs;
 
326
            ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
 
327
 
 
328
            // LAS 1.2, Point Format 0
 
329
            liblas::Header header;
 
330
            header.SetPointRecordsCount(12);
 
331
            liblas::Writer writer(ofs, header);
 
332
            
 
333
            for ( size_t i = 0; i < count ; i++ )
 
334
            {
 
335
                liblas::Point point;
 
336
                point.SetCoordinates( 10.0 + i, 20.0 + i, 30.0 + i );
 
337
                writer.WritePoint( point );
 
338
            }
 
339
 
 
340
        }
 
341
 
 
342
        // Read previously created LAS file and check its header block
 
343
        {
 
344
            std::ifstream ifs;
 
345
            ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
 
346
            ensure(ifs.is_open());
 
347
 
 
348
            liblas::Reader reader(ifs);
 
349
 
 
350
            ensure_equals(reader.GetHeader().GetPointRecordsCount(), count);
 
351
 
 
352
 
 
353
        }
 
354
    }
 
355
 
 
356
 
 
357
}
 
358