~ubuntu-branches/ubuntu/wily/liblas/wily

« back to all changes in this revision

Viewing changes to test/unit/lasreader_iterator_test.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2009-10-02 12:36:21 UTC
  • Revision ID: james.westby@ubuntu.com-20091002123621-xrf0hhzxbwloga43
Tags: upstream-1.2.1
ImportĀ upstreamĀ versionĀ 1.2.1

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
#include <liblas/cstdint.hpp>
 
9
#include <liblas/iterator.hpp>
 
10
#include <liblas/laspoint.hpp>
 
11
#include <liblas/lasreader.hpp>
 
12
#include <liblas/detail/utility.hpp>
 
13
#include <tut/tut.hpp>
 
14
#include <algorithm>
 
15
#include <fstream>
 
16
#include <iterator>
 
17
#include <list>
 
18
#include "liblas_test.hpp"
 
19
#include "common.hpp"
 
20
using namespace liblas;
 
21
 
 
22
namespace tut
 
23
 
24
    struct lasreader_iterator_data
 
25
    {
 
26
        std::string file10_;
 
27
        std::ifstream ifs_;
 
28
        LASReader reader_;
 
29
 
 
30
        lasreader_iterator_data() :
 
31
            file10_(g_test_data_path + "//TO_core_last_clip.las"),
 
32
            ifs_(file10_.c_str(), std::ios::in | std::ios::binary),
 
33
            reader_(ifs_)
 
34
        {}
 
35
    };
 
36
 
 
37
    typedef test_group<lasreader_iterator_data> tg;
 
38
    typedef tg::object to;
 
39
 
 
40
    tg test_group_lasreader_iterator("liblas::lasreader_iterator");
 
41
 
 
42
    // Test default constructor
 
43
    template<>
 
44
    template<>
 
45
    void to::test<1>()
 
46
    {
 
47
        lasreader_iterator it;
 
48
    }
 
49
 
 
50
    // Test user-defined constructor
 
51
    template<>
 
52
    template<>
 
53
    void to::test<2>()
 
54
    {
 
55
        lasreader_iterator it(reader_);
 
56
    }
 
57
 
 
58
    // Test copy constructor with default initialized iterator
 
59
    template<>
 
60
    template<>
 
61
    void to::test<3>()
 
62
    {
 
63
        lasreader_iterator it1;
 
64
        lasreader_iterator it2(it1);
 
65
 
 
66
        ensure(it1 == it2);
 
67
    }
 
68
 
 
69
    // Test copy constructor with initialized iterator
 
70
    template<>
 
71
    template<>
 
72
    void to::test<4>()
 
73
    {
 
74
        lasreader_iterator it1(reader_);
 
75
        lasreader_iterator it2(it1);
 
76
 
 
77
        ensure(it1 == it2);
 
78
    }
 
79
 
 
80
    // Test assignment operator with default initialized iterator
 
81
    template<>
 
82
    template<>
 
83
    void to::test<5>()
 
84
    {
 
85
        lasreader_iterator it1;
 
86
        lasreader_iterator it2;
 
87
        it1 = it2;
 
88
 
 
89
        ensure(it1 == it2);
 
90
    }
 
91
 
 
92
    // Test assignment operator with initialized iterator
 
93
    template<>
 
94
    template<>
 
95
    void to::test<6>()
 
96
    {
 
97
        lasreader_iterator it1(reader_);
 
98
        lasreader_iterator it2;
 
99
        it1 = it2;
 
100
 
 
101
        ensure(it1 == it2);
 
102
    }
 
103
 
 
104
    // Test dereference operator
 
105
    template<>
 
106
    template<>
 
107
    void to::test<7>()
 
108
    {
 
109
        lasreader_iterator it(reader_);
 
110
 
 
111
        test_file10_point1(*it);
 
112
    }
 
113
 
 
114
    // Test pointer-to-member operator
 
115
    template<>
 
116
    template<>
 
117
    void to::test<8>()
 
118
    {
 
119
        lasreader_iterator it(reader_);
 
120
 
 
121
        // test 1st point data record 
 
122
        ensure_distance(it->GetX(), double(630262.30), 0.0001);
 
123
        ensure_distance(it->GetY(), double(4834500), 0.0001);
 
124
        ensure_distance(it->GetZ(), double(51.53), 0.0001);
 
125
        ensure_equals(it->GetIntensity(), 670);
 
126
        ensure_equals(it->GetClassification(), liblas::uint8_t(1));
 
127
        ensure_equals(it->GetScanAngleRank(), 0);
 
128
        ensure_equals(it->GetUserData(), 3);
 
129
        ensure_equals(it->GetScanFlags(), 9);
 
130
        ensure_distance(it->GetTime(), double(413665.23360000004), 0.0001);
 
131
    }
 
