Skip to content

Commit

Permalink
Fix issue #88 and add option to enable or disable unsafe casts
Browse files Browse the repository at this point in the history
  • Loading branch information
madmann91 committed Jul 7, 2024
1 parent 3490634 commit 77a08ca
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 3.21)
project(bvh VERSION 2.0)

option(BVH_BUILD_C_API "Builds the C API library wrapper" OFF)
option(BVH_STATIC_LINK_STDLIB_C_API "Link the C API library statically against the standard C++ library" OFF)
option(BVH_C_API_STATIC_LINK_STDLIB "Link the C API library statically against the standard C++ library (only supported by clang/gcc)" OFF)
option(BVH_C_API_UNSAFE_CASTS "Enable unsafe casts in C API" OFF)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
Expand Down
1 change: 1 addition & 0 deletions src/bvh/v2/bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <vector>
#include <stack>
#include <utility>
#include <tuple>
#include <algorithm>

namespace bvh::v2 {
Expand Down
6 changes: 5 additions & 1 deletion src/bvh/v2/c_api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ set_target_properties(bvh_c PROPERTIES
CXX_VISIBILITY_PRESET hidden
INTERPROCEDURAL_OPTIMIZATION_RELEASE ON)

if (BVH_STATIC_LINK_STDLIB_C_API)
if (BVH_C_API_STATIC_LINK_STDLIB)
# Link statically against standard C++ library
target_link_options(bvh_c PRIVATE $<$<C_COMPILER_ID:GNU,Clang>:-static-libstdc++>)
endif()

if (BVH_C_API_UNSAFE_CASTS)
target_compile_definitions(bvh_c PRIVATE -DBVH_C_API_UNSAFE_CASTS)
endif()

install(FILES bvh.h DESTINATION include/bvh/v2/c_api/)
6 changes: 3 additions & 3 deletions src/bvh/v2/c_api/bvh_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ template <> struct BvhCallback<double> { using Type = bvh_intersect_callbackd; }
template <typename T, size_t Dim>
static auto translate(enum bvh_build_quality quality) {
switch (quality) {
#ifndef BVH_C_UNSAFE_CASTS
#ifndef BVH_C_API_UNSAFE_CASTS
case BVH_BUILD_QUALITY_LOW:
return bvh::v2::DefaultBuilder<bvh::v2::Node<T, Dim>>::Quality::Low;
case BVH_BUILD_QUALITY_MEDIUM:
Expand All @@ -28,7 +28,7 @@ static auto translate(enum bvh_build_quality quality) {
return bvh::v2::DefaultBuilder<bvh::v2::Node<T, Dim>>::Quality::High;
#endif
default:
return static_cast<bvh::v2::DefaultBuilder<bvh::v2::Node<T, Dim>>::Quality>(quality);
return static_cast<typename bvh::v2::DefaultBuilder<bvh::v2::Node<T, Dim>>::Quality>(quality);
}
}

Expand Down Expand Up @@ -89,7 +89,7 @@ static typename BvhTypes<T, Dim>::Bvh* bvh_build(
{
bvh::v2::BBox<T, Dim>* translated_bboxes = nullptr;
bvh::v2::Vec<T, Dim>* translated_centers = nullptr;
#ifdef BVH_C_UNSAFE_CASTS
#ifdef BVH_C_API_UNSAFE_CASTS
translated_bboxes = reinterpret_cast<decltype(translated_bboxes)>(bboxes);
translated_centers = reinterpret_cast<decltype(translated_centers)>(centers);
#else
Expand Down

0 comments on commit 77a08ca

Please sign in to comment.