Files
robot_line_extraction/CMakeLists.txt
duongtd 1d38e77afa Extract line segments from an ordered range scan
Fitting a line needs the ranges in ray order with a known angular step. A
costmap has neither: at 0.05 m with inflation it has already destroyed ray
order and object shape, so the two 0.2 m wings of a dock plate are one blob
there. This is the consumer that robot_sensor_ports keeps readings raw for.

Pure geometry - the input is a scan layout and a vector of ranges, the output
is segments in polar form with their endpoints, supporting rays and
covariance. Eigen is the only dependency and only the merge step uses it. No
robot_cpp, no costmap, no message types, which is what makes it testable
against synthetic scans with no runtime around it: build the shape, generate
the rays, and the answer is known in advance.

Polar rather than slope-intercept because a vertical wall has no finite slope
and a laser sees vertical walls constantly. Radius is kept non-negative so one
line has one representation - otherwise the merge step sees two.

Ported from laser_line_extraction (Marc Gallant, BSD) with the ROS layer
removed, and four defects fixed, each with a test that fails without the fix:

split() sliced with &indices[i_split - 1] and &indices.back(), dropping a point
off each half at every split. Anything angular came out a ray short at both
ends of every arm.

mergeLines() called resize() and then inserted at end(), leaving the first half
of the vector zero-filled. A merged line reported twice its real support and
every index in that half pointed at ray 0.

The least-squares loop had no iteration cap. The increment is a ratio of two
sums that can both approach zero on a degenerate run, and when it oscillates
the loop never exits - in a control thread that is a stopped robot, not a slow
frame. Segments that will not converge are now dropped, since their covariance
would be meaningless and the merge step decides by covariance.

Merging compared line parameters and nothing else, so two halves of a wall
either side of a doorway - perfectly collinear - were rejoined into one segment
spanning the doorway, undoing the split that had just separated them for that
exact reason. Merging now also requires the facing endpoints to be within
max_line_gap, so split and merge agree on what one surface means.

The corner point at a distance split now belongs to both halves. It is the last
point of one surface and the first of the next; giving it to one side shortens
the other arm by a ray and tilts its fit.

split() is iterative over an explicit stack rather than recursive: depth is
bounded only by the ray count, and a noisy scan that splits at nearly every
point would recurse a thousand deep in the control thread.

Parameters are validated at configure time rather than per scan. A threshold
that can never produce a fit otherwise shows up as "the detector finds
nothing", which is indistinguishable from a sensor fault.
2026-08-26 12:09:28 +07:00

164 lines
4.6 KiB
CMake

cmake_minimum_required(VERSION 3.0.2)
project(robot_line_extraction VERSION 0.1.0 LANGUAGES CXX)
# ========================================================
# Build mode detection
# ========================================================
if(DEFINED CATKIN_DEVEL_PREFIX OR DEFINED CATKIN_TOPLEVEL)
set(BUILDING_WITH_CATKIN TRUE)
message(STATUS "Building robot_line_extraction with Catkin")
else()
set(BUILDING_WITH_CATKIN FALSE)
message(STATUS "Building robot_line_extraction with Standalone CMake")
endif()
# ========================================================
# C++ Standard
# ========================================================
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# ========================================================
# Dependencies
# ========================================================
# Eigen là phụ thuộc DUY NHẤT, và chỉ dùng ở bước gộp hai đoạn. Gói này cố ý không kéo robot_cpp,
# costmap hay message nào — đó là thứ khiến nó test được bằng scan tổng hợp, không cần runtime.
find_package(Eigen3 REQUIRED)
# ========================================================
# Catkin configuration
# ========================================================
if(BUILDING_WITH_CATKIN)
find_package(catkin REQUIRED)
catkin_package(
INCLUDE_DIRS
include
LIBRARIES
robot_line_extraction
DEPENDS
EIGEN3
)
endif()
# ========================================================
# Library
# ========================================================
add_library(robot_line_extraction SHARED
src/types.cpp
src/line.cpp
src/line_extractor.cpp
)
target_compile_options(robot_line_extraction PRIVATE -Wall -Wextra)
target_include_directories(robot_line_extraction
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
# Header của Eigen vào dạng SYSTEM: gói này build với -Wall -Wextra, mà Eigen sinh warning riêng
# của nó — để lẫn vào thì warning của chính gói này vô hình.
target_include_directories(robot_line_extraction SYSTEM
PUBLIC
${EIGEN3_INCLUDE_DIRS}
)
if(NOT BUILDING_WITH_CATKIN)
set_target_properties(robot_line_extraction PROPERTIES
POSITION_INDEPENDENT_CODE ON
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
BUILD_RPATH "${CMAKE_BINARY_DIR}"
INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib"
)
endif()
# ========================================================
# Install
# ========================================================
if(BUILDING_WITH_CATKIN)
install(TARGETS robot_line_extraction
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
)
else()
include(GNUInstallDirs)
install(TARGETS robot_line_extraction
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)
install(DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
FILES_MATCHING PATTERN "*.h"
)
endif()
# ========================================================
# Tests
# ========================================================
if(CATKIN_ENABLE_TESTING AND BUILDING_WITH_CATKIN)
if(NOT COMMAND catkin_add_gtest)
message(FATAL_ERROR "catkin_add_gtest NOT FOUND")
endif()
catkin_add_gtest(line_extraction_test test/line_extraction_test.cpp)
if(TARGET line_extraction_test)
# catkin_add_gtest đặt target EXCLUDE_FROM_ALL; mở ra để binary có mặt sau `catkin_make`
# thường, đúng quy trình verify của repo.
set_target_properties(line_extraction_test PROPERTIES EXCLUDE_FROM_ALL FALSE)
target_compile_options(line_extraction_test PRIVATE -Wall -Wextra)
target_include_directories(line_extraction_test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
)
target_include_directories(line_extraction_test SYSTEM
PRIVATE
${EIGEN3_INCLUDE_DIRS}
)
# catkin_add_gtest dùng target_link_libraries dạng plain, nên phần bổ sung cũng phải plain.
target_link_libraries(line_extraction_test
robot_line_extraction
pthread
)
endif()
endif()