From 010f05dfeb9de5eb32f7094eec4c60d80bc33c77 Mon Sep 17 00:00:00 2001 From: Liangliang Nan Date: Wed, 19 Jun 2019 15:02:27 +0200 Subject: [PATCH] find GLPK - added FindGLPK.cmake - try to find SCIP first. If not, try to find GLPK --- Installation/cmake/modules/FindGLPK.cmake | 30 +++++++++++ .../CMakeLists.txt | 54 ++++++++++++------- ...olyfit_example_model_complexty_control.cpp | 24 ++++++++- .../polyfit_example_user_provided_planes.cpp | 24 ++++++++- .../polyfit_example_without_input_planes.cpp | 28 +++++++++- .../CMakeLists.txt | 49 +++++++++-------- .../polygonal_surface_reconstruction_test.cpp | 47 ++++++++++------ .../examples/Solver_interface/CMakeLists.txt | 31 ++++++++++- .../mixed_integer_program.cpp | 23 ++++++-- .../CGAL/GLPK_mixed_integer_program_traits.h | 4 ++ .../CGAL/SCIP_mixed_integer_program_traits.h | 4 ++ 11 files changed, 251 insertions(+), 67 deletions(-) create mode 100644 Installation/cmake/modules/FindGLPK.cmake diff --git a/Installation/cmake/modules/FindGLPK.cmake b/Installation/cmake/modules/FindGLPK.cmake new file mode 100644 index 00000000000..b2e6f4476c5 --- /dev/null +++ b/Installation/cmake/modules/FindGLPK.cmake @@ -0,0 +1,30 @@ +# This file sets up GLPK for CMake. Once done this will define +# GLPK_FOUND - system has GLPK lib +# GLPK_INCLUDE_DIR - the GLPK include directory +# GLPK_LIBRARIES - Link these to use GLPK + + +# Is it already configured? +if (NOT GLPK_FOUND) + + # first look in user defined locations + find_path(GLPK_INCLUDE_DIR + NAMES glpk.h + PATHS /usr/local/include/LASlib/ + ENV GLPK_INC_DIR + ) + + find_library(GLPK_LIBRARIES + NAMES libglpk + PATHS ENV LD_LIBRARY_PATH + ENV LIBRARY_PATH + /usr/local/lib + ${GLPK_INCLUDE_DIR}/../lib + ENV GLPK_LIB_DIR + ) + + if(GLPK_LIBRARIES AND GLPK_INCLUDE_DIR) + set(GLPK_FOUND TRUE) + endif() + +endif() diff --git a/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/CMakeLists.txt b/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/CMakeLists.txt index e94dda33344..b03959441ab 100644 --- a/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/CMakeLists.txt +++ b/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/CMakeLists.txt @@ -31,22 +31,7 @@ if ( NOT Boost_FOUND ) endif() -find_package( SCIP ) - -if ( NOT SCIP_FOUND ) - - message(STATUS "This project requires the SCIP library, and will not be compiled.") - - return() - -endif() - - -# include for local directory - -# include for local package include_directories( BEFORE ../../include ) -include_directories( BEFORE ${SCIP_INCLUDE_DIRS} ) # Creating entries for all C++ files with "main" routine # ########################################################## @@ -57,6 +42,39 @@ create_single_source_cgal_program( "polyfit_example_without_input_planes.cpp" ) create_single_source_cgal_program( "polyfit_example_user_provided_planes.cpp" ) create_single_source_cgal_program( "polyfit_example_model_complexty_control.cpp" ) -target_link_libraries( polyfit_example_without_input_planes PRIVATE ${SCIP_LIBRARIES} ) -target_link_libraries( polyfit_example_user_provided_planes PRIVATE ${SCIP_LIBRARIES} ) -target_link_libraries( polyfit_example_model_complexty_control PRIVATE ${SCIP_LIBRARIES} ) + +find_package( SCIP ) + +if ( NOT SCIP_FOUND ) + find_package( GLPK ) + + if ( NOT GLPK_FOUND ) + message(STATUS "This project requires either SCIP or GLPK, and will not be compiled. " + "Please proivde either 'SCIP_DIR' or 'GLPK_INCLUDE_DIR' and 'GLPK_LIBRARIES'") + else() + add_compile_definitions(CGAL_USE_GLPK) + + include_directories( BEFORE ${GLPK_INCLUDE_DIR} ) + + target_link_libraries( polyfit_example_without_input_planes PRIVATE ${GLPK_LIBRARIES} ) + target_link_libraries( polyfit_example_user_provided_planes PRIVATE ${GLPK_LIBRARIES} ) + target_link_libraries( polyfit_example_model_complexty_control PRIVATE ${GLPK_LIBRARIES} ) + + message("GLPK found and used") + + endif() + +else() + + add_compile_definitions(CGAL_USE_SCIP) + + include_directories( BEFORE ${SCIP_INCLUDE_DIRS} ) + + target_link_libraries( polyfit_example_without_input_planes PRIVATE ${SCIP_LIBRARIES} ) + target_link_libraries( polyfit_example_user_provided_planes PRIVATE ${SCIP_LIBRARIES} ) + target_link_libraries( polyfit_example_model_complexty_control PRIVATE ${SCIP_LIBRARIES} ) + + message("SCIP found and used") + +endif() + diff --git a/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_model_complexty_control.cpp b/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_model_complexty_control.cpp index 85ad419611f..5ff14cbc0fd 100644 --- a/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_model_complexty_control.cpp +++ b/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_model_complexty_control.cpp @@ -4,7 +4,18 @@ #include #include #include + +#ifdef CGAL_USE_SCIP #include +typedef CGAL::SCIP_mixed_integer_program_traits MIP_Solver; +#elif defined(CGAL_USE_GLPK) +#include +typedef CGAL::GLPK_mixed_integer_program_traits MIP_Solver; +#endif + + +#if defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) + #include #include @@ -16,8 +27,6 @@ typedef Kernel::Vector_3 Vector; typedef CGAL::Polygonal_surface_reconstruction Polygonal_surface_reconstruction; typedef CGAL::Surface_mesh Surface_mesh; -typedef CGAL::SCIP_mixed_integer_program_traits MIP_Solver; - // Point with normal, and plane index typedef boost::tuple PNI; typedef CGAL::Nth_of_tuple_property_map<0, PNI> Point_map; @@ -131,3 +140,14 @@ int main() } return EXIT_SUCCESS; } + + +#else + +int main(int argc, char* argv[]) +{ + std::cerr << "This test requires either GLPK or SCIP.\n"; + return EXIT_FAILURE; +} + +#endif // defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) diff --git a/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_user_provided_planes.cpp b/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_user_provided_planes.cpp index 7c38265a8a1..b6d801bed7d 100644 --- a/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_user_provided_planes.cpp +++ b/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_user_provided_planes.cpp @@ -4,7 +4,18 @@ #include #include #include + +#ifdef CGAL_USE_SCIP #include +typedef CGAL::SCIP_mixed_integer_program_traits MIP_Solver; +#elif defined(CGAL_USE_GLPK) +#include +typedef CGAL::GLPK_mixed_integer_program_traits MIP_Solver; +#endif + + +#if defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) + #include #include @@ -22,8 +33,6 @@ typedef CGAL::Nth_of_tuple_property_map<0, PNI> Point_map; typedef CGAL::Nth_of_tuple_property_map<1, PNI> Normal_map; typedef CGAL::Nth_of_tuple_property_map<2, PNI> Plane_index_map; -typedef CGAL::SCIP_mixed_integer_program_traits MIP_Solver; - /* * The following example shows the reconstruction using user-provided * planar segments stored in PLY format. In the PLY format, a property @@ -94,3 +103,14 @@ int main() return EXIT_SUCCESS; } + + +#else + +int main(int argc, char* argv[]) +{ + std::cerr << "This test requires either GLPK or SCIP.\n"; + return EXIT_FAILURE; +} + +#endif // defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) diff --git a/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_without_input_planes.cpp b/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_without_input_planes.cpp index 338b97f2088..e00bc6cf861 100644 --- a/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_without_input_planes.cpp +++ b/Polygonal_surface_reconstruction/examples/Polygonal_surface_reconstruction/polyfit_example_without_input_planes.cpp @@ -5,7 +5,22 @@ #include #include #include + +#ifdef CGAL_USE_SCIP + #include +typedef CGAL::SCIP_mixed_integer_program_traits MIP_Solver; + +#elif defined(CGAL_USE_GLPK) + +#include +typedef CGAL::GLPK_mixed_integer_program_traits MIP_Solver; + +#endif + + +#if defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) + #include #include @@ -32,8 +47,6 @@ typedef CGAL::Shape_detection::Point_to_shape_index_map Point_to_sha typedef CGAL::Polygonal_surface_reconstruction Polygonal_surface_reconstruction; typedef CGAL::Surface_mesh Surface_mesh; -typedef CGAL::SCIP_mixed_integer_program_traits MIP_Solver; - /* * This example first extracts planes from the input point cloud * (using RANSAC with default parameters) and then reconstructs @@ -135,3 +148,14 @@ int main() return EXIT_SUCCESS; } + + +#else + +int main(int argc, char* argv[]) +{ + std::cerr << "This test requires either GLPK or SCIP.\n"; + return EXIT_FAILURE; +} + +#endif // defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) diff --git a/Polygonal_surface_reconstruction/test/Polygonal_surface_reconstruction/CMakeLists.txt b/Polygonal_surface_reconstruction/test/Polygonal_surface_reconstruction/CMakeLists.txt index c8b983a13bc..8c4d1514c58 100644 --- a/Polygonal_surface_reconstruction/test/Polygonal_surface_reconstruction/CMakeLists.txt +++ b/Polygonal_surface_reconstruction/test/Polygonal_surface_reconstruction/CMakeLists.txt @@ -45,32 +45,39 @@ include( CGAL_CreateSingleSourceCGALProgram ) create_single_source_cgal_program( "polygonal_surface_reconstruction_test.cpp" ) -if ( SCIP_FOUND ) +find_package( SCIP ) + +if (SCIP_FOUND) + + include_directories( BEFORE ${SCIP_INCLUDE_DIRS} ) + target_link_libraries( polygonal_surface_reconstruction_test PRIVATE ${SCIP_LIBRARIES} ) -endif() -find_path(GLPK_INCLUDE_DIR - NAMES glpk.h - DOC "Path to GLPK headers" - ) -find_library(GLPK_LIBRARIES NAMES GLPK DOC "Path to the GLPK library") + add_compile_definitions(CGAL_USE_SCIP) -if(GLPK_LIBRARIES AND GLPK_INCLUDE_DIR) - set( GLPK_FOUND TRUE) -endif() + message("SCIP found and used") -if ( NOT SCIP_FOUND ) - if ( NOT GLPK_FOUND ) - message(STATUS "This project requires the SCIP library or the GLPK library, and will not be compiled.") - target_compile_definitions(polygonal_surface_reconstruction_test PRIVATE SKIP_TEST_USING_GLPK) - message("NOTICE: SCIP and GLPK not found") - return() - endif() endif() -if ( GLPK_FOUND ) - include_directories( BEFORE ${GLPK_INCLUDE_DIR} ) - target_link_libraries( polygonal_surface_reconstruction_test PRIVATE ${GLPK_LIBRARIES} ) - message("GLPK found and used") +find_package( GLPK ) + + if (GLPK_FOUND) + + include_directories( BEFORE ${GLPK_INCLUDE_DIR} ) + + target_link_libraries( polygonal_surface_reconstruction_test PRIVATE ${GLPK_LIBRARIES} ) + + add_compile_definitions(CGAL_USE_GLPK) + + message("GLPK found and used") + +endif() + + +if (NOT SCIP_FOUND AND NOT GLPK_FOUND) + + message(STATUS "This project requires either SCIP or GLPK, and will not be compiled. " + "Please proivde either 'SCIP_DIR' or 'GLPK_INCLUDE_DIR' and 'GLPK_LIBRARIES'") + endif() diff --git a/Polygonal_surface_reconstruction/test/Polygonal_surface_reconstruction/polygonal_surface_reconstruction_test.cpp b/Polygonal_surface_reconstruction/test/Polygonal_surface_reconstruction/polygonal_surface_reconstruction_test.cpp index 28e47493cbd..1f32470fcbf 100644 --- a/Polygonal_surface_reconstruction/test/Polygonal_surface_reconstruction/polygonal_surface_reconstruction_test.cpp +++ b/Polygonal_surface_reconstruction/test/Polygonal_surface_reconstruction/polygonal_surface_reconstruction_test.cpp @@ -8,28 +8,30 @@ // must be present storing the plane index for each point(-1 if the point is // not assigned to a plane). +#include + +#ifdef CGAL_USE_GLPK +#include +typedef CGAL::GLPK_mixed_integer_program_traits GLPK_Solver; +#endif + +#ifdef CGAL_USE_SCIP +#include +typedef CGAL::SCIP_mixed_integer_program_traits SCIP_Solver; +#endif + +#if defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) + #include #include -#ifndef SKIP_TEST_USING_GLPK -#include -#endif -#include - #include "polygonal_surface_reconstruction_test_framework.h" - // kernels: typedef CGAL::Simple_cartesian Cartesian; typedef CGAL::Exact_predicates_inexact_constructions_kernel Epick; -// solvers: -#ifndef SKIP_TEST_USING_GLPK -typedef CGAL::GLPK_mixed_integer_program_traits GLPK_Solver; -#endif -typedef CGAL::SCIP_mixed_integer_program_traits SCIP_Solver; - int main(int argc, char* argv[]) { @@ -60,7 +62,7 @@ int main(int argc, char* argv[]) //--------------------------------------------------------------------- -#ifndef SKIP_TEST_USING_GLPK +#ifdef CGAL_USE_GLPK std::cerr << "\n\t---- Using GLPK solver\n"; std::cerr << "\t\t---- using provided planes\n"; @@ -70,6 +72,7 @@ int main(int argc, char* argv[]) reconstruct(input_file, true); #endif +#ifdef CGAL_USE_SCIP std::cerr << "\n\t---- Using SCIP solver\n"; std::cerr << "\t\t---- using provided planes\n"; @@ -77,7 +80,7 @@ int main(int argc, char* argv[]) std::cerr << "\t\t---- re-extract planes\n"; reconstruct(input_file, true); - +#endif //--------------------------------------------------------------------- @@ -85,7 +88,7 @@ int main(int argc, char* argv[]) //--------------------------------------------------------------------- -#ifndef SKIP_TEST_USING_GLPK +#ifdef CGAL_USE_GLPK std::cerr << "\n\t---- Using GLPK solver\n"; std::cerr << "\t\t---- using provided planes\n"; @@ -95,6 +98,7 @@ int main(int argc, char* argv[]) reconstruct(input_file, true); #endif +#ifdef CGAL_USE_SCIP std::cerr << "\n\t---- Using SCIP solver\n"; std::cerr << "\t\t---- using provided planes\n"; @@ -102,6 +106,17 @@ int main(int argc, char* argv[]) std::cerr << "\t\t---- re-extract planes\n"; reconstruct(input_file, true); - +#endif + return EXIT_SUCCESS; } + +#else + +int main(int argc, char* argv[]) +{ + std::cerr << "This test requires either GLPK or SCIP.\n"; + return EXIT_FAILURE; +} + +#endif // defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) diff --git a/Solver_interface/examples/Solver_interface/CMakeLists.txt b/Solver_interface/examples/Solver_interface/CMakeLists.txt index 5d5e971cb40..7b488ac7a14 100644 --- a/Solver_interface/examples/Solver_interface/CMakeLists.txt +++ b/Solver_interface/examples/Solver_interface/CMakeLists.txt @@ -20,14 +20,41 @@ if ( CGAL_FOUND ) endif() create_single_source_cgal_program( "diagonalize_matrix.cpp" ) + create_single_source_cgal_program( "mixed_integer_program.cpp" ) find_package( SCIP ) + if (SCIP_FOUND) + include_directories( BEFORE ${SCIP_INCLUDE_DIRS} ) - create_single_source_cgal_program( "mixed_integer_program.cpp" ) + target_link_libraries( mixed_integer_program PRIVATE ${SCIP_LIBRARIES} ) + + add_compile_definitions(CGAL_USE_SCIP) + + message("SCIP found and used") + else() - message(STATUS "The MIP example requires the SCIP library, and will not be compiled.") + + find_package( GLPK ) + + if (GLPK_FOUND) + + include_directories( BEFORE ${GLPK_INCLUDE_DIR} ) + + target_link_libraries( mixed_integer_program PRIVATE ${GLPK_LIBRARIES} ) + + add_compile_definitions(CGAL_USE_GLPK) + + message("GLPK found and used") + + else() + + message(STATUS "This project requires either SCIP or GLPK, and will not be compiled. " + "Please proivde either 'SCIP_DIR' or 'GLPK_INCLUDE_DIR' and 'GLPK_LIBRARIES'") + + endif() + endif() else() diff --git a/Solver_interface/examples/Solver_interface/mixed_integer_program.cpp b/Solver_interface/examples/Solver_interface/mixed_integer_program.cpp index 962448461ac..4368628cd51 100644 --- a/Solver_interface/examples/Solver_interface/mixed_integer_program.cpp +++ b/Solver_interface/examples/Solver_interface/mixed_integer_program.cpp @@ -18,24 +18,27 @@ */ -#if 1 // Uses SCIP +#include + +#ifdef CGAL_USE_SCIP #include typedef CGAL::SCIP_mixed_integer_program_traits MIP_Solver; -#else // Uses GLPK +#elif defined(CGAL_USE_GLPK) #include typedef CGAL::GLPK_mixed_integer_program_traits MIP_Solver; #endif + +#if defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) + typedef typename MIP_Solver::Variable Variable; typedef typename MIP_Solver::Linear_objective Linear_objective; typedef typename MIP_Solver::Linear_constraint Linear_constraint; -#include - int main() { @@ -96,3 +99,15 @@ int main() return EXIT_FAILURE; } } + + +#else + +int main(int argc, char* argv[]) +{ + std::cerr << "This test requires either GLPK or SCIP.\n"; + return EXIT_FAILURE; +} + +#endif // defined(CGAL_USE_GLPK) || defined(CGAL_USE_SCIP) + diff --git a/Solver_interface/include/CGAL/GLPK_mixed_integer_program_traits.h b/Solver_interface/include/CGAL/GLPK_mixed_integer_program_traits.h index ed7b5cc28cc..cbd45ffa363 100644 --- a/Solver_interface/include/CGAL/GLPK_mixed_integer_program_traits.h +++ b/Solver_interface/include/CGAL/GLPK_mixed_integer_program_traits.h @@ -22,6 +22,8 @@ #include +#ifdef CGAL_USE_GLPK + #include @@ -292,4 +294,6 @@ namespace CGAL { } // namespace CGAL +#endif // CGAL_USE_GLPK + #endif // CGAL_GLPK_MIXED_INTEGER_PROGRAM_TRAITS_H diff --git a/Solver_interface/include/CGAL/SCIP_mixed_integer_program_traits.h b/Solver_interface/include/CGAL/SCIP_mixed_integer_program_traits.h index a6ccc264cab..1f093681d75 100644 --- a/Solver_interface/include/CGAL/SCIP_mixed_integer_program_traits.h +++ b/Solver_interface/include/CGAL/SCIP_mixed_integer_program_traits.h @@ -22,6 +22,8 @@ #include +#ifdef CGAL_USE_SCIP + #include "scip/scip.h" #include "scip/scipdefplugins.h" @@ -240,4 +242,6 @@ namespace CGAL { } // namespace CGAL +#endif // CGAL_USE_SCIP + #endif // CGAL_SCIP_MIXED_INTEGER_PROGRAM_TRAITS_H