-
Notifications
You must be signed in to change notification settings - Fork 432
/
PointBuffer.hpp
1073 lines (942 loc) · 47.1 KB
/
PointBuffer.hpp
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
*
* 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 Hobu, Inc. or Flaxen Geo Consulting 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 INCLUDED_POINTBUFFER_HPP
#define INCLUDED_POINTBUFFER_HPP
#include <boost/cstdint.hpp>
#include <boost/scoped_array.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <pdal/pdal_internal.hpp>
#include <pdal/Bounds.hpp>
#include <pdal/Schema.hpp>
#include <pdal/Metadata.hpp>
#include <pdal/third/nanoflann.hpp>
#include <vector>
namespace pdal
{
namespace pointbuffer
{
typedef boost::uuids::uuid id;
typedef std::vector<boost::uint8_t>::size_type PointBufferByteSize;
typedef boost::interprocess::allocator<boost::uint8_t, boost::interprocess::managed_shared_memory::segment_manager> ShmemAllocator;
typedef boost::container::vector<boost::uint8_t, ShmemAllocator> PointBufferVector;
} // pointbuffer
/// A PointBuffer is the object that is passed through pdal::Stage instances
/// to form a pipeline. A PointBuffer is composed of a pdal::Schema that determines
/// the layout of the data contained within, along with a dictionary of pdal::Metadata
/// entries. The capacity of a PointBuffer is determined by the number of points
/// it contains, and the number of points possible in a PointBuffer is limited to
/// std::numeric_limits<boost::uint32_t>::max(). Underneath the covers, a PointBuffer
/// is simply the composed array of bytes described in the pdal::Schema. You can
/// operate on the raw bytes if you need to, but PointBuffer provides a number of
/// convienence methods to make things easier.
/*!
\verbatim embed:rst
.. note::
The arrangement of PointBuffer's bytes might either be point-interleaved or
dimension-interleaved, with point-interleave being the default organization.
If you are directly modifying a PointBuffer's bytes, you must respect the
:cpp:class:`pdal::pointbuffer::Orientation`.
\endverbatim
*/
class PDAL_DLL PointBuffer
{
public:
/** @name Constructors
*/
/*! Base constructor for pdal::PointBuffer.
\param schema pdal::Schema instance to use to describe the layout. It is copied.
\param capacity size of the pdal::PointBuffer in number of points.
*/
PointBuffer(const Schema& schema, boost::uint32_t capacity=65536);
/// Copy constructor. The data array is simply memcpy'd.
PointBuffer(const PointBuffer&);
/// Assignment constructor.
PointBuffer& operator=(const PointBuffer&);
/// Destructor.
~PointBuffer();
/** @name Attribute access
*/
/*! @return the pdal::Bounds instance associated with this pdal::PointBuffer.
\verbatim embed:rst
.. note::
It is not a requirement that stages keep the pdal::Bounds instance
up-to-date when operating on the PointBuffer.
\endverbatim
*/
const Bounds<double>& getSpatialBounds() const;
/// sets the pdal::Bounds instance for this pdal::PointBuffer
/// @param bounds bounds instance to set.
void setSpatialBounds(const Bounds<double>& bounds);
/// @return the number of points that are set for the PointBuffer. It is
/// an arbitrary number that must be <= getCapacity() is the number of
/// active points for the PointBuffer
inline boost::uint32_t getNumPoints() const
{
return m_numPoints;
}
/// sets the number of active points for the PointBuffer.
/// @param v number of points to set.
inline void setNumPoints(boost::uint32_t v)
{
assert(v <= m_capacity);
m_numPoints = v;
}
/*! @return the maximum number of points the PointBuffer can hold.
\verbatim embed:rst
.. note::
This value is given at construction time, and it in conjunction
with the given :cpp:class:`pdal::Schema` determine the size of the raw byte
buffer that contains the point data. It can be changed by
a call to :cpp:func:`pdal::PointBuffer::resize`.
\endverbatim
*/
inline boost::uint32_t const& getCapacity() const
{
return m_capacity;
}
/// A const reference to the internally copied pdal::Schema instance that
/// was given at construction time.
const Schema& getSchema() const
{
return m_schema;
}
/// @return the size of the currently allocated raw byte array
inline pointbuffer::PointBufferByteSize getBufferByteLength() const
{
return m_data.size();
}
/// @return the size of the theoretically filled raw byte array.
/// Should be equivalent to PointBuffer::getCapacity() * PointBuffer::getSchema()::getByteSize().
inline pointbuffer::PointBufferByteSize getBufferByteCapacity() const
{
return static_cast<pointbuffer::PointBufferByteSize>(m_byteSize) * static_cast<pointbuffer::PointBufferByteSize>(m_capacity);
}
/** @name Point data access
*/
/*! fetch the value T for a given :cpp:class:`pdal::Dimension` dim at pointIndex `i`.
\param dim a pdal::Dimension instance describing the dimension to select
\param pointIndex the point index of the PointBuffer to select.
\verbatim embed:rst
.. warning::
If the data type of T is not the same as described in :cpp:class:`pdal::Dimension`,
the data value will be casted into the appropriate type. In some
situations this may not be what you want. In situations where the T
is smaller than the datatype given by `dim`, the return value T
will simply be saturated.
\endverbatim
*/
template<class T> T const& getField(Dimension const& dim, boost::uint32_t pointIndex) const;
/*! set the value T for a given :cpp:class:`pdal::Dimension` dim at pointIndex i.
\param dim a pdal::Dimension instance describing the dimension to select
\param pointIndex the point index of the PointBuffer to select.
\param value the T value to set
\verbatim embed:rst
.. warning::
If the data type of T is not the same as described in :cpp:class:`pdal::Dimension`,
the data value will be casted into the appropriate type. In some
situations this may not be what you want. In situations where the T
is smaller than the datatype given by `dim`, the return value T
will simply be saturated.
\endverbatim
*/
template<class T> void setField(Dimension const& dim, boost::uint32_t pointIndex, T value);
/*! bulk copy all the fields from the given point into this object
\param destPointIndex the destination point index to copy the data from
srcPointBuffer at srcPointIndex.
\param srcPointIndex the point index of the PointBuffer to copy from.
\param srcPointBuffer the source PointBuffer to copy from
\verbatim embed:rst
.. warning::
This is only legal if the source and destination schemas are
exactly the same. It is up the caller to ensure this is the case.
Additionally, if the schemas are the same :cpp:func:`pdal::Schema::getByteSize()` but of
different compositions, congratulations :)
\endverbatim
*/
inline void copyPointFast(boost::uint32_t destPointIndex,
boost::uint32_t srcPointIndex,
const PointBuffer& srcPointBuffer)
{
assert (srcPointBuffer.getSchema().getOrientation() == getSchema().getOrientation());
const boost::uint8_t* src = srcPointBuffer.getData(srcPointIndex);
boost::uint8_t* dest = getData(destPointIndex);
assert(srcPointBuffer.getSchema().getByteSize() == m_byteSize);
memcpy(dest, src, m_byteSize);
assert(m_numPoints <= m_capacity);
return;
}
/*! bulk copy all the fields from the given point into this object
\param destPointIndex the destination point index to copy the data from
srcPointBuffer at srcPointIndex.
\param srcPointIndex the point index of the PointBuffer to copy from.
\param srcPointBuffer the source PointBuffer to copy from
\param numPoints the number of points to copy
\verbatim embed:rst
.. warning::
This is only legal if the source and destination schemas are
exactly the same. It is up the caller to ensure this is the case.
Additionally, if the schemas are the same :cpp:func:`pdal::Schema::getByteSize()` but of
different compositions, congratulations :)
\endverbatim
*/
inline void copyPointsFast(boost::uint32_t destPointIndex,
boost::uint32_t srcPointIndex,
const PointBuffer& srcPointBuffer,
boost::uint32_t numPoints)
{
assert (srcPointBuffer.getSchema().getOrientation() == getSchema().getOrientation());
const boost::uint8_t* src = srcPointBuffer.getData(srcPointIndex);
boost::uint8_t* dest = getData(destPointIndex);
assert(m_numPoints <= m_capacity);
memcpy(dest, src, m_byteSize * numPoints);
return;
}
/** @name Raw Data Access
*/
/// access to the raw byte data at specified pointIndex
/// @param pointIndex position to start accessing
inline boost::uint8_t* getData(boost::uint32_t pointIndex) const
{
#if DEBUG
#endif
pointbuffer::PointBufferByteSize position(0);
if (m_orientation == schema::POINT_INTERLEAVED)
{
position = static_cast<pointbuffer::PointBufferByteSize>(m_byteSize) * \
static_cast<pointbuffer::PointBufferByteSize>(pointIndex);
}
else if (m_orientation == schema::DIMENSION_INTERLEAVED)
{
pointbuffer::PointBufferByteSize offset(0);
offset = m_schema.getDimension(pointIndex).getByteOffset();
position = static_cast<pointbuffer::PointBufferByteSize>(m_capacity) * offset;
}
return const_cast<boost::uint8_t*>(&(m_data.front())) + position;
}
inline boost::uint8_t* getDataStart() { return &(m_data.front()); }
/// copies the raw data into your own byte array and sets the size
/// @param data pointer to your byte array
/// @param size size of the array in bytes
void getData(boost::uint8_t** data, boost::uint64_t* size) const;
/// set the data for a single point at given pointIndex from a
/// raw byte array
/// @param data raw byte array
/// @param pointIndex point position to set data.
void setData(boost::uint8_t* data, boost::uint32_t pointIndex);
/// overwrite raw data at a given pointIndex for a given number of byteCount
/// @param data raw byte array
/// @param pointIndex position to start writing
/// @param byteCount number of bytes to overwrite at given position
void setDataStride(boost::uint8_t* data, boost::uint32_t pointIndex, boost::uint32_t byteCount);
/** @name Metadata
*/
/// return Metadata const& for the PointBuffer
inline Metadata const& getMetadata() const
{
return m_metadata;
}
/// @return Metadata& for the PointBuffer
inline Metadata& getMetadataRef()
{
return m_metadata;
}
/** @name Memory Operations
*/
/// Reallocates a new data buffer with the given schema
/// @param new_schema The new schema to use.
void reset(Schema const& new_schema);
/// Resizes the PointBuffer to the given capacity. If the
/// PointBuffer is already big enough to hold all of the bytes,
/// it is not reallocated unless bExact is true
/// @param capacity The new number of points to use for the instance
/// @param bExact Whether or not to exactly resize the PointBuffer instance
/// with the given point size and force a new reallocation of the data buffer.
void resize(boost::uint32_t const& capacity, bool bExact=false);
/// @return a new PointBuffer with all ignored dimensions removed
PointBuffer* pack(bool bRemoveIgnoredDimensions = true) const;
/// @return a new PointBuffer with the opposite orientation
PointBuffer* flipOrientation() const;
/** @name Serialization
*/
/*! returns a boost::property_tree containing the point records, which is
useful for dumping and such.
\verbatim embed:rst
::
0:
X: 1.00
Y: 2.00
Z: 3.00
1:
X: 1.00
Y: 2.00
Z: 3.00
.. note::
If you want to print out details about the fields, e.g. the byte
offset or the datatype, use the :cpp:func:`pdal::Schema::getPtree()` dumper.
\endverbatim
*/
boost::property_tree::ptree toPTree() const;
/*! @return a cumulated bounds of all points in the PointBuffer.
\verbatim embed:rst
.. note::
This method requires that an `X`, `Y`, and `Z` dimension be
available, and that it can be casted into a *double* data
type using the :cpp:func:`pdal::Dimension::applyScaling`
method. Otherwise, an exception will be thrown.
\endverbatim
*/
pdal::Bounds<double> calculateBounds(bool bis3d=true) const;
/// Copies dimensions from the given PointBuffer that have both
/// similar names and data types. Create a schema::DimensionMap
/// and adjust as necessary before utilizing this method to copy
/// dimensions from one PointBuffer instance to another.
static void copyLikeDimensions( PointBuffer const& source,
PointBuffer& destination,
schema::DimensionMap const& dimensions,
boost::uint32_t source_starting_position,
boost::uint32_t destination_starting_position,
boost::uint32_t howMany);
static void scaleData(PointBuffer const& source_buffer,
PointBuffer& destination_buffer,
Dimension const& source_dimension,
Dimension const& destination_dimension,
boost::uint32_t source_index,
boost::uint32_t destination_index);
double applyScaling(Dimension const& d,
std::size_t pointIndex) const;
/** @name private attributes
*/
protected:
Schema m_schema;
std::vector<boost::uint8_t> m_data;
boost::uint32_t m_numPoints;
boost::uint32_t m_capacity;
Bounds<double> m_bounds;
// We cache m_schema.getByteSize() here because it would end up
// being dereferenced for every point read otherwise.
schema::size_type m_byteSize;
// We cache m_schema.getOrientation() here because it would end
// up being dereferenced for every point read
schema::Orientation m_orientation;
Metadata m_metadata;
boost::interprocess::managed_shared_memory *m_segment;
pointbuffer::id m_uuid;
template<class T> static void scale(Dimension const& source_dimension,
Dimension const& destination_dimension,
T& value);
};
template <class T>
inline void PointBuffer::setField(pdal::Dimension const& dim, boost::uint32_t pointIndex, T value)
{
if (dim.getPosition() == -1)
{
throw buffer_error("This dimension has no identified position in a schema. Use the setRawField method to access an arbitrary byte position.");
}
pointbuffer::PointBufferByteSize point_start_byte_position(0);
pointbuffer::PointBufferByteSize offset(0);
if (m_orientation == schema::POINT_INTERLEAVED)
{
point_start_byte_position = static_cast<pointbuffer::PointBufferByteSize>(pointIndex) * \
static_cast<pointbuffer::PointBufferByteSize>(m_byteSize);
offset = point_start_byte_position + \
static_cast<pointbuffer::PointBufferByteSize>(dim.getByteOffset());
}
else if (m_orientation == schema::DIMENSION_INTERLEAVED)
{
point_start_byte_position = static_cast<pointbuffer::PointBufferByteSize>(m_capacity) * \
static_cast<pointbuffer::PointBufferByteSize>(dim.getByteOffset());
offset = point_start_byte_position + \
static_cast<pointbuffer::PointBufferByteSize>(dim.getByteSize()) * \
static_cast<pointbuffer::PointBufferByteSize>(pointIndex);
} else
{
throw buffer_error("unknown pdal::Schema::m_orientation provided!");
}
assert(offset + sizeof(T) <= getBufferByteLength());
boost::uint8_t* p = (boost::uint8_t*)&(m_data.front()) + offset;
if (sizeof(T) == dim.getByteSize())
{
// Winner, winner, chicken dinner. We're not going to try to
// do anything magical. It's up to you to get the interpretation right.
*(T*)(void*)p = value;
return;
}
T output(0);
output = boost::lexical_cast<T>(value);
*(T*)(void*)p = output;
}
template <class T>
inline T const& PointBuffer::getField(pdal::Dimension const& dim, boost::uint32_t pointIndex) const
{
if (dim.getPosition() == -1)
{
// this is a little harsh, but we'll keep it for now as we shake things out
throw buffer_error("This dimension has no identified position in a schema.");
}
// pointbuffer::PointBufferByteSize point_start_byte_position = static_cast<pointbuffer::PointBufferByteSize>(pointIndex) * static_cast<pointbuffer::PointBufferByteSize>(m_byteSize);
// boost::uint64_t offset = point_start_byte_position + static_cast<pointbuffer::PointBufferByteSize>(dim.getByteOffset());
pointbuffer::PointBufferByteSize point_start_byte_position(0);
pointbuffer::PointBufferByteSize offset(0);
if (m_orientation == schema::POINT_INTERLEAVED)
{
point_start_byte_position = static_cast<pointbuffer::PointBufferByteSize>(pointIndex) * \
static_cast<pointbuffer::PointBufferByteSize>(m_byteSize);
offset = point_start_byte_position + \
static_cast<pointbuffer::PointBufferByteSize>(dim.getByteOffset());
} else if (m_orientation == schema::DIMENSION_INTERLEAVED)
{
point_start_byte_position = static_cast<pointbuffer::PointBufferByteSize>(m_capacity) * \
static_cast<pointbuffer::PointBufferByteSize>(dim.getByteOffset());
offset = point_start_byte_position + \
static_cast<pointbuffer::PointBufferByteSize>(dim.getByteSize()) * \
static_cast<pointbuffer::PointBufferByteSize>(pointIndex);
} else
{
throw buffer_error("unknown pdal::Schema::m_orientation provided!");
}
assert(offset + sizeof(T) <= getBufferByteLength());
boost::uint8_t const* p = (boost::uint8_t const*)&(m_data.front()) + offset;
T const& output = *(T const*)(void const*)p;
return output;
}
#ifdef PDAL_COMPILER_MSVC
// the writeScaledData function causes lots of conversion warnings which
// are real but which we will ignore: blame the filter's user for casting badly
# pragma warning(push)
# pragma warning(disable: 4244) // conversion from T1 to T2, possible loss of data
#endif
inline void PointBuffer::scaleData(PointBuffer const& source,
PointBuffer& destination,
Dimension const& source_dimension,
Dimension const& destination_dimension,
boost::uint32_t source_index,
boost::uint32_t destination_index)
{
if (source_dimension.getInterpretation() == dimension::Float)
{
if (source_dimension.getByteSize() == 4)
{
float v = source.getField<float>(source_dimension, source_index);
scale(source_dimension,
destination_dimension,
v);
if (destination_dimension.getInterpretation() == dimension::SignedInteger)
{
if (destination_dimension.getByteSize() == 1)
destination.setField<boost::int8_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 2)
destination.setField<boost::int16_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 4)
destination.setField<boost::int32_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 8)
destination.setField<boost::int64_t>(destination_dimension, destination_index, v);
else
throw pdal_error("Unable to convert dimension::Float to dimension::SignedInteger of size >8");
return;
}
}
else if (source_dimension.getByteSize() == 8)
{
double v = source.getField<double>(source_dimension, source_index);
scale(source_dimension,
destination_dimension,
v);
if (destination_dimension.getInterpretation() == dimension::SignedInteger)
{
if (destination_dimension.getByteSize() == 1)
destination.setField<boost::int8_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 2)
destination.setField<boost::int16_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 4)
destination.setField<boost::int32_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 8)
destination.setField<boost::int64_t>(destination_dimension, destination_index, v);
else
throw pdal_error("Unable to convert dimension::Float to dimension::SignedInteger of size >8");
return;
}
}
else
{
std::ostringstream oss;
oss << "Unable to interpret Float of size '" << source_dimension.getByteSize() <<"'";
throw pdal_error(oss.str());
}
}
if (source_dimension.getInterpretation() == dimension::SignedInteger)
{
if (destination_dimension.getInterpretation() == dimension::SignedInteger)
{
if (source_dimension.getByteSize() == 1)
{
boost::int8_t v = source.getField<boost::int8_t>(source_dimension, source_index);
scale(source_dimension,
destination_dimension,
v);
if (destination_dimension.getByteSize() == 1)
destination.setField<boost::int8_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 2)
destination.setField<boost::int16_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 4)
destination.setField<boost::int32_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 8)
destination.setField<boost::int64_t>(destination_dimension, destination_index, v);
else
throw pdal_error("Unable to convert dimension from SignedInteger 1 to SignedInteger of size > 8");
return;
}
else if (source_dimension.getByteSize() == 2)
{
boost::int16_t v = source.getField<boost::int16_t>(source_dimension, source_index);
scale(source_dimension,
destination_dimension,
v);
if (destination_dimension.getByteSize() == 1)
destination.setField<boost::int8_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 2)
destination.setField<boost::int16_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 4)
destination.setField<boost::int32_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 8)
destination.setField<boost::int64_t>(destination_dimension, destination_index, v);
else
throw pdal_error("Unable to convert dimension from SignedInteger 2 to SignedInteger of size > 8");
return;
}
else if (source_dimension.getByteSize() == 4)
{
boost::int32_t v = source.getField<boost::int32_t>(source_dimension, source_index);
scale(source_dimension,
destination_dimension,
v);
if (destination_dimension.getByteSize() == 1)
destination.setField<boost::int8_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 2)
destination.setField<boost::int16_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 4)
destination.setField<boost::int32_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 8)
destination.setField<boost::int64_t>(destination_dimension, destination_index, v);
else
throw pdal_error("Unable to convert dimension from SignedInteger 4 to SignedInteger of size > 8");
return;
}
else if (source_dimension.getByteSize() == 8)
{
boost::int64_t v = source.getField<boost::int64_t>(source_dimension, source_index);
scale(source_dimension,
destination_dimension,
v);
if (destination_dimension.getByteSize() == 1)
destination.setField<boost::int8_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 2)
destination.setField<boost::int16_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 4)
destination.setField<boost::int32_t>(destination_dimension, destination_index, v);
else if (destination_dimension.getByteSize() == 8)
destination.setField<boost::int64_t>(destination_dimension, destination_index, v);
else
throw pdal_error("Unable to convert dimension from SignedInteger 8 to SignedInteger of size > 8");
return;
}
else
{
throw pdal_error("Unable to convert dimension::SignedInteger >8 to dimension::SignedInteger of size >8");
}
}
else if (destination_dimension.getInterpretation() == dimension::Float)
{
if (source_dimension.getByteSize() == 1)
{
boost::int8_t v = source.getField<boost::int8_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
d = destination_dimension.removeScaling<double>(d);
if (destination_dimension.getByteSize() == 4)
destination.setField<float>(destination_dimension, destination_index, (float)d);
else if (destination_dimension.getByteSize() == 8)
destination.setField<double>(destination_dimension, destination_index, d);
else
throw pdal_error("Unable to convert dimension from SignedInteger 1 to Float of size > 8");
return;
}
else if (source_dimension.getByteSize() == 2)
{
boost::int16_t v = source.getField<boost::int16_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
d = destination_dimension.removeScaling<double>(d);
if (destination_dimension.getByteSize() == 4)
destination.setField<float>(destination_dimension, destination_index, (float)d);
else if (destination_dimension.getByteSize() == 8)
destination.setField<double>(destination_dimension, destination_index, d);
else
throw pdal_error("Unable to convert dimension from SignedInteger 2 to Float of size > 8");
return;
}
else if (source_dimension.getByteSize() == 4)
{
boost::int32_t v = source.getField<boost::int32_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
d = destination_dimension.removeScaling<double>(d);
if (destination_dimension.getByteSize() == 4)
destination.setField<float>(destination_dimension, destination_index, (float)d);
else if (destination_dimension.getByteSize() == 8)
destination.setField<double>(destination_dimension, destination_index, d);
else
throw pdal_error("Unable to convert dimension from SignedInteger 4 to Float of size > 8");
return;
}
else if (source_dimension.getByteSize() == 8)
{
boost::int64_t v = source.getField<boost::int64_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
d = destination_dimension.removeScaling<double>(d);
if (destination_dimension.getByteSize() == 4)
destination.setField<float>(destination_dimension, destination_index, (float)d);
else if (destination_dimension.getByteSize() == 8)
destination.setField<double>(destination_dimension, destination_index, d);
else
throw pdal_error("Unable to convert dimension from SignedInteger 8 to Float of size > 8");
return;
}
else
{
throw pdal_error("Unable to convert dimension::SignedInteger >8 to dimension::Float of size >8");
}
}
}
if (source_dimension.getInterpretation() == dimension::UnsignedInteger)
{
if (destination_dimension.getInterpretation() == dimension::UnsignedInteger)
{
if (source_dimension.getByteSize() == 1)
{
boost::uint8_t v = source.getField<boost::uint8_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
if (destination_dimension.getByteSize() == 1)
{
boost::uint8_t i = destination_dimension.removeScaling<boost::uint8_t>(d);
destination.setField<boost::uint8_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 2)
{
boost::uint16_t i = destination_dimension.removeScaling<boost::uint16_t>(d);
destination.setField<boost::uint16_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 4)
{
boost::uint32_t i = destination_dimension.removeScaling<boost::uint32_t>(d);
destination.setField<boost::uint32_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 8)
{
boost::uint64_t i = destination_dimension.removeScaling<boost::uint64_t>(d);
destination.setField<boost::uint64_t>(destination_dimension, destination_index, i);
}
else
throw pdal_error("Unable to convert dimension from UnsignedInteger 8 to UnsignedInteger of size > 8");
return;
}
else if (source_dimension.getByteSize() == 2)
{
boost::uint16_t v = source.getField<boost::uint16_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
if (destination_dimension.getByteSize() == 1)
{
boost::uint8_t i = destination_dimension.removeScaling<boost::uint8_t>(d);
destination.setField<boost::uint8_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 2)
{
boost::uint16_t i = destination_dimension.removeScaling<boost::uint16_t>(d);
destination.setField<boost::uint16_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 4)
{
boost::uint32_t i = destination_dimension.removeScaling<boost::uint32_t>(d);
destination.setField<boost::uint32_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 8)
{
boost::uint64_t i = destination_dimension.removeScaling<boost::uint64_t>(d);
destination.setField<boost::uint64_t>(destination_dimension, destination_index, i);
}
else
throw pdal_error("Unable to convert dimension from UnsignedInteger 2 to UnsignedInteger of size > 8");
return;
}
else if (source_dimension.getByteSize() == 4)
{
boost::uint32_t v = source.getField<boost::uint32_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
if (destination_dimension.getByteSize() == 1)
{
boost::uint8_t i = destination_dimension.removeScaling<boost::uint8_t>(d);
destination.setField<boost::uint8_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 2)
{
boost::uint16_t i = destination_dimension.removeScaling<boost::uint16_t>(d);
destination.setField<boost::uint16_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 4)
{
boost::uint32_t i = destination_dimension.removeScaling<boost::uint32_t>(d);
destination.setField<boost::uint32_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 8)
{
boost::uint64_t i = destination_dimension.removeScaling<boost::uint64_t>(d);
destination.setField<boost::uint64_t>(destination_dimension, destination_index, i);
}
else
throw pdal_error("Unable to convert dimension from UnsignedInteger 4 to UnsignedInteger of size > 8");
return;
}
else if (source_dimension.getByteSize() == 8)
{
boost::uint64_t v = source.getField<boost::uint64_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
if (destination_dimension.getByteSize() == 1)
{
boost::uint8_t i = destination_dimension.removeScaling<boost::uint8_t>(d);
destination.setField<boost::uint8_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 2)
{
boost::uint16_t i = destination_dimension.removeScaling<boost::uint16_t>(d);
destination.setField<boost::uint16_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 4)
{
boost::uint32_t i = destination_dimension.removeScaling<boost::uint32_t>(d);
destination.setField<boost::uint32_t>(destination_dimension, destination_index, i);
}
else if (destination_dimension.getByteSize() == 8)
{
boost::uint64_t i = destination_dimension.removeScaling<boost::uint64_t>(d);
destination.setField<boost::uint64_t>(destination_dimension, destination_index, i);
}
else
throw pdal_error("Unable to convert dimension from UnsignedInteger 8 to UnsignedInteger of size > 8");
return;
}
else
{
throw pdal_error("Unable to convert dimension::UnsignedInteger >8 to dimension::UnsignedInteger of size >8");
}
}
else if (destination_dimension.getInterpretation() == dimension::Float)
{
if (source_dimension.getByteSize() == 1)
{
boost::uint8_t v = source.getField<boost::uint8_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
d = destination_dimension.removeScaling<double>(d);
if (destination_dimension.getByteSize() == 4)
destination.setField<float>(destination_dimension, destination_index, (float)d);
else if (destination_dimension.getByteSize() == 8)
destination.setField<double>(destination_dimension, destination_index, d);
else
throw pdal_error("Unable to convert dimension from UnsignedInteger 1 to Float of size > 8");
return;
}
else if (source_dimension.getByteSize() == 2)
{
boost::uint16_t v = source.getField<boost::uint16_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
d = destination_dimension.removeScaling<double>(d);
if (destination_dimension.getByteSize() == 4)
destination.setField<float>(destination_dimension, destination_index, (float)d);
else if (destination_dimension.getByteSize() == 8)
destination.setField<double>(destination_dimension, destination_index, d);
else
throw pdal_error("Unable to convert dimension from UnsignedInteger 2 to Float of size > 8");
return;
}
else if (source_dimension.getByteSize() == 4)
{
boost::uint32_t v = source.getField<boost::uint32_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
d = destination_dimension.removeScaling<double>(d);
if (destination_dimension.getByteSize() == 4)
destination.setField<float>(destination_dimension, destination_index, (float)d);
else if (destination_dimension.getByteSize() == 8)
destination.setField<double>(destination_dimension, destination_index, d);
else
throw pdal_error("Unable to convert dimension from UnsignedInteger 4 to Float of size > 8");
return;
}
else if (source_dimension.getByteSize() == 8)
{
boost::uint64_t v = source.getField<boost::uint64_t>(source_dimension, source_index);
double d = source_dimension.applyScaling<double>(v);
d = destination_dimension.removeScaling<double>(d);
if (destination_dimension.getByteSize() == 4)
destination.setField<float>(destination_dimension, destination_index, (float)d);
else if (destination_dimension.getByteSize() == 8)
destination.setField<double>(destination_dimension, destination_index, d);
else
throw pdal_error("Unable to convert dimension from UnsignedInteger 8 to Float of size > 8");
return;
}
else
{
throw pdal_error("Unable to convert dimension::UnsignedInteger >8 to dimension::Float of size >8");
}
}
}
throw pdal_error("Dimension data type unable to be scaled because it is Undefined");
}
#ifdef PDAL_COMPILER_MSVC
# pragma warning(pop)
#endif
#ifdef PDAL_COMPILER_MSVC
// when template T is know, std::numeric_limits<T>::is_exact is a constant...
# pragma warning(push)
# pragma warning(disable: 4127) // conditional expression is constant
#endif
// #pragma GCC diagnostic ignored "-Wfloat-equal"
// #pragma GCC diagnostic push
template <class T>
inline void PointBuffer::scale(Dimension const& source_dimension,
Dimension const& destination_dimension,
T& value)
{
double v = static_cast<double>(value);
double out = (v*source_dimension.getNumericScale() + source_dimension.getNumericOffset() - destination_dimension.getNumericOffset())/destination_dimension.getNumericScale();
T output = static_cast<T>(out);
if (std::numeric_limits<T>::is_exact) //
{
if (Utils::compare_distance<T>(output, (std::numeric_limits<T>::max)()))
{
std::ostringstream oss;
oss << "PointBuffer::scale: scale and/or offset combination causes "
"re-scaled value to be greater than std::numeric_limits::max for dimension '" << destination_dimension.getName() << "'. " <<
"value is: " << output << " and max() is: " << (std::numeric_limits<T>::max)();
}
else if (Utils::compare_distance<T>(output, (std::numeric_limits<T>::min)()))
{
std::ostringstream oss;
oss << "PointBuffer::scale: scale and/or offset combination causes "
"re-scaled value to be less than std::numeric_limits::min for dimension '" << destination_dimension.getName() << "'. " <<
"value is: " << output << " and min() is: " << (std::numeric_limits<T>::min)();
throw std::out_of_range(oss.str());
}
}
value = output;
return;
}
class IndexedPointBuffer : public PointBuffer
{
public:
IndexedPointBuffer(const Schema& schema, boost::uint32_t capacity=65536);
IndexedPointBuffer(IndexedPointBuffer const& buffer);
IndexedPointBuffer(PointBuffer const& buffer);
~IndexedPointBuffer();
// Satisfy nanoflann::DatasetAdapter
inline size_t kdtree_get_point_count() const
{
return PointBuffer::getNumPoints();
}
inline double kdtree_get_pt(const size_t idx, int dim) const
{
if (dim == 0)
return applyScaling(*m_dimX, idx);
if (dim == 1)
return applyScaling(*m_dimY, idx);
if (dim == 2)
return applyScaling(*m_dimZ, idx);
std::stringstream oss;
oss << "dimension '" << dim << "' is out of range 0-2";
throw std::runtime_error(oss.str());
}