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

« back to all changes in this revision

Viewing changes to src/detail/reader10.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
 
/******************************************************************************
2
 
 * $Id$
3
 
 *
4
 
 * Project:  libLAS - http://liblas.org - A BSD library for LAS format data.
5
 
 * Purpose:  LAS 1.0 reader implementation for C++ libLAS 
6
 
 * Author:   Mateusz Loskot, mateusz@loskot.net
7
 
 *
8
 
 ******************************************************************************
9
 
 * Copyright (c) 2008, Mateusz Loskot
10
 
 *
11
 
 * All rights reserved.
12
 
 * 
13
 
 * Redistribution and use in source and binary forms, with or without 
14
 
 * modification, are permitted provided that the following 
15
 
 * conditions are met:
16
 
 * 
17
 
 *     * Redistributions of source code must retain the above copyright 
18
 
 *       notice, this list of conditions and the following disclaimer.
19
 
 *     * Redistributions in binary form must reproduce the above copyright 
20
 
 *       notice, this list of conditions and the following disclaimer in 
21
 
 *       the documentation and/or other materials provided 
22
 
 *       with the distribution.
23
 
 *     * Neither the name of the Martin Isenburg or Iowa Department 
24
 
 *       of Natural Resources nor the names of its contributors may be 
25
 
 *       used to endorse or promote products derived from this software 
26
 
 *       without specific prior written permission.
27
 
 * 
28
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
29
 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
30
 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
31
 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
32
 
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
33
 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
34
 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
35
 
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
36
 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
37
 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
38
 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
39
 
 * OF SUCH DAMAGE.
40
 
 ****************************************************************************/
41
 
 
42
 
#include <liblas/detail/reader10.hpp>
43
 
#include <liblas/detail/utility.hpp>
44
 
#include <liblas/lasvariablerecord.hpp>
45
 
#include <liblas/liblas.hpp>
46
 
#include <liblas/lasheader.hpp>
47
 
#include <liblas/laspoint.hpp>
48
 
 
49
 
// std
50
 
#include <fstream>
51
 
#include <iostream>
52
 
#include <stdexcept>
53
 
#include <cstdlib> // std::size_t, std::free
54
 
#include <cassert>
55
 
 
56
 
namespace liblas { namespace detail { namespace v10 {
57
 
 
58
 
ReaderImpl::ReaderImpl(std::istream& ifs) : Base(ifs)
59
 
{
60
 
}
61
 
 
62
 
std::size_t ReaderImpl::GetVersion() const
63
 
{
64
 
    return eLASVersion10;
65
 
}
66
 
 
67
 
bool ReaderImpl::ReadHeader(LASHeader& header)
68
 
{
69
 
    using detail::read_n;
70
 
 
71
 
    // Helper variables
72
 
    uint8_t n1 = 0;
73
 
    uint16_t n2 = 0;
74
 
    uint32_t n4 = 0;
75
 
    double x1 = 0;
76
 
    double y1 = 0;
77
 
    double z1 = 0;
78
 
    double x2 = 0;
79
 
    double y2 = 0;
80
 
    double z2 = 0;
81
 
    std::string buf;
82
 
    std::string fsig;
83
 
 
84
 
    m_ifs.seekg(0);
85
 
 
86
 
    // 1. File Signature
87
 
    read_n(fsig, m_ifs, 4);
88
 
    header.SetFileSignature(fsig);
89
 
 
90
 
    // 2. Reserved
91
 
    // This data must always contain Zeros.
92
 
    read_n(n4, m_ifs, sizeof(n4));
93
 
 
94
 
    // 3-6. GUID data
95
 
    uint32_t d1 = 0;
96
 
    uint16_t d2 = 0;
97
 
    uint16_t d3 = 0;
98
 
    uint8_t d4[8] = { 0 };
99
 
    read_n(d1, m_ifs, sizeof(d1));
100
 
    read_n(d2, m_ifs, sizeof(d2));
101
 
    read_n(d3, m_ifs, sizeof(d3));
102
 
    read_n(d4, m_ifs, sizeof(d4));
103
 
    liblas::guid g(d1, d2, d3, d4);
104
 
    header.SetProjectId(g);
105
 
 
106
 
    // 7. Version major
107
 
    read_n(n1, m_ifs, sizeof(n1));
108
 
    header.SetVersionMajor(n1);
109
 
 
110
 
    // 8. Version minor
111
 
    read_n(n1, m_ifs, sizeof(n1));
112
 
    header.SetVersionMinor(n1);
113
 
 
114
 
    // 9. System ID
115
 
    read_n(buf, m_ifs, 32);
116
 
    header.SetSystemId(buf);
117
 
 
118
 
    // 10. Generating Software ID
119
 
    read_n(buf, m_ifs, 32);
120
 
    header.SetSoftwareId(buf);
121
 
 
122
 
    // 11. Flight Date Julian
123
 
    read_n(n2, m_ifs, sizeof(n2));
124
 
    header.SetCreationDOY(n2);
125
 
 
126
 
    // 12. Year
127
 
    read_n(n2, m_ifs, sizeof(n2));
128
 
    header.SetCreationYear(n2);
129
 
 
130
 
    // 13. Header Size
131
 
    // NOTE: Size of the stanard header block must always be 227 bytes
132
 
    read_n(n2, m_ifs, sizeof(n2));
133
 
 
134
 
    // 14. Offset to data
135
 
    read_n(n4, m_ifs, sizeof(n4));
136
 
    if (n4 < header.GetHeaderSize())
137
 
    {
138
 
        // TODO: Move this test to LASHeader::Validate()
139
 
        throw std::domain_error("offset to point data smaller than header size");
140
 
    }
141
 
    header.SetDataOffset(n4);
142
 
 
143
 
    // 15. Number of variable length records
144
 
    read_n(n4, m_ifs, sizeof(n4));
145
 
    header.SetRecordsCount(n4);
146
 
 
147
 
    // 16. Point Data Format ID
148
 
    read_n(n1, m_ifs, sizeof(n1));
149
 
    if (n1 == LASHeader::ePointFormat0) {
150
 
        header.SetDataFormatId(LASHeader::ePointFormat0);
151
 
    } 
152
 
    else if (n1 == LASHeader::ePointFormat1) {
153
 
        header.SetDataFormatId(LASHeader::ePointFormat1);
154
 
    }
155
 
    else if (n1 == LASHeader::ePointFormat2) {
156
 
        header.SetDataFormatId(LASHeader::ePointFormat2);
157
 
    }
158
 
    else if (n1 == LASHeader::ePointFormat3) {
159
 
        header.SetDataFormatId(LASHeader::ePointFormat3);
160
 
    }
161
 
    else {
162
 
        throw std::domain_error("invalid point data format");
163
 
    }
164
 
    
165
 
    // 17. Point Data Record Length
166
 
    // NOTE: No need to set record length because it's
167
 
    // determined on basis of point data format.
168
 
    read_n(n2, m_ifs, sizeof(n2));
169
 
 
170
 
    // 18. Number of point records
171
 
    read_n(n4, m_ifs, sizeof(n4));
172
 
    header.SetPointRecordsCount(n4);
173
 
 
174
 
    // 19. Number of points by return
175
 
    std::vector<uint32_t>::size_type const srbyr = 5;
176
 
    uint32_t rbyr[srbyr] = { 0 };
177
 
    read_n(rbyr, m_ifs, sizeof(rbyr));
178
 
    for (std::size_t i = 0; i < srbyr; ++i)
179
 
    {
180
 
        header.SetPointRecordsByReturnCount(i, rbyr[i]);
181
 
    }
182
 
 
183
 
    // 20-22. Scale factors
184
 
    read_n(x1, m_ifs, sizeof(x1));
185
 
    read_n(y1, m_ifs, sizeof(y1));
186
 
    read_n(z1, m_ifs, sizeof(z1));
187
 
    header.SetScale(x1, y1, z1);
188
 
 
189
 
    // 23-25. Offsets
190
 
    read_n(x1, m_ifs, sizeof(x1));
191
 
    read_n(y1, m_ifs, sizeof(y1));
192
 
    read_n(z1, m_ifs, sizeof(z1));
193
 
    header.SetOffset(x1, y1, z1);
194
 
 
195
 
    // 26-27. Max/Min X
196
 
    read_n(x1, m_ifs, sizeof(x1));
197
 
    read_n(x2, m_ifs, sizeof(x2));
198
 
 
199
 
    // 28-29. Max/Min Y
200
 
    read_n(y1, m_ifs, sizeof(y1));
201
 
    read_n(y2, m_ifs, sizeof(y2));
202
 
 
203
 
    // 30-31. Max/Min Z
204
 
    read_n(z1, m_ifs, sizeof(z1));
205
 
    read_n(z2, m_ifs, sizeof(z2));
206
 
 
207
 
    header.SetMax(x1, y1, z1);
208
 
    header.SetMin(x2, y2, z2);
209
 
 
210
 
    Reset(header);
211
 
 
212
 
    return true;
213
 
}
214
 
 
215
 
bool ReaderImpl::ReadNextPoint(LASPoint& point, const LASHeader& header)
216
 
{
217
 
    // Read point data record format 0
218
 
 
219
 
    // TODO: Replace with compile-time assert
220
 
    
221
 
    detail::PointRecord record;
222
 
    double t=0;
223
 
    
224
 
    assert(LASHeader::ePointSize0 == sizeof(record));
225
 
 
226
 
    if (0 == m_current)
227
 
    {
228
 
        m_ifs.clear();
229
 
        m_ifs.seekg(header.GetDataOffset(), std::ios::beg);
230
 
    }
231
 
 
232
 
    if (m_current < m_size)
233
 
    {
234
 
        try
235
 
        {
236
 
            detail::read_n(record, m_ifs, sizeof(PointRecord));
237
 
            ++m_current;    
238
 
        }
239
 
        catch (std::out_of_range const& e) // we reached the end of the file
240
 
        {
241
 
            std::cerr << e.what() << std::endl;
242
 
            return false;
243
 
        }
244
 
 
245
 
        Reader::FillPoint(record, point);
246
 
        point.SetCoordinates(header, point.GetX(), point.GetY(), point.GetZ());
247
 
        
248
 
        if (header.GetDataFormatId() == LASHeader::ePointFormat1) {
249
 
            detail::read_n(t, m_ifs, sizeof(double));
250
 
            point.SetTime(t);
251
 
        }
252
 
        return true;
253
 
    }
254
 
 
255
 
    return false;
256
 
}
257
 
 
258
 
bool ReaderImpl::ReadPointAt(std::size_t n, LASPoint& point, const LASHeader& header)
259
 
{
260
 
    // Read point data record format 0
261
 
 
262
 
    // TODO: Replace with compile-time assert
263
 
    double t=0;
264
 
    detail::PointRecord record;
265
 
    assert(LASHeader::ePointSize0 == sizeof(record));
266
 
 
267
 
    if (m_size <= n)
268
 
        return false;
269
 
    std::streamsize pos = (static_cast<std::streamsize>(n) * header.GetDataRecordLength()) + header.GetDataOffset();    
270
 
    
271
 
    m_ifs.clear();
272
 
    m_ifs.seekg(pos, std::ios::beg);
273
 
    detail::read_n(record, m_ifs, sizeof(record));
274
 
 
275
 
    Reader::FillPoint(record, point);
276
 
    point.SetCoordinates(header, point.GetX(), point.GetY(), point.GetZ());
277
 
 
278
 
    
279
 
    if (header.GetDataFormatId() == LASHeader::ePointFormat1) {
280
 
        detail::read_n(t, m_ifs, sizeof(double));
281
 
        point.SetTime(t);
282
 
    }
283
 
 
284
 
    return true;
285
 
}
286
 
 
287
 
}}} // namespace liblas::detail::v10
288