Skip to content

Instantly share code, notes, and snippets.

@melund
Created December 19, 2019 23:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melund/7588156c0f962eb1b41160bc0cd0c813 to your computer and use it in GitHub Desktop.
Save melund/7588156c0f962eb1b41160bc0cd0c813 to your computer and use it in GitHub Desktop.
Updates to BTK for Python3 support
From 86e1ad72b08e0a421efda791e87ed77c87b2866d Mon Sep 17 00:00:00 2001
From: Allard Hoeve <allardhoeve@gmail.com>
Date: Thu, 28 Mar 2019 12:03:41 +0100
Subject: [PATCH] Python 3 support
Enable Python 3 build
Moved the *.tpp includes from the .h to the .cpp.
Fix the problem with using BTK in other libraries (like OpenSim) on Windows.
Problem -- Including the BTK headers causes an error on Windows with VS2015.
" .... cstdint: 'uint8_t' is not a member of global namespace .... ".
The error appears to be in the standard library but is actually
caused by the fact that Utilities/stdint.h is included for MSVC
builds and the directory Utilities is in the include path for the
compiler. When trying to search for a header named stdint.h, the
compiler keeps running into Utilities/stdint.h even when the
original intention was to find the header in the system standard
path.
Solution -- Rename the file Utilities/stdint.h to something else that does not
conflict with standard library header of the same name therefore
avoiding compiler choosing this header when standard library header
is meant to be found.
Fix RPATH for OSX.
Fix from https://github.com/Biomechanical-ToolKit/BTKCore/issues/28
Remove xrange
Avoid tr1 with VS2017
https://github.com/Biomechanical-ToolKit/BTKCore/issues/28
Fix problem with test
Fixes a problem with unittest.assertAlmostEqual not working on numpy ndarrays.
Reporting that ndarray doesn't have a __round__ methods.
Fix test error
---
CMake/FindNumPy.cmake | 4 ++--
CMakeLists.txt | 2 ++
Code/Common/btkConvert.h | 2 +-
Code/Common/btkIMU.h | 4 ++--
Code/Common/btkMetaDataInfo.h | 2 +-
Code/Common/btkSharedPtr.h | 16 +++++++++++-----
Code/IO/btkBSFFileIO.cpp | 2 +-
Code/IO/btkBSFFileIO.h | 2 +-
Code/IO/btkBinaryByteOrderFormat.h | 4 ++--
Code/IO/btkBinaryFileStream.h | 4 +---
Code/IO/btkBinaryStream.h | 4 +---
Code/IO/btkC3DFileIO.cpp | 2 ++
Code/IO/btkGRxFileIO.cpp | 2 ++
Code/IO/btkHPFFileIO.cpp | 2 ++
Code/IO/btkMotionAnalysisFileIOUtils.h | 4 ++--
Code/IO/btkTDFFileIO.cpp | 2 ++
Code/IO/btkTRBFileIO.cpp | 2 ++
Packaging/Python/setuptools/setup.py.in | 2 +-
Testing/Python/AcquisitionUnitConverterTest.py | 11 +++++++++++
Testing/Python/C3DFileReaderTest.py | 11 +++++++++++
Testing/Python/C3DFileWriterTest.py | 13 +++++++++++--
Testing/Python/ForcePlatformsExtractorTest.py | 11 +++++++++++
Utilities/eigen3/Eigen/src/Core/VectorwiseOp.h | 4 ++++
Utilities/{stdint.h => msvc_stdint.h} | 0
24 files changed, 86 insertions(+), 26 deletions(-)
rename Utilities/{stdint.h => msvc_stdint.h} (100%)
diff --git a/CMake/FindNumPy.cmake b/CMake/FindNumPy.cmake
index 31b0d052..b5ba8aef 100644
--- a/CMake/FindNumPy.cmake
+++ b/CMake/FindNumPy.cmake
@@ -16,7 +16,7 @@ FIND_PACKAGE(PythonInterp REQUIRED)
# Look for the include path
# WARNING: The variable PYTHON_EXECUTABLE is defined by the script FindPythonInterp.cmake
-EXECUTE_PROCESS(COMMAND "${PYTHON_EXECUTABLE}" -c "import numpy; print numpy.get_include(); print numpy.version.version"
+EXECUTE_PROCESS(COMMAND "${PYTHON_EXECUTABLE}" -c "from __future__ import print_function; import numpy; print(numpy.get_include()); print(numpy.version.version)"
OUTPUT_VARIABLE NUMPY_OUTPUT
ERROR_VARIABLE NUMPY_ERROR)
@@ -30,4 +30,4 @@ INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NumPy DEFAULT_MSG NUMPY_VERSION NUMPY_INCLUDE_DIR)
-MARK_AS_ADVANCED(NUMPY_INCLUDE_DIR)
\ No newline at end of file
+MARK_AS_ADVANCED(NUMPY_INCLUDE_DIR)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c9f2c0fa..dd59d64c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -21,6 +21,8 @@ SET(BTK_LIBRARY_VERSION_STRING "${BTK_VERSION_MAJOR}.${BTK_VERSION_MINOR}")
# Using the configuration "Always full RPATH"
# from http://www.cmake.org/Wiki/CMake_RPATH_handling
# --------------------------
+# To silence warning for policy CMP0042.
+SET(CMAKE_MACOSX_RPATH ON)
# use, i.e. don't skip the full RPATH for the build tree
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
# when building, don't use the install RPATH already
diff --git a/Code/Common/btkConvert.h b/Code/Common/btkConvert.h
index e0dd5f30..da80b605 100644
--- a/Code/Common/btkConvert.h
+++ b/Code/Common/btkConvert.h
@@ -45,7 +45,7 @@
#include <limits>
#ifdef _MSC_VER
- #include "Utilities/stdint.h"
+ #include "Utilities/msvc_stdint.h"
#else
#include <stdint.h>
#endif
diff --git a/Code/Common/btkIMU.h b/Code/Common/btkIMU.h
index 41778bed..d6db7aad 100644
--- a/Code/Common/btkIMU.h
+++ b/Code/Common/btkIMU.h
@@ -67,7 +67,7 @@ namespace btk
BTK_COMMON_EXPORT Analog::ConstPointer GetChannel(int id) const;
BTK_COMMON_EXPORT Analog::Pointer GetChannel(const std::string& label);
BTK_COMMON_EXPORT Analog::ConstPointer GetChannel(const std::string& label) const;
- BTK_COMMON_EXPORT int GetChannelNumber() const {return this->m_Channels.size();};
+ BTK_COMMON_EXPORT int GetChannelNumber() const {return (int)this->m_Channels.size();};
int GetType() const {return this->m_Type;};
@@ -141,4 +141,4 @@ namespace btk
CalMatrix m_CalMatrix;
};
};
-#endif
+#endif
\ No newline at end of file
diff --git a/Code/Common/btkMetaDataInfo.h b/Code/Common/btkMetaDataInfo.h
index 3a34caa3..fdd65d9a 100644
--- a/Code/Common/btkMetaDataInfo.h
+++ b/Code/Common/btkMetaDataInfo.h
@@ -43,7 +43,7 @@
#include <vector>
#ifdef _MSC_VER
- #include "Utilities/stdint.h"
+ #include "Utilities/msvc_stdint.h"
#else
#include <stdint.h>
#endif
diff --git a/Code/Common/btkSharedPtr.h b/Code/Common/btkSharedPtr.h
index 14f915c9..62db9c42 100644
--- a/Code/Common/btkSharedPtr.h
+++ b/Code/Common/btkSharedPtr.h
@@ -58,12 +58,18 @@
#define btkSharedPtr std::shared_ptr
using std::static_pointer_cast;
#endif
-#elif defined(_MSC_VER) && (_MSC_VER >= 1500)
- /* included with MSVC 2008 SP1 */
- #if defined(HAVE_SYS_MEMORY_H)
+#elif defined(_MSC_VER)
+ #if (_MSC_VER >= 1910)
#include <memory>
- #define btkSharedPtr std::tr1::shared_ptr
- using std::tr1::static_pointer_cast;
+ #define btkSharedPtr std::shared_ptr
+ using std::static_pointer_cast;
+ #elif (_MSC_VER >= 1500)
+ /* included with MSVC 2008 SP1 */
+ #if defined(HAVE_SYS_MEMORY_H)
+ #include <memory>
+ #define btkSharedPtr std::tr1::shared_ptr
+ using std::tr1::static_pointer_cast;
+ #endif
#endif
#elif defined(HAVE_BOOST_MEMORY_HPP)
#include <boost/memory.hpp>
diff --git a/Code/IO/btkBSFFileIO.cpp b/Code/IO/btkBSFFileIO.cpp
index 4ec7acb9..dbd3ad58 100644
--- a/Code/IO/btkBSFFileIO.cpp
+++ b/Code/IO/btkBSFFileIO.cpp
@@ -36,7 +36,7 @@
#include "btkBSFFileIO.h"
#include "btkBinaryFileStream.h"
#include "btkMetaDataUtils.h"
-
+#include "btkBinaryStream.tpp"
// Number found in the documentation.
const int _btk_bsf_maximum_channel_number = 32;
diff --git a/Code/IO/btkBSFFileIO.h b/Code/IO/btkBSFFileIO.h
index c3ff3d8f..a7ed88c5 100644
--- a/Code/IO/btkBSFFileIO.h
+++ b/Code/IO/btkBSFFileIO.h
@@ -40,7 +40,7 @@
#include "btkException.h"
#ifdef _MSC_VER
- #include "Utilities/stdint.h"
+ #include "Utilities/msvc_stdint.h"
#else
#include <stdint.h>
#endif
diff --git a/Code/IO/btkBinaryByteOrderFormat.h b/Code/IO/btkBinaryByteOrderFormat.h
index f3799f98..6e294e4c 100644
--- a/Code/IO/btkBinaryByteOrderFormat.h
+++ b/Code/IO/btkBinaryByteOrderFormat.h
@@ -63,7 +63,7 @@
// MSVC doesn't have the header stdint.h
#ifdef _MSC_VER
- #include "Utilities/stdint.h"
+ #include "Utilities/msvc_stdint.h"
#else
#include <stdint.h>
#endif
@@ -887,4 +887,4 @@ namespace btk
};
};
-#endif // __btkByteOrderFormat_h
\ No newline at end of file
+#endif // __btkByteOrderFormat_h
diff --git a/Code/IO/btkBinaryFileStream.h b/Code/IO/btkBinaryFileStream.h
index ce82e5be..a5514cb5 100644
--- a/Code/IO/btkBinaryFileStream.h
+++ b/Code/IO/btkBinaryFileStream.h
@@ -56,7 +56,7 @@
// MSVC doesn't have the header stdint.h
#ifdef _MSC_VER
- #include "Utilities/stdint.h"
+ #include "Utilities/msvc_stdint.h"
#else
#include <stdint.h>
#endif
@@ -250,6 +250,4 @@ namespace btk
};
};
-#include "btkBinaryFileStream.tpp"
-
#endif // __btkBinaryFileStream_h
diff --git a/Code/IO/btkBinaryStream.h b/Code/IO/btkBinaryStream.h
index 15bd2ee2..4b429307 100644
--- a/Code/IO/btkBinaryStream.h
+++ b/Code/IO/btkBinaryStream.h
@@ -41,7 +41,7 @@
// MSVC doesn't have the header stdint.h
#ifdef _MSC_VER
- #include "Utilities/stdint.h"
+ #include "Utilities/msvc_stdint.h"
#else
#include <stdint.h>
#endif
@@ -150,6 +150,4 @@ namespace btk
};
};
-#include "btkBinaryStream.tpp"
-
#endif // __btkBinaryStream_h
diff --git a/Code/IO/btkC3DFileIO.cpp b/Code/IO/btkC3DFileIO.cpp
index c39aea9e..0121bfe2 100644
--- a/Code/IO/btkC3DFileIO.cpp
+++ b/Code/IO/btkC3DFileIO.cpp
@@ -38,6 +38,8 @@
#include "btkConvert.h"
#include "btkLogger.h"
+#include "btkBinaryStream.tpp"
+
#include <algorithm>
#include <cctype>
#include <iostream>
diff --git a/Code/IO/btkGRxFileIO.cpp b/Code/IO/btkGRxFileIO.cpp
index 8e94d855..5dce3f3e 100644
--- a/Code/IO/btkGRxFileIO.cpp
+++ b/Code/IO/btkGRxFileIO.cpp
@@ -38,6 +38,8 @@
#include "btkMetaDataUtils.h"
#include "btkWrench.h"
+#include "btkBinaryStream.tpp"
+
#include <fstream>
#include <algorithm>
#include <cctype>
diff --git a/Code/IO/btkHPFFileIO.cpp b/Code/IO/btkHPFFileIO.cpp
index f17462bf..b6abe2ca 100644
--- a/Code/IO/btkHPFFileIO.cpp
+++ b/Code/IO/btkHPFFileIO.cpp
@@ -39,6 +39,8 @@
#include "btkConvert.h"
#include "btkLogger.h"
+#include "btkBinaryStream.tpp"
+
#include <limits>
#include <pugixml.hpp>
diff --git a/Code/IO/btkMotionAnalysisFileIOUtils.h b/Code/IO/btkMotionAnalysisFileIOUtils.h
index 4a175e94..acda0d6a 100644
--- a/Code/IO/btkMotionAnalysisFileIOUtils.h
+++ b/Code/IO/btkMotionAnalysisFileIOUtils.h
@@ -41,7 +41,7 @@
#include "btkException.h"
#ifdef _MSC_VER
- #include "Utilities/stdint.h"
+ #include "Utilities/msvc_stdint.h"
#else
#include <stdint.h>
#endif
@@ -82,4 +82,4 @@ namespace btk
};
};
-#endif // __btkMotionAnalysisFileIOUtils_h
\ No newline at end of file
+#endif // __btkMotionAnalysisFileIOUtils_h
diff --git a/Code/IO/btkTDFFileIO.cpp b/Code/IO/btkTDFFileIO.cpp
index d77486b9..3686ba5a 100644
--- a/Code/IO/btkTDFFileIO.cpp
+++ b/Code/IO/btkTDFFileIO.cpp
@@ -39,6 +39,8 @@
#include "btkMetaDataUtils.h"
#include "btkLogger.h"
+#include "btkBinaryStream.tpp"
+
#include <algorithm>
#include <cctype>
diff --git a/Code/IO/btkTRBFileIO.cpp b/Code/IO/btkTRBFileIO.cpp
index 7a7d2b2e..b0e6a6dc 100644
--- a/Code/IO/btkTRBFileIO.cpp
+++ b/Code/IO/btkTRBFileIO.cpp
@@ -37,6 +37,8 @@
#include "btkMetaDataUtils.h"
#include "btkConvert.h"
+#include "btkBinaryFileStream.tpp"
+
#include <algorithm>
#include <cctype>
#include <vector>
diff --git a/Packaging/Python/setuptools/setup.py.in b/Packaging/Python/setuptools/setup.py.in
index 51095843..b815e0cc 100644
--- a/Packaging/Python/setuptools/setup.py.in
+++ b/Packaging/Python/setuptools/setup.py.in
@@ -19,7 +19,7 @@ elif platform.system() == 'Linux':
bdist_original_filename = ''
bdist_final_filename = ''
if len(sys.argv) > 1:
- for i in xrange(1,len(sys.argv)):
+ for i in range(1,len(sys.argv)):
if (sys.argv[i] == 'bdist_egg'):
bdist_original_filename = name + '-' + version + '-py' + get_python_version() + '.egg'
bdist_final_filename = name + '-' + version + '-py' + get_python_version() + '_' + get_build_platform() + '.egg'
diff --git a/Testing/Python/AcquisitionUnitConverterTest.py b/Testing/Python/AcquisitionUnitConverterTest.py
index 78c87387..3618c23b 100644
--- a/Testing/Python/AcquisitionUnitConverterTest.py
+++ b/Testing/Python/AcquisitionUnitConverterTest.py
@@ -3,6 +3,17 @@ import unittest
import _TDDConfigure
class AcquisitionUnitConverterTest(unittest.TestCase):
+
+ def assertAlmostEqual(self, first, second, places=7, msg=None, delta=None):
+ try:
+ super().assertAlmostEqual(first,second,places,msg, delta)
+ except TypeError:
+ first = float(first)
+ second = float(second)
+ super().assertAlmostEqual(first,second,places,msg, delta)
+
+
+
def test_NoInputNoConversion(self):
uc = btk.btkAcquisitionUnitConverter()
uc.Update()
diff --git a/Testing/Python/C3DFileReaderTest.py b/Testing/Python/C3DFileReaderTest.py
index 01614ede..17438428 100755
--- a/Testing/Python/C3DFileReaderTest.py
+++ b/Testing/Python/C3DFileReaderTest.py
@@ -17,6 +17,17 @@ class C3DFileReaderTest(unittest.TestCase):
self.assertRaises(RuntimeError, reader.Update)
# self.assertRaisesRegexp(RuntimeError, 'File doesn\'t exist\nFilename: test.c3d', reader.Update) # Python 2.7+
+
+ def assertAlmostEqual(self, first, second, places=7, msg=None, delta=None):
+ try:
+ super().assertAlmostEqual(first,second,places,msg, delta)
+ except TypeError:
+ first = float(first)
+ second = float(second)
+ super().assertAlmostEqual(first,second,places,msg, delta)
+
+
+
def test_Sample01_Eb015pi(self):
reader = btk.btkAcquisitionFileReader()
reader.SetFilename(_TDDConfigure.C3DFilePathIN + 'sample01/Eb015pi.c3d')
diff --git a/Testing/Python/C3DFileWriterTest.py b/Testing/Python/C3DFileWriterTest.py
index 741e9100..db74769f 100644
--- a/Testing/Python/C3DFileWriterTest.py
+++ b/Testing/Python/C3DFileWriterTest.py
@@ -5,6 +5,15 @@ import unittest
import _TDDConfigure
class C3DFileWriterTest(unittest.TestCase):
+
+ def assertAlmostEqual(self, first, second, places=7, msg=None, delta=None):
+ try:
+ super().assertAlmostEqual(first,second,places,msg, delta)
+ except TypeError:
+ first = float(first)
+ second = float(second)
+ super().assertAlmostEqual(first,second,places,msg, delta)
+
def test_NoFileNoInput(self):
writer = btk.btkAcquisitionFileWriter()
self.assertRaises(RuntimeError, writer.Update)
@@ -1172,11 +1181,11 @@ class C3DFileWriterTest(unittest.TestCase):
dims = metadata.GetChild('DESCRIPTIONS').GetInfo().GetDimensions()
blank = metadata.GetChild('DESCRIPTIONS').GetInfo().ToString()
self.assertEqual(dims[0], 0)
- for i in xrange(0,num):
+ for i in range(0,num):
self.assertEqual(blank[i], '')
dims = metadata.GetChild('SUBJECTS').GetInfo().GetDimensions()
blank = metadata.GetChild('SUBJECTS').GetInfo().ToString()
self.assertEqual(dims[0], 0)
- for i in xrange(0,num):
+ for i in range(0,num):
self.assertEqual(blank[i], '')
\ No newline at end of file
diff --git a/Testing/Python/ForcePlatformsExtractorTest.py b/Testing/Python/ForcePlatformsExtractorTest.py
index d052395d..a21945f5 100644
--- a/Testing/Python/ForcePlatformsExtractorTest.py
+++ b/Testing/Python/ForcePlatformsExtractorTest.py
@@ -4,6 +4,17 @@ import _TDDConfigure
import numpy
class ForcePlatformsExtractorTest(unittest.TestCase):
+
+ def assertAlmostEqual(self, first, second, places=7, msg=None, delta=None):
+ try:
+ super().assertAlmostEqual(first,second,places,msg, delta)
+ except TypeError:
+ first = float(first)
+ second = float(second)
+ super().assertAlmostEqual(first,second,places,msg, delta)
+
+
+
def test_FileSample01Eb015pi(self):
reader = btk.btkAcquisitionFileReader()
reader.SetFilename(_TDDConfigure.C3DFilePathIN + "sample01/Eb015pi.c3d")
diff --git a/Utilities/eigen3/Eigen/src/Core/VectorwiseOp.h b/Utilities/eigen3/Eigen/src/Core/VectorwiseOp.h
index d5ab0366..06c8b9c0 100644
--- a/Utilities/eigen3/Eigen/src/Core/VectorwiseOp.h
+++ b/Utilities/eigen3/Eigen/src/Core/VectorwiseOp.h
@@ -582,6 +582,10 @@ template<typename ExpressionType, int Direction> class VectorwiseOp
HNormalizedReturnType;
const HNormalizedReturnType hnormalized() const;
+
+ #ifdef EIGEN_VECTOROP_PLUGIN
+ #include EIGEN_VECTOROP_PLUGIN
+ #endif
protected:
ExpressionTypeNested m_matrix;
diff --git a/Utilities/stdint.h b/Utilities/msvc_stdint.h
similarity index 100%
rename from Utilities/stdint.h
rename to Utilities/msvc_stdint.h
--
2.23.0.windows.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment