cmake_minimum_required(VERSION 2.6)


####################   Macro   #######################
MACRO(CHECK_FILES _FILES _DIR)
	SET(_MISSING_FILES)
	FOREACH(_FILE ${${_FILES}})
		IF(NOT EXISTS "${_FILE}")
			SET(KinectSDK_FOUND NO)
			get_filename_component(_FILE ${_FILE} NAME)
			SET(_MISSING_FILES "${_MISSING_FILES}${_FILE}, ")
		ENDIF()
	ENDFOREACH()
	IF(_MISSING_FILES)
		MESSAGE(STATUS "In folder \"${${_DIR}}\" not found files: ${_MISSING_FILES}")
		SET(KinectSDK_FOUND NO)
	ENDIF()
ENDMACRO(CHECK_FILES)

MACRO(CHECK_DIR _DIR)
	IF(NOT EXISTS "${${_DIR}}")
		MESSAGE(STATUS "Folder \"${${_DIR}}\" not found.")
		SET(KinectSDK_FOUND NO)
	ENDIF()
ENDMACRO(CHECK_DIR)


# Required packages
# =================

# ITK
FIND_PACKAGE(ITK REQUIRED)
IF(ITK_FOUND)
	INCLUDE_DIRECTORIES(${ITK_INCLUDE_DIRS})
	INCLUDE(${ITK_USE_FILE})
ELSE()
	MESSAGE("ITK not found, but needed.")
ENDIF(ITK_FOUND)

# Qt4
FIND_PACKAGE(Qt4 REQUIRED)
IF(QT4_FOUND)
	INCLUDE_DIRECTORIES(${QT_INCLUDE_DIR})
	INCLUDE(${QT_USE_FILE})	
ELSE()
	MESSAGE("QT4 not found, but needed.")
ENDIF(QT4_FOUND)

# RITK
FIND_PACKAGE(RITK REQUIRED)
IF(RITK_FOUND)
	INCLUDE_DIRECTORIES(${RITK_INCLUDE_DIR})
	INCLUDE(${RITK_USE_FILE})	
ELSE()
	MESSAGE("RITK not found, but needed.")
ENDIF(RITK_FOUND)

# CUDA
FIND_PACKAGE(CUDA)
IF(CUDA_FOUND)
ELSE()
	MESSAGE(FATAL_ERROR "CUDA not found, but needed.")
ENDIF(CUDA_FOUND)


# The headers and sources for the KinectImage plugin
#===================================================
SET(HDRS
	KinectImageExport.h
	KinectImagePluginFactory.h
	KinectImagePlugin.h
	KinectImage.h
	KinectImageIO.h
	KinectImageDepthToWorldFilter.h
	KinectImageCudaDepthToWorldFilter.h
	KinectSkeleton.h
	KinectSkeletonPayloadContainer.h
	OpenGLSkeletonControlUnit.h
	OpenGLSkeletonEntity.h
)

SET(SRCS
	KinectImagePluginFactory.cxx
	KinectImagePlugin.cxx
	KinectImage.txx
	KinectImageIO.txx
	KinectImageDepthToWorldFilter.txx
	KinectImageCudaDepthToWorldFilter.txx
	KinectSkeleton.cxx
	KinectSkeletonPayloadContainer.cxx
	KinectImageInstantiations.cxx
	OpenGLSkeletonControlUnit.cxx
	OpenGLSkeletonEntity.cxx
)

SET(CUDA_HDRS
	KinectImageCudaDepthToWorldFilterKernel.h
)

SET(CUDA_SRCS
	KinectImageCudaDepthToWorldFilterKernel.cu
)

SET(MOC_HDRS 	
	KinectImagePluginFactory.h
	OpenGLSkeletonControlUnit.h
	OpenGLSkeletonEntity.h
)

SET(UIS
	OpenGLSkeletonControlUnit.ui
)

QT4_WRAP_UI(UI_HDRS ${UIS})	
QT4_WRAP_CPP(MOC_SRCS ${MOC_HDRS})



# Set the library name for the KinectImage plugin
# ===============================================
SET(KINECTIMAGEPLUGIN_LIBRARY KinectImagePlugin CACHE INTERNAL "")
SET(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)


# Project settings
# ================
SET(KINECTIMAGEPLUGIN_INCLUDE_DIRS	
	${CMAKE_CURRENT_SOURCE_DIR}
	${CMAKE_CURRENT_BINARY_DIR}
	${QT_QTOPENGL_INCLUDE_DIR}
	${RITK_INCLUDE_DIRS}
	${CUDA_INCLUDE_DIRS}
	CACHE INTERNAL ""
)
INCLUDE_DIRECTORIES(${KINECTIMAGEPLUGIN_INCLUDE_DIRS})
ADD_DEFINITIONS("/DKINECTIMAGE_SHAREDLIB_EXPORT")
ADD_DEFINITIONS(-DQT_PLUGIN)
ADD_DEFINITIONS(-DQT_SHARED)
CUDA_ADD_LIBRARY(${KINECTIMAGEPLUGIN_LIBRARY} SHARED ${SRCS} ${HDRS} ${MOC_SRCS} ${UI_HDRS} ${CUDA_HDRS} ${CUDA_SRCS}) 
TARGET_LINK_LIBRARIES(${KINECTIMAGEPLUGIN_LIBRARY} 
	debug ${RITK_COMMON_LIBRARY_DEBUG}
	debug ${RITK_CORE_LIBRARY_DEBUG}
	debug ${RITK_CUDA_LIBRARY_DEBUG}
	debug ${RITK_VISUALIZATION_LIBRARY_DEBUG}
	optimized ${RITK_COMMON_LIBRARY}
	optimized ${RITK_CORE_LIBRARY}
	optimized ${RITK_CUDA_LIBRARY}
	optimized ${RITK_VISUALIZATION_LIBRARY}
	${ITK_LIBRARIES} 
	${QT_LIBRARIES} 
	${CUDA_CUDA_LIBRARY} 
	${CUDA_CUDART_LIBRARY} 
)
SET_TARGET_PROPERTIES(${KINECTIMAGEPLUGIN_LIBRARY} PROPERTIES DEBUG_POSTFIX "d")


# Module installation
# ====================
INSTALL(TARGETS ${KINECTIMAGEPLUGIN_LIBRARY} CONFIGURATIONS RELEASE	
	RUNTIME DESTINATION ${RITK_BINARY_DIR}/release/plugins 
	LIBRARY DESTINATION ${RITK_BINARY_DIR}/release/plugins 
)
INSTALL(TARGETS ${KINECTIMAGEPLUGIN_LIBRARY} CONFIGURATIONS DEBUG	
	RUNTIME DESTINATION ${RITK_BINARY_DIR}/debug/plugins 
	LIBRARY DESTINATION ${RITK_BINARY_DIR}/debug/plugins 
)
