utility.hpp 11.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/******************************************************************************
 * $Id: utility.hpp 883 2008-09-26 15:23:11Z hobu $
 *
 * Project:  libLAS - http://liblas.org - A BSD library for LAS format data.
 * Purpose:  A grab bucket 'o fun for C++ libLAS 
 * Author:   Mateusz Loskot, mateusz@loskot.net
 *
 ******************************************************************************
 * Copyright (c) 2008, Mateusz Loskot
 *
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following 
 * conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright 
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in 
 *       the documentation and/or other materials provided 
 *       with the distribution.
 *     * Neither the name of the Martin Isenburg or Iowa Department 
 *       of Natural Resources nor the names of its contributors may be 
 *       used to endorse or promote products derived from this software 
 *       without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef LIBLAS_DETAIL_UTILITY_HPP_INCLUDED
#define LIBLAS_DETAIL_UTILITY_HPP_INCLUDED

#include <liblas/cstdint.hpp>
#include <liblas/detail/endian.hpp>
#include <algorithm>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <iosfwd>
#include <cstring>
#include <cassert>

//
// Private macros definition
//

#ifndef UNREFERENCED_PARAMETER
#define UNREFERENCED_PARAMETER(param) ((void)param)
#endif

//
// Private utilities for internal use in libLAS
//

namespace liblas { namespace detail {

/// Simple RAII wrapper.
/// It's dedicated to use with types associated with custom deleter,
/// opaque pointers and C API objects.
template <typename T>
class raii_wrapper
{
    typedef void(*deleter_type)(T* p);

public:

    raii_wrapper(T* p, deleter_type d)
        : p_(p), del_(d)
    {
        assert(0 != p_);
        assert(0 != del_);
    }

    ~raii_wrapper()
    {
        do_delete(p_);
    }

    void reset(T* p)
    {
        do_delete(p_);
        p_= p;
    }

    T* get() const
    {
        return p_;
    }

    void swap(raii_wrapper& other)
    {
        std::swap(p_, other.p_);
    }

private:

    raii_wrapper(raii_wrapper const& other);
    raii_wrapper& operator=(raii_wrapper const& rhs);

    void do_delete(T* p)
    {
        assert(del_);
        if (0 != p)
            del_(p);
    }

    T* p_;
    deleter_type del_;
};


/// Definition of variable-length record header.
struct VLRHeader
{
  uint16_t reserved;
  char userId[16]; 
  uint16_t recordId;
  uint16_t recordLengthAfterHeader;
  char description[32];
};

struct GeoKeysHeader
{
    uint16_t keyDirectoryVersion;
    uint16_t keyRevision;
    uint16_t minorRevision;
    uint16_t numberOfKeys;
};

struct GeoKeyEntry
{
  uint16_t keyId;
  uint16_t tiffTagLocation;
  uint16_t count;
  uint16_t valueOffset;
};

class VariableLengthRecord
{
    VariableLengthRecord()
        : reserved(0), record_id(0), record_length_after_header(0)
    {
        std::memset(user_id, 0, eUserIdSize);
        std::memset(description, 0, eDescriptionSize);
    }
 
    enum
    {
        eUserIdSize = 16,
        eDescriptionSize = 32
    };

    uint16_t reserved;
    int8_t user_id[eUserIdSize]; 
    uint16_t record_id;
    uint16_t record_length_after_header;
    int8_t description[eDescriptionSize];
    uint8_t data;
};

struct PointRecord
{
    PointRecord() :
        x(0), y(0), z(0),
        intensity(0),
        flags(0),
        classification(0),
        scan_angle_rank(0),
        user_data(0),
        point_source_id(0)
    {}

    int32_t x;
    int32_t y;
    int32_t z;
    uint16_t intensity;
    uint8_t flags; // TODO: Replace with portable std::bitset<8>
    uint8_t classification;
    int8_t scan_angle_rank;
    uint8_t user_data;
    uint16_t point_source_id;
};

template <typename T>
struct Point
{
    Point() : x(T()), y(T()), z(T()) {}
    Point(T const& x, T const& y, T const& z) : x(x), y(y), z(z) {}
    T x;
    T y;
    T z;

    bool equal(Point<T> const& other) const
    {
        return ((x == other.x) && (y == other.y) && (z == other.z));
    }
};

template <typename T>
bool operator==(Point<T> const& lhs, Point<T> const& rhs)
{
    return lhs.equal(rhs);
}

template <typename T>
bool operator!=(Point<T> const& lhs, Point<T> const& rhs)
{
    return (!lhs.equal(rhs));
}

template <typename T>
struct Extents
{
    typename detail::Point < T > min;
    typename detail::Point < T > max;

    bool equal(Extents<T> const& other) const
    {
        return (min == other.min && max == other.max);
    }
};

template <typename T>
bool operator==(Extents<T> const& lhs, Extents<T> const& rhs)
{
    return lhs.equal(rhs);
}

template <typename T>
bool operator!=(Extents<T> const& lhs, Extents<T> const& rhs)
{
    return (!lhs.equal(rhs));
}

template <typename T>
inline T generate_random_byte()
{
    // Requires pseudo-random numbers generator to be initialized
    // in create_random_based() function - a poor man solution.
    T const rmin = std::numeric_limits<T>::min();
    T const rmax = std::numeric_limits<T>::max();
    unsigned int const rnd = std::rand() % rmax + rmin;

    assert(rnd <= 255);
    return static_cast<T>(rnd);
}

template <typename T> 
bool compare_doubles(const T& actual, const T& expected) 
{ 
    const T epsilon = std::numeric_limits<T>::epsilon();  
    const T diff = actual - expected; 

    if ( !((diff <= epsilon) && (diff >= -epsilon )) ) 
    { 
        return false; 
    } 

    return true;
}

template<typename T>
inline char* as_buffer(T& data)
{
    return static_cast<char*>(static_cast<void*>(&data));
}

template<typename T>
inline char* as_buffer(T* data)
{
    return static_cast<char*>(static_cast<void*>(data));
}

template<typename T>
inline char const* as_bytes(T const& data)
{
    return static_cast<char const*>(static_cast<void const*>(&data));
}

template<typename T>
inline char const* as_bytes(T const* data)
{
    return static_cast<char const*>(static_cast<void const*>(data));
}

template <typename C, typename T>
inline void check_stream_state(std::basic_ios<C, T>& srtm)
{
    // NOTE: Detailed stream check disabled in optimized
    //       builds to increase performance.
#if defined(DEBUG) || defined(_DEBUG)
    // Test stream state bits
    if (srtm.eof())
        throw std::out_of_range("end of file encountered");
    else if (srtm.fail())
        throw std::runtime_error("non-fatal I/O error occured");
    else if (srtm.bad())
        throw std::runtime_error("fatal I/O error occured");
#else
312
313
// OTB Modifications for cygwin compilation
#ifndef __CYGWIN__
314
315
    UNREFERENCED_PARAMETER(srtm);
#endif
316
#endif
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
}

#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4127) // conditional expression is constant.
#endif

template <typename T>
inline void read_n(T& dest, std::istream& src, std::streamsize const& num)
{
    // TODO: Review and redesign errors handling logic if necessary
    if (!src)
        throw std::runtime_error("detail::liblas::read_n input stream is not readable");

    src.read(detail::as_buffer(dest), num);

    // Fix little-endian
    LIBLAS_SWAP_BYTES_N(dest, num);

    check_stream_state(src);
}

template <>
inline void read_n<PointRecord>(PointRecord& dest, std::istream& src, std::streamsize const& num)
{
    // TODO: Review and redesign errors handling logic if necessary
    if (!src)
        throw std::runtime_error("detail::liblas::read_n input stream is not readable");

    src.read(detail::as_buffer(dest), num);
    check_stream_state(src);

    // Fix little-endian
    LIBLAS_SWAP_BYTES(dest.x);
    LIBLAS_SWAP_BYTES(dest.y);
    LIBLAS_SWAP_BYTES(dest.z);
    LIBLAS_SWAP_BYTES(dest.intensity);
    LIBLAS_SWAP_BYTES(dest.flags);
    LIBLAS_SWAP_BYTES(dest.classification);
    LIBLAS_SWAP_BYTES(dest.scan_angle_rank);
    LIBLAS_SWAP_BYTES(dest.user_data);
    LIBLAS_SWAP_BYTES(dest.point_source_id);
}

template <>
inline void read_n<VLRHeader>(VLRHeader& dest, std::istream& src, std::streamsize const& num)
{
    // TODO: Review and redesign errors handling logic if necessary
    if (!src)
        throw std::runtime_error("detail::liblas::read_n input stream is not readable");

    src.read(detail::as_buffer(dest), num);
    check_stream_state(src);

    // Fix little-endian
    LIBLAS_SWAP_BYTES(dest.reserved);
    LIBLAS_SWAP_BYTES(dest.recordId);
    LIBLAS_SWAP_BYTES(dest.recordLengthAfterHeader);
}

template <>
inline void read_n<std::string>(std::string& dest, std::istream& src, std::streamsize const& num)
{
    assert(dest.max_size() >= static_cast<std::string::size_type>(num));

    // TODO: Review and redesign errors handling logic if necessary
    if (!src)
        throw std::runtime_error("detail::liblas::read_n input stream is not readable");

    // Read bytes into temporary buffer then assign as string
    char* buf = new char[num];
    src.read(buf, num);
    dest.assign(buf, num);
    delete [] buf;

    assert(dest.size() == static_cast<std::string::size_type>(num));
    check_stream_state(src);
}

template <typename T>
inline void write_n(std::ostream& dest, T const& src, std::streamsize const& num)
{
    if (!dest)
        throw std::runtime_error("detail::liblas::write_n: output stream is not writable");

    // Fix little-endian
    T& tmp = const_cast<T&>(src);
    LIBLAS_SWAP_BYTES_N(tmp, num);

    dest.write(detail::as_bytes(tmp), num);
    check_stream_state(dest);
}

template <>
inline void write_n<PointRecord>(std::ostream& dest, PointRecord const& src, std::streamsize const& num)
{
    if (!dest)
        throw std::runtime_error("detail::liblas::write_n: output stream is not writable");

    // Fix little-endian
    PointRecord& tmp = const_cast<PointRecord&>(src);
    LIBLAS_SWAP_BYTES(tmp.x);
    LIBLAS_SWAP_BYTES(tmp.y);
    LIBLAS_SWAP_BYTES(tmp.z);
    LIBLAS_SWAP_BYTES(tmp.intensity);
    LIBLAS_SWAP_BYTES(tmp.flags);
    LIBLAS_SWAP_BYTES(tmp.classification);
    LIBLAS_SWAP_BYTES(tmp.scan_angle_rank);
    LIBLAS_SWAP_BYTES(tmp.user_data);
    LIBLAS_SWAP_BYTES(tmp.point_source_id);

    dest.write(detail::as_bytes(tmp), num);
    check_stream_state(dest);
}

template <>
inline void write_n<VLRHeader>(std::ostream& dest, VLRHeader const& src, std::streamsize const& num)
{
    if (!dest)
        throw std::runtime_error("detail::liblas::write_n: output stream is not writable");

    // Fix little-endian
    VLRHeader& tmp = const_cast<VLRHeader&>(src);
    LIBLAS_SWAP_BYTES(tmp.reserved);
    LIBLAS_SWAP_BYTES(tmp.recordId);
    LIBLAS_SWAP_BYTES(tmp.recordLengthAfterHeader);
    
    dest.write(detail::as_bytes(tmp), num);
    check_stream_state(dest);
}

template <>
inline void write_n<std::string>(std::ostream& dest, std::string const& src, std::streamsize const& num)
{
    if (!dest)
        throw std::runtime_error("detail::liblas::write_n: output stream is not writable");

    dest.write(src.c_str(), num);
    check_stream_state(dest);
}

#ifdef _MSC_VER
# pragma warning(push)
#endif

}} // namespace liblas::detail

#endif // LIBLAS_DETAIL_UTILITY_HPP_INCLUDED