Skip to content

Commit aa3155c

Browse files
author
Dan Koschier
committed
Initial commit.
0 parents  commit aa3155c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1719669
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.bmp
2+
resource_path.hpp
3+
*.*sdf

CMakeLists.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.0)
2+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules")
3+
4+
project(Discregrid)
5+
6+
# Visual studio solution directories.
7+
set_property(GLOBAL PROPERTY USE_FOLDERS on)
8+
9+
# Enable simultaneous compilation of source files.
10+
if(WIN32)
11+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
12+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /openmp")
13+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4250")
14+
endif(WIN32)
15+
16+
set(CMAKE_DEBUG_POSTFIX "_d")
17+
18+
# Put all executables and libraries into a common directory.
19+
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin")
20+
set(LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin")
21+
22+
add_subdirectory(discregrid)
23+
24+
option(BUILD_CMD_EXECUTABLE "Build command line executable" ON)
25+
if(BUILD_CMD_EXECUTABLE)
26+
add_subdirectory(cmd)
27+
endif(BUILD_CMD_EXECUTABLE)

FindDiscregrid.cmake

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# - Try to find Discregrid library
2+
#
3+
# The find script can be invoked by using the following command:
4+
# find_package(Discregrid)
5+
#
6+
# Once done this will define
7+
#
8+
# DISCREGRID_FOUND - System has found the discregrid library
9+
# DISCREGRID_INCLUDE_DIRS - Path to the discregrid include directory
10+
# DISCREGRID_LIBRARIES - Path to the static discregrid library
11+
12+
# ============================================================================
13+
# _DISCREGRID_FIND_INCLUDE_DIR
14+
# Internal function to find the include directories
15+
# _var = variable to set
16+
# _hdr = header file to look for
17+
# ============================================================================
18+
function(_DISCREGRID_FIND_INCLUDE_DIR _var _hdr)
19+
find_path(${_var} ${_hdr}
20+
PATHS
21+
$ENV{DISCREGRID_ROOT}
22+
${DISCREGRID_ROOT}
23+
PATH_SUFFIXES
24+
/include
25+
)
26+
27+
if (${_var})
28+
set(DISCREGRID_INCLUDE_DIRS ${DISCREGRID_INCLUDE_DIRS} ${${_var}} PARENT_SCOPE)
29+
if (NOT DISCREGRID_SKIP_MARK_AS_ADVANCED)
30+
mark_as_advanced(${_var})
31+
endif()
32+
endif()
33+
endfunction(_DISCREGRID_FIND_INCLUDE_DIR)
34+
35+
# ============================================================================
36+
# _DISCREGRID_FIND_LIBRARY
37+
# Internal function to find libraries packaged with DISCREGRID
38+
# _var = library variable to create
39+
# ============================================================================
40+
function(_DISCREGRID_FIND_LIBRARY _var _lib _mode)
41+
find_library(${_var}
42+
NAMES ${_lib}
43+
PATHS
44+
$ENV{DISCREGRID_ROOT}
45+
${DISCREGRID_ROOT}
46+
PATH_SUFFIXES
47+
/lib
48+
)
49+
50+
if(${_var})
51+
set(DISCREGRID_LIBRARIES ${DISCREGRID_LIBRARIES} ${_mode} ${${_var}} PARENT_SCOPE)
52+
if(NOT DISCREGRID_SKIP_MARK_AS_ADVANCED)
53+
mark_as_advanced(${_var})
54+
endif()
55+
endif()
56+
endfunction(_DISCREGRID_FIND_LIBRARY)
57+
58+
# ============================================================================
59+
#
60+
# main()
61+
#
62+
# ============================================================================
63+
64+
#
65+
# Find all libraries and include directories.
66+
#
67+
_DISCREGRID_FIND_INCLUDE_DIR(DISCREGRID_DISCREGRIDH_INCLUDE_DIR Discregrid/All)
68+
_DISCREGRID_FIND_LIBRARY(DISCREGRID_LIBRARY_RELEASE "discregrid" optimized)
69+
_DISCREGRID_FIND_LIBRARY(DISCREGRID_LIBRARY_DEBUG "discregrid_d" debug)
70+
71+
#
72+
# Try to enforce components.
73+
#
74+
include(FindPackageHandleStandardArgs)
75+
76+
find_package_handle_standard_args(DISCREGRID DEFAULT_MSG
77+
DISCREGRID_DISCREGRIDH_INCLUDE_DIR
78+
DISCREGRID_LIBRARY_DEBUG
79+
DISCREGRID_LIBRARY_RELEASE
80+
)
81+
82+
if(NOT DISCREGRID_FOUND)
83+
set(DISCREGRID_INCLUDE_DIRS)
84+
set(DISCREGRID_LIBRARIES)
85+
endif()
86+
87+
if(DISCREGRID_INCLUDE_DIRS)
88+
list(REMOVE_DUPLICATES DISCREGRID_INCLUDE_DIRS)
89+
endif()

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Jan Bender
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Discregrid
2+
3+
**Discregrid** is a static C++ library for the parallel discretization of (preferably smooth) functions on regular grids. Given a box-shaped domain, a grid resolution and a function mapping a three-dimensional position in space to a scalar real value a polynomial discretization is computed. In the current implementation cubic polynomials of Serendipity type are employed for the cell-wise discretization. The coefficient vector for the discrete polynomial basis is computed using regular sampling of the input function at the higher-order grid's nodes. However, I plan to provide a spatially adaptive version of the cubic discretization and moreover an implementation of the hp-adaptive discretization algorithm described in [KDBB17]. The algorithm to generate the discretization is moreover *fully parallelized** using OpenMP is especially well-suited for the discretization of signed distance functions. The library moreover provides the functionality to serialize and deserialize the a generated discrete grid.
4+
5+
Besides the library the project includes three executable programs that serve the following purposes:
6+
* *GenerateSDF*: Computes a discrete (cubic) signed distance field from a triangle mesh in OBJ format.
7+
* *DiscreteFieldToBitmap*: Generates an image in bitmap format of a two-dimensional slice of a previously computed discretization.
8+
* *GenerateDensityMap*: Generates a density map according to the approach presented in [KB17] from a previously generated discrete signed distance field.
9+
10+
**Author**: Dan Koschier, **License**: MIT
11+
12+
## Libraries using CompactNSearch
13+
* [PBD] - A C++ library for physically-based simulation of rigid bodies, deformables, cloth and fluids using Position-Based Dynamics. Discregrid is used to compute discrete signed distance fields of rigid objects for collision handling purposes.
14+
* [SPlisHSPlasH] - A C++ library for the physically-based simulation of fluids using Smoothed Particle Hydrodynamics. Discregrid is used to compute density maps according to my paper [KB17] for boundary handling.
15+
16+
## Build Instructions
17+
18+
This project is based on [CMake](https://cmake.org/). Simply generate project, Makefiles, etc. using [CMake](https://cmake.org/) and compile the project with the compiler of your choice. The code was tested with the following configurations:
19+
- Windows 10 64-bit, CMake 3.8, Visual Studio 2017
20+
- Debian 8 64-bit, CMake 3.8, GCC 4.9.2.
21+
22+
## Usage
23+
In order to use the library the main header has to be included and the static library has to be compiled and linked against the client program.
24+
In this regard a find script for CMake is provided, i.e. FindDiscregrid.cmake.
25+
The main header can be included as follows:
26+
```c++
27+
#include <Discregrid/All>
28+
```
29+
30+
A base class for the data structure that generates and holds a discretization a function f: R -> R^3 can be constructed as follows:
31+
```c++
32+
// Firstly, create a domain on which a discretization will be generated.
33+
Eigen::AlignedBox3d domain;
34+
// Then specify domain extents using e.g. domain.extend(...).
35+
// Secondly, specify a grid resolution.
36+
std::array<unsigned int, 3> resolution = {{10, 10, 10}}
37+
// Finally, instantiate the grid.
38+
Discregrid::CubicLagrangeDiscreteGrid discrete_grid(domain, resolution);
39+
```
40+
Then, an arbitrary number of functions can be discretized on the initiated grid:
41+
```c++
42+
Discregrid::DiscreteGrid::ContinuousFunction func1 = ...;
43+
Discregrid::DiscreteGrid::ContinuousFunction func2 = ...;
44+
45+
auto df_index1 = discrete_grid.addFunction(func1);
46+
auto df_index2 = discrete_grid.addFunction(func2);
47+
```
48+
Optionally, only coefficients at nodes fulfilling a certain predicate can be generated by specifying the predicate:
49+
```c++
50+
Discregrid::DiscreteGrid::ContinuousFunction func3 = ...;
51+
auto df_index3 = discrete_grid.addFunction(func3, false, [&](Vector3d const& x)
52+
{
53+
...
54+
// Return true if a certain criterion for the node location x is fulfilled, e.g.
55+
return x.y() > 0.0;
56+
});
57+
```
58+
A value of a discrete field can be evaluated by interpolation.
59+
Additionally, the gradient at the given query point can be computed if desired.
60+
```c++
61+
auto val1 = sdf->interpolate(df_index1, {0.1, 0.2, 0.3});
62+
auto grad2 = Eigen::Vector3d{};
63+
auto val2 = sdf->interpolate(df_index2, {0.3, 0.2, 0.1}, &grad2);
64+
```
65+
66+
If a discretization of the input function is only required in certain regions of the given domain, the discretization can be reduced resulting in a sparsely populated grid to save memory:
67+
```c++
68+
discrete_grid.reduce_field(df_index1, [](Eigen::Vector3d const& x, double v)
69+
{
70+
// E.g.
71+
return x.x() < 0.0 && v > 0.0;
72+
});
73+
```
74+
Here x represents the location of sample point in the grid and v represents the sampled value of the input function. If the predicated function evaluates to true the sample point is kept but discarded otherwise.
75+
76+
Optionally, the data structure can be serialized and deserialized via
77+
```c++
78+
discrete_grid.save(filename);
79+
discrete_grid.load(filename); // or
80+
discrete_grid = Discregrid::CubicLagrangeDiscreteGrid(filename);
81+
```
82+
83+
## References
84+
85+
* [KDBB17] D. Koschier, C. Deul, M. Brand and J. Bender, 2017. "An hp-Adaptive Discretization Algorithm for Signed Distance Field Generation", IEEE Transactions on Visualiztion and Computer Graphics 23, 10, 2208-2221.
86+
* [KB17] D. Koschier and J. Bender, 2017. "Density Maps for Improved SPH Boundary Handling", ACM SIGGRAPH/Eurographics Symposium on Computer Animation, 1-10.
87+
88+
[PBD]: <https://github.com/InteractiveComputerGraphics/PositionBasedDynamics>
89+
[SPlisHSPlasH]: <https://github.com/InteractiveComputerGraphics/SPlisHSPlasH>

cmake/Modules/FindEigen3.cmake

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# - Try to find Eigen3 lib
2+
#
3+
# This module supports requiring a minimum version, e.g. you can do
4+
# find_package(Eigen3 3.1.2)
5+
# to require version 3.1.2 or newer of Eigen3.
6+
#
7+
# Once done this will define
8+
#
9+
# EIGEN3_FOUND - system has eigen lib with correct version
10+
# EIGEN3_INCLUDE_DIR - the eigen include directory
11+
# EIGEN3_VERSION - eigen version
12+
13+
# Copyright (c) 2006, 2007 Montel Laurent, <[email protected]>
14+
# Copyright (c) 2008, 2009 Gael Guennebaud, <[email protected]>
15+
# Copyright (c) 2009 Benoit Jacob <[email protected]>
16+
# Redistribution and use is allowed according to the terms of the 2-clause BSD license.
17+
18+
if(NOT Eigen3_FIND_VERSION)
19+
if(NOT Eigen3_FIND_VERSION_MAJOR)
20+
set(Eigen3_FIND_VERSION_MAJOR 2)
21+
endif(NOT Eigen3_FIND_VERSION_MAJOR)
22+
if(NOT Eigen3_FIND_VERSION_MINOR)
23+
set(Eigen3_FIND_VERSION_MINOR 91)
24+
endif(NOT Eigen3_FIND_VERSION_MINOR)
25+
if(NOT Eigen3_FIND_VERSION_PATCH)
26+
set(Eigen3_FIND_VERSION_PATCH 0)
27+
endif(NOT Eigen3_FIND_VERSION_PATCH)
28+
29+
set(Eigen3_FIND_VERSION "${Eigen3_FIND_VERSION_MAJOR}.${Eigen3_FIND_VERSION_MINOR}.${Eigen3_FIND_VERSION_PATCH}")
30+
endif(NOT Eigen3_FIND_VERSION)
31+
32+
macro(_eigen3_check_version)
33+
file(READ "${EIGEN3_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h" _eigen3_version_header)
34+
35+
string(REGEX MATCH "define[ \t]+EIGEN_WORLD_VERSION[ \t]+([0-9]+)" _eigen3_world_version_match "${_eigen3_version_header}")
36+
set(EIGEN3_WORLD_VERSION "${CMAKE_MATCH_1}")
37+
string(REGEX MATCH "define[ \t]+EIGEN_MAJOR_VERSION[ \t]+([0-9]+)" _eigen3_major_version_match "${_eigen3_version_header}")
38+
set(EIGEN3_MAJOR_VERSION "${CMAKE_MATCH_1}")
39+
string(REGEX MATCH "define[ \t]+EIGEN_MINOR_VERSION[ \t]+([0-9]+)" _eigen3_minor_version_match "${_eigen3_version_header}")
40+
set(EIGEN3_MINOR_VERSION "${CMAKE_MATCH_1}")
41+
42+
set(EIGEN3_VERSION ${EIGEN3_WORLD_VERSION}.${EIGEN3_MAJOR_VERSION}.${EIGEN3_MINOR_VERSION})
43+
if(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION})
44+
set(EIGEN3_VERSION_OK FALSE)
45+
else(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION})
46+
set(EIGEN3_VERSION_OK TRUE)
47+
endif(${EIGEN3_VERSION} VERSION_LESS ${Eigen3_FIND_VERSION})
48+
49+
if(NOT EIGEN3_VERSION_OK)
50+
51+
message(STATUS "Eigen3 version ${EIGEN3_VERSION} found in ${EIGEN3_INCLUDE_DIR}, "
52+
"but at least version ${Eigen3_FIND_VERSION} is required")
53+
endif(NOT EIGEN3_VERSION_OK)
54+
endmacro(_eigen3_check_version)
55+
56+
if (EIGEN3_INCLUDE_DIR)
57+
58+
# in cache already
59+
_eigen3_check_version()
60+
set(EIGEN3_FOUND ${EIGEN3_VERSION_OK})
61+
62+
else (EIGEN3_INCLUDE_DIR)
63+
64+
find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library
65+
PATHS
66+
${CMAKE_INSTALL_PREFIX}/include
67+
${KDE4_INCLUDE_DIR}
68+
PATH_SUFFIXES eigen3 eigen
69+
)
70+
71+
if(EIGEN3_INCLUDE_DIR)
72+
_eigen3_check_version()
73+
endif(EIGEN3_INCLUDE_DIR)
74+
75+
include(FindPackageHandleStandardArgs)
76+
find_package_handle_standard_args(Eigen3 DEFAULT_MSG EIGEN3_INCLUDE_DIR EIGEN3_VERSION_OK)
77+
78+
mark_as_advanced(EIGEN3_INCLUDE_DIR)
79+
80+
endif(EIGEN3_INCLUDE_DIR)
81+

cmd/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
add_subdirectory(generate_sdf)
2+
add_subdirectory(discrete_field_to_bitmap)
3+
add_subdirectory(generate_density_map)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Eigen library.
2+
find_package(Eigen3 REQUIRED)
3+
4+
# Set include directories.
5+
include_directories(
6+
../../extern
7+
../../discregrid/include
8+
${EIGEN3_INCLUDE_DIR}
9+
)
10+
11+
12+
if(WIN32)
13+
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
14+
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
15+
endif(WIN32)
16+
17+
if ( CMAKE_COMPILER_IS_GNUCC )
18+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-multichar")
19+
endif ( CMAKE_COMPILER_IS_GNUCC )
20+
21+
# OpenMP support.
22+
find_package(OpenMP REQUIRED)
23+
if(OPENMP_FOUND)
24+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
25+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
26+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
27+
endif()
28+
29+
add_executable(DiscreteFieldToBitmap
30+
main.cpp
31+
bmp_file.hpp
32+
bmp_file.cpp
33+
)
34+
35+
add_dependencies(DiscreteFieldToBitmap
36+
Discregrid
37+
)
38+
39+
target_link_libraries(DiscreteFieldToBitmap
40+
Discregrid
41+
)
42+
43+
set_target_properties(DiscreteFieldToBitmap PROPERTIES FOLDER Cmd)

0 commit comments

Comments
 (0)