/usr/include/liblas/detail/private_utility.hpp is in liblas-dev 1.8.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 312 313 314 315 316 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 465 466 467 468 469 470 471 472 473 | /******************************************************************************
* $Id$
*
* 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/detail/pointrecord.hpp>
#include <liblas/detail/endian.hpp>
#include <liblas/detail/binary.hpp>
// boost
#include <boost/concept_check.hpp>
// std
#include <cassert>
#include <cstddef>
#include <cstring>
#include <algorithm>
#include <iosfwd>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <cmath>
#include <vector>
/// Defines utilities for internal use in libLAS.
/// The liblas::detail elements do not belong to the public
/// interface of libLAS.
namespace liblas { namespace detail {
// From http://stackoverflow.com/questions/485525/round-for-float-in-c
inline double sround(double r) {
return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5);
}
/// Compile-time calculation size of array defined statically.
template <typename T, std::size_t N>
inline std::size_t static_array_size(T (&t)[N])
{
boost::ignore_unused_variable_warning(t);
return (sizeof(t) / sizeof(t[0]));
}
/// 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& operator=(raii_wrapper const& rhs)
{
if (&rhs != this)
{
p_ = rhs.p_;
del_ = rhs.del_;
}
return *this;
}
~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]; // TODO: replace wtih boost::array --mloskot
uint16_t recordId;
uint16_t recordLengthAfterHeader;
char description[32]; // TODO: replace wtih boost::array --mloskot
};
template <typename T>
bool compare_distance(const T& actual, const T& expected);
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)
{}
bool equal(Point<T> const& other) const
{
return (compare_distance(x, other.x)
&& compare_distance(y, other.y)
&& compare_distance(z, other.z));
}
T x;
T y;
T 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
// {
// Extents() {}
// Extents(detail::Point<T> const& min, detail::Point<T> const& max)
// : min(min), max(max)
// {}
//
// bool equal(Extents<T> const& other) const
// {
// return (min == other.min && max == other.max);
// }
//
// typename detail::Point<T> min;
// typename detail::Point<T> 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>
bool compare_distance(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
boost::ignore_unused_variable_warning(srtm);
#endif
}
#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<T> 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<PointRecord> 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<VLRHeader> 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<std::string> input stream is not readable");
// Read bytes into temporary buffer then assign as string
std::size_t const bufsize = static_cast<std::size_t>(num);
char* buf = new char[bufsize]; // TODO:this is a leak, make exception safe with RAII
src.read(buf, num);
dest.assign(buf, bufsize);
delete [] buf;
assert(dest.size() == static_cast<std::string::size_type>(num));
check_stream_state(src);
}
// adapted from http://www.cplusplus.com/forum/beginner/3076/
template <typename IntegerType>
inline IntegerType bitsToInt(IntegerType& output,
std::vector<uint8_t> const& data,
std::size_t index)
{
binary::endian_value<IntegerType> value;
value.template load<binary::little_endian_tag>(&data[0] + index);
output = value;
return output;
}
template <typename IntegerType>
inline void intToBits(IntegerType input,
std::vector<uint8_t>& data,
std::size_t index)
{
binary::endian_value<IntegerType> value(input);
value.template store<binary::little_endian_tag>(&data[0] + index);
}
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<T>: 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<PointRecord>: 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<VLRHeader>: 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<std::string>: output stream is not writable");
dest.write(src.c_str(), num);
check_stream_state(dest);
}
#ifdef _MSC_VER
# pragma warning(pop)
#endif
// Utility functor with accompanying to print GeoTIFF directory.
struct geotiff_dir_printer
{
geotiff_dir_printer() {}
std::string output() const { return m_oss.str(); }
std::string::size_type size() const { return m_oss.str().size(); }
void operator()(char* data, void* aux)
{
::boost::ignore_unused_variable_warning(aux);
if (0 != data)
{
m_oss << data;
}
}
private:
std::ostringstream m_oss;
};
extern "C" int libLASGeoTIFFPrint(char* data, void* aux);
}} // namespace liblas::detail
#endif // LIBLAS_DETAIL_UTILITY_HPP_INCLUDED
|