[Liblas-commits] hg: default to using reinterpret_cast and assuming the data are ...

liblas-commits at liblas.org liblas-commits at liblas.org
Fri Jan 7 10:54:13 EST 2011


details:   http://hg.liblas.orghg/rev/8034511a1aff
changeset: 2703:8034511a1aff
user:      Howard Butler <hobu.inc at gmail.com>
date:      Fri Jan 07 09:54:07 2011 -0600
description:
default to using reinterpret_cast and assuming the data are in little endian order instead of using bitsToInt and friends.  You can use the other, more proper approach by defining WITH_ENDIANAWARE in your cmake configuration

diffstat:

 CMakeLists.txt |    5 ++
 src/point.cpp  |  117 +++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 91 insertions(+), 31 deletions(-)

diffs (230 lines):

diff -r b345fa32dc3d -r 8034511a1aff CMakeLists.txt
--- a/CMakeLists.txt	Fri Jan 07 09:25:10 2011 -0600
+++ b/CMakeLists.txt	Fri Jan 07 09:54:07 2011 -0600
@@ -56,6 +56,8 @@
 set(WITH_PKGCONFIG FALSE CACHE BOOL
   "Choose whether a pkgconfig file (libLAS.pc) should be installed")
 
+set(WITH_ENDIANAWARE FALSE CACHE BOOL "Choose whether or not libLAS should do runtime endianness switching.  Note that this can slow things down considerably if enabled by default")
+
 # Enable CTest and submissions to libLAS dashboard at CDash
 # http://my.cdash.org/index.php?project=libLAS
 set(ENABLE_CTEST FALSE CACHE BOOL
@@ -306,6 +308,9 @@
   endif()
 endif()
 
+if(WITH_ENDIANAWARE)
+    add_definitions(-DLIBLAS_ENDIAN_AWARE=1)
+endif()
 ###############################################################################
 # Installation settings
 
diff -r b345fa32dc3d -r 8034511a1aff src/point.cpp
--- a/src/point.cpp	Fri Jan 07 09:25:10 2011 -0600
+++ b/src/point.cpp	Fri Jan 07 09:54:07 2011 -0600
@@ -479,9 +479,15 @@
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionPosition("X");
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionBytePosition(0);
     std::vector<boost::uint8_t>::size_type pos = 0;
+    
+#ifdef LIBLAS_ENDIAN_AWARE
     boost::int32_t output = liblas::detail::bitsToInt<boost::int32_t>(output, m_data, pos);
-
-    return output;
+    return output
+#else
+    boost::uint8_t* data = const_cast<boost::uint8_t*>(&m_data[0] + pos);
+    boost::int32_t* output = reinterpret_cast<boost::int32_t*>(data);
+    return *output;
+#endif
 }
 
 boost::int32_t Point::GetRawY() const
@@ -489,20 +495,31 @@
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionPosition("Y");
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionBytePosition(1);
     std::vector<boost::uint8_t>::size_type pos = 4;
+
+#ifdef LIBLAS_ENDIAN_AWARE
     boost::int32_t output = liblas::detail::bitsToInt<boost::int32_t>(output, m_data, pos);
-
-    return output;
+    return output
+#else
+    boost::uint8_t* data = const_cast<boost::uint8_t*>(&m_data[0] + pos);
+    boost::int32_t* output = reinterpret_cast<boost::int32_t*>(data);
+    return *output;
+#endif
 }
 
 boost::int32_t Point::GetRawZ() const
 {
-    boost::int32_t output;
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionPosition("Z");
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionBytePosition(2);
     std::vector<boost::uint8_t>::size_type pos = 8;
-    output = liblas::detail::bitsToInt<boost::int32_t>(output, m_data, pos);
 
-    return output;
+#ifdef LIBLAS_ENDIAN_AWARE
+    boost::int32_t output = liblas::detail::bitsToInt<boost::int32_t>(output, m_data, pos);
+    return output
+#else
+    boost::uint8_t* data = const_cast<boost::uint8_t*>(&m_data[0] + pos);
+    boost::int32_t* output = reinterpret_cast<boost::int32_t*>(data);
+    return *output;
+#endif
 }
 
 void Point::SetRawX( boost::int32_t const& value)
@@ -533,12 +550,17 @@
 {
     // Intensity's position is always the 4th dimension
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionBytePosition(3);
-    std::vector<boost::uint8_t>::size_type pos = 12;    
-    boost::uint16_t output = 
-        liblas::detail::bitsToInt<boost::uint16_t>(
-            output, m_data, pos);
+    std::vector<boost::uint8_t>::size_type pos = 12;
 
-    return output;
+#ifdef LIBLAS_ENDIAN_AWARE
+    boost::uint16_t output = liblas::detail::bitsToInt<boost::int32_t>(output, m_data, pos);
+    return output
+#else
+    boost::uint8_t* data = const_cast<boost::uint8_t*>(&m_data[0] + pos);
+    boost::uint16_t* output = reinterpret_cast<boost::uint16_t*>(data);
+    return *output;
+#endif
+
 }
 
 
@@ -753,17 +775,20 @@
 
 boost::uint16_t Point::GetPointSourceID() const
 {
-    boost::uint16_t output;
-    
+
     // "Point Source ID" is always the 12th dimension    
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionBytePosition(11);
     std::vector<boost::uint8_t>::size_type pos = 18;
-    
-    output = liblas::detail::bitsToInt<boost::uint16_t>(output, 
-                                                        m_data, 
-                                                        pos);
 
-    return output;
+#ifdef LIBLAS_ENDIAN_AWARE
+    boost::uint16_t output = liblas::detail::bitsToInt<boost::int32_t>(output, m_data, pos);
+    return output
+#else
+    boost::uint8_t* data = const_cast<boost::uint8_t*>(&m_data[0] + pos);
+    boost::uint16_t* output = reinterpret_cast<boost::uint16_t*>(data);
+    return *output;
+#endif
+
 }
 
 void Point::SetPointSourceID(boost::uint16_t const& id)
@@ -795,7 +820,7 @@
     }
 
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionBytePosition(index_pos);
-    std::vector<boost::uint8_t>::size_type pos = 20;    
+    std::vector<boost::uint8_t>::size_type pos = 20;
     detail::binary::endian_value<double> value(t);
     value.store<detail::binary::little_endian_tag>(&m_data[0] + pos);
 }
@@ -825,22 +850,29 @@
     // std::size_t const index_pos = 12;
     // std::vector<boost::uint8_t>::size_type pos = GetDimensionBytePosition(index_pos);
     std::vector<boost::uint8_t>::size_type pos = 20;   
-    
-    detail::binary::endian_value<double> value;
-    value.load<detail::binary::little_endian_tag>(&m_data[0] + pos);
-    return value;
+
+#ifdef LIBLAS_ENDIAN_AWARE
+    detail::binary::endian_value<double> output;
+    output.load<detail::binary::little_endian_tag>(&m_data[0] + pos);
+    return output;
+#else
+    boost::uint8_t* data = const_cast<boost::uint8_t*>(&m_data[0] + pos);
+    double* output = reinterpret_cast<double*>(data);
+    return *output;
+#endif
+
 }
 
 Color Point::GetColor() const
 {
-    boost::uint16_t red(0);
-    boost::uint16_t green(0);
-    boost::uint16_t blue(0);
+
 
     // "Color" starts at the 14th dimension if it exists
     // std::size_t index_pos = 13;
 
+    Color color;
     PointFormatName f;
+    
     if (m_header) {
         f = m_header->GetDataFormatId();
     } else {
@@ -848,12 +880,11 @@
     }   
     
     if ( f == ePointFormat0 || f == ePointFormat1 ) {
-        return Color(0, 0, 0);
+        return color;
     }
     
     assert(!(f == ePointFormat0 || f == ePointFormat1));
     
-    using liblas::detail::bitsToInt;
     
     std::size_t index_pos = 20;
 
@@ -869,12 +900,36 @@
     assert(red_pos <= m_data.size());
     assert(blue_pos <= m_data.size());
     assert(green_pos <= m_data.size());
-    
+
+#ifdef LIBLAS_ENDIAN_AWARE
+    using liblas::detail::bitsToInt;
+    boost::uint16_t red(0);
+    boost::uint16_t green(0);
+    boost::uint16_t blue(0);
     red = bitsToInt<boost::uint16_t>(red, m_data, red_pos);
     green = bitsToInt<boost::uint16_t>(green, m_data, green_pos);
     blue = bitsToInt<boost::uint16_t>(blue, m_data, blue_pos);
+    color[0] = red;
+    color[1] = green;
+    color[2] = blue;
+#else
+    boost::uint8_t* red_data = const_cast<boost::uint8_t*>(&m_data[0] + red_pos);
+    boost::uint16_t* p_red = reinterpret_cast<boost::uint16_t*>(red_data);
 
-  return Color(red, green, blue);
+    boost::uint8_t* green_data = const_cast<boost::uint8_t*>(&m_data[0] + green_pos);
+    boost::uint16_t* p_green = reinterpret_cast<boost::uint16_t*>(green_data);
+
+    boost::uint8_t* blue_data = const_cast<boost::uint8_t*>(&m_data[0] + blue_pos);
+    boost::uint16_t* p_blue = reinterpret_cast<boost::uint16_t*>(blue_data);
+
+    color[0] = *p_red;
+    color[1] = *p_green;
+    color[2] = *p_blue;    
+#endif
+    
+    return color;
+
+
 }
 
 void Point::SetColor(Color const& value)


More information about the Liblas-commits mailing list