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
|
// $Id$
//
// (C) Copyright Mateusz Loskot 2008, mateusz@loskot.net
// Distributed under the BSD License
// (See accompanying file LICENSE.txt or copy at
// http://www.opensource.org/licenses/bsd-license.php)
//
#include <liblas/liblas.hpp>
#include <tut/tut.hpp>
#include <cstdio>
#include <bitset>
#include <fstream>
#include <string>
#include "liblas_test.hpp"
#include "common.hpp"
namespace tut
{
struct laswriter_data
{
std::string tmpfile_;
std::string file10_;
laswriter_data() :
tmpfile_(g_test_data_path + "//tmp.las"),
file10_(g_test_data_path + "//TO_core_last_clip.las")
{}
~laswriter_data()
{
// remove temporary file after each test case
int const ret = std::remove(tmpfile_.c_str());
if (0 != ret)
{
; // ignore, file may not exist
}
}
};
typedef test_group<laswriter_data> tg;
typedef tg::object to;
tg test_group_laswriter("liblas::Writer");
// Test user-declared constructor
template<>
template<>
void to::test<1>()
{
// Create new LAS file using default header block
{
std::ofstream ofs;
ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
// LAS 1.2, Point Format 0
liblas::Header header;
liblas::Writer writer(ofs, header);
ensure_equals(writer.GetHeader().GetVersionMinor(), 2);
liblas::Header const& hdr_default = writer.GetHeader();
test_default_header(hdr_default);
}
// Read previously created LAS file and check its header block
{
std::ifstream ifs;
ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
ensure(ifs.is_open());
liblas::Reader reader(ifs);
ensure_equals(reader.GetHeader().GetVersionMinor(), 2);
liblas::Header const& hdr_default = reader.GetHeader();
test_default_header(hdr_default);
}
}
// Test WritePoint method
template<>
template<>
void to::test<2>()
{
{
std::ofstream ofs;
ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
// LAS 1.1, Point Format 0
liblas::Header header;
liblas::Writer writer(ofs, header);
liblas::Point point(&header);
// Write 1st point
point.SetCoordinates(10, 20, 30);
point.SetIntensity(5);
point.SetReturnNumber(1);
point.SetNumberOfReturns(1);
point.SetScanDirection(1);
point.SetFlightLineEdge(1);
point.SetClassification(7);
point.SetScanAngleRank(90);
point.SetUserData(0);
point.SetPointSourceID(1);
writer.WritePoint(point);
// write 2nd point
point.SetCoordinates(40, 50, 60);
point.SetPointSourceID(2);
writer.WritePoint(point);
// write 3rd point
point.SetCoordinates(70, 80, 90);
point.SetPointSourceID(3);
writer.WritePoint(point);
}
// Read previously create LAS file with 3 point records
{
std::ifstream ifs;
ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
ensure(ifs.is_open());
liblas::Reader reader(ifs);
// read 1st point
reader.ReadNextPoint();
liblas::Point point = reader.GetPoint();
ensure_distance(point.GetX(), 10.0, 0.1);
ensure_distance(point.GetY(), 20.0, 0.1);
ensure_distance(point.GetZ(), 30.0, 0.1);
ensure_equals(point.GetIntensity(), 5);
ensure_equals(point.GetReturnNumber(), 1);
ensure_equals(point.GetNumberOfReturns(), 1);
ensure_equals(point.GetScanDirection(), 1);
ensure_equals(point.GetFlightLineEdge(), 1);
ensure_equals(point.GetScanAngleRank(), 90);
ensure_equals(point.GetUserData(), 0);
ensure_equals(point.GetPointSourceID(), 1);
typedef liblas::Classification::bitset_type bitset_type;
ensure_equals(bitset_type(point.GetClassification()), bitset_type(7));
// read 2nd point
reader.ReadNextPoint();
point = reader.GetPoint();
ensure_distance(point.GetX(), 40.0, 0.1);
ensure_distance(point.GetY(), 50.0, 0.1);
ensure_distance(point.GetZ(), 60.0, 0.1);
ensure_equals(point.GetIntensity(), 5);
ensure_equals(point.GetReturnNumber(), 1);
ensure_equals(point.GetNumberOfReturns(), 1);
ensure_equals(point.GetScanDirection(), 1);
ensure_equals(point.GetFlightLineEdge(), 1);
ensure_equals(point.GetScanAngleRank(), 90);
ensure_equals(point.GetUserData(), 0);
ensure_equals(point.GetPointSourceID(), 2);
// read 3rd point
reader.ReadNextPoint();
point = reader.GetPoint();
ensure_distance(point.GetX(), 70.0, 0.1);
ensure_distance(point.GetY(), 80.0, 0.1);
ensure_distance(point.GetZ(), 90.0, 0.1);
ensure_equals(point.GetIntensity(), 5);
ensure_equals(point.GetReturnNumber(), 1);
ensure_equals(point.GetNumberOfReturns(), 1);
ensure_equals(point.GetScanDirection(), 1);
ensure_equals(point.GetFlightLineEdge(), 1);
ensure_equals(point.GetScanAngleRank(), 90);
ensure_equals(point.GetUserData(), 0);
ensure_equals(point.GetPointSourceID(), 3);
typedef liblas::Classification::bitset_type bitset_type;
ensure_equals(bitset_type(point.GetClassification()), bitset_type(7));
}
}
// Test WriteHeader method
template<>
template<>
void to::test<3>()
{
{
std::ofstream ofs;
ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
liblas::Header header;
liblas::Writer writer(ofs, header);
// test initially written header
liblas::Header const& hdr_default = writer.GetHeader();
test_default_header(hdr_default);
// update some header data and overwrite header block
header.SetReserved(1);
header.SetFileSourceId(65535);
header.SetSystemId("Unit Test libLAS System");
header.SetSoftwareId("Unit Test libLAS Software");
header.SetCreationDOY(100);
header.SetCreationYear(2008);
header.SetScale(1.123, 2.123, 3.123);
header.SetOffset(4.321, 5.321, 6.321);
writer.SetHeader(header);
writer.WriteHeader();
liblas::Header const& written_header = writer.GetHeader();
ensure_equals("written_header fileSource id", written_header.GetFileSourceId(), 65535);
ensure_equals("written_header SystemId", written_header.GetSystemId(), "Unit Test libLAS System");
ensure_equals("written_header SoftwareId", written_header.GetSoftwareId(), "Unit Test libLAS Software");
ensure_equals("written_header CreationDOY", written_header.GetCreationDOY(), 100);
ensure_equals("written_header CreationYear", written_header.GetCreationYear(), 2008);
}
// read and check updated header block
{
std::ifstream ifs;
ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
ensure(ifs.is_open());
liblas::Reader reader(ifs);
liblas::Header const& header = reader.GetHeader();
ensure_equals(header.GetReserved(), 1);
ensure_equals(header.GetFileSourceId(), 65535);
ensure_equals(header.GetSystemId(), std::string("Unit Test libLAS System"));
ensure_equals(header.GetSoftwareId(), std::string("Unit Test libLAS Software"));
ensure_equals(header.GetCreationDOY(), 100);
ensure_equals(header.GetCreationYear(), 2008);
ensure_equals(header.GetScaleX(), 1.123);
ensure_equals(header.GetScaleY(), 2.123);
ensure_equals(header.GetScaleZ(), 3.123);
ensure_equals(header.GetOffsetX(), 4.321);
ensure_equals(header.GetOffsetY(), 5.321);
ensure_equals(header.GetOffsetZ(), 6.321);
}
}
// Test appending to an existing file
template<>
template<>
void to::test<4>()
{
std::ofstream ofs;
ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
liblas::Header header;
header.SetDataOffset(759);//Toggle to see the differences
header.SetDataFormatId( liblas::ePointFormat1 );
{
liblas::Writer testWriter( ofs, header);
}
ofs.close();
ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
{
liblas::Writer test2Writer( ofs, header);
size_t count = 500;
for ( size_t i = 0; i < count ; i++ )
{
liblas::Point point(&test2Writer.GetHeader());
point.SetCoordinates( 10.0 + i, 20.0 + i, 30.0 + i );
test2Writer.WritePoint( point );
}
}
ofs.close();
std::ifstream ifs;
ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
liblas::Reader reader(ifs);
liblas::Header const& h = reader.GetHeader();
ensure_equals("Appended point count does not match", h.GetPointRecordsCount(), static_cast<boost::uint32_t>(500));
}
// Test padding application for 1.0 files
template<>
template<>
void to::test<5>()
{
{
std::ofstream ofs;
ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
liblas::Header header;
header.SetDataOffset(227);
header.SetDataFormatId( liblas::ePointFormat0 );
header.SetVersionMinor(0);
liblas::Writer testWriter( ofs, header);
ofs.close();
}
{
std::ifstream ifs;
ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
liblas::Reader reader( ifs);
liblas::Header const& h = reader.GetHeader();
ensure_equals("DataOffset is not expected value", h.GetDataOffset(), static_cast<boost::uint32_t>(229));
}
}
// Test using a bogus record count in the header
template<>
template<>
void to::test<6>()
{
size_t count = 10;
// Create new LAS file using default header block
{
std::ofstream ofs;
ofs.open(tmpfile_.c_str(), std::ios::out | std::ios::binary);
// LAS 1.2, Point Format 0
liblas::Header header;
header.SetPointRecordsCount(12);
liblas::Writer writer(ofs, header);
for ( size_t i = 0; i < count ; i++ )
{
liblas::Point point(&writer.GetHeader());
point.SetCoordinates( 10.0 + i, 20.0 + i, 30.0 + i );
writer.WritePoint( point );
}
}
// Read previously created LAS file and check its header block
{
std::ifstream ifs;
ifs.open(tmpfile_.c_str(), std::ios::in | std::ios::binary);
ensure(ifs.is_open());
liblas::Reader reader(ifs);
ensure_equals(reader.GetHeader().GetPointRecordsCount(), count);
}
}
}
|