Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPM support #17

Open
triuk opened this issue Nov 18, 2022 · 1 comment
Open

CPM support #17

triuk opened this issue Nov 18, 2022 · 1 comment

Comments

@triuk
Copy link

triuk commented Nov 18, 2022

Hi, this piece of code is precious. I really love it exists. But the setup is not that simple, because it links to other repositories when downloaded with git and if you already use imgui in the project, it means you'll end up linking all things manually and probably have target conflicts.
So to ease this integration, I made a CMakeLists.txt that uses CPM to download and build imgui-vtk. The benefit is obvious - the CPM checks for new version of imgui-vtk every time you configure your project and run cmake on it. That means it is always up to date without effort. When you add other's project to yours, you definitely do not want to do it manually. I'll be happy, if you comment or improve this idea :)

Usage: download just main.cpp from the imgui-vtk project. Add my CMakeFiles.txt code to the same folder (so the working dir has only two files: main.cpp and this CMakeFiles.txt. Configure and build the executable.

cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)

# #####################
# # DOWNLOAD SECTION ##
# #####################
set(CPM_DOWNLOAD_VERSION 0.36.0)

if(CPM_SOURCE_CACHE)
  set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
  set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
  set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()

if(NOT(EXISTS ${CPM_DOWNLOAD_LOCATION}))
  message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
  file(DOWNLOAD
    https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
    ${CPM_DOWNLOAD_LOCATION}
  )
endif()

include(${CPM_DOWNLOAD_LOCATION})

# # ImGUI VTK ########
find_package(imvtk QUIET)

if(NOT imvtk_FOUND)
  CPMAddPackage(
    NAME imvtk
    GITHUB_REPOSITORY trlsmax/imgui-vtk
    GIT_TAG origin/master
  )
endif()

# ###### END OF DOWNLOAD SECTION ########################
# #######################################################
project(imgui-vtk)

set(CMAKE_CXX_STANDARD 11)
set(EXEC_NAME imvtkCPM)
set(PROGRAM_SRC main.cpp)
add_executable(${EXEC_NAME} ${PROGRAM_SRC})

# VTK
find_package(VTK COMPONENTS
  CommonCore
  CommonColor
  CommonDataModel
  FiltersCore
  InteractionStyle
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
  QUIET
)

if(NOT VTK_FOUND)
  message(FATAL_ERROR "VTK not found!")
  return()
endif()

if(VTK_VERSION VERSION_LESS "9.0.0")
  include(${VTK_USE_FILE})
else()
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ${EXEC_NAME}
    MODULES ${VTK_LIBRARIES}
  )
endif()

target_link_libraries(${EXEC_NAME} imgui_vtk_viewer)

Successfully tried on Ubuntu 22.04 with VS Code

@rajkundu
Copy link
Collaborator

I definitely hear you; bundling gl3w, glfw, and ImGui as Git submodules is nice for demonstration purposes (e.g., reliably cloning, building, and running this repo), but it is not so great when integrating imgui-vtk into larger CMake projects. However, I think most of that is because of ImGui's design. Since it's a lightweight, cross-platform, cross-toolchain library, it doesn't have any official CMake integration (at least as far as I know). There are a lot of files in this repo, but I think all you really need are VtkViewer.h and VtkViewer.cpp if you already have ImGui & VTK.

Personally, I'm not familiar with CPM, but thank you very much for sharing your CMakeLists.txt. I'm glad you're able to make good use of imgui-vtk! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants