From 1add931c42302847921ae5696ea043e5ff622f76 Mon Sep 17 00:00:00 2001
From: Peter Kaiser <peter.kaiser@kit.edu>
Date: Mon, 7 Mar 2016 15:46:33 +0100
Subject: [PATCH] CMake: Added switch for NLOPT

---
 CMakeModules/FindNLOPT.cmake | 41 ++++++++++++++++++++++++++++++++++++
 VirtualRobot/CMakeLists.txt  | 16 ++++++++++++--
 config.cmake                 | 13 ++++++++++++
 3 files changed, 68 insertions(+), 2 deletions(-)
 create mode 100644 CMakeModules/FindNLOPT.cmake

diff --git a/CMakeModules/FindNLOPT.cmake b/CMakeModules/FindNLOPT.cmake
new file mode 100644
index 000000000..c73967496
--- /dev/null
+++ b/CMakeModules/FindNLOPT.cmake
@@ -0,0 +1,41 @@
+# Copyright (c) 2015, Georgia Tech Graphics Lab and Humanoid Robotics Lab
+# This file is provided under the "BSD-style" License
+
+# Find NLOPT
+#
+# This sets the following variables:
+# NLOPT_FOUND
+# NLOPT_INCLUDE_DIRS
+# NLOPT_LIBRARIES
+# NLOPT_DEFINITIONS
+# NLOPT_VERSION
+
+find_package(PkgConfig QUIET)
+
+# Check to see if pkgconfig is installed.
+pkg_check_modules(PC_NLOPT nlopt QUIET)
+
+# Definitions
+set(NLOPT_DEFINITIONS ${PC_NLOPT_CFLAGS_OTHER})
+
+# Include directories
+find_path(NLOPT_INCLUDE_DIRS
+    NAMES nlopt.h
+    HINTS ${PC_NLOPT_INCLUDEDIR}
+    PATHS "${CMAKE_INSTALL_PREFIX}/include")
+
+# Libraries
+find_library(NLOPT_LIBRARIES
+    NAMES nlopt nlopt_cxx
+    HINTS ${PC_NLOPT_LIBDIR})
+
+# Version
+set(NLOPT_VERSION ${PC_NLOPT_VERSION})
+
+# Set (NAME)_FOUND if all the variables and the version are satisfied.
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(NLOPT
+    FAIL_MESSAGE  DEFAULT_MSG
+    REQUIRED_VARS NLOPT_INCLUDE_DIRS NLOPT_LIBRARIES
+    VERSION_VAR   NLOPT_VERSION)
+ 
diff --git a/VirtualRobot/CMakeLists.txt b/VirtualRobot/CMakeLists.txt
index 00a4a3561..4269c6352 100644
--- a/VirtualRobot/CMakeLists.txt
+++ b/VirtualRobot/CMakeLists.txt
@@ -19,6 +19,10 @@ if (Simox_USE_RBDL)
     FIND_PACKAGE (RBDL)
 endif()
 
+if (Simox_USE_NLOPT)
+    FIND_PACKAGE (NLOPT)
+endif()
+
 ########################### TESTING #####################################
     MACRO(ADD_VR_TEST TEST_NAME)  
     INCLUDE_DIRECTORIES(${Simox_EXTERNAL_INCLUDE_DIRS})
@@ -86,7 +90,6 @@ IK/FeetPosture.cpp
 IK/ConstrainedIK.cpp
 IK/ConstrainedHierarchicalIK.cpp
 IK/ConstrainedStackedIK.cpp
-IK/ConstrainedOptimizationIK.cpp
 IK/Constraint.cpp
 IK/constraints/BalanceConstraint.cpp
 IK/constraints/PoseConstraint.cpp
@@ -196,7 +199,6 @@ IK/FeetPosture.h
 IK/ConstrainedIK.h
 IK/ConstrainedHierarchicalIK.h
 IK/ConstrainedStackedIK.h
-IK/ConstrainedOptimizationIK.h
 IK/Constraint.h
 IK/constraints/BalanceConstraint.h
 IK/constraints/PoseConstraint.h
@@ -246,6 +248,16 @@ ADD_SUBDIRECTORY(Visualization/tests)
 ADD_SUBDIRECTORY(Workspace/tests)
 ADD_SUBDIRECTORY(Compression/tests)
 
+if (Simox_USE_NLOPT AND NLOPT_FOUND)
+    SET(SOURCES ${SOURCES}
+        IK/ConstrainedOptimizationIK.cpp
+    )
+
+    SET(INCLUDES ${INCLUDES}
+        IK/ConstrainedOptimizationIK.h
+    )
+endif ()
+
 if (Simox_VISUALIZATION AND Simox_USE_COIN_VISUALIZATION)
 
     SET(SOURCES
diff --git a/config.cmake b/config.cmake
index 4b8c9d891..d6cb9a087 100644
--- a/config.cmake
+++ b/config.cmake
@@ -163,6 +163,19 @@ IF (NOT Simox_CONFIGURED)
     #urdf
     OPTION (Simox_USE_URDF "Use URDF" OFF)
 
+    #### NLOPT
+    OPTION (Simox_USE_NLOPT "Use NLOPT" OFF)
+    if (Simox_USE_NLOPT)
+        FIND_PACKAGE(NLOPT)
+        if (NLOPT_FOUND)
+            MESSAGE(STATUS "NLOPT found at: ${NLOPT_INCLUDE_DIRS}")
+            SET (Simox_EXTERNAL_INCLUDE_DIRS ${Simox_EXTERNAL_INCLUDE_DIRS} ${NLOPT_INCLUDE_DIRS})
+            SET (Simox_EXTERNAL_LIBRARIES ${Simox_EXTERNAL_LIBRARIES} ${NLOPT_LIBRARIES})
+        else ()
+            MESSAGE(STATUS "NLOPT not found")
+        endif ()
+    endif ()
+
     #### Eigen
     FIND_PACKAGE (Eigen3 REQUIRED)
     if (Eigen3_FOUND)
-- 
GitLab