#
# author: Nicolas Van der Noot
#
# Visualization project for Robotran, with OpenGL
#

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                       PROJECT MAIN CONFIGURATIONS
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


# CMake minimum version
cmake_minimum_required(VERSION 2.8.12)

# project name
project (VtkOutput)

# Retriev the MBsysC base directory
set (ROBOTRAN_SOURCE_DIR "${PROJECT_SOURCE_DIR}/../..")


# message to display the project name
message(STATUS "Processing ${PROJECT_NAME}")

# for Unix: display all the warnings, except the ones related to /* -- */
if (UNIX)
  set(CMAKE_C_FLAGS "-g -Wall -Wno-comment")
endif (UNIX)

# copy dll files used for Windows
# these dll files are copied in the Executable directory (Debug or Release)
if (WIN32)

    # avoid warnings
    if(COMMAND cmake_policy AND ${CMAKE_VERSION} VERSION_GREATER "3.1")
        cmake_policy(SET CMP0054 NEW)
    endif()

    # Visual studio version
    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
        if (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 18.0)
            message("Visual Studio version set to VS2012 (${CMAKE_CXX_COMPILER_VERSION})")
            set(MSVC_VERSION lib-vc2012)
        elseif (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 19.0)
            message("Visual Studio version set to VS2013 (${CMAKE_CXX_COMPILER_VERSION})")
            set(MSVC_VERSION lib-vc2013)
        else ( )
            message("Visual Studio version set to VS2015 (${CMAKE_CXX_COMPILER_VERSION})")
            set(MSVC_VERSION lib-vc2015)
        endif ( )
    else ( )
        message(FATAL_ERROR "CMake error: only Visual Studio projects can currently be used on Windows !")
    endif ( )

    # WIN32 or WIN64 detetction
    if( CMAKE_SIZEOF_VOID_P EQUAL 8 ) # 64bit Windows
        SET(WIN_LIB_DIRECTORY win64_include_lib)
    else( ) # 32bit Windows
        SET(WIN_LIB_DIRECTORY win32_include_lib)
    endif( )

    # common dll
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/ DESTINATION ${CMAKE_BINARY_DIR}/Debug)
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/ DESTINATION ${CMAKE_BINARY_DIR}/Release)

    # GLFW
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/GLFW/${MSVC_VERSION}/ DESTINATION ${CMAKE_BINARY_DIR}/Debug)
    file(COPY ${ROBOTRAN_SOURCE_DIR}/${WIN_LIB_DIRECTORY}/dll/GLFW/${MSVC_VERSION}/ DESTINATION ${CMAKE_BINARY_DIR}/Release)
endif ( )

# additional CMake functions
set(CMAKE_AUX ${ROBOTRAN_SOURCE_DIR}/cmake_aux)
set(CMAKE_AUX_BIN ${PROJECT_BINARY_DIR}/cmake_aux)

add_subdirectory ( ${CMAKE_AUX}/listing/   ${CMAKE_AUX_BIN}/listing/   )
add_subdirectory ( ${CMAKE_AUX}/libraries/ ${CMAKE_AUX_BIN}/libraries/ )

# Windows M_PI definitions
if (WIN32)
    add_definitions(-D _USE_MATH_DEFINES)
endif (WIN32)

# project own definition
add_definitions(-D SEPARATE_OPEN_GL)



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           USER ABSOLUTE PATHS
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# configure header files to pass some of the CMake settings to the source code

configure_file (
  "${ROBOTRAN_SOURCE_DIR}/conf/cmake_config.h.in"
  "${PROJECT_BINARY_DIR}/conf/cmake_config.h"
)

set(OPEN_GL_FILES_PATH ${ROBOTRAN_SOURCE_DIR}/mbs_common/mbs_realtime/open_gl/)

configure_file (
    "${OPEN_GL_FILES_PATH}/conf/cmake_open_gl_config.h.in"
    "${PROJECT_BINARY_DIR}/conf/cmake_open_gl_config.h"
)

# 'cmake_config.h.in' and 'cmake_open_gl_config.h.in' is in the 'conf' folder
include_directories (${PROJECT_BINARY_DIR}/conf)



# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           EXTERNAL LIBRARIES
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

set(CMAKE_MODULE_PATH ${ROBOTRAN_SOURCE_DIR}/conf)

# Libxml2
xml_lib(main)

# VTK
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

# Glm
find_package(Glm REQUIRED)
include_directories("${GLM_INCLUDE_PATH}/")

message("Glm ${GLM_INCLUDE_PATH}")

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           LIST FILES
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

 
# list source files to compile
init_src()

increment_src("${PROJECT_SOURCE_DIR}/src/renderer")

increment_src("${OPEN_GL_FILES_PATH}/anim_read")
increment_src("${OPEN_GL_FILES_PATH}/components")
increment_src("${OPEN_GL_FILES_PATH}/lights")
increment_src("${OPEN_GL_FILES_PATH}/lights/specific")
increment_src("${OPEN_GL_FILES_PATH}/loader")
increment_src("${OPEN_GL_FILES_PATH}/mbs_read")
increment_src("${OPEN_GL_FILES_PATH}/shapes")
increment_src("${OPEN_GL_FILES_PATH}/viewpoint")
increment_src("${OPEN_GL_FILES_PATH}/renderer/default")

set(SOURCE_FILES ${SOURCE_FILES} "${OPEN_GL_FILES_PATH}/world/MbsWorld3D.cc")
set(SOURCE_FILES ${SOURCE_FILES} src/MbsVtkOutputter.cc)


# list include directories (to find headers)
init_include()

increment_include("${PROJECT_SOURCE_DIR}/src/")
increment_include("${PROJECT_SOURCE_DIR}/src/renderer")

include_directories("${OPEN_GL_FILES_PATH}/")
include_directories("${OPEN_GL_FILES_PATH}/anim_read")
include_directories("${OPEN_GL_FILES_PATH}/components")
include_directories("${OPEN_GL_FILES_PATH}/lights")
include_directories("${OPEN_GL_FILES_PATH}/lights/specific")
include_directories("${OPEN_GL_FILES_PATH}/loader")
include_directories("${OPEN_GL_FILES_PATH}/loader/vrml")
include_directories("${OPEN_GL_FILES_PATH}/loader/obj")
include_directories("${OPEN_GL_FILES_PATH}/loader/stl")
include_directories("${OPEN_GL_FILES_PATH}/mbs_read")
include_directories("${OPEN_GL_FILES_PATH}/shaders")
include_directories("${OPEN_GL_FILES_PATH}/shapes")
include_directories("${OPEN_GL_FILES_PATH}/shapes/specific")
include_directories("${OPEN_GL_FILES_PATH}/viewpoint")
include_directories("${OPEN_GL_FILES_PATH}/viewpoint/specific")
include_directories("${OPEN_GL_FILES_PATH}/world")
include_directories("${OPEN_GL_FILES_PATH}/renderer")
include_directories("${OPEN_GL_FILES_PATH}/renderer/default")
include_directories("${OPEN_GL_FILES_PATH}/window")

add_definitions( -DOPEN_GL ) 


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                         CONFIGURE main library
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# include these directories
include_directories (${INCLUDE_DIR})

# name of the libraryr
set (LibMbsVtk MbsVtk)

add_library(${LibMbsVtk} SHARED  ${SOURCE_FILES})

target_link_libraries ( ${LibMbsVtk} ${LIBXML2_LIBRARIES} ${VTK_LIBRARIES})
# math external library (for Unix): linkage
if (UNIX)
    target_link_libraries (${LibMbsVtk} m)
endif (UNIX)

# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                         CONFIGURE standalone executable
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


# name of the executable
set (Executable Vtk_output)

# generate the executable
add_executable (${Executable} src/main.cc)

# link library: Libxml2
target_link_libraries ( ${Executable} ${LibMbsVtk} ${LIBXML2_LIBRARIES} ${VTK_LIBRARIES})

# math external library (for Unix): linkage
if (UNIX)
    target_link_libraries (${Executable} m)
endif (UNIX)


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                         Python interface
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


  
find_package(Python REQUIRED)  

include_directories(${PYTHON_INCLUDE_DIR} 
                  ${NUMPY_INCLUDE_DIR}
                  ${CMAKE_CURRENT_SOURCE_DIR}
                 )  

# Set library extension
if(UNIX)
  set(IPY_LIB_EXT ".so")
elseif(WIN32)
  set(IPY_LIB_EXT ".pyd")
else()
  message("Unknow operating system, extension for cython library arbitrary choosen: .so")
  set(IPY_LIB_EXT ".so")
endif()

set( cxx_arg "--cplus" )
set( pyx_lang "CXX" )

set(LibPyMbsVtk CyMbsVtk)

file(REMOVE ${CMAKE_CURRENT_BINARY_DIR}/CyMbsVtk.cpp)
add_custom_command(OUTPUT CyMbsVtk.cpp
                 COMMAND ${CYTHON_EXECUTABLE} -f ${CMAKE_CURRENT_SOURCE_DIR}/src/CyMbsVtk.pyx  -o ${CMAKE_CURRENT_BINARY_DIR}/CyMbsVtk.cpp
                 ARGS ${cxx_arg}
                 IMPLICIT_DEPENDS ${pyx_lang}
                 DEPENDS src/CyMbsVtk.pyx
                 )
add_library(${LibPyMbsVtk} SHARED  ${CMAKE_CURRENT_BINARY_DIR}/CyMbsVtk.cpp)
set_target_properties(${LibPyMbsVtk}   PROPERTIES PREFIX ""      )
set_target_properties(${LibPyMbsVtk}   PROPERTIES SUFFIX "${IPY_LIB_EXT}"   )
target_link_libraries(${LibPyMbsVtk} ${LibMbsVtk} ${LIBXML2_LIBRARIES} m ${VTK_LIBRARIES} ${PYTHON_LIBRARIES})



