Skip to content

Commit

Permalink
mesh convex hull
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Dec 18, 2023
1 parent 9558224 commit 5b9b74d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,6 @@
[submodule "externals/protobuf/protobuf"]
path = externals/protobuf/protobuf
url = https://github.com/protocolbuffers/protobuf.git
[submodule "externals/quickhull/quickhull"]
path = externals/quickhull/quickhull
url = https://github.com/akuukka/quickhull.git
2 changes: 1 addition & 1 deletion cmake/cage_copy_redist.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function(cage_copy_redist)
cage_copy_redist_license("${cage_copy_redist_current_list_dir}/../externals/pmp/pmp/LICENSE.txt" "pmp" COPYONLY)
cage_copy_redist_license("${cage_copy_redist_current_list_dir}/../externals/png/png/LICENSE" "png" COPYONLY)
cage_copy_redist_license("${cage_copy_redist_current_list_dir}/../externals/protobuf/protobuf/LICENSE" "protobuf" COPYONLY)
cage_copy_redist_license("${cage_copy_redist_current_list_dir}/../externals/quickhull/quickhull/README.md" "quickhull" COPYONLY)
cage_copy_redist_license("${cage_copy_redist_current_list_dir}/../externals/samplerate/libsamplerate/COPYING" "samplerate" COPYONLY)
cage_copy_redist_license("${cage_copy_redist_current_list_dir}/../externals/simplefilewatcher/simplefilewatcher/License.txt" "simplefilewatcher" COPYONLY)
cage_copy_redist_license("${cage_copy_redist_current_list_dir}/../externals/stb/stb/LICENSE" "stb" COPYONLY)
Expand All @@ -45,4 +46,3 @@ function(cage_copy_redist)
cage_copy_redist_license("${cage_copy_redist_current_list_dir}/../externals/zstd/zstd/LICENSE" "zstd" COPYONLY)
cage_copy_redist_license("${cage_copy_redist_current_list_dir}/../LICENSE" "cage" COPYONLY)
endfunction(cage_copy_redist)

3 changes: 2 additions & 1 deletion externals/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ add_subdirectory(samplerate)
add_subdirectory(bc7enc_rdo)
add_subdirectory(avir)
add_subdirectory(openxr-sdk)
add_subdirectory(quickhull)

option(cage_use_steam_sockets "include Game Networking Sockets library by Valve/Steam" OFF)
if (cage_use_steam_sockets)
Expand Down Expand Up @@ -171,4 +172,4 @@ external_target_properties(libprotoc)
external_target_properties(protoc)
external_target_properties(utf8_validity)
external_target_properties(GameNetworkingSockets_s)

external_target_properties(quickhull)
11 changes: 11 additions & 0 deletions externals/quickhull/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.20)

message(STATUS "----------------------------------------------------------------")
message(STATUS "quickhull")
message(STATUS "----------------------------------------------------------------")

file(GLOB SOURCES quickhull/*.cpp quickhull/*.hpp quickhull/Structs/*)

add_library(quickhull STATIC ${SOURCES})

target_include_directories(quickhull INTERFACE .)
1 change: 1 addition & 0 deletions externals/quickhull/quickhull
Submodule quickhull added at 9f81d1
2 changes: 1 addition & 1 deletion sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ file(GLOB_RECURSE cage-core-sources "libcore/*" "include/cage-core/*")
add_library(cage-core SHARED ${cage-core-sources})
target_compile_features(cage-core PUBLIC cxx_std_${CMAKE_CXX_STANDARD})
target_include_directories(cage-core PUBLIC $<INSTALL_INTERFACE:include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_link_libraries(cage-core PRIVATE zlib libzstd_static simplefilewatcher utfcpp fastnoise hsluv png_static cage-jpeg tiff tiffxx tinyexr stb xatlas pmp dualmc dr_libs ogg vorbis vorbisfile vorbisenc samplerate assimp bc7enc_rdo avir)
target_link_libraries(cage-core PRIVATE zlib libzstd_static simplefilewatcher utfcpp fastnoise hsluv png_static cage-jpeg tiff tiffxx tinyexr stb xatlas pmp dualmc dr_libs ogg vorbis vorbisfile vorbisenc samplerate assimp bc7enc_rdo avir quickhull)
if (cage_use_steam_sockets)
target_link_libraries(cage-core PRIVATE GameNetworkingSockets_s)
target_compile_definitions(cage-core PUBLIC "CAGE_USE_STEAM_SOCKETS")
Expand Down
4 changes: 4 additions & 0 deletions sources/include/cage-core/meshAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ namespace cage
};
CAGE_CORE_API Holder<PointerRange<Holder<Mesh>>> meshChunking(const Mesh *msh, const MeshChunkingConfig &config);

struct CAGE_CORE_API MeshConvexHullConfig
{};
CAGE_CORE_API Holder<Mesh> meshConvexHull(const Mesh *msh, const MeshConvexHullConfig &config);

struct CAGE_CORE_API MeshUnwrapConfig
{
// charting determines which triangles will form islands
Expand Down
33 changes: 33 additions & 0 deletions sources/libcore/mesh/quickhull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <quickhull/QuickHull.hpp>

#include "mesh.h"

#include <cage-core/meshAlgorithms.h>
#include <cage-core/serialization.h>

namespace cage
{
CAGE_CORE_API Holder<Mesh> meshConvexHull(const Mesh *msh, const MeshConvexHullConfig &config)
{
if (msh->type() != MeshTypeEnum::Triangles)
CAGE_THROW_ERROR(Exception, "mesh convex hull requires triangles mesh");

quickhull::QuickHull<float> qh;
const auto tmp = qh.getConvexHull(reinterpret_cast<const float *>(msh->positions().data()), msh->positions().size(), false, false);

Holder<Mesh> res = newMesh();
{
static_assert(sizeof(quickhull::Vector3<float>) == sizeof(Vec3));
PointerRange r(tmp.getVertexBuffer().begin(), tmp.getVertexBuffer().end());
res->positions(bufferCast<const Vec3>(r));
}
{
std::vector<uint32> inds;
inds.reserve(tmp.getIndexBuffer().size());
for (auto it : tmp.getIndexBuffer())
inds.push_back(numeric_cast<uint32>(it));
res->indices(inds);
}
return res;
}
}
8 changes: 8 additions & 0 deletions sources/test-core/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,14 @@ namespace
}
}

{
CAGE_TESTCASE("convex hull");
auto p = makeDoubleBalls();
MeshConvexHullConfig cfg;
const auto hull = meshConvexHull(+p, cfg);
hull->exportFile("meshes/algorithms/convexHull.obj");
}

{
CAGE_TESTCASE("unwrap");
auto p = makeSphere();
Expand Down

0 comments on commit 5b9b74d

Please sign in to comment.