#
# 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 (OpenGLRobotran)


# 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)


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                           PROJECT OPTIONS
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

include(CMakeDependentOption)
option (FLAG_FRAME_CAPTURE "Frame capture" ON)

# subflag of frame capture -> output to png
cmake_dependent_option(FLAG_FRAME_CAPTURE_PNG "Enable frame capture to PNG (need libpng)" ON "FLAG_FRAME_CAPTURE" ON)

# subflag of frame capture -> output to mpeg
cmake_dependent_option(FLAG_FRAME_CAPTURE_MPEG "Enable frame capture to PNG (need libmpeg)" ON "FLAG_FRAME_CAPTURE" ON)

# subflag of frame capture -> output to opencv
cmake_dependent_option(FLAG_FRAME_CAPTURE_OPENCV "Enable frame capture to PNG (need opencv)" ON "FLAG_FRAME_CAPTURE" ON)

add_definitions( -DFRAME_CAPTURE )
if(FLAG_FRAME_CAPTURE_PNG)
    add_definitions( -DFRAME_CAPTURE_PNG )
endif( )

if(FLAG_FRAME_CAPTURE_MPEG)
    add_definitions( -DFRAME_CAPTURE_MPEG )
endif( )

if(FLAG_FRAME_CAPTURE_OPENCV)
    add_definitions( -DFRAME_CAPTURE_OPENCV )
endif( )


option (FLAG_IPY_FRAME_CAPTURE "Python interface to the frame capture app" OFF)



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

# configure header files to pass some of the CMake settings to the source code
set (ROBOTRAN_SOURCE_DIR "${PROJECT_SOURCE_DIR}/../../mbsysCopy")

configure_file (
  "${ROBOTRAN_SOURCE_DIR}/conf/cmake_config.h.in"
  "${PROJECT_BINARY_DIR}/conf/cmake_config.h"
)
set(OPEN_GL_FILES_PATH ${PROJECT_SOURCE_DIR}/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)


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                          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)


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

set(CMAKE_MODULE_PATH ${ROBOTRAN_SOURCE_DIR}/conf)

# Libxml2
xml_lib(main)

# OpenGL
open_gl_lib(main)

# Frame capture
if(FLAG_FRAME_CAPTURE)
    include_directories("./open_gl/frame_capture")
    if(FLAG_FRAME_CAPTURE_PNG)
        find_package(PNG)
    endif()
    
    if(FLAG_FRAME_CAPTURE_MPEG)
        set(FFmpeg_FIND_COMPONENTS AVCODEC AVFORMAT AVUTIL SWSCALE)
        find_package(FFMPEG REQUIRED)
    endif()
    
    if(FLAG_FRAME_CAPTURE_OPENCV)
        find_package( OpenCV REQUIRED )
        include_directories( ${OpenCV_INCLUDE_DIRS} )    
    endif()
    
endif()


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

set(LIST_OBS_FILES_PATH ${PROJECT_SOURCE_DIR}/../../userFiles/simu/forces/meshGCM/specific_ground/equation/list_obs/)

# list source files to compile
init_src()

increment_src("${PROJECT_SOURCE_DIR}/src")
increment_src("${OPEN_GL_FILES_PATH}")
increment_src("${LIST_OBS_FILES_PATH}")


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

increment_include("${PROJECT_SOURCE_DIR}/src")
increment_include("${OPEN_GL_FILES_PATH}")
increment_include("${LIST_OBS_FILES_PATH}")


# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
#                         CONFIGURE EXECUTABLE
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# include these directories
include_directories (${INCLUDE_DIR})

# name of the executable
set (Executable Exec)

# generate the executable
add_executable (${Executable} ${SOURCE_FILES})

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

# link library: OpenGL
target_link_libraries ( ${Executable} ${OPEN_GL_LIBS})

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

# Frame capture
if(FLAG_FRAME_CAPTURE)
	if(FLAG_FRAME_CAPTURE_PNG)
		target_link_libraries (${Executable} ${PNG_LIBRARIES})
	endif()
	
	if(FLAG_FRAME_CAPTURE_MPEG)
		target_link_libraries (${Executable} ${FFMPEG_LIBRARIES})
	endif()
	
	if(FLAG_FRAME_CAPTURE_OPENCV)
		target_link_libraries( ${Executable} ${OpenCV_LIBS} )
	endif()
endif()

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

if(FLAG_IPY_FRAME_CAPTURE)
  
    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 CyScreenCapture)

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

endif()
