Skip to content

Commit

Permalink
Python interface: Renames methods to match PEP8 style (#226)
Browse files Browse the repository at this point in the history
* Use separate folder for ruby and python swig files and tests
* Renames python interface to match PEP8 style
* Update python examples

Signed-off-by: Franco Cipollone <[email protected]>
Co-authored-by: Steve Peters <[email protected]>
  • Loading branch information
francocipollone and scpeters authored Aug 19, 2021
1 parent cf5e33c commit 86dc7a8
Show file tree
Hide file tree
Showing 35 changed files with 899 additions and 476 deletions.
26 changes: 13 additions & 13 deletions examples/angle_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@

import ignition.math

print("PI in degrees = {}\n".format(ignition.math.Angle.Pi.Degree()))
print("PI in degrees = {}\n".format(ignition.math.Angle.PI.degree()))

a1 = ignition.math.Angle(1.5707)
a2 = ignition.math.Angle(0.7854)
print("a1 = {} radians, {} degrees\n".format(a1.Radian(), a1.Degree()))
print("a2 = {} radians, {} degrees\n".format(a2.Radian(), a2.Degree()))
print("a1 * a2 = {} radians, {} degrees\n".format((a1 * a2).Radian(),
(a1 * a2).Degree()))
print("a1 + a2 = {} radians, {} degrees\n".format((a1 + a2).Radian(),
(a1 + a2).Degree()))
print("a1 - a2 = {} radians, {} degrees\n".format((a1 - a2).Radian(),
(a1 - a2).Degree()))
print("a1 = {} radians, {} degrees\n".format(a1.radian(), a1.degree()))
print("a2 = {} radians, {} degrees\n".format(a2.radian(), a2.degree()))
print("a1 * a2 = {} radians, {} degrees\n".format((a1 * a2).radian(),
(a1 * a2).degree()))
print("a1 + a2 = {} radians, {} degrees\n".format((a1 + a2).radian(),
(a1 + a2).degree()))
print("a1 - a2 = {} radians, {} degrees\n".format((a1 - a2).radian(),
(a1 - a2).degree()))

a3 = ignition.math.Angle(15.707)
print("a3 = {} radians, {} degrees\n".format(a3.Radian(), a3.Degree()))
a3.Normalize()
print("a3.Normalize = {} radians, {} degrees\n".format(a3.Radian(),
a3.Degree()))
print("a3 = {} radians, {} degrees\n".format(a3.radian(), a3.degree()))
a3.normalize()
print("a3.Normalize = {} radians, {} degrees\n".format(a3.radian(),
a3.degree()))
14 changes: 7 additions & 7 deletions examples/vector2_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
vb = ignition.math.Vector2d(3, 4)
vc = ignition.math.Vector2d(vb)

print("va = {} {}\n".format(va.X(), va.Y()))
print("vb = {} {}\n".format(vb.X(), vb.Y()))
print("vc = {} {}\n".format(vc.X(), vc.Y()))
print("va = {} {}\n".format(va.x(), va.y()))
print("vb = {} {}\n".format(vb.x(), vb.y()))
print("vc = {} {}\n".format(vc.x(), vc.y()))

vb += va
print("vb += va: {} {}\n".format(vb.X(), vb.Y()))
print("vb += va: {} {}\n".format(vb.x(), vb.y()))

vb.Normalize()
print("vb.Normalize = {} {}\n".format(vb.X(), vb.Y()))
vb.normalize()
print("vb.normalize = {} {}\n".format(vb.x(), vb.y()))

print("vb.Distance(va) = {}\n".format(vb.Distance(va)))
print("vb.distance(va) = {}\n".format(vb.distance(va)))
8 changes: 4 additions & 4 deletions examples/vector3_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import ignition.math

v1 = ignition.math.Vector3d(0, 0, 3)
print("v =: {} {} {}\n".format(v1.X(), v1.Y(), v1.Z()))
print("v =: {} {} {}\n".format(v1.x(), v1.y(), v1.z()))

v2 = ignition.math.Vector3d(4, 0, 0)
print("v2 = {} {} {}\n".format(v2.X(), v2.Y(), v2.Z()))
print("v2 = {} {} {}\n".format(v2.x(), v2.y(), v2.z()))

v3 = v1 + v2
print("v1 + v2 = {} {} {}\n".format(v3.X(), v3.Y(), v3.Z()))
print("v1 + v2 = {} {} {}\n".format(v3.x(), v3.y(), v3.z()))

print("v1.Distance(v2) = {}\n".format(v1.Distance(v2)))
print("v1.Distance(v2) = {}\n".format(v1.distance(v2)))
145 changes: 3 additions & 142 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,145 +12,6 @@ ign_build_tests(TYPE UNIT SOURCES ${gtest_sources})
# graph namespace
add_subdirectory(graph)

#################################################
# Setup swig
if (SWIG_FOUND)
if (POLICY CMP0078)
cmake_policy(SET CMP0078 NEW)
endif()
if (POLICY CMP0086)
cmake_policy(SET CMP0086 NEW)
endif()

include(${SWIG_USE_FILE})
set(CMAKE_SWIG_FLAGS "")

include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PYTHON_INCLUDE_PATH})

