# ------------------------------------------------------------------------------
#        A Modular Optimization framework for Localization and mApping
#                               (MOLA)
#
# Copyright (C) 2018-2026, Jose Luis Blanco-Claraco.
# All rights reserved.
# Released under GNU GPL v3. See LICENSE file
# ------------------------------------------------------------------------------

# Minimum CMake vesion: limited by CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS
cmake_minimum_required(VERSION 3.22...3.31)  # Ubuntu 22.04+

if("$ENV{ROS_VERSION}" STREQUAL "2")
  set(DETECTED_ROS2 TRUE)
endif()

# Tell CMake we'll use C++ for use in its tests/flags
project(mola_metric_maps LANGUAGES CXX)

# MOLA CMake scripts: "mola_xxx()"
find_package(mola_common REQUIRED)

# find CMake dependencies:
find_package(mrpt_maps REQUIRED)
find_package(mrpt_viz REQUIRED)
find_package(mp2p_icp_map REQUIRED)
find_package(Eigen3 REQUIRED NO_MODULE)

find_package(TBB) # optional

find_package(mola_test_datasets QUIET) # optional (for testing builds only)

if(TBB_FOUND)
  option(MOLA_METRIC_MAPS_USE_TBB "If found TBB, this option controls whether to use it or not" ON)
endif()

# mola::IncrementalPointCloud's k-d tree index needs nanoflann's incremental
# (self-balancing) k-d tree, added in nanoflann 1.10.0.
find_package(nanoflann REQUIRED)

# 3rdparty deps:
add_subdirectory(3rdparty)

# -----------------------
# define lib:
set(LIB_SRCS
  src/HashedVoxelPointCloud.cpp
  src/KeyframePointCloudMap.cpp
  src/NDT.cpp
  src/OccGrid.cpp
  src/OptionsIniIO.cpp
  src/register.cpp
  src/SparseTreesPointCloud.cpp
  src/SparseVoxelPointCloud.cpp
  src/TSDF.cpp
)
set(LIB_PUBLIC_HDRS
  include/mola_metric_maps/FixedDenseGrid3D.h
  include/mola_metric_maps/HashedVoxelPointCloud.h
  include/mola_metric_maps/index3d_t.h
  include/mola_metric_maps/KeyframePointCloudMap.h
  include/mola_metric_maps/NDT.h
  include/mola_metric_maps/OccGrid.h
  include/mola_metric_maps/OptionsCapable.h
  include/mola_metric_maps/OptionsIniIO.h
  include/mola_metric_maps/SparseTreesPointCloud.h
  include/mola_metric_maps/SparseVoxelPointCloud.h
  include/mola_metric_maps/TSDF.h
  include/mola_metric_maps/ViewDirectionFilter.h
)

list(APPEND LIB_SRCS
  src/IncrementalKDTree.cpp
  src/IncrementalPointCloud.cpp
  src/VoxelHashIndex.cpp
)
list(APPEND LIB_PUBLIC_HDRS
  include/mola_metric_maps/IncrementalPointCloud.h
  include/mola_metric_maps/PointIndexType.h
)

mola_add_library(
  TARGET ${PROJECT_NAME}
  SOURCES ${LIB_SRCS} ${LIB_PUBLIC_HDRS}
  PUBLIC_LINK_LIBRARIES
    mrpt::mrpt_maps
    mrpt::mrpt_viz
    tsl::robin_map
    mola::mp2p_icp_map
  PRIVATE_LINK_LIBRARIES
    Eigen3::Eigen
  CMAKE_DEPENDENCIES
    mrpt_maps
    mrpt_viz
    mp2p_icp_map
    robin_map
)

if (TBB_FOUND AND MOLA_METRIC_MAPS_USE_TBB)
  target_compile_definitions(${PROJECT_NAME} PRIVATE MOLA_METRIC_MAPS_USE_TBB)
  target_link_libraries(${PROJECT_NAME} PRIVATE TBB::tbb)

  # Sorting mp2p_icp::point_with_cov_pair_t (which embeds an over-aligned
  # Eigen-backed covariance matrix) makes GCC emit a -Wpsabi note about an
  # internal ABI change. The affected comparator never crosses a shared-library
  # boundary, so the note is safe to silence.
  if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    target_compile_options(${PROJECT_NAME} PRIVATE -Wno-psabi)
  endif()
endif()

# Public feature macro, always defined now, kept for code that still tests it:
target_compile_definitions(${PROJECT_NAME}
  PUBLIC MOLA_METRIC_MAPS_HAS_INCREMENTAL_POINT_CLOUD)

# mola::IncrementalPointCloud's k-d tree *baking* (TCreationOptions::serialize_kdtree,
# the mm-ipc-bake-kdtree CLI tool) needs nanoflann's
# KDTreeSingleIndexIncrementalAdaptor::saveIndex()/loadIndex(), added in 1.11.0.
# Every ROS distribution ships it, but the PyPI wheel builds against the older
# nanoflann embedded in the mrpt3 wheel. Without it, the option is a no-op on
# write and a loaded .mm's baked blob (if any) is skipped on read.
if(nanoflann_VERSION VERSION_GREATER_EQUAL 1.11)
  target_compile_definitions(${PROJECT_NAME}
    PUBLIC MOLA_METRIC_MAPS_HAS_INCREMENTAL_KDTREE_BAKE)
endif()

# nanoflann is deliberately NOT linked as a target, and its header is NOT
# reached through the include path at all: src/IncrementalKDTree.cpp includes
# it by ABSOLUTE path via the macro below.
#
# The reason is that MRPT ships (and its binaries are built against) its own,
# usually older, bundled copy of the very same "nanoflann.hpp", exported as an
# -isystem directory. Adding the right directory as "-I" does not help: when a
# directory appears both as -I and as -isystem, GCC drops the -I entry and
# keeps the system one, so MRPT's copy would still win. An absolute path
# removes the ambiguity entirely, and keeps the header out of every other
# translation unit of this library.
get_target_property(MOLA_NANOFLANN_INCLUDE_DIRS nanoflann::nanoflann INTERFACE_INCLUDE_DIRECTORIES)
find_file(MOLA_NANOFLANN_HEADER nanoflann.hpp
  PATHS ${MOLA_NANOFLANN_INCLUDE_DIRS}
  NO_DEFAULT_PATH
)
if(NOT MOLA_NANOFLANN_HEADER)
  message(FATAL_ERROR "nanoflann was found, but its nanoflann.hpp could not be located in '${MOLA_NANOFLANN_INCLUDE_DIRS}'")
endif()

set_source_files_properties(src/IncrementalKDTree.cpp
  PROPERTIES COMPILE_DEFINITIONS "MOLA_NANOFLANN_HEADER=\"${MOLA_NANOFLANN_HEADER}\"")

# -----------------------
# define apps:
option(MOLA_METRIC_MAPS_BUILD_APPS "Build the command-line apps" ON)
if(MOLA_METRIC_MAPS_BUILD_APPS)
  add_subdirectory(apps)
endif()

# -----------------------
# define tests:
option(MOLA_METRIC_MAPS_BUILD_TESTING "Build the unit tests" ON)
if(MOLA_METRIC_MAPS_BUILD_TESTING)
  enable_testing()
  add_subdirectory(tests)
endif()

# -----------------------
# Python bindings (skipped if pybind11 is not found):
option(MOLA_METRIC_MAPS_BUILD_PYTHON "Build the Python bindings, if pybind11 is found" ON)
if(MOLA_METRIC_MAPS_BUILD_PYTHON)
  add_subdirectory(python)
endif()

# -----------------------------------------------------------------------------
#  ROS2
# -----------------------------------------------------------------------------
if(DETECTED_ROS2)
  # find dependencies
  find_package(ament_cmake REQUIRED)

  ament_package()
endif()