132
 
 
133
    // Test pre-increment operator
 
134
    template<>
 
135
    template<>
 
136
    void to::test<9>()
 
137
    {
 
138
        lasreader_iterator it(reader_); // move to 1st point
 
139
        ++it; // move to 2nd record
 
140
 
 
141
        test_file10_point2(*it);
 
142
    }
 
143
 
 
144
    // Test post-increment operator
 
145
    template<>
 
146
    template<>
 
147
    void to::test<10>()
 
148
    {
 
149
        lasreader_iterator it(reader_); // move to 1st point
 
150
        it++; // move to 2nd record
 
151
 
 
152
        test_file10_point2(*it);
 
153
    }
 
154
 
 
155
    // Test equal-to operator
 
156
    template<>
 
157
    template<>
 
158
    void to::test<11>()
 
159
    {
 
160
        lasreader_iterator it(reader_); // move to 1st point
 
161
        lasreader_iterator end;
 
162
 
 
163
        ensure_not(end == it);
 
164
    }
 
165
 
 
166
    // Test not-equal-to operator
 
167
    template<>
 
168
    template<>
 
169
    void to::test<12>()
 
170
    {
 
171
        lasreader_iterator it(reader_); // move to 1st point
 
172
        lasreader_iterator end;
 
173
 
 
174
        ensure(end != it);
 
175
    }
 
176
 
 
177
    // Test iteration
 
178
    template<>
 
179
    template<>
 
180
    void to::test<13>()
 
181
    {
 
182
        liblas::uint32_t const cnt = reader_.GetHeader().GetPointRecordsCount();
 
183
        lasreader_iterator it(reader_); // move to 1st point
 
184
        lasreader_iterator end;
 
185
 
 
186
        liblas::uint32_t s = 0;
 
187
        while (end != it)
 
188
        {
 
189
            s++;
 
190
            ++it;
 
191
        }
 
192
 
 
193
        ensure_equals(cnt, s);
 
194
    }
 
195
 
 
196
    // Test std::distance operation
 
197
    template<>
 
198
    template<>
 
199
    void to::test<14>()
 
200
    {
 
201
        liblas::uint32_t const cnt = reader_.GetHeader().GetPointRecordsCount();
 
202
        lasreader_iterator it(reader_); // move to 1st point
 
203
        lasreader_iterator end;
 
204
 
 
205
        lasreader_iterator::difference_type const d = std::distance(it, end);
 
206
        ensure_equals(d, cnt);
 
207
    }
 
208
 
 
209
    // Test std::distance operation
 
210
    template<>
 
211
    template<>
 
212
    void to::test<15>()
 
213
    {
 
214
        std::size_t a = std::distance(lasreader_iterator(reader_), lasreader_iterator());
 
215
 
 
216
        // Reader state is set to "past-the-end-of-file"
 
217
        // So, reset is needed
 
218
        reader_.Reset();
 
219
 
 
220
        std::size_t b = std::distance(lasreader_iterator(reader_), lasreader_iterator());
 
221
 
 
222
        ensure_equals(a, b);
 
223
    }
 
224
 
 
225
    // Test std::advance operation
 
226
    template<>
 
227
    template<>
 
228
    void to::test<16>()
 
229
    {
 
230
        lasreader_iterator it(reader_); // move to 1st point
 
231
 
 
232
        std::advance(it, 1); // move to 2nd record
 
233
        test_file10_point2(*it);
 
234
 
 
235
        std::advance(it, 2); // move to 4th record
 
236
        test_file10_point4(*it);
 
237
    }
 
238
 
 
239
    // Test std::copy algorithm
 
240
    template<>
 
241
    template<>
 
242
    void to::test<17>()
 
243
    {
 
244
        liblas::uint32_t const size = reader_.GetHeader().GetPointRecordsCount();
 
245
        lasreader_iterator it(reader_);
 
246
        lasreader_iterator end;
 
247
 
 
248
        typedef std::list<LASPoint> list_t;
 
249
        typedef std::back_insert_iterator<list_t> inserter_t;
 
250
        list_t cache;
 
251
 
 
252
        // Test copying LAS records to std::list based cache
 
253
        std::copy(it, end, inserter_t(cache));
 
254
        ensure_equals(cache.size(), size);
 
255
 
 
256
        // Test copied data
 
257
        list_t::const_iterator cit = cache.begin(); // 1st element
 
258
        std::advance(cit, 1); // move to 2nd element in cache
 
259
        test_file10_point2(*cit);
 
260
        std::advance(cit, 2); // move to 4th element in cache
 
261
        test_file10_point4(*cit);
 
262
    }
 
263
 
 
264
    // Test std::count algorithm
 
265
    template<>
 
266
    template<>
 
267
    void to::test<18>()
 
268
    {
 
269
        // Construct copy of 2nd point record from tested file
 
270
        LASPoint pt;
 
271
        pt.SetCoordinates(630282.45, 4834500, 51.63);
 
272
        pt.SetIntensity(350);
 
273
        pt.SetClassification(1);
 
274
        pt.SetScanAngleRank(0);
 
275
        pt.SetUserData(3);
 
276
        pt.SetScanFlags(9);
 
277
        pt.SetTime(413665.52880000003);
 
278
        ensure(pt.IsValid());
 
279
        test_file10_point2(pt);
 
280
 
 
281
        lasreader_iterator it(reader_);
 
282
        lasreader_iterator end;
 
283
 
 
284
        // Count records equal to given point object
 
285
        lasreader_iterator::difference_type const expected = 1;
 
286
        lasreader_iterator::difference_type n = std::count(it, end, pt);
 
287
        ensure_equals(n, expected);
 
288
    }
 
289
 
 
290
    // Test std::equal algorithm
 
291
    template<>
 
292
    template<>
 
293
    void to::test<19>()
 
294
    {
 
295
        std::ifstream ifs(file10_.c_str(), std::ios::in | std::ios::binary);
 
296
        LASReader reader(ifs);
 
297
 
 
298
        // Copy LAS records to std::list based cache
 
299
        typedef std::list<LASPoint> list_t;
 
300
        typedef std::back_insert_iterator<list_t> inserter_t;
 
301
        list_t cache;
 
302
        {
 
303
            lasreader_iterator it(reader);
 
304
            lasreader_iterator end;
 
305
            ensure(it != end);
 
306
 
 
307
            std::copy(it, end, inserter_t(cache));
 
308
            ensure_equals(cache.size(), reader.GetHeader().GetPointRecordsCount());
 
309
        }
 
310
 
 
311
        // Reset reader to the beginning of LAS file
 
312
        reader.Reset();
 
313
 
 
314
        // Compare LAS file with cache
 
315
        {
 
316
            lasreader_iterator it(reader);
 
317
            lasreader_iterator end;
 
318
            ensure(it != end);
 
319
 
 
320
            bool eq = std::equal(it, end, cache.begin());
 
321
            ensure(eq);
 
322
        }
 
323
    }
 
324
 
 
325
    // Test std::find algorithm
 
326
    template<>
 
327
    template<>
 
328
    void to::test<20>()
 
329
    {
 
330
        // Construct copy of 2nd point record from tested file
 
331
        LASPoint pt;
 
332
        pt.SetCoordinates(630282.45, 4834500, 51.63);
 
333
        pt.SetIntensity(350);
 
334
        pt.SetClassification(1);
 
335
        pt.SetScanAngleRank(0);
 
336
        pt.SetUserData(3);
 
337
        pt.SetScanFlags(9);
 
338
        pt.SetTime(413665.52880000003);
 
339
        ensure(pt.IsValid());
 
340
        test_file10_point2(pt);
 
341
 
 
342
        lasreader_iterator it(reader_);
 
343
        lasreader_iterator end;
 
344
 
 
345
        // find 2nd point data record
 
346
        lasreader_iterator fit;
 
347
        fit = std::find(it, end, pt);
 
348
        ensure(fit != end);
 
349
        test_file10_point2(*fit);
 
350
    }
 
351
 
 
352
    // Test std::find_if algorithm
 
353
    template<>
 
354
    template<>
 
355
    void to::test<21>()
 
356
    {
 
357
        lasreader_iterator it(reader_);
 
358
        lasreader_iterator end;
 
359
 
 
360
        // find 2nd point data record comparing XY coordinates
 
361
        lasreader_iterator fit;
 
362
        fit = std::find_if(it, end, is_xy(630282.45, 4834500, 0.0001));
 
363
        ensure(fit != end);
 
364
        test_file10_point2(*fit);
 
365
    }
 
366
 
 
367
    // Test std::for_each algorithm
 
368
    template<>
 
369
    template<>
 
370
    void to::test<22>()
 
371
    {
 
372
        lasreader_iterator it(reader_);
 
373
        lasreader_iterator end;
 
374
 
 
375
        typedef liblas::detail::Point<double> point_t;
 
376
        typedef liblas::detail::Extents<double> bbox_t;
 
377
 
 
378
        LASHeader const& h = reader_.GetHeader();
 
379
        bbox_t lasbbox(point_t(h.GetMinX(), h.GetMinY(), h.GetMinZ()),
 
380
                       point_t(h.GetMaxX(), h.GetMaxY(), h.GetMaxZ()));
 
381
 
 
382
        // Accumulate points extents to common bounding box
 
383
        bbox_t bbox;
 
384
        std::for_each(it, end, bbox_calculator(bbox));
 
385
 
 
386
        ensure(lasbbox == bbox);
 
387
    }
 
388
}