set(swig_files
Angle
GaussMarkovProcess
Rand
Vector2
Vector3
Vector4)
endif()

#################################################
# Create and install Ruby interfaces
# Example usage
# $ export RUBYLIB=/usr/local/lib/ruby
# $ ruby -e "require 'ignition/math'; a = Ignition::Math::Angle.new(20); puts a.Degree()"
if (RUBY_FOUND)
foreach (swig_file ${swig_files})
# Assuming that each swig file has a test
list(APPEND ruby_tests ${swig_file}_TEST)

# Generate the list if .i files
list(APPEND swig_i_files ${swig_file}.i)
endforeach()
list(APPEND ruby_tests ruby_TEST)

# Turn on c++
set_source_files_properties(${swig_i_files} ruby/ruby.i PROPERTIES CPLUSPLUS ON)

# Create the ruby library

set(CMAKE_SWIG_OUTDIR "${CMAKE_BINARY_DIR}/lib/ruby")
if(CMAKE_VERSION VERSION_GREATER 3.8.0)
SWIG_ADD_LIBRARY(math LANGUAGE ruby SOURCES ruby/ruby.i ${swig_i_files})
else()
SWIG_ADD_MODULE(math ruby ruby/ruby.i ${swig_i_files})
endif()

# Suppress warnings on SWIG-generated files
target_compile_options(math PRIVATE
$<$<CXX_COMPILER_ID:GNU>:-Wno-pedantic -Wno-shadow -Wno-maybe-uninitialized -Wno-unused-parameter>
$<$<CXX_COMPILER_ID:Clang>:-Wno-shadow -Wno-maybe-uninitialized -Wno-unused-parameter>
$<$<CXX_COMPILER_ID:AppleClang>:-Wno-shadow -Wno-maybe-uninitialized -Wno-unused-parameter>
)
target_include_directories(math SYSTEM PUBLIC ${RUBY_INCLUDE_DIRS})

SWIG_LINK_LIBRARIES(math
${RUBY_LIBRARY}
ignition-math${PROJECT_VERSION_MAJOR}
)
target_compile_features(math PUBLIC ${IGN_CXX_${c++standard}_FEATURES})
install(TARGETS math DESTINATION ${IGN_LIB_INSTALL_DIR}/ruby/ignition)

# Add the ruby tests
foreach (test ${ruby_tests})
add_test(NAME ${test}.rb COMMAND
ruby -I${CMAKE_BINARY_DIR}/lib ${CMAKE_SOURCE_DIR}/src/${test}.rb
--gtest_output=xml:${CMAKE_BINARY_DIR}/test_results/${test}rb.xml)
endforeach()
endif()

#################################
# Create and install Python interfaces
# Example usage
# $ export PYTHONPATH=/ws/install/lib/python/:$PYTHONPATH
if (PYTHONLIBS_FOUND)
set_source_files_properties(python/python.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties(python/python.i PROPERTIES SWIG_FLAGS "-includeall")
set_source_files_properties(python/python.i PROPERTIES SWIG_MODULE_NAME "math")
set(SWIG_PY_LIB pymath)
set(SWIG_PY_LIB_OUTPUT math)

set(CMAKE_SWIG_OUTDIR "${CMAKE_BINARY_DIR}/lib/python")
if(CMAKE_VERSION VERSION_GREATER 3.8.0)
SWIG_ADD_LIBRARY(${SWIG_PY_LIB} LANGUAGE python SOURCES python/python.i)
else()
SWIG_ADD_MODULE(${SWIG_PY_LIB} python python/python.i)
endif()

SWIG_LINK_LIBRARIES(${SWIG_PY_LIB}
${PYTHON_LIBRARIES}
ignition-math${PROJECT_VERSION_MAJOR}
)

if(NOT CMAKE_VERSION VERSION_GREATER_EQUAL 3.13.0)
set(SWIG_PY_LIB "_${SWIG_PY_LIB}")
set(SWIG_PY_LIB_OUTPUT "_${SWIG_PY_LIB_OUTPUT}")
endif()

set_target_properties(${SWIG_PY_LIB}
PROPERTIES
OUTPUT_NAME ${SWIG_PY_LIB_OUTPUT}
)

# Suppress warnings on SWIG-generated files
target_compile_options(${SWIG_PY_LIB} PRIVATE
$<$<CXX_COMPILER_ID:GNU>:-Wno-pedantic -Wno-shadow -Wno-maybe-uninitialized -Wno-unused-parameter -Wno-cast-function-type -Wno-missing-field-initializers>
$<$<CXX_COMPILER_ID:Clang>:-Wno-shadow -Wno-maybe-uninitialized -Wno-unused-parameter -Wno-cast-function-type -Wno-missing-field-initializers>
$<$<CXX_COMPILER_ID:AppleClang>:-Wno-shadow -Wno-maybe-uninitialized -Wno-unused-parameter -Wno-cast-function-type -Wno-missing-field-initializers>
)
install(TARGETS ${SWIG_PY_LIB} DESTINATION ${IGN_LIB_INSTALL_DIR}/python/ignition)
install(FILES ${CMAKE_BINARY_DIR}/lib/python/math.py DESTINATION ${IGN_LIB_INSTALL_DIR}/python/ignition)

if (BUILD_TESTING)
# Add the Python tests
set(python_tests
Angle_TEST
GaussMarkovProcess_TEST
python_TEST
Rand_TEST
Vector2_TEST
Vector3_TEST
Vector4_TEST
)

foreach (test ${python_tests})
add_test(NAME ${test}.py COMMAND
python3 ${CMAKE_SOURCE_DIR}/src/${test}.py)

set(_env_vars)
list(APPEND _env_vars "PYTHONPATH=${FAKE_INSTALL_PREFIX}/lib/python/")
list(APPEND _env_vars "LD_LIBRARY_PATH=${FAKE_INSTALL_PREFIX}/lib:$ENV{LD_LIBRARY_PATH}")
set_tests_properties(${test}.py PROPERTIES
ENVIRONMENT "${_env_vars}")
endforeach()
endif()

endif()
# Bindings subdirectories
add_subdirectory(python)
add_subdirectory(ruby)
47 changes: 0 additions & 47 deletions src/Rand_TEST.py

This file was deleted.

59 changes: 59 additions & 0 deletions src/python/Angle.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

%module angle
%{
#include <ignition/math/Angle.hh>
%}

namespace ignition
{
namespace math
{
class Angle
{
%rename("%(undercase)s", %$isfunction, %$ismember, %$not %$isconstructor) "";
%rename("%(uppercase)s", %$isstatic, %$isvariable) "";
public: static const Angle Zero;
public: static const Angle Pi;
%rename(HALF_PI) HalfPi;
public: static const Angle HalfPi;
%rename(TWO_PI) TwoPi;
public: static const Angle TwoPi;
public: Angle();
public: Angle(double _radian);
public: Angle(const Angle &_angle);
public: virtual ~Angle();
public: void SetRadian(double _radian);
public: void SetDegree(double _degree);
public: double Radian() const;
public: double Degree() const;
public: void Normalize();
public: Angle Normalized() const;
public: inline double operator*() const;
public: Angle operator-(const Angle &_angle) const;
public: Angle operator+(const Angle &_angle) const;
public: Angle operator*(const Angle &_angle) const;
public: Angle operator/(const Angle &_angle) const;
public: bool operator==(const Angle &_angle) const;
public: bool operator<(const Angle &_angle) const;
public: bool operator<=(const Angle &_angle) const;
public: bool operator>(const Angle &_angle) const;
public: bool operator>=(const Angle &_angle) const;
};
}
}
Loading

0 comments on commit 86dc7a8

Please sign in to comment.