Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为 SimplePhysicsEngine 添加软体仿真 #31

Merged
merged 34 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
729002f
ci: fix crash bug and recording gif in test
Sep 11, 2024
724df19
ci: fix macos build and artifact error
Sep 11, 2024
7268251
ci: fix macos build and resolution error
Sep 11, 2024
330a87e
ci: fix macos build and resolution error
Sep 11, 2024
6f091c3
ci: fix macos build error
Sep 11, 2024
b063797
ci: fix macos build error
Sep 12, 2024
0695b21
ci: fix macos build error
Sep 12, 2024
0d8440b
ci: fix macos build error
Sep 12, 2024
0b812a6
ci: fix macos build error
Sep 12, 2024
f73dbc9
ci: fix macos build error
Sep 12, 2024
4126130
ci: fix macos build error
Sep 12, 2024
c381c81
ci: fix macos build error
Sep 12, 2024
46c7dab
ci: fix macos build error
Sep 12, 2024
e2207e2
ci: fix macos build error
Sep 12, 2024
0e01f4b
ci: fix macos build error
Sep 12, 2024
e6b8c91
ci: fix macos build error
Sep 12, 2024
8cdb56f
docs: update READEME, add demo img
Sep 12, 2024
95f5507
to: use cuda
For-Chance Sep 23, 2024
2c4fa9c
fix: fix bug in using cuda and make program faster
For-Chance Sep 26, 2024
0295d2b
ci: CUDA is not mandatory
For-Chance Sep 26, 2024
b84c459
docs: update demo imgs
For-Chance Sep 26, 2024
859278a
ci: fix macos test bug
For-Chance Sep 26, 2024
0d90b4e
ci: fix macos test bug
For-Chance Sep 26, 2024
16a1e0d
ci: try build ubuntu with cuda
For-Chance Sep 26, 2024
f30dcc1
ci: try build ubuntu with cuda
For-Chance Sep 26, 2024
89f1480
ci: try build ubuntu with cuda
For-Chance Sep 26, 2024
0d2c8e3
ci: try build ubuntu with cuda
For-Chance Sep 26, 2024
f6e16fe
ci: try build ubuntu with cuda
For-Chance Sep 26, 2024
376e8e2
ci: try build ubuntu with cuda
For-Chance Sep 26, 2024
fa0006a
ci: try build ubuntu with cuda
For-Chance Sep 27, 2024
0248ead
ci: try build ubuntu with cuda
For-Chance Sep 27, 2024
575b54b
feat: add move_pos and update cuda demo
For-Chance Sep 28, 2024
4f87433
ci: update test args
For-Chance Sep 28, 2024
c065d6d
docs: update demo imgs
For-Chance Sep 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 174 additions & 14 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ name: build

on:
- push
- pull_request
- release

env:
Expand Down Expand Up @@ -44,12 +43,28 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Check for CUDA
id: check_cuda
run: |
if command -v nvcc &> /dev/null; then
echo "CUDA is installed."
echo "has_cuda=true" >> $GITHUB_ENV
else
echo "CUDA is not installed."
echo "has_cuda=false" >> $GITHUB_ENV
fi

- name: Install dependencies
run: |
sudo apt update
sudo apt install --fix-missing -y doxygen graphviz clang-format clang-tidy cppcheck lcov
sudo apt install --fix-missing -y gcc g++ libspdlog-dev libcgal-dev freeglut3-dev libboost-all-dev libvtk9-dev qtbase5-dev xorg-dev libglu1-mesa-dev libglm-dev libglfw3-dev libglew-dev
sudo apt-get install --no-install-recommends -y xvfb
sudo apt-get install --no-install-recommends -y xvfb ffmpeg

# Optionally install CUDA dependencies if CUDA is available
if [ "$has_cuda" = "true" ]; then
sudo apt install --fix-missing -y cuda-toolkit
fi

- name: Build
run: |
Expand All @@ -59,21 +74,133 @@ jobs:

- name: Start Xvfb
run: |
nohup Xvfb :99 -screen 0 1024x768x24 &
nohup Xvfb :99 -screen 0 800x600x24 &
echo "DISPLAY=:99" >> $GITHUB_ENV

- name: Run demo2d
run: |
for i in {1..7}; do
./build/bin/demo2d --test --instruction $i
# Start recording with ffmpeg
ffmpeg -y -f x11grab -video_size 800x600 -i :99 -t 5 -r 10 ./build/bin/demo2d_$i.gif &
FFmpeg_PID=$!
# Run the demo2d program in the background
./build/bin/demo2d --instruction $i &
Demo2d_PID=$!
# Wait for 5 seconds
sleep 5
# Stop the ffmpeg recording
kill $FFmpeg_PID
# Terminate the demo2d program
kill $Demo2d_PID
done
env:
DISPLAY: :99

- name: Run MassSpring3D
run: ./build/bin/MassSpring3D --test --instruction 1
run: |
for i in {1..1}; do
ffmpeg -y -f x11grab -video_size 800x600 -i :99 -t 5 -r 10 ./build/bin/MassSpring3D_$i.gif &
FFmpeg_PID=$!
./build/bin/MassSpring3D --instruction $i --edge_num 101 &
MassSpring3D_PID=$!
sleep 5
kill $FFmpeg_PID
kill $MassSpring3D_PID
done
env:
DISPLAY: :99

- name: Upload GIFs
uses: actions/upload-artifact@v4
with:
name: gifs_ubuntu
path: build/bin/*.gif

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
files: build/coverage/coverage.info
verbose: true


check_nvidia_gpu:
runs-on: ubuntu-latest # use own runner with NVIDIA GPU
outputs:
has_gpu: ${{ steps.check_gpu.outputs.has_gpu }}
steps:
- name: Check for NVIDIA GPU
id: check_gpu
run: |
if lspci | grep -i nvidia; then
echo "NVIDIA GPU found."
echo "::set-output name=has_gpu::true"
else
echo "No NVIDIA GPU found."
echo "::set-output name=has_gpu::false"
fi

build_ubuntu_with_cuda:
runs-on: ubuntu-latest # use own runner with NVIDIA GPU
needs: check_nvidia_gpu
if: needs.check_nvidia_gpu.outputs.has_gpu == 'true'
steps:
- uses: actions/checkout@v3

- name: Set up CUDA
uses: Jimver/[email protected] # https://github.com/marketplace/actions/cuda-toolkit
with:
cuda: 11.7.0
sub-packages: '[]'
non-cuda-sub-packages: '[]'
method: network
linux-local-args: '["--toolkit", "--samples"]'
use-github-cache: true
use-local-cache: true
log-file-suffix: log.txt

- name: Install dependencies
run: |
sudo apt update
sudo apt install --fix-missing -y doxygen graphviz clang-format clang-tidy cppcheck lcov
sudo apt install --fix-missing -y gcc g++ libspdlog-dev libcgal-dev freeglut3-dev libboost-all-dev libvtk9-dev qtbase5-dev xorg-dev libglu1-mesa-dev libglm-dev libglfw3-dev libglew-dev
sudo apt-get install --no-install-recommends -y xvfb ffmpeg

# Optionally install CUDA dependencies if CUDA is available
if [ "$has_cuda" = "true" ]; then
sudo apt install --fix-missing -y cuda-toolkit
fi

- name: Build
run: |
ls
cmake --preset=build
cmake --build build --target all
cmake --build build --target coverage

- name: Start Xvfb
run: |
nohup Xvfb :99 -screen 0 800x600x24 &
echo "DISPLAY=:99" >> $GITHUB_ENV

- name: Run MassSpring3D
run: |
for i in {1..1}; do
ffmpeg -y -f x11grab -video_size 800x600 -i :99 -t 5 -r 10 ./build/bin/MassSpring3D_$i.gif &
FFmpeg_PID=$!
./build/bin/MassSpring3D --instruction $i --edge_num 101 &
MassSpring3D_PID=$!
sleep 5
kill $FFmpeg_PID
kill $MassSpring3D_PID
done
env:
DISPLAY: :99

- name: Upload GIFs
uses: actions/upload-artifact@v4
with:
name: gifs_ubuntu
path: build/bin/*.gif

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand All @@ -88,19 +215,18 @@ jobs:

- name: Install dependencies
run: |
brew install doxygen graphviz llvm cppcheck lcov spdlog boost freeglut glfw glew vtk cgal glm
brew install doxygen graphviz llvm cppcheck lcov spdlog boost freeglut glfw glew vtk cgal glm ffmpeg
ln -s "$(brew --prefix llvm)/bin/clang-format" "/usr/local/bin/clang-format"
ln -s "$(brew --prefix llvm)/bin/clang-tidy" "/usr/local/bin/clang-tidy"

- name: Install XQuartz
run: |
brew install --cask xquartz

- name: Start Xvfb
run: |
sudo Xvfb :99 -screen 0 1024x768x24 &
sudo /opt/X11/bin/Xvfb :99 -screen 0 800x600x24 &
echo "DISPLAY=:99" >> $GITHUB_ENV

export DISPLAY=:99
sleep 3 # Give Xvfb some time to start

- name: Build
run: |
cmake --preset=build
Expand All @@ -109,20 +235,54 @@ jobs:
- name: Run demo2d
run: |
for i in {1..7}; do
./build/bin/demo2d --test --instruction $i
# Start recording with ffmpeg
ffmpeg -y -f x11grab -video_size 800x600 -i :99 -t 5 -r 10 ./build/bin/demo2d_$i.gif &
FFmpeg_PID=$!
# Run the demo2d program in the background
./build/bin/demo2d --instruction $i &
Demo2d_PID=$!
# Wait for 5 seconds
sleep 5
# Stop the ffmpeg recording
if ps -p $FFmpeg_PID > /dev/null; then
kill $FFmpeg_PID
fi
# Terminate the demo2d program
if ps -p $Demo2d_PID > /dev/null; then
kill $Demo2d_PID
fi
done
env:
DISPLAY: :99

- name: Run MassSpring3D
run: ./build/bin/MassSpring3D --test --instruction 1
run: |
for i in {1..1}; do
ffmpeg -y -f x11grab -video_size 800x600 -i :99 -t 5 -r 10 ./build/bin/MassSpring3D_$i.gif &
FFmpeg_PID=$!
./build/bin/MassSpring3D --instruction $i --edge_num 101 &
MassSpring3D_PID=$!
sleep 5
if ps -p $FFmpeg_PID > /dev/null; then
kill $FFmpeg_PID
fi
if ps -p $MassSpring3D_PID > /dev/null; then
kill $MassSpring3D_PID
fi
done
env:
DISPLAY: :99

- name: Upload GIFs
uses: actions/upload-artifact@v4
with:
name: gifs_macos
path: build/bin/*.gif

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
files: build/coverage/coverage.info
verbose: true
# @bug 在 osx 下无法正常执行 lcov
# cmake --build build --target coverage
# cmake --build build --target coverage
12 changes: 4 additions & 8 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
"CMAKE_C_STANDARD": "17",
"CMAKE_C_STANDARD_REQUIRED": "ON",
"CMAKE_CXX_EXTENSIONS": "OFF",
"CMAKE_CXX_STANDARD": "20",
"CMAKE_CXX_STANDARD": "17",
"CMAKE_CXX_STANDARD_REQUIRED": "ON"
}
},
{
"name": "configurePresets_base",
"hidden": true,
"inherits": [
"std"
],
"inherits": ["std"],
"displayName": "configurePresets_base",
"description": "base configurePresets",
"binaryDir": "${sourceDir}/build",
Expand Down Expand Up @@ -54,11 +52,9 @@
{
"name": "build",
"hidden": false,
"inherits": [
"configurePresets_base"
],
"inherits": ["configurePresets_base"],
"displayName": "build",
"description": "build"
}
]
}
}
71 changes: 33 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ dtk Render by Opengl.

[ The Visualization ToolKit (VTK)](https://vtk.org/Wiki/VTK) is an open source, freely available software system for 3D computer graphics, image processing, and visualization used by thousands of researchers and developers around the world. VTK consists of a C++ class library, and several interpreted interface layers including Tcl/Tk, Java, and Python. Professional support and products for VTK are provided by [Kitware, Inc.](http://www.kitware.com/) VTK supports a wide variety of visualization algorithms including scalar, vector, tensor, texture, and volumetric methods; and advanced modeling techniques such as implicit modelling, polygon reduction, mesh smoothing, cutting, contouring, and Delaunay triangulation. In addition, dozens of imaging algorithms have been directly integrated to allow the user to mix 2D imaging / 3D graphics algorithms and data.

## [Cuda](https://developer.nvidia.com/cuda-zone) \(Optional\)

CUDA® is a parallel computing platform and programming model developed by NVIDIA for general computing on graphical processing units (GPUs). With CUDA, developers are able to dramatically speed up computing applications by harnessing the power of GPUs.

## How to build

dtk is build by CMake. so you can build easily. You could build this project either in windows or linux. [tutorial on cmake build](https://preshing.com/20170511/how-to-build-a-cmake-based-project/)
Expand Down Expand Up @@ -178,44 +182,35 @@ $ cmake --build "DESTINATION_SHARED_DIR" --config Release --target install

## Demo with dtk

### Rigid body Simulation Demo

A physical simulation demo for 2D rigid body in real time. It deals with the collision of the rigid body which has no deformation by SAT and AABB methods.

<div align="center">
<img src="./demo/img/dtkRigidBodySimulation.gif" width = "50%", height = "50%"/>
</div>

### Finite Element Method Simulation

A finite element method physical simulation for 2D hyperelasticity deformation meterial body in real time.

<div align="center">
<img src="./demo/img/dtkFemSimulation.gif" width = "50%", height = "50%"/>
</div>

### SPH Methods Simulation

A physical simulation demo for 2D fluid in real time. It deals with the fluid body by a series of SPH methods, which include WCSPH, PCISPH and DFSPH.

<div align = center>
<img src="./demo/img/WCSPH.gif" width = "30%", height = "30%" /><img src="./demo/img/PCISPH.gif" width = "30%", height = "30%"/><img src="./demo/img/DFSPH.gif" width = "30%", height = "30%"/>
</div>

The SPH method is WCSPH, PCISPH and DFSPH from left to right.

### Guidewire Simulation

A blood flow induced physical simulation of guidewire shape for virtual vascular intervention training system in real time. Virtual vascular intervention training system, which is a low cost, safe and effective solution, is able to provide an immersive virtual training environment for trainees.

<div align="center">
<img src="./demo/img/guideWire.jpg" width = "50%", height = "50%" />
</div>
### Finite Element Method Simulation in 3D

<div align="center">
<img src="./demo/img/3dFEM.gif" width = "50%", height = "50%" />
</div>
### Demo2d

<table align="center">
<tr>
<td><img src="./demo/img/demo2d_1.gif" style="width: 100%; height: auto;" /></td>
<td><img src="./demo/img/demo2d_2.gif" style="width: 100%; height: auto;" /></td>
<td><img src="./demo/img/demo2d_3.gif" style="width: 100%; height: auto;" /></td>
<td><img src="./demo/img/demo2d_4.gif" style="width: 100%; height: auto;" /></td>
</tr>
<tr>
<td><img src="./demo/img/demo2d_5.gif" style="width: 100%; height: auto;" /></td>
<td><img src="./demo/img/demo2d_6.gif" style="width: 100%; height: auto;" /></td>
<td><img src="./demo/img/demo2d_7.gif" style="width: 100%; height: auto;" /></td>
<td></td>
</tr>
</table>

### MassSpring3D

CUDA can be used to accelerate the simulation of mass spring system. The following is the comparison between the simulation without CUDA (left) and with CUDA (right, Nvidia GPU 4060Ti).

<table align="center">
<tr>
<td><img src="./demo/img/MassSpring3D_1.gif" style="width: 100%; height: auto;" /></td>
<td><img src="./demo/img/MassSpring3D_cuda_1.gif" style="width: 100%; height: auto;" /></td>
<td></td>
<td></td>
</tr>
</table>

## Documentation

Expand Down
10 changes: 10 additions & 0 deletions cmake/3rd.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,14 @@ find_package(glfw3 REQUIRED)
if (NOT glfw3_FOUND)
message(FATAL_ERROR "glfw3 not found.\n"
"Following https://www.glfw.org to install.")
endif ()

find_package(CUDAToolkit 11.7 QUIET)
if (CUDAToolkit_FOUND)
message(STATUS "CUDAToolkit found.")
set(CUDA_ENABLED TRUE)
else ()
message(WARNING "CUDAToolkit not found. CUDA features will be disabled.\n"
"Follow https://developer.nvidia.com/cuda-downloads to install.")
set(CUDA_ENABLED FALSE)
endif ()
Binary file added demo/img/MassSpring3D_1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/img/MassSpring3D_cuda_1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/img/demo2d_1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/img/demo2d_2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/img/demo2d_3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/img/demo2d_4.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/img/demo2d_5.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/img/demo2d_6.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/img/demo2d_7.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading