From 05834e357530c9cdbb1b9ca798681fe3165e595a Mon Sep 17 00:00:00 2001 From: "Zone.N" Date: Thu, 9 Nov 2023 20:24:27 +0800 Subject: [PATCH 1/5] test: fix demo3d Signed-off-by: Zone.N --- test/system_test/demo3d/CMakeLists.txt | 4 +- ...dtkFemSimulation.cpp => FemSimulation.cpp} | 38 +++++++++---------- .../{dtkFemSimulation.h => FemSimulation.h} | 14 +++---- .../demo3d/{dtkScene.cpp => Scene.cpp} | 16 ++++---- .../demo3d/{dtkScene.h => Scene.h} | 12 +++--- test/system_test/demo3d/main.cpp | 14 ++++--- 6 files changed, 50 insertions(+), 48 deletions(-) rename test/system_test/demo3d/{dtkFemSimulation.cpp => FemSimulation.cpp} (94%) rename test/system_test/demo3d/{dtkFemSimulation.h => FemSimulation.h} (87%) rename test/system_test/demo3d/{dtkScene.cpp => Scene.cpp} (81%) rename test/system_test/demo3d/{dtkScene.h => Scene.h} (85%) diff --git a/test/system_test/demo3d/CMakeLists.txt b/test/system_test/demo3d/CMakeLists.txt index 5b50148..b3f309c 100644 --- a/test/system_test/demo3d/CMakeLists.txt +++ b/test/system_test/demo3d/CMakeLists.txt @@ -8,8 +8,8 @@ project(demo3d) add_executable(demo3d main.cpp - dtkFemSimulation.cpp - dtkScene.cpp + FemSimulation.cpp + Scene.cpp ) target_include_directories(${PROJECT_NAME} PRIVATE diff --git a/test/system_test/demo3d/dtkFemSimulation.cpp b/test/system_test/demo3d/FemSimulation.cpp similarity index 94% rename from test/system_test/demo3d/dtkFemSimulation.cpp rename to test/system_test/demo3d/FemSimulation.cpp index b53a685..3c631e3 100644 --- a/test/system_test/demo3d/dtkFemSimulation.cpp +++ b/test/system_test/demo3d/FemSimulation.cpp @@ -37,7 +37,7 @@ using namespace Eigen; // #include // using namespace irrklang; -#include "dtkFemSimulation.h" +#include "FemSimulation.h" #include "GL/freeglut.h" @@ -71,13 +71,13 @@ inline int mesh(int i, int j, int k) { return k * (n_node_y * n_node_x) + j * n_node_x + i; } -dtkFemSimulation::dtkFemSimulation(unsigned int width, unsigned int height) - : dtkScene(width, height), points(n_node), pre_points(n_node), +FemSimulation::FemSimulation(unsigned int width, unsigned int height) + : Scene(width, height), points(n_node), pre_points(n_node), points_v(n_node), points_force(n_node), B(n_fem_element), PyramidTable(n_fem_element, std::vector(4, 0)), sphere(0.5, 0.5, 0.0, radius), total_energy(0), pre_total_energy(0) {} -Matrix3f dtkFemSimulation::compute_D(int i) { +Matrix3f FemSimulation::compute_D(int i) { int a = PyramidTable[i][0]; int b = PyramidTable[i][1]; int c = PyramidTable[i][2]; @@ -98,14 +98,14 @@ Matrix3f dtkFemSimulation::compute_D(int i) { return ans; } -void dtkFemSimulation::compute_B() { +void FemSimulation::compute_B() { for (int i = 0; i < n_fem_element; ++i) { this->B[i] = compute_D(i).inverse(); // cout << setw(6) << B[i] << endl; } } -Matrix3f dtkFemSimulation::compute_P(int i) { +Matrix3f FemSimulation::compute_P(int i) { Matrix3f D = compute_D(i); Matrix3f F = D * B[i]; @@ -117,7 +117,7 @@ Matrix3f dtkFemSimulation::compute_P(int i) { // Matrix2f ans = D * this->B[i]; } -void dtkFemSimulation::compute_total_energy() { +void FemSimulation::compute_total_energy() { this->total_energy = 0.0f; for (int i = 0; i < n_fem_element; ++i) { @@ -137,9 +137,9 @@ void dtkFemSimulation::compute_total_energy() { } } -dtkFemSimulation::~dtkFemSimulation() {} +FemSimulation::~FemSimulation() {} -void dtkFemSimulation::InitShell() { +void FemSimulation::InitShell() { int scale = 1; // 面x=x_min,x=x_max for (int i = 0; i < n_node_y - scale; i += scale) { @@ -181,8 +181,8 @@ void dtkFemSimulation::InitShell() { } } -void dtkFemSimulation::Init() { - dtkScene::Init(); +void FemSimulation::Init() { + Scene::Init(); // TODO: load shaders // TODO: configure shaders @@ -259,7 +259,7 @@ void dtkFemSimulation::Init() { // TODO: audio } -void dtkFemSimulation::compute_force() { +void FemSimulation::compute_force() { for (int i = 0; i < n_node; ++i) { this->points_force[i] = Vector3f(0.0f, -10.0f * node_mass, 0.0f); } @@ -285,7 +285,7 @@ void dtkFemSimulation::compute_force() { } } -void dtkFemSimulation::Update(float dt) { +void FemSimulation::Update(float dt) { // TODO: update objects // TODO: check for object collisions if (this->State == SCENE_ACTIVE) { @@ -332,8 +332,8 @@ void dtkFemSimulation::Update(float dt) { } } -void dtkFemSimulation::ProcessInput(float dt) { - dtkScene::ProcessInput(dt); +void FemSimulation::ProcessInput(float dt) { + Scene::ProcessInput(dt); // TODO: process input(keys) } @@ -343,7 +343,7 @@ inline void GLSpherePoint(Vector3f center, int n, int i, int j) { center[2] + radius * cos(2 * M_PI / n * j)); } -void dtkFemSimulation::Render() { +void FemSimulation::Render() { Vector3f center = Vector3f(sphere.x, sphere.y, sphere.z); glEnable(GL_DEPTH_TEST); @@ -427,7 +427,7 @@ void dtkFemSimulation::Render() { // collision detection -void dtkFemSimulation::DoCollisions() { +void FemSimulation::DoCollisions() { if (this->State == SCENE_ACTIVE) { Vector3f center = Vector3f(sphere.x, sphere.y, sphere.z); // Vector2f(this->sphere.center()[0], this->sphere.center()[1]); @@ -479,9 +479,9 @@ void dtkFemSimulation::DoCollisions() { } } -void dtkFemSimulation::moveBall(int x, int y) { +void FemSimulation::moveBall(int x, int y) { // this->spherecenter = Vector2f((x) * 1.0 / 800 , (600 - y) * 1.0 / 600 ); } -float dtkFemSimulation::getEnergy() { return this->total_energy; } \ No newline at end of file +float FemSimulation::getEnergy() { return this->total_energy; } \ No newline at end of file diff --git a/test/system_test/demo3d/dtkFemSimulation.h b/test/system_test/demo3d/FemSimulation.h similarity index 87% rename from test/system_test/demo3d/dtkFemSimulation.h rename to test/system_test/demo3d/FemSimulation.h index c84cfbe..8e03ce5 100644 --- a/test/system_test/demo3d/dtkFemSimulation.h +++ b/test/system_test/demo3d/FemSimulation.h @@ -21,10 +21,10 @@ * @Last Modified time: 2021-09-03 17:22:45 */ -#ifndef SIMPLEPHYSICSENGINE_DTKFEMSIMULATION_H -#define SIMPLEPHYSICSENGINE_DTKFEMSIMULATION_H +#ifndef SIMPLEPHYSICSENGINE_FEMSIMULATION_H +#define SIMPLEPHYSICSENGINE_FEMSIMULATION_H -#include "dtkScene.h" +#include "Scene.h" #include #include @@ -48,7 +48,7 @@ class Sphere3d { : x(px), y(py), z(pz), radius(praius) {} }; -class dtkFemSimulation : public dtkScene { +class FemSimulation : public Scene { private: /* data */ @@ -82,8 +82,8 @@ class dtkFemSimulation : public dtkScene { vector> mesh_index_list; Sphere3d sphere; //(dtk::dtkGraphicsKernel::Point2(0.5,0.2), 0.1); - dtkFemSimulation(unsigned int width, unsigned int height); - ~dtkFemSimulation(); + FemSimulation(unsigned int width, unsigned int height); + ~FemSimulation(); void Init(); void InitShell(); @@ -97,4 +97,4 @@ class dtkFemSimulation : public dtkScene { void DoCollisions(); }; -#endif /* SIMPLEPHYSICSENGINE_DTKFEMSIMULATION_H */ +#endif /* SIMPLEPHYSICSENGINE_FEMSIMULATION_H */ diff --git a/test/system_test/demo3d/dtkScene.cpp b/test/system_test/demo3d/Scene.cpp similarity index 81% rename from test/system_test/demo3d/dtkScene.cpp rename to test/system_test/demo3d/Scene.cpp index 6358a8a..ef7f575 100644 --- a/test/system_test/demo3d/dtkScene.cpp +++ b/test/system_test/demo3d/Scene.cpp @@ -14,15 +14,15 @@ * */ -#include "dtkScene.h" +#include "Scene.h" -dtkScene::dtkScene(unsigned int width, unsigned int height) +Scene::Scene(unsigned int width, unsigned int height) : State(SCENE_ACTIVE), Keys(), KeysProcessed(), Width(width), Height(height) {} -dtkScene::~dtkScene() {} +Scene::~Scene() {} -void dtkScene::Init() { +void Scene::Init() { // TODO: load shaders // TODO: configure shaders @@ -36,12 +36,12 @@ void dtkScene::Init() { // TODO: audio } -void dtkScene::Update(float dt) { +void Scene::Update(float dt) { // TODO: update objects // TODO: check for object collisions } -void dtkScene::ProcessInput(float dt) { +void Scene::ProcessInput(float dt) { // TODO: process input(keys) /* @@ -64,8 +64,8 @@ void dtkScene::ProcessInput(float dt) { */ } -void dtkScene::Render() {} +void Scene::Render() {} // collision detection -void dtkScene::DoCollisions() {} \ No newline at end of file +void Scene::DoCollisions() {} \ No newline at end of file diff --git a/test/system_test/demo3d/dtkScene.h b/test/system_test/demo3d/Scene.h similarity index 85% rename from test/system_test/demo3d/dtkScene.h rename to test/system_test/demo3d/Scene.h index ac25d42..295d74a 100644 --- a/test/system_test/demo3d/dtkScene.h +++ b/test/system_test/demo3d/Scene.h @@ -21,8 +21,8 @@ * @Last Modified time: 2021-09-03 16:35:44 */ -#ifndef SIMPLEPHYSICSENGINE_DTKSCENE_H -#define SIMPLEPHYSICSENGINE_DTKSCENE_H +#ifndef SIMPLEPHYSICSENGINE_SCENE_H +#define SIMPLEPHYSICSENGINE_SCENE_H // #include // #include @@ -33,7 +33,7 @@ enum dtkSceneState { SCENE_ACTIVE, SCENE_PAUSE }; // Scene holds all Scene-related state and functionality. // Combines all Scene-related data into a single class for // easy access to each of the components and manageability. -class dtkScene { +class Scene { public: // Scene state dtkSceneState State; @@ -42,9 +42,9 @@ class dtkScene { unsigned int Width, Height; // constructor/destructor - dtkScene(unsigned int width, unsigned int height); + Scene(unsigned int width, unsigned int height); - ~dtkScene(); + ~Scene(); // initialize Scene state (load all shaders/textures) void Init(); // Scene loop @@ -54,4 +54,4 @@ class dtkScene { virtual void DoCollisions() = 0; }; -#endif /* SIMPLEPHYSICSENGINE_DTKSCENE_H */ +#endif /* SIMPLEPHYSICSENGINE_SCENE_H */ diff --git a/test/system_test/demo3d/main.cpp b/test/system_test/demo3d/main.cpp index 39662f3..f3fb71b 100644 --- a/test/system_test/demo3d/main.cpp +++ b/test/system_test/demo3d/main.cpp @@ -25,13 +25,15 @@ * */ -#include "GL/freeglut.h" -#include "dtkFemSimulation.h" #include #include #include #include +#include "GL/freeglut.h" + +#include "FemSimulation.h" + static auto last_clock = std::chrono::high_resolution_clock::now(); // The Width of the screen @@ -39,7 +41,7 @@ const unsigned int SCREEN_WIDTH = 800; // The height of the screen const unsigned int SCREEN_HEIGHT = 600; -dtkFemSimulation Breakout(SCREEN_WIDTH, SCREEN_HEIGHT); +FemSimulation Breakout(SCREEN_WIDTH, SCREEN_HEIGHT); #ifdef USE_VTK @@ -224,11 +226,11 @@ void gl_vis(int argc, char *argv[]) { glutInitWindowSize(800, 600); glutInitWindowPosition(50, 50); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); - glutCreateWindow("Physics Engine -- dtk"); - glutKeyboardFunc(&keyboard); - Breakout.Init(); + glutCreateWindow("SimplePhysicsEngine-ST-demo3d"); glutDisplayFunc(&display); glutReshapeFunc(&reshape); + glutKeyboardFunc(&keyboard); + Breakout.Init(); glutIdleFunc(&idle); // glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION); glutMainLoop(); From 6e8e81b6aaa3f9adcdfc054e07fb33c89684d03d Mon Sep 17 00:00:00 2001 From: "Zone.N" Date: Fri, 10 Nov 2023 10:11:32 +0800 Subject: [PATCH 2/5] refactor: update code; comment: update file header Signed-off-by: Zone.N --- test/system_test/demo3d/CMakeLists.txt | 1 - test/system_test/demo3d/FemSimulation.cpp | 83 +++++++++-------------- test/system_test/demo3d/FemSimulation.h | 36 ++++------ test/system_test/demo3d/Scene.cpp | 71 ------------------- test/system_test/demo3d/Scene.h | 57 ---------------- test/system_test/demo3d/main.cpp | 28 +++----- 6 files changed, 55 insertions(+), 221 deletions(-) delete mode 100644 test/system_test/demo3d/Scene.cpp delete mode 100644 test/system_test/demo3d/Scene.h diff --git a/test/system_test/demo3d/CMakeLists.txt b/test/system_test/demo3d/CMakeLists.txt index b3f309c..b3ac44d 100644 --- a/test/system_test/demo3d/CMakeLists.txt +++ b/test/system_test/demo3d/CMakeLists.txt @@ -9,7 +9,6 @@ project(demo3d) add_executable(demo3d main.cpp FemSimulation.cpp - Scene.cpp ) target_include_directories(${PROJECT_NAME} PRIVATE diff --git a/test/system_test/demo3d/FemSimulation.cpp b/test/system_test/demo3d/FemSimulation.cpp index 3c631e3..75e2bf9 100644 --- a/test/system_test/demo3d/FemSimulation.cpp +++ b/test/system_test/demo3d/FemSimulation.cpp @@ -2,48 +2,29 @@ /** * @file dtkFemSimulation.cpp * @brief dtkFemSimulation 实现 - * @author Zone.N (Zone.Niuzh@hotmail.com) + * @author tom (https://github.com/TOMsworkspace) * @version 1.0 - * @date 2023-10-31 + * @date 2021-09-03 * @copyright MIT LICENSE * https://github.com/Simple-XX/SimplePhysicsEngine * @par change log: * *
DateAuthorDescription + *
2021-09-03tom创建文件 *
2023-10-31Zone.N迁移到 doxygen *
*/ -/* - * @Author: tom: https://github.com/TOMsworkspace - * @Date: 2021-09-03 15:32:52 - * @Last Modified by: tom: https://github.com/TOMsworkspace - * @Last Modified time: 2021-09-03 19:17:13 - */ - #include +#include #include -#include #include #include -using namespace std; -using namespace Eigen; - -// #include -// #include -// #include - -// #include -// using namespace irrklang; +#include #include "FemSimulation.h" -#include "GL/freeglut.h" - -// #include "resource_manager.h" -// #include "sprite_renderer.h" - int dim = 3; int n_node_x = 30; int n_node_y = 4; @@ -72,18 +53,18 @@ inline int mesh(int i, int j, int k) { } FemSimulation::FemSimulation(unsigned int width, unsigned int height) - : Scene(width, height), points(n_node), pre_points(n_node), + : dtkScene(width, height), points(n_node), pre_points(n_node), points_v(n_node), points_force(n_node), B(n_fem_element), PyramidTable(n_fem_element, std::vector(4, 0)), sphere(0.5, 0.5, 0.0, radius), total_energy(0), pre_total_energy(0) {} -Matrix3f FemSimulation::compute_D(int i) { +Eigen::Matrix3f FemSimulation::compute_D(int i) { int a = PyramidTable[i][0]; int b = PyramidTable[i][1]; int c = PyramidTable[i][2]; int d = PyramidTable[i][3]; - Matrix3f ans; + Eigen::Matrix3f ans; ans(0, 0) = points[a][0] - points[d][0]; ans(0, 1) = points[b][0] - points[d][0]; ans(0, 2) = points[c][0] - points[d][0]; @@ -105,13 +86,13 @@ void FemSimulation::compute_B() { } } -Matrix3f FemSimulation::compute_P(int i) { - Matrix3f D = compute_D(i); - Matrix3f F = D * B[i]; +Eigen::Matrix3f FemSimulation::compute_P(int i) { + Eigen::Matrix3f D = compute_D(i); + Eigen::Matrix3f F = D * B[i]; - Matrix3f F_T = F.transpose().inverse(); + Eigen::Matrix3f F_T = F.transpose().inverse(); - float J = max(0.5f, F.determinant()); /**< 形变率 */ + float J = std::fmax(0.5f, F.determinant()); /**< 形变率 */ return Lame_parameter_1 * (F - F_T) + Lame_parameter_2 * log(J) * F_T; // Matrix2f ans = D * this->B[i]; @@ -121,12 +102,12 @@ void FemSimulation::compute_total_energy() { this->total_energy = 0.0f; for (int i = 0; i < n_fem_element; ++i) { - Matrix3f D = compute_D(i); - Matrix3f F = D * B[i]; + Eigen::Matrix3f D = compute_D(i); + Eigen::Matrix3f F = D * B[i]; // NeoHooken float I1 = (F * F.transpose()).trace(); - float J = max(0.2f, (float)F.determinant()); /**< 形变率 */ + float J = std::fmax(0.2f, (float)F.determinant()); /**< 形变率 */ // cout << J << endl; @@ -182,7 +163,7 @@ void FemSimulation::InitShell() { } void FemSimulation::Init() { - Scene::Init(); + dtkScene::Init(); // TODO: load shaders // TODO: configure shaders @@ -255,23 +236,23 @@ void FemSimulation::Init() { compute_B(); InitShell(); - this->State = SCENE_ACTIVE; + this->State = dtk::SCENE_ACTIVE; // TODO: audio } void FemSimulation::compute_force() { for (int i = 0; i < n_node; ++i) { - this->points_force[i] = Vector3f(0.0f, -10.0f * node_mass, 0.0f); + this->points_force[i] = Eigen::Vector3f(0.0f, -10.0f * node_mass, 0.0f); } for (int i = 0; i < n_fem_element; ++i) { - Matrix3f P = compute_P(i); - Matrix3f H = -element_v * (P * (this->B[i].transpose())); + Eigen::Matrix3f P = compute_P(i); + Eigen::Matrix3f H = -element_v * (P * (this->B[i].transpose())); - Vector3f h1 = Vector3f(H(0, 0), H(1, 0), H(2, 0)); - Vector3f h2 = Vector3f(H(0, 1), H(1, 1), H(2, 1)); - Vector3f h3 = Vector3f(H(0, 2), H(1, 2), H(2, 2)); + Eigen::Vector3f h1 = Eigen::Vector3f(H(0, 0), H(1, 0), H(2, 0)); + Eigen::Vector3f h2 = Eigen::Vector3f(H(0, 1), H(1, 1), H(2, 1)); + Eigen::Vector3f h3 = Eigen::Vector3f(H(0, 2), H(1, 2), H(2, 2)); int a = this->PyramidTable[i][0]; int b = this->PyramidTable[i][1]; @@ -288,7 +269,7 @@ void FemSimulation::compute_force() { void FemSimulation::Update(float dt) { // TODO: update objects // TODO: check for object collisions - if (this->State == SCENE_ACTIVE) { + if (this->State == dtk::SCENE_ACTIVE) { this->total_energy = 0; // 迭代多轮, 防止穿透 for (int i = 0; i < iterate_time; ++i) { @@ -333,18 +314,18 @@ void FemSimulation::Update(float dt) { } void FemSimulation::ProcessInput(float dt) { - Scene::ProcessInput(dt); + dtkScene::ProcessInput(dt); // TODO: process input(keys) } -inline void GLSpherePoint(Vector3f center, int n, int i, int j) { +inline void GLSpherePoint(Eigen::Vector3f center, int n, int i, int j) { glVertex3f(center[0] + radius * cos(2 * M_PI / n * i) * sin(2 * M_PI / n * j), center[1] + radius * sin(2 * M_PI / n * i) * sin(2 * M_PI / n * j), center[2] + radius * cos(2 * M_PI / n * j)); } void FemSimulation::Render() { - Vector3f center = Vector3f(sphere.x, sphere.y, sphere.z); + Eigen::Vector3f center = Eigen::Vector3f(sphere.x, sphere.y, sphere.z); glEnable(GL_DEPTH_TEST); int n = 20; @@ -428,17 +409,17 @@ void FemSimulation::Render() { // collision detection void FemSimulation::DoCollisions() { - if (this->State == SCENE_ACTIVE) { - Vector3f center = Vector3f(sphere.x, sphere.y, sphere.z); + if (this->State == dtk::SCENE_ACTIVE) { + Eigen::Vector3f center = Eigen::Vector3f(sphere.x, sphere.y, sphere.z); // Vector2f(this->sphere.center()[0], this->sphere.center()[1]); float radius = this->sphere.radius; for (int i = 0; i < n_node; ++i) { // # Collide with sphere - Vector3f dis = this->points[i] - center; + Eigen::Vector3f dis = this->points[i] - center; if ((float)(dis.dot(dis)) < radius * radius) { - Vector3f normal = dis.normalized(); + Eigen::Vector3f normal = dis.normalized(); this->points[i] = center + radius * normal; this->points_v[i] -= (this->points_v[i].dot(normal)) * normal; diff --git a/test/system_test/demo3d/FemSimulation.h b/test/system_test/demo3d/FemSimulation.h index 8e03ce5..39f4edc 100644 --- a/test/system_test/demo3d/FemSimulation.h +++ b/test/system_test/demo3d/FemSimulation.h @@ -2,35 +2,27 @@ /** * @file dtkFemSimulation.h * @brief dtkFemSimulation 头文件 - * @author Zone.N (Zone.Niuzh@hotmail.com) + * @author tom (https://github.com/TOMsworkspace) * @version 1.0 - * @date 2023-10-31 + * @date 2021-09-03 * @copyright MIT LICENSE * https://github.com/Simple-XX/SimplePhysicsEngine * @par change log: * *
DateAuthorDescription + *
2021-09-03tom创建文件 *
2023-10-31Zone.N迁移到 doxygen *
*/ -/* - * @Author: tom: https://github.com/TOMsworkspace - * @Date: 2021-09-03 16:12:05 - * @Last Modified by: tom: https://github.com/TOMsworkspace - * @Last Modified time: 2021-09-03 17:22:45 - */ - #ifndef SIMPLEPHYSICSENGINE_FEMSIMULATION_H #define SIMPLEPHYSICSENGINE_FEMSIMULATION_H -#include "Scene.h" #include #include #include -using namespace std; -using namespace Eigen; +#include class Circle2d { public: @@ -48,28 +40,28 @@ class Sphere3d { : x(px), y(py), z(pz), radius(praius) {} }; -class FemSimulation : public Scene { +class FemSimulation : public dtk::dtkScene { private: /* data */ - vector pre_points; //(n_node); /**< 点之前的位置 */ + std::vector pre_points; //(n_node); /**< 点之前的位置 */ - vector points_force; + std::vector points_force; - vector points_v; //(n_node); /**< 点的速度 */ + std::vector points_v; //(n_node); /**< 点的速度 */ - vector B; //(n_fem_element); /**< 微元本身长度的逆 */ + std::vector B; //(n_fem_element); /**< 微元本身长度的逆 */ float total_energy; /** 总势能 */ float pre_total_energy; /** 之前的总势能 */ std::vector> PyramidTable; //(n_fem_element, std::vector(3,0)); /**< - //四面体微元下标 */ + // 四面体微元下标 */ - Matrix3f compute_D(int i); + Eigen::Matrix3f compute_D(int i); - Matrix3f compute_P(int i); + Eigen::Matrix3f compute_P(int i); void compute_B(); @@ -78,8 +70,8 @@ class FemSimulation : public Scene { void compute_total_energy(); public: - vector points; //(n_node); /**< 点的位置 */ - vector> mesh_index_list; + std::vector points; //(n_node); /**< 点的位置 */ + std::vector> mesh_index_list; Sphere3d sphere; //(dtk::dtkGraphicsKernel::Point2(0.5,0.2), 0.1); FemSimulation(unsigned int width, unsigned int height); diff --git a/test/system_test/demo3d/Scene.cpp b/test/system_test/demo3d/Scene.cpp deleted file mode 100644 index ef7f575..0000000 --- a/test/system_test/demo3d/Scene.cpp +++ /dev/null @@ -1,71 +0,0 @@ - -/** - * @file dtkScene.cpp - * @brief dtkScene 实现 - * @author Zone.N (Zone.Niuzh@hotmail.com) - * @version 1.0 - * @date 2023-10-31 - * @copyright MIT LICENSE - * https://github.com/Simple-XX/SimplePhysicsEngine - * @par change log: - * - *
DateAuthorDescription - *
2023-10-31Zone.N迁移到 doxygen - *
- */ - -#include "Scene.h" - -Scene::Scene(unsigned int width, unsigned int height) - : State(SCENE_ACTIVE), Keys(), KeysProcessed(), Width(width), - Height(height) {} - -Scene::~Scene() {} - -void Scene::Init() { - // TODO: load shaders - - // TODO: configure shaders - - // TODO: load textures - - // TODO: set render-specific controls - - // TODO: configure Scene objects - - // TODO: audio -} - -void Scene::Update(float dt) { - // TODO: update objects - // TODO: check for object collisions -} - -void Scene::ProcessInput(float dt) { - // TODO: process input(keys) - /* - - if (this->State == SCENE_ACTIVE) - { - if (this->Keys[GLFW_KEY_SPACE] && !this->KeysProcessed[GLFW_KEY_SPACE]) - { - this->State = SCENE_PAUSE; - this->KeysProcessed[GLFW_KEY_SPACE] = true; - } - } - if (this->State == SCENE_PAUSE) - { - if (this->Keys[GLFW_KEY_ENTER]) - { - this->State = SCENE_ACTIVE; - this->KeysProcessed[GLFW_KEY_ENTER] = true; - } - } - */ -} - -void Scene::Render() {} - -// collision detection - -void Scene::DoCollisions() {} \ No newline at end of file diff --git a/test/system_test/demo3d/Scene.h b/test/system_test/demo3d/Scene.h deleted file mode 100644 index 295d74a..0000000 --- a/test/system_test/demo3d/Scene.h +++ /dev/null @@ -1,57 +0,0 @@ - -/** - * @file dtkScene.h - * @brief dtkScene 头文件 - * @author Zone.N (Zone.Niuzh@hotmail.com) - * @version 1.0 - * @date 2023-10-31 - * @copyright MIT LICENSE - * https://github.com/Simple-XX/SimplePhysicsEngine - * @par change log: - * - *
DateAuthorDescription - *
2023-10-31Zone.N迁移到 doxygen - *
- */ - -/* - * @Author: tom: https://github.com/TOMsworkspace - * @Date: 2021-09-03 15:18:24 - * @Last Modified by: tom: https://github.com/TOMsworkspace - * @Last Modified time: 2021-09-03 16:35:44 - */ - -#ifndef SIMPLEPHYSICSENGINE_SCENE_H -#define SIMPLEPHYSICSENGINE_SCENE_H - -// #include -// #include - -// Represents the current state of the scene -enum dtkSceneState { SCENE_ACTIVE, SCENE_PAUSE }; - -// Scene holds all Scene-related state and functionality. -// Combines all Scene-related data into a single class for -// easy access to each of the components and manageability. -class Scene { -public: - // Scene state - dtkSceneState State; - bool Keys[1024]; - bool KeysProcessed[1024]; - unsigned int Width, Height; - - // constructor/destructor - Scene(unsigned int width, unsigned int height); - - ~Scene(); - // initialize Scene state (load all shaders/textures) - void Init(); - // Scene loop - virtual void ProcessInput(float dt) = 0; - virtual void Update(float dt) = 0; - virtual void Render() = 0; - virtual void DoCollisions() = 0; -}; - -#endif /* SIMPLEPHYSICSENGINE_SCENE_H */ diff --git a/test/system_test/demo3d/main.cpp b/test/system_test/demo3d/main.cpp index f3fb71b..fbcff28 100644 --- a/test/system_test/demo3d/main.cpp +++ b/test/system_test/demo3d/main.cpp @@ -2,35 +2,25 @@ /** * @file main.cpp * @brief main 实现 - * @author Zone.N (Zone.Niuzh@hotmail.com) + * @author TOMsworkspace (2683322180@qq.com) * @version 1.0 - * @date 2023-10-31 + * @date 2021-09-13 * @copyright MIT LICENSE * https://github.com/Simple-XX/SimplePhysicsEngine * @par change log: * *
DateAuthorDescription + *
2021-09-13TOMsworkspace创建文件 *
2023-10-31Zone.N迁移到 doxygen *
*/ -/** - * @file main.cpp - * @author TOMsworkspace (2683322180@qq.com) - * @brief - * @version 1.0 - * @date 2021-09-13 - * - * @copyright Copyright (c) 2021 - * - */ - #include #include #include #include -#include "GL/freeglut.h" +#include #include "FemSimulation.h" @@ -186,7 +176,7 @@ void display() { // draw_text(5, 40, "Push [1-5] to switch scene"); // draw_text(w - 150, h - 20, "refer: apollonia"); - if (Breakout.State == SCENE_PAUSE) + if (Breakout.State == dtk::SCENE_PAUSE) draw_text(5, h - 20, "dt: %.2f ms (%.2F FPS) PAUSED", dt * 1000, 1.0 / dt); else draw_text(5, h - 20, "dt: %.2f ms (%.2F FPS)", dt * 1000, 1.0 / dt); @@ -205,12 +195,12 @@ void idle() { display(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case ' ': - if (Breakout.State == SCENE_PAUSE) { - Breakout.State = SCENE_ACTIVE; + if (Breakout.State == dtk::SCENE_PAUSE) { + Breakout.State = dtk::SCENE_ACTIVE; break; } - if (Breakout.State == SCENE_ACTIVE) { - Breakout.State = SCENE_PAUSE; + if (Breakout.State == dtk::SCENE_ACTIVE) { + Breakout.State = dtk::SCENE_PAUSE; break; } case 27: From 0ecf5884feb10fe2b7c68846d363e11e64e60d72 Mon Sep 17 00:00:00 2001 From: "Zone.N" Date: Fri, 10 Nov 2023 10:31:17 +0800 Subject: [PATCH 3/5] refactor: remove unused codes Signed-off-by: Zone.N --- src/CMakeLists.txt | 1 - src/dtkAssert.cpp | 17 - src/dtkPhysCore.cpp.bak | 1582 --------------------------- src/include/dtkGraphicsKernel.h.bak | 286 ----- src/include/dtkPhysMassSpring.h.bak | 219 ---- 5 files changed, 2105 deletions(-) delete mode 100644 src/dtkAssert.cpp delete mode 100644 src/dtkPhysCore.cpp.bak delete mode 100644 src/include/dtkGraphicsKernel.h.bak delete mode 100644 src/include/dtkPhysMassSpring.h.bak diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a62f859..f97b571 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,7 +13,6 @@ set(lib_src collision_detect/dtkCollisionDetectPrimitive.cpp collision_detect/dtkCollisionDetectStage.cpp dtk.cpp - dtkAssert.cpp dtkErrorManager.cpp dtkGraphicsKernel.cpp dtkIntersectTest.cpp diff --git a/src/dtkAssert.cpp b/src/dtkAssert.cpp deleted file mode 100644 index 71eddd4..0000000 --- a/src/dtkAssert.cpp +++ /dev/null @@ -1,17 +0,0 @@ - -/** - * @file dtkAssert.cpp - * @brief dtkAssert 实现 - * @author Zone.N (Zone.Niuzh@hotmail.com) - * @version 1.0 - * @date 2023-10-31 - * @copyright MIT LICENSE - * https://github.com/Simple-XX/SimplePhysicsEngine - * @par change log: - * - *
DateAuthorDescription - *
2023-10-31Zone.N迁移到 doxygen - *
- */ - -#include "dtkAssert.h" diff --git a/src/dtkPhysCore.cpp.bak b/src/dtkPhysCore.cpp.bak deleted file mode 100644 index 7cfe978..0000000 --- a/src/dtkPhysCore.cpp.bak +++ /dev/null @@ -1,1582 +0,0 @@ -#include "dtkPhysCore.h" -#include "dtkStaticTetraMeshReader.h" -#include "dtkStaticTriangleMeshReader.h" - -#define K 3 - -#include -#include -#include -using namespace std; -using namespace boost; - -namespace dtk { -void dtkphyscore_mt_update(dtkID id, dtkPhysCore *core) { - vector> emptyIntervals; - - do { - // const std::vector< dtkID >* massSpringRange = &( core->mAllocator[id][0] - // ); const std::vector< dtkID >* collisionPairRange = &( - // core->mAllocator[id][1] ); - - // const std::vector< dtkID >& massSpringRange = core->mAllocator[id][0]; - // const std::vector< dtkID >& collisionPairRange = core->mAllocator[id][1]; - - // Phase 1 - core->mEnterBarrier->wait(); - - if (!core->mLive) - break; - - std::vector massSpringRange; - for (dtkID i = 0; - i < core->mAllocator[id][core->mAllocatePosMassSpring].size(); i++) - massSpringRange.push_back( - core->mAllocator[id][core->mAllocatePosMassSpring][i]); - - std::vector primitiveRange; - for (dtkID i = 0; - i < core->mAllocator[id][core->mAllocatePosPrimitive].size(); i++) - primitiveRange.push_back( - core->mAllocator[id][core->mAllocatePosPrimitive][i]); - - std::vector collisionPairRange; - for (dtkID i = 0; - i < core->mAllocator[id][core->mAllocatePosCollisionDetect].size(); - i++) - collisionPairRange.push_back( - core->mAllocator[id][core->mAllocatePosCollisionDetect][i]); - - std::vector internalCollisionPairRange; - for (dtkID i = 0; - i < - core->mAllocator[id][core->mAllocatePosInternalCollisionDetect].size(); - i++) - internalCollisionPairRange.push_back( - core->mAllocator[id][core->mAllocatePosInternalCollisionDetect][i]); - - for (dtkID i = 0; i < massSpringRange.size(); i++) { - if (core->mTimeslice == 0 || - core->mMassSprings[massSpringRange[i]]->IsUnderControl()) - continue; - core->mMassSprings[massSpringRange[i]]->PreUpdate(core->mTimeslice, - Collision, 0); - core->mMassSprings[massSpringRange[i]]->UpdateStrings(core->mTimeslice, - Collision, 0, true); - } - - // Phase 2 - core->mEnterBarrier->wait(); - - for (dtkID i = 0; i < massSpringRange.size(); i++) { - if (core->mTimeslice == 0 || - core->mMassSprings[massSpringRange[i]]->IsUnderControl()) - continue; - core->mMassSprings[massSpringRange[i]]->TransportForce(core->mTimeslice); - core->mMassSprings[massSpringRange[i]]->UpdateMassPoints(core->mTimeslice, - Collision, 0); - // core->mMassSprings[ massSpringRange[i] ]->PostUpdate(Collision, 0); - } - - // Phase 2.1 - core->mEnterBarrier->wait(); - - for (dtkID i = 0; i < primitiveRange.size(); i++) { - dtkID type = primitiveRange[i] / core->mPairOffset; - dtkID id = primitiveRange[i] % core->mPairOffset; - switch (type) { - case 0: - core->mCollisionDetectHierarchies[id]->Update(); - break; - case 1: - core->mThreadCollisionDetectHierarchies[id]->Update(); - break; - case 2: - core->mInteriorCollisionDetectHierarchies[id]->Update(); - break; - case 3: - core->mThreadHeadCollisionDetectHierarchies[id]->Update(); - break; - default: - break; - } - } - - // Phase 2.2 - core->mEnterBarrier->wait(); - - for (dtkID i = 0; i < collisionPairRange.size(); i++) { - vector intersectResults; - core->mStage->DoIntersect( - core->mCollisionDetectResponseSets[collisionPairRange[i]] - .hierarchy_pair, - intersectResults, - core->mCollisionDetectResponseSets[collisionPairRange[i]].self, - false); - if (core->mCollisionDetectResponseSets[collisionPairRange[i]] - .responseType == dtkPhysCore::THREAD_SURFACE) { - core->mCollisionDetectResponse->Update( - core->mTimeslice, intersectResults, emptyIntervals, - core->mThreadCollisionDetectResponse->GetAvoidIntervals( - collisionPairRange[i] % core->mPairOffset), - core->mCollisionDetectResponseSets[collisionPairRange[i]].strength); - } else if (core->mCollisionDetectResponseSets[collisionPairRange[i]] - .responseType == dtkPhysCore::KNOTPLANNING) { - core->mCollisionDetectResponse->Update( - core->mTimeslice, intersectResults, - core->mKnotPlanners[collisionPairRange[i] % core->mPairOffset] - ->GetAvoidIntervals(), - core->mKnotPlanners[collisionPairRange[i] % core->mPairOffset] - ->GetAvoidIntervals(), - core->mCollisionDetectResponseSets[collisionPairRange[i]].strength); - } else if (core->mCollisionDetectResponseSets[collisionPairRange[i]] - .responseType == dtkPhysCore::INTERIOR_THREADHEAD) { - assert(false); - } else { - core->mCollisionDetectResponse->Update( - core->mTimeslice, intersectResults, emptyIntervals, emptyIntervals, - core->mCollisionDetectResponseSets[collisionPairRange[i]].strength); - } - - if (core->mCollisionDetectResponseSets[collisionPairRange[i]] - .responseType == dtkPhysCore::KNOTPLANNING) { - core->mKnotPlanners[collisionPairRange[i] / core->mPairOffset] - ->KnotRecognition(intersectResults); - } - - if (intersectResults.size() != 0 && - core->mCollisionDetectResponseSets[collisionPairRange[i]] - .custom_handle != 0) - core->mCollisionDetectResponseSets[collisionPairRange[i]].custom_handle( - intersectResults, - core->mCollisionDetectResponseSets[collisionPairRange[i]].pContext); - } - - // Phase 2.3 - core->mEnterBarrier->wait(); - - for (dtkID i = 0; i < internalCollisionPairRange.size(); i++) { - vector intersectResults; - core->mStage->DoIntersect(core->mInternalCollisionDetectResponseSets - [internalCollisionPairRange[i]] - .hierarchy_pair, - intersectResults, - core->mInternalCollisionDetectResponseSets - [internalCollisionPairRange[i]] - .self, - false); - core->mThreadCollisionDetectResponse->Update(core->mTimeslice, - intersectResults); - - const std::vector> &internalIntervals = - core->mThreadCollisionDetectResponse->GetInternalIntervals( - internalCollisionPairRange[i] % core->mPairOffset); - dtkCollisionDetectHierarchyKDOPS::Ptr threadHierarchy = - core->mThreadCollisionDetectHierarchies - [internalCollisionPairRange[i] % core->mPairOffset]; - for (dtkID n = 0; n < threadHierarchy->GetNumberOfPrimitives(); n++) { - threadHierarchy->GetPrimitive(n)->mInvert = 0; - } - for (dtkID n = 0; n < internalIntervals.size(); n++) { - for (int l = internalIntervals[n][0]; l <= internalIntervals[n][1]; - l++) { - threadHierarchy->GetPrimitive(l)->mInvert = 1; - } - } - } - - // Phase 3 - core->mEnterBarrier->wait(); - - for (dtkID i = 0; i < massSpringRange.size(); i++) { - if (core->mTimeslice == 0) - continue; - - if (core->mMassSprings[massSpringRange[i]]->IsUnderControl()) { - core->mMassSprings[massSpringRange[i]]->ConvertImpulseToForce( - core->mTimeslice); - continue; - } - - core->mMassSprings[massSpringRange[i]]->ApplyImpulse(core->mTimeslice); - core->mMassSprings[massSpringRange[i]]->PreUpdate(core->mTimeslice, - Collision, 1); - core->mMassSprings[massSpringRange[i]]->UpdateStrings(core->mTimeslice, - Collision, 1, true); - } - - // exit_barrier->wait(); - - // Phase 4 - core->mEnterBarrier->wait(); - - for (dtkID i = 0; i < massSpringRange.size(); i++) { - if (core->mTimeslice == 0 || - core->mMassSprings[massSpringRange[i]]->IsUnderControl()) - continue; - core->mMassSprings[massSpringRange[i]]->UpdateMassPoints(core->mTimeslice, - Collision, 1); - core->mMassSprings[massSpringRange[i]]->PostUpdate(Collision, 1); - } - - core->mExitBarrier->wait(); - } while (true); -} - -dtkPhysCore::dtkPhysCore(double clothDepth) { - mStage = dtkCollisionDetectStage::New(); - mCollisionDetectResponse = dtkPhysMassSpringCollisionResponse::New(); - mThreadCollisionDetectResponse = - dtkPhysMassSpringThreadCollisionResponse::New(mCollisionDetectResponse); - mStaticMeshEliminator = dtkStaticMeshEliminator::New(); - - mNumberOfThreads = 0; - mThreadGroup = 0; - mEnterBarrier = 0; - mExitBarrier = 0; - - mClothDepth = clothDepth; -} - -dtkPhysCore::~dtkPhysCore() { - if (mNumberOfThreads > 0) { - mLive = false; - mEnterBarrier->wait(); - mThreadGroup->join_all(); - delete mThreadGroup; - delete mEnterBarrier; - delete mExitBarrier; - } -} - -void dtkPhysCore::Update(double timeslice) { - if (mNumberOfThreads > 1) - _Update_mt(timeslice); - else - _Update_s(timeslice); -} - -void dtkPhysCore::_Update_s(double timeslice) { - mTimeslice = timeslice; - - // update mass-spring model:iteration 0 - for (map::iterator itr = mMassSprings.begin(); - itr != mMassSprings.end(); itr++) { - if (timeslice == 0 || itr->second->IsUnderControl()) - continue; - itr->second->PreUpdate(timeslice, Collision, 0); - itr->second->UpdateStrings(timeslice, Collision, 0, true); - } - for (map::iterator itr = mMassSprings.begin(); - itr != mMassSprings.end(); itr++) { - if (timeslice == 0 || itr->second->IsUnderControl()) - continue; - itr->second->TransportForce(timeslice); - itr->second->UpdateMassPoints(timeslice, Collision, 0); - itr->second->PostUpdate(Collision, 0); - } - - // update hierachy - mStage->Update(); - - // update collision response result - vector> emptyIntervals; - for (map::iterator itr = - mCollisionDetectResponseSets.begin(); - itr != mCollisionDetectResponseSets.end(); itr++) { - vector intersectResults; - mStage->DoIntersect(itr->second.hierarchy_pair, intersectResults, - itr->second.self, false); - if (itr->second.responseType == THREAD_SURFACE) { - mCollisionDetectResponse->Update( - timeslice, intersectResults, emptyIntervals, - mThreadCollisionDetectResponse->GetAvoidIntervals(itr->first % - mPairOffset), - itr->second.strength); - } else if (itr->second.responseType == KNOTPLANNING) { - mCollisionDetectResponse->Update( - timeslice, intersectResults, - mKnotPlanners[itr->first % mPairOffset]->GetAvoidIntervals(), - mKnotPlanners[itr->first % mPairOffset]->GetAvoidIntervals(), - itr->second.strength); - } else if (itr->second.responseType == INTERIOR_THREADHEAD) { - assert(false); - } else { - mCollisionDetectResponse->Update(timeslice, intersectResults, - emptyIntervals, emptyIntervals, - itr->second.strength); - } - - if (itr->second.responseType == KNOTPLANNING) { - mKnotPlanners[itr->first / mPairOffset]->KnotRecognition( - intersectResults); - } - - if (intersectResults.size() != 0 && itr->second.custom_handle != 0) - itr->second.custom_handle(intersectResults, itr->second.pContext); - } - - // update internal collision response result - for (map::iterator itr = - mInternalCollisionDetectResponseSets.begin(); - itr != mInternalCollisionDetectResponseSets.end(); itr++) { - vector intersectResults; - mStage->DoIntersect(itr->second.hierarchy_pair, intersectResults, - itr->second.self, false); - mThreadCollisionDetectResponse->Update(timeslice, intersectResults); - - const std::vector> &internalIntervals = - mThreadCollisionDetectResponse->GetInternalIntervals(itr->first % - mPairOffset); - dtkCollisionDetectHierarchyKDOPS::Ptr threadHierarchy = - mThreadCollisionDetectHierarchies[itr->first % mPairOffset]; - for (dtkID n = 0; n < threadHierarchy->GetNumberOfPrimitives(); n++) { - threadHierarchy->GetPrimitive(n)->mInvert = 0; - } - for (dtkID n = 0; n < internalIntervals.size(); n++) { - for (int l = internalIntervals[n][0]; l <= internalIntervals[n][1]; l++) { - threadHierarchy->GetPrimitive(l)->mInvert = 1; - } - } - } - - // apply impulse into the mass points and update mass-spring model iteration : - // 1 - for (map::iterator itr = mMassSprings.begin(); - itr != mMassSprings.end(); itr++) { - if (timeslice == 0) - continue; - - if (itr->second->IsUnderControl()) { - itr->second->ConvertImpulseToForce(timeslice); - continue; - } - - itr->second->ApplyImpulse(timeslice); - itr->second->PreUpdate(timeslice, Collision, 1); - itr->second->UpdateStrings(timeslice, Collision, 1, true); - } - for (map::iterator itr = mMassSprings.begin(); - itr != mMassSprings.end(); itr++) { - if (timeslice == 0 || itr->second->IsUnderControl()) - continue; - itr->second->UpdateMassPoints(timeslice, Collision, 1); - itr->second->PostUpdate(Collision, 1); - } - - for (dtkID i = 0; i < mAdherePointSets.size(); i++) { - AdherePointSet &adherePointSet = mAdherePointSets[i]; - GK::Point3 p_tri = barycenter( - adherePointSet.dominate_pts->GetPoint(adherePointSet.dominate_tri[0]), - adherePointSet.uvw[0], - adherePointSet.dominate_pts->GetPoint(adherePointSet.dominate_tri[1]), - adherePointSet.uvw[1], - adherePointSet.dominate_pts->GetPoint(adherePointSet.dominate_tri[2]), - adherePointSet.uvw[2]); - GK::Point3 p_p = adherePointSet.slave_pts->GetPoint(adherePointSet.slave_p); - GK::Vector3 normal = p_p - p_tri; - - dtkPhysMassSpring::Ptr dominate_ms = - mMassSprings[adherePointSet.dominate_ID]; - dtkPhysMassSpring::Ptr slave_ms = mMassSprings[adherePointSet.slave_ID]; - - for (dtkID j = 0; j < 3; j++) { - dtkPhysMassPoint *dominate_mp = - dominate_ms->GetMassPoint(adherePointSet.dominate_tri[j]); - GK::Vector3 dominate_vel = - normal * adherePointSet.slave_ratio * adherePointSet.uvw[j]; - dominate_mp->SetPoint(adherePointSet.dominate_pts->GetPoint( - adherePointSet.dominate_tri[j]) + - dominate_vel); - dominate_vel = dominate_vel * (1 / mTimeslice); - dominate_mp->SetVel( - dominate_mp->GetVel() + - dtkT3(dominate_vel[0], dominate_vel[1], dominate_vel[2])); - } - - dtkPhysMassPoint *slave_mp = slave_ms->GetMassPoint(adherePointSet.slave_p); - GK::Vector3 slave_vel = -normal * (1.0 - adherePointSet.slave_ratio); - slave_mp->SetPoint( - adherePointSet.slave_pts->GetPoint(adherePointSet.slave_p) + slave_vel); - slave_vel = slave_vel * (1 / mTimeslice); - slave_mp->SetVel(slave_mp->GetVel() + - dtkT3(slave_vel[0], slave_vel[1], slave_vel[2])); - } - - for (map::iterator itr = - mParticleSystems.begin(); - itr != mParticleSystems.end(); itr++) { - if (timeslice == 0) - continue; - itr->second->Update(timeslice); - } - - for (map::iterator itr = mObstacleSets.begin(); - itr != mObstacleSets.end(); itr++) { - vector particleIntersectResult; - if (timeslice == 0) - continue; - for (dtkID i = 0; i < itr->second.particlesystem->GetNumberOfParticles(); - i++) { - GK::Point3 particle = itr->second.particlesystem->GetPoint(i); - itr->second.pts->SetPoint(0, particle); - itr->second.hierarchy_pair.second->Update(); - vector intersectResults; - mStage->DoIntersect(itr->second.hierarchy_pair, intersectResults, false, - false); - for (dtkID j = 0; j < intersectResults.size(); j++) { - dtkIntersectTest::IntersectResult::Ptr result = intersectResults[j]; - GK::Vector3 normal; - result->GetProperty(dtkIntersectTest::INTERSECT_NORMAL, normal); - itr->second.particlesystem->SetPoint(i, particle + normal); - itr->second.particlesystem->GetParticle(i)->AddForce( - dtkDouble3(-normal[0], -normal[1], -normal[2]) * - itr->second.viscosityCoef); - - particleIntersectResult.push_back(intersectResults[j]); - } - } - if (particleIntersectResult.size() != 0 && itr->second.custom_handle != 0) - itr->second.custom_handle(particleIntersectResult, itr->second.pContext); - } - - for (std::map>::iterator itr_device = - mDeviceLabels.begin(); - itr_device != mDeviceLabels.end(); itr_device++) { - dtkT3 forceFeedback(0, 0, 0); - for (map::iterator itr_ms = - mMassSprings.begin(); - itr_ms != mMassSprings.end(); itr_ms++) { - forceFeedback = - forceFeedback + itr_ms->second->GetTransportForce(itr_device->first); - } - if (forceFeedback[0] == 0 && forceFeedback[1] == 0 && - forceFeedback[2] == 0) { - for (dtkID i = 0; i < itr_device->second.size(); i++) { - forceFeedback = forceFeedback + - mMassSprings[itr_device->second[i]]->GetImpulseForce(); - } - } else { - forceFeedback = forceFeedback * 1.0; - } - mDeviceForceFeedbacks[itr_device->first] = forceFeedback; - } - - for (map::iterator itr = - mSutureThreads.begin(); - itr != mSutureThreads.end(); itr++) { - mKnotPlanners[itr->first]->DoKnotFormation(); - mKnotPlanners[itr->first]->UpdateKnot(timeslice); - } - - for (map::iterator itr = - mSutureThreads.begin(); - itr != mSutureThreads.end(); itr++) { - vector controlPoints; - - double interval = itr->second->GetInterval() * 0.5; - - for (dtkID i = 0; i < itr->second->GetNumberOfSegments() + 1; i++) { - dtkDouble3 curPoint = itr->second->GetMassPoint(i * 2)->GetPosition(); - dtkDouble3 smoothedDirection(0, 0, 0); - int num = 0; - if (i != 0) { - smoothedDirection = smoothedDirection + curPoint - - itr->second->GetMassPoint(i * 2 - 2)->GetPosition(); - num++; - } - if (i != itr->second->GetNumberOfSegments()) { - smoothedDirection = - smoothedDirection + - itr->second->GetMassPoint(i * 2 + 2)->GetPosition() - curPoint; - num++; - } - if (num == 0) - assert(false); - smoothedDirection = normalize(smoothedDirection); - controlPoints.push_back(curPoint - smoothedDirection * interval); - controlPoints.push_back(curPoint); - controlPoints.push_back(curPoint + smoothedDirection * interval); - } - - dtkID mSmoothedNumber = 10; - for (dtkID i = 0; i < itr->second->GetNumberOfSegments(); i++) { - double step = 1.0 / (double)mSmoothedNumber; - double t; - for (dtkID j = 0; j < mSmoothedNumber; j++) { - t = step * j; - dtkDouble3 smoothedPoint = - controlPoints[i * 3 + 1] * ((1 - t) * (1 - t) * (1 - t)) + - controlPoints[i * 3 + 2] * (3.0 * t * (1 - t) * (1 - t)) + - controlPoints[i * 3 + 3] * (3.0 * t * t * (1 - t)) + - controlPoints[i * 3 + 4] * (t * t * t); - mThreadPoints[itr->first]->SetPoint( - i * mSmoothedNumber + j, - GK::Point3(smoothedPoint[0], smoothedPoint[1], smoothedPoint[2])); - } - } - } -} - -void dtkPhysCore::SetNumberOfThreads(size_t n) { - if (n < 2) - return; - mNumberOfThreads = n; - mLive = true; - mThreadGroup = new thread_group(); - mEnterBarrier = new barrier(mNumberOfThreads + 1); - mExitBarrier = new barrier(mNumberOfThreads + 1); - - Reallocate(); - - // create threads - for (dtkID i = 0; i < mNumberOfThreads; i++) { - mThreadGroup->add_thread( - new boost::thread(dtkphyscore_mt_update, i, this - // mNumberOfThreads, &(mAllocator[i]), - // mEnterBarrier, mExitBarrier, &mLive, - //&mMassSprings, &mCollisionDetectHierarchies, - //&mCollisionDetectResponseSets, &mStage, - //&mCollisionDetectResponse, &mTimeslice - )); - } -} - -// allocate different id group to different thread. -dtkID dtkPhysCore::AllocateDetails( - const multimap> &sortedMap, size_t average) { - vector allocatedNumberOfMassSpringPoints; - for (dtkID i = 0; i < mNumberOfThreads; i++) { - allocatedNumberOfMassSpringPoints.push_back(0); - mAllocator[i].push_back(std::vector()); - } - dtkID posInAllocator = mAllocator[0].size() - 1; - for (std::multimap>::const_iterator itr = - sortedMap.begin(); - itr != sortedMap.end(); itr++) { - dtkID allocator = 0; - int bestMatch = dtkIntMax; - for (dtkID j = 0; j < mNumberOfThreads; j++) { - int match = abs((int)allocatedNumberOfMassSpringPoints[j] + - (int)itr->first - (int)average); - if (match < bestMatch) { - bestMatch = match; - allocator = j; - } - } - mAllocator[allocator][posInAllocator].push_back(itr->second); - allocatedNumberOfMassSpringPoints[allocator] += itr->first; - } - return posInAllocator; -} - -void dtkPhysCore::Reallocate() { - mAllocator.clear(); - for (dtkID i = 0; i < mNumberOfThreads; i++) { - mAllocator.push_back(std::vector>()); - } - - // 0: allocate mass spring - size_t sumOfMassSpringPoints = 0; - multimap> sortedMassSprings; - for (std::map::iterator itr = - mMassSprings.begin(); - itr != mMassSprings.end(); itr++) { - dtkID num = 0; - if (mConnectMasterMap.find(itr->first) != mConnectMasterMap.end()) { - set bundle = mConnectMasterMap[itr->first]; - for (set::iterator bundleItr = bundle.begin(); - bundleItr != bundle.end(); bundleItr++) { - num += mMassSprings[*bundleItr]->GetNumberOfMassPoints(); - } - } else if (mConnectedMassSpring.find(itr->first) != - mConnectedMassSpring.end()) { - continue; - } else { - num = itr->second->GetNumberOfMassPoints(); - } - sumOfMassSpringPoints += num; - sortedMassSprings.insert(pair(num, itr->first)); - } - - size_t averageOfMassSpringPoints = sumOfMassSpringPoints / mNumberOfThreads; - - mAllocatePosMassSpring = - AllocateDetails(sortedMassSprings, averageOfMassSpringPoints); - - for (dtkID i = 0; i < mNumberOfThreads; i++) { - vector &massSprings = mAllocator[i][mAllocatePosMassSpring]; - vector newMassSprings; - for (dtkID j = 0; j < massSprings.size(); j++) { - if (mConnectMasterMap.find(massSprings[j]) != mConnectMasterMap.end()) { - set &bundle = mConnectMasterMap[massSprings[j]]; - newMassSprings.insert(newMassSprings.end(), bundle.begin(), - bundle.end()); - } else { - newMassSprings.push_back(massSprings[j]); - } - } - mAllocator[i][mAllocatePosMassSpring] = newMassSprings; - } - - // 1: allocate collision primitives update - size_t sumOfPrimitives = 0; - multimap> sortedPrimitives; - for (std::map::iterator itr = - mCollisionDetectHierarchies.begin(); - itr != mCollisionDetectHierarchies.end(); itr++) { - dtkID num = itr->second->GetNumberOfPrimitives(); - sumOfPrimitives += num; - sortedPrimitives.insert( - pair(num, itr->first + mPairOffset * 0)); - } - for (std::map::iterator itr = - mThreadCollisionDetectHierarchies.begin(); - itr != mThreadCollisionDetectHierarchies.end(); itr++) { - dtkID num = itr->second->GetNumberOfPrimitives(); - sumOfPrimitives += num; - sortedPrimitives.insert( - pair(num, itr->first + mPairOffset * 1)); - } - for (std::map::iterator itr = - mInteriorCollisionDetectHierarchies.begin(); - itr != mInteriorCollisionDetectHierarchies.end(); itr++) { - dtkID num = itr->second->GetNumberOfPrimitives(); - sumOfPrimitives += num; - sortedPrimitives.insert( - pair(num, itr->first + mPairOffset * 2)); - } - for (std::map::iterator itr = - mThreadHeadCollisionDetectHierarchies.begin(); - itr != mThreadHeadCollisionDetectHierarchies.end(); itr++) { - dtkID num = itr->second->GetNumberOfPrimitives(); - sumOfPrimitives += num; - sortedPrimitives.insert( - pair(num, itr->first + mPairOffset * 3)); - } - - size_t averageOfPrimitives = sumOfPrimitives / mNumberOfThreads; - - mAllocatePosPrimitive = - AllocateDetails(sortedPrimitives, averageOfPrimitives); - - // 2: allocate collision detect - size_t sumOfCollisionDetectPairs = 0; - multimap> sortedCollisionDetectResponseSets; - for (std::map::iterator itr = - mCollisionDetectResponseSets.begin(); - itr != mCollisionDetectResponseSets.end(); itr++) { - dtkID num = itr->second.hierarchy_pair.first->GetNumberOfPrimitives() * - itr->second.hierarchy_pair.second->GetNumberOfPrimitives(); - sumOfCollisionDetectPairs += num; - sortedCollisionDetectResponseSets.insert( - pair(num, itr->first)); - } - size_t averageOfCollisionDetectPairs = - sumOfCollisionDetectPairs / mNumberOfThreads; - - mAllocatePosCollisionDetect = AllocateDetails( - sortedCollisionDetectResponseSets, averageOfCollisionDetectPairs); - - // 3: allocate internal collision detect - size_t sumOfInternalCollisionDetectPairs = 0; - multimap> - sortedInternalCollisionDetectResponseSets; - for (std::map::iterator itr = - mInternalCollisionDetectResponseSets.begin(); - itr != mInternalCollisionDetectResponseSets.end(); itr++) { - dtkID num = itr->second.hierarchy_pair.first->GetNumberOfPrimitives() * - itr->second.hierarchy_pair.second->GetNumberOfPrimitives(); - sumOfInternalCollisionDetectPairs += num; - sortedInternalCollisionDetectResponseSets.insert( - pair(num, itr->first)); - } - size_t averageOfInternalCollisionDetectPairs = - sumOfInternalCollisionDetectPairs / mNumberOfThreads; - - mAllocatePosInternalCollisionDetect = - AllocateDetails(sortedInternalCollisionDetectResponseSets, - averageOfInternalCollisionDetectPairs); - - return; -} - -void dtkPhysCore::_Update_mt(double timeslice) { - mTimeslice = timeslice; - - // Phase 1 - mEnterBarrier->wait(); - - // Phase 2 - mEnterBarrier->wait(); - - // Phase 2.1 - mEnterBarrier->wait(); - - // Phase 2.2 - mEnterBarrier->wait(); - - // Phase 2.3 - mEnterBarrier->wait(); - - // Phase 3 - mEnterBarrier->wait(); - - // Phase 4 - mEnterBarrier->wait(); - mExitBarrier->wait(); - - for (dtkID i = 0; i < mAdherePointSets.size(); i++) { - AdherePointSet &adherePointSet = mAdherePointSets[i]; - GK::Point3 p_tri = barycenter( - adherePointSet.dominate_pts->GetPoint(adherePointSet.dominate_tri[0]), - adherePointSet.uvw[0], - adherePointSet.dominate_pts->GetPoint(adherePointSet.dominate_tri[1]), - adherePointSet.uvw[1], - adherePointSet.dominate_pts->GetPoint(adherePointSet.dominate_tri[2]), - adherePointSet.uvw[2]); - GK::Point3 p_p = adherePointSet.slave_pts->GetPoint(adherePointSet.slave_p); - GK::Vector3 normal = p_p - p_tri; - - dtkPhysMassSpring::Ptr dominate_ms = - mMassSprings[adherePointSet.dominate_ID]; - dtkPhysMassSpring::Ptr slave_ms = mMassSprings[adherePointSet.slave_ID]; - - for (dtkID j = 0; j < 3; j++) { - dtkPhysMassPoint *dominate_mp = - dominate_ms->GetMassPoint(adherePointSet.dominate_tri[j]); - GK::Vector3 dominate_vel = - normal * adherePointSet.slave_ratio * adherePointSet.uvw[j]; - dominate_mp->SetPoint(adherePointSet.dominate_pts->GetPoint( - adherePointSet.dominate_tri[j]) + - dominate_vel); - dominate_vel = dominate_vel * (1 / mTimeslice); - dominate_mp->SetVel( - dominate_mp->GetVel() + - dtkT3(dominate_vel[0], dominate_vel[1], dominate_vel[2])); - } - - dtkPhysMassPoint *slave_mp = slave_ms->GetMassPoint(adherePointSet.slave_p); - GK::Vector3 slave_vel = -normal * (1.0 - adherePointSet.slave_ratio); - slave_mp->SetPoint( - adherePointSet.slave_pts->GetPoint(adherePointSet.slave_p) + slave_vel); - slave_vel = slave_vel * (1 / mTimeslice); - slave_mp->SetVel(slave_mp->GetVel() + - dtkT3(slave_vel[0], slave_vel[1], slave_vel[2])); - } - - for (map::iterator itr = - mParticleSystems.begin(); - itr != mParticleSystems.end(); itr++) { - if (timeslice == 0) - continue; - itr->second->Update(timeslice); - } - - for (map::iterator itr = mObstacleSets.begin(); - itr != mObstacleSets.end(); itr++) { - vector particleIntersectResult; - if (timeslice == 0) - continue; - for (dtkID i = 0; i < itr->second.particlesystem->GetNumberOfParticles(); - i++) { - GK::Point3 particle = itr->second.particlesystem->GetPoint(i); - itr->second.pts->SetPoint(0, particle); - itr->second.hierarchy_pair.second->Update(); - vector intersectResults; - mStage->DoIntersect(itr->second.hierarchy_pair, intersectResults, false, - false); - for (dtkID j = 0; j < intersectResults.size(); j++) { - dtkIntersectTest::IntersectResult::Ptr result = intersectResults[j]; - GK::Vector3 normal; - result->GetProperty(dtkIntersectTest::INTERSECT_NORMAL, normal); - itr->second.particlesystem->SetPoint(i, particle + normal); - itr->second.particlesystem->GetParticle(i)->AddForce( - dtkDouble3(-normal[0], -normal[1], -normal[2]) * - itr->second.viscosityCoef); - - particleIntersectResult.push_back(intersectResults[j]); - } - } - if (particleIntersectResult.size() != 0 && itr->second.custom_handle != 0) - itr->second.custom_handle(particleIntersectResult, itr->second.pContext); - } - - for (std::map>::iterator itr_device = - mDeviceLabels.begin(); - itr_device != mDeviceLabels.end(); itr_device++) { - dtkT3 forceFeedback(0, 0, 0); - for (map::iterator itr_ms = - mMassSprings.begin(); - itr_ms != mMassSprings.end(); itr_ms++) { - forceFeedback = - forceFeedback + itr_ms->second->GetTransportForce(itr_device->first); - } - if (forceFeedback[0] == 0 && forceFeedback[1] == 0 && - forceFeedback[2] == 0) { - for (dtkID i = 0; i < itr_device->second.size(); i++) { - forceFeedback = forceFeedback + - mMassSprings[itr_device->second[i]]->GetImpulseForce(); - } - } else { - forceFeedback = forceFeedback * 1.0; - } - mDeviceForceFeedbacks[itr_device->first] = forceFeedback; - } - - for (map::iterator itr = - mSutureThreads.begin(); - itr != mSutureThreads.end(); itr++) { - mKnotPlanners[itr->first]->DoKnotFormation(); - mKnotPlanners[itr->first]->UpdateKnot(timeslice); - } - - for (map::iterator itr = - mSutureThreads.begin(); - itr != mSutureThreads.end(); itr++) { - vector controlPoints; - - double interval = itr->second->GetInterval() * 0.5; - - for (dtkID i = 0; i < itr->second->GetNumberOfSegments() + 1; i++) { - dtkDouble3 curPoint = itr->second->GetMassPoint(i * 2)->GetPosition(); - dtkDouble3 smoothedDirection(0, 0, 0); - int num = 0; - if (i != 0) { - smoothedDirection = smoothedDirection + curPoint - - itr->second->GetMassPoint(i * 2 - 2)->GetPosition(); - num++; - } - if (i != itr->second->GetNumberOfSegments()) { - smoothedDirection = - smoothedDirection + - itr->second->GetMassPoint(i * 2 + 2)->GetPosition() - curPoint; - num++; - } - if (num == 0) - assert(false); - smoothedDirection = normalize(smoothedDirection); - controlPoints.push_back(curPoint - smoothedDirection * interval); - controlPoints.push_back(curPoint); - controlPoints.push_back(curPoint + smoothedDirection * interval); - } - - dtkID mSmoothedNumber = 10; - for (dtkID i = 0; i < itr->second->GetNumberOfSegments(); i++) { - double step = 1.0 / (double)mSmoothedNumber; - double t; - for (dtkID j = 0; j < mSmoothedNumber; j++) { - t = step * j; - dtkDouble3 smoothedPoint = - controlPoints[i * 3 + 1] * ((1 - t) * (1 - t) * (1 - t)) + - controlPoints[i * 3 + 2] * (3.0 * t * (1 - t) * (1 - t)) + - controlPoints[i * 3 + 3] * (3.0 * t * t * (1 - t)) + - controlPoints[i * 3 + 4] * (t * t * t); - mThreadPoints[itr->first]->SetPoint( - i * mSmoothedNumber + j, - GK::Point3(smoothedPoint[0], smoothedPoint[1], smoothedPoint[2])); - } - } - } -} - -void dtkPhysCore::CreateSutureThread( - dtkID id, double length, dtkDouble3 firstPointPos, - dtkPhysMassSpringThread::Orientation orientation, double mass, - double edgeStiff, double bendStiff, double torsionStiff, double edgeDamp, - double extraEdgeDamp, double bendDamp, double torsionDamp, double interval, - double radius, double selfCollisionStrength) { - dtkID numberOfSegments = static_cast(length / interval) + 1; - - mSutureThreads[id] = dtkPhysMassSpringThread::New( - interval, numberOfSegments, firstPointPos, orientation, mass, edgeStiff, - bendStiff, torsionStiff, edgeDamp, extraEdgeDamp, bendDamp, torsionDamp); - - mThreadCollisionDetectHierarchies[id] = - dtkCollisionDetectHierarchyKDOPS::New(K); - mThreadHeadCollisionDetectHierarchies[id] = - dtkCollisionDetectHierarchyKDOPS::New(K); - - dtkCollisionDetectPrimitive *pri; - for (dtkID i = 0; i < mSutureThreads[id]->GetNumberOfSegments(); i++) { - pri = mThreadCollisionDetectHierarchies[id]->InsertSegment( - mSutureThreads[id]->GetPoints(), dtkID2(i * 2, i * 2 + 2)); - pri->SetExtend(radius); - pri->mMajorID = id; - pri->mMinorID = i; - pri->mDetailIDs[0] = i * 2; - pri->mDetailIDs[1] = i * 2 + 2; - } - - pri = mThreadHeadCollisionDetectHierarchies[id]->InsertSegment( - mSutureThreads[id]->GetPoints(), dtkID2(0, 2)); - pri->SetExtend(0); - pri->mMajorID = id; - pri->mMinorID = 0; - pri->mDetailIDs[0] = 0; - pri->mDetailIDs[1] = 2; - - mThreadCollisionDetectHierarchies[id]->Build(); - mThreadHeadCollisionDetectHierarchies[id]->Build(); - - mStage->AddHierarchy(mThreadCollisionDetectHierarchies[id]); - mStage->AddHierarchy(mThreadHeadCollisionDetectHierarchies[id]); - - mCollisionDetectResponse->SetMassSpring(id, mSutureThreads[id]); - mCollisionDetectResponse->AddPierceSegment(id, 0); - - mThreadCollisionDetectResponse->SetThread(id, mSutureThreads[id]); - - mMassSprings[id] = mSutureThreads[id]; - - mKnotPlanners[id] = dtkPhysKnotPlanner::New(mSutureThreads[id]); - - mThreadPoints[id] = dtkPointsVector::New(numberOfSegments * 10); - - CreateCollisionResponse(id, THREAD, id, THREAD, selfCollisionStrength); -} - -void dtkPhysCore::CreateTetraMassSpring( - const char *filename, dtkID id, double point_mass, double stiffness, - double damp, double pointDamp, double pointResistence, - dtkDouble3 gravityAccel, /*bool isSurface, */ - dtkStaticMeshEliminator::MeshEliminatorResultsCallback callback, - void *pContext) { - // Target Mesh - dtkPoints::Ptr targetPts = dtkPointsVector::New(); - dtkStaticTetraMesh::Ptr targetMesh = dtkStaticTetraMesh::New(); - targetMesh->SetPoints(targetPts); - - dtkStaticTetraMeshReader::Ptr reader_tetramesh = - dtkStaticTetraMeshReader::New(); - reader_tetramesh->SetOutput(targetMesh); - reader_tetramesh->SetFileName(filename); - reader_tetramesh->Read(); - - mTetraMeshes[id] = targetMesh; - - dtkPhysTetraMassSpring::Ptr tetraMassSpring = - dtkPhysTetraMassSpring::New(false, point_mass, stiffness, damp, pointDamp, - pointResistence, gravityAccel); - tetraMassSpring->SetTetraMesh(targetMesh); - mMassSprings[id] = tetraMassSpring; - mTetraMassSprings[id] = tetraMassSpring; - - mCollisionDetectHierarchies[id] = dtkCollisionDetectHierarchyKDOPS::New(K); - mCollisionDetectHierarchies[id]->InsertTetraMesh( - targetMesh, id, dtkCollisionDetectHierarchy::SURFACE, mClothDepth); - mCollisionDetectHierarchies[id]->AutoSetMaxLevel(); - mCollisionDetectHierarchies[id]->Build(); - - mStage->AddHierarchy(mCollisionDetectHierarchies[id]); - mCollisionDetectResponse->SetMassSpring(id, mMassSprings[id]); - - dtkStaticTriangleMesh::Ptr surface = dtkStaticTriangleMesh::New(); - targetMesh->GetSurface(surface); - mTriangleMeshes[id] = surface; - - mStaticMeshEliminator->AddElimniateTarget( - id, mTriangleMeshes[id], targetMesh, mCollisionDetectHierarchies[id], - tetraMassSpring, callback, pContext); - - mAdhereCounts.insert( - pair>(id, map())); - - if (mNumberOfThreads >= 2) - Reallocate(); -} - -void dtkPhysCore::CreateTriangleMassSpring(const char *filename, dtkID id, - double point_mass, double stiffness, - double damp, double pointDamp, - double pointResistence, - dtkDouble3 gravityAccel) { - dtkPoints::Ptr targetPts = dtkPointsVector::New(); - dtkStaticTriangleMesh::Ptr targetMesh = dtkStaticTriangleMesh::New(); - targetMesh->SetPoints(targetPts); - - dtkStaticTriangleMeshReader::Ptr reader_trianglemesh = - dtkStaticTriangleMeshReader::New(); - reader_trianglemesh->SetOutput(targetMesh); - reader_trianglemesh->SetFileName(filename); - reader_trianglemesh->Read(); - - dtkPhysMassSpring::Ptr triangleMassSpring = dtkPhysMassSpring::New( - point_mass, stiffness, damp, pointDamp, pointResistence, gravityAccel); - triangleMassSpring->SetTriangleMesh(targetMesh); - mMassSprings[id] = triangleMassSpring; - - mCollisionDetectHierarchies[id] = dtkCollisionDetectHierarchyKDOPS::New(K); - mCollisionDetectHierarchies[id]->InsertTriangleMesh(targetMesh, id); - mCollisionDetectHierarchies[id]->AutoSetMaxLevel(); - mCollisionDetectHierarchies[id]->Build(); - - mStage->AddHierarchy(mCollisionDetectHierarchies[id]); - mCollisionDetectResponse->SetMassSpring(id, mMassSprings[id]); - - mTriangleMeshes[id] = targetMesh; - - mAdhereCounts.insert( - pair>(id, map())); - - if (mNumberOfThreads >= 2) - Reallocate(); -} - -// build a primitive by some edges. -void dtkPhysCore::CreateMassSpring(const char *filename, dtkID id, - double point_mass, double stiffness, - double damp, double pointDamp, - double pointResistence, - dtkDouble3 gravityAccel, - double specialExtend) { - dtkPoints::Ptr targetPts = dtkPointsVector::New(); - - dtkPhysMassSpring::Ptr massSpring = dtkPhysMassSpring::New( - point_mass, stiffness, damp, pointDamp, pointResistence, gravityAccel); - massSpring->SetPoints(targetPts); - - mMassSprings[id] = massSpring; - - mCollisionDetectHierarchies[id] = dtkCollisionDetectHierarchyKDOPS::New(K); - - std::ifstream file(filename); - size_t numOfPts; - dtkID maxID; - - file >> numOfPts >> maxID; - - // the size of targetPts ??? - for (size_t i = 0; i < numOfPts; ++i) { - dtkID id; - dtkFloat3 coord; - - file >> id >> coord.x >> coord.y >> coord.z; - targetPts->SetPoint(id, GK::Point3(coord.x, coord.y, coord.z)); - } - - for (dtkID i = 0; i < targetPts->GetNumberOfPoints(); i++) { - massSpring->AddMassPoint(i, point_mass, dtkT3(0, 0, 0), pointDamp, - pointResistence, gravityAccel); - } - - size_t numOfEdges; - - file >> numOfEdges; - - // the mCollisionDetectHierarcies have only segment ??? - for (size_t i = 0; i < numOfEdges; ++i) { - dtkID id0, id1; - - file >> id0 >> id1; - - massSpring->AddSpring(id0, id1, stiffness, damp); - dtkCollisionDetectPrimitive *primitive = - mCollisionDetectHierarchies[id]->InsertSegment(targetPts, - dtkID2(id0, id1)); - primitive->SetExtend(specialExtend); - primitive->mMajorID = id; - primitive->mMinorID = i; - primitive->mDetailIDs[0] = id0; - primitive->mDetailIDs[1] = id1; - } - - file.close(); - - mCollisionDetectHierarchies[id]->AutoSetMaxLevel(); - mCollisionDetectHierarchies[id]->Build(); - - mStage->AddHierarchy(mCollisionDetectHierarchies[id]); - mCollisionDetectResponse->SetMassSpring(id, mMassSprings[id]); - - mAdhereCounts.insert( - pair>(id, map())); - - if (mNumberOfThreads >= 2) - Reallocate(); -} - -dtkPhysMassSpring::Ptr dtkPhysCore::GetMassSpring(dtkID id) { - if (mMassSprings.find(id) == mMassSprings.end()) - assert(false); - - return mMassSprings[id]; -} - -dtkPhysMassSpring::Ptr dtkPhysCore::GetTetraMassSpring(dtkID id) { - if (mTetraMassSprings.find(id) == mTetraMassSprings.end()) - assert(false); - - return mTetraMassSprings[id]; -} - -dtkPhysMassSpringThread::Ptr dtkPhysCore::GetMassSpringThread(dtkID id) { - if (mSutureThreads.find(id) == mSutureThreads.end()) - assert(false); - - return mSutureThreads[id]; -} - -dtkStaticTriangleMesh::Ptr dtkPhysCore::GetTriangleMesh(dtkID id) { - if (mTriangleMeshes.find(id) == mTriangleMeshes.end()) - assert(false); - - return mTriangleMeshes[id]; -} - -dtkCollisionDetectHierarchyKDOPS::Ptr -dtkPhysCore::GetCollisionDetectHierarchy(dtkID id, - CollisionHierarchyType type) { - switch (type) { - case SURFACE: - if (mCollisionDetectHierarchies.find(id) == - mCollisionDetectHierarchies.end()) - assert(false); - - return mCollisionDetectHierarchies[id]; - case THREAD: - if (mThreadCollisionDetectHierarchies.find(id) == - mThreadCollisionDetectHierarchies.end()) - assert(false); - - return mThreadCollisionDetectHierarchies[id]; - case INTERIOR: - if (mInteriorCollisionDetectHierarchies.find(id) == - mInteriorCollisionDetectHierarchies.end()) - assert(false); - - return mInteriorCollisionDetectHierarchies[id]; - case THREADHEAD: - if (mThreadHeadCollisionDetectHierarchies.find(id) == - mThreadHeadCollisionDetectHierarchies.end()) - assert(false); - - return mThreadHeadCollisionDetectHierarchies[id]; - default: - assert(false); - } -} - -dtkPhysMassSpringThreadCollisionResponse::Ptr -dtkPhysCore::GetThreadCollisionResponse() { - - return mThreadCollisionDetectResponse; -} - -dtkPhysKnotPlanner::Ptr dtkPhysCore::GetKnotPlanner(dtkID plannerID) { - if (mKnotPlanners.find(plannerID) == mKnotPlanners.end()) - assert(false); - - return mKnotPlanners[plannerID]; -} - -void dtkPhysCore::CreateCollisionResponse( - dtkID object1_id, dtkID object2_id, double strength, - void (*custom_handle)( - const std::vector - &intersectResults, - void *pContext), - void *pContext) { - CreateCollisionResponse(object1_id, SURFACE, object2_id, SURFACE, strength, - custom_handle, pContext); -} - -void dtkPhysCore::CreateCollisionResponse( - dtkID object1_id, CollisionHierarchyType obj1_type, dtkID object2_id, - CollisionHierarchyType obj2_type, double strength, - void (*custom_handle)( - const vector &intersectResults, - void *pContext), - void *pContext) { - dtkID response_id = object1_id * mPairOffset + object2_id; - - CollisionResponseSet newset; - newset.hierarchy_pair = dtkCollisionDetectStage::HierarchyPair( - GetCollisionDetectHierarchy(object1_id, obj1_type), - GetCollisionDetectHierarchy(object2_id, obj2_type)); - newset.strength = strength; - newset.custom_handle = custom_handle; - newset.pContext = pContext; - newset.self = (object1_id == object2_id); - - bool isInterior = false; - if (newset.self && obj1_type == THREAD) { - newset.responseType = KNOTPLANNING; - } else if (!newset.self && ((obj1_type == THREAD && obj2_type == SURFACE) || - (obj1_type == SURFACE && obj2_type == THREAD))) { - newset.responseType = THREAD_SURFACE; - } else if (obj1_type == INTERIOR && obj2_type == THREADHEAD) { - newset.responseType = INTERIOR_THREADHEAD; - isInterior = true; - } - - if (!isInterior) - mCollisionDetectResponseSets[response_id] = newset; - else - mInternalCollisionDetectResponseSets[response_id] = newset; - - if (mNumberOfThreads >= 2) - Reallocate(); -} - -void dtkPhysCore::CreateSutureResponse( - dtkID object_id, dtkID thread_id, double strength, - void (*custom_handle)( - const vector &intersectResults, - void *pContext), - void *pContext) { - // create hierarchies for object interior - mInteriorCollisionDetectHierarchies[object_id] = - dtkCollisionDetectHierarchyKDOPS::New(K); - mInteriorCollisionDetectHierarchies[object_id]->InsertTetraMesh( - mTetraMeshes[object_id], object_id, dtkCollisionDetectHierarchy::INTERIOR, - 0); - mInteriorCollisionDetectHierarchies[object_id]->AutoSetMaxLevel(); - mInteriorCollisionDetectHierarchies[object_id]->Build(); - - mStage->AddHierarchy(mInteriorCollisionDetectHierarchies[object_id]); - mCollisionDetectResponse->SetMassSpring(object_id, mMassSprings[object_id]); - - CreateCollisionResponse(object_id, SURFACE, thread_id, THREAD, strength, - custom_handle, pContext); - CreateCollisionResponse(object_id, INTERIOR, thread_id, THREADHEAD, 0); -} - -dtkPoints::Ptr dtkPhysCore::CreateCustomDetectTriangleArea(dtkID id, - double half_width) { - dtkPoints::Ptr targetPts = dtkPointsVector::New(); - targetPts->SetPoint(0, GK::Point3(0, 0, 0)); - targetPts->SetPoint(1, GK::Point3(1, 0, 0)); - targetPts->SetPoint(2, GK::Point3(0, 1, 0)); - - mCollisionDetectHierarchies[id] = dtkCollisionDetectHierarchyKDOPS::New(K); - dtkCollisionDetectPrimitive *triangle = - mCollisionDetectHierarchies[id]->InsertTriangle(targetPts, - dtkID3(0, 1, 2)); - triangle->mMajorID = id; - triangle->mMinorID = 0; - triangle->mDetailIDs[0] = 0; - triangle->mDetailIDs[1] = 1; - triangle->mDetailIDs[2] = 2; - triangle->mInvert = 2; - triangle->SetExtend(half_width); - mCollisionDetectHierarchies[id]->AutoSetMaxLevel(); - mCollisionDetectHierarchies[id]->Build(); - - // mStage->AddHierarchy( mCollisionDetectHierarchies[id] ); - - return targetPts; -} - -void dtkPhysCore::ExecuteCustomDetectTriangleArea( - dtkID id, dtkID targetID, - void (*custom_handle)( - const std::vector - &intersectResults, - void *pContext), - void *pContext) { - vector intersectResults; - mCollisionDetectHierarchies[id]->Update(); - mStage->DoIntersect(dtkCollisionDetectStage::HierarchyPair( - mCollisionDetectHierarchies[targetID], - mCollisionDetectHierarchies[id]), - intersectResults, false, false); - if (intersectResults.size() != 0) - custom_handle(intersectResults, pContext); -} - -size_t dtkPhysCore::ConnectMassSpring(dtkID object1_id, dtkID object2_id, - double range) { - size_t num = - mMassSprings[object1_id]->FindTwins(mMassSprings[object2_id], range); - if (num > 0) { - mConnectMap.push_back(dtkID2(object1_id, object2_id)); - mConnectedMassSpring.insert(object1_id); - mConnectedMassSpring.insert(object2_id); - RebundleConnectedMassSpring(); - - if (mNumberOfThreads > 1) - Reallocate(); - } - return num; -} - -void dtkPhysCore::RebundleConnectedMassSpring() { - mConnectMasterMap.clear(); - - set remainMassSprings; - for (std::map::iterator itr = - mMassSprings.begin(); - itr != mMassSprings.end(); itr++) { - remainMassSprings.insert(itr->first); - } - while (!remainMassSprings.empty()) { - set possibleBundle; - queue bundleFront; - dtkID startPoint = *(remainMassSprings.begin()); - remainMassSprings.erase(startPoint); - possibleBundle.insert(startPoint); - bundleFront.push(startPoint); - while (!bundleFront.empty()) { - dtkID curFront = bundleFront.front(); - bundleFront.pop(); - for (dtkID i = 0; i < mConnectMap.size(); i++) { - if (curFront == mConnectMap[i][0] && - possibleBundle.find(mConnectMap[i][1]) == possibleBundle.end()) { - dtkID newFront = mConnectMap[i][1]; - remainMassSprings.erase(newFront); - possibleBundle.insert(newFront); - bundleFront.push(newFront); - } else if (curFront == mConnectMap[i][1] && - possibleBundle.find(mConnectMap[i][0]) == - possibleBundle.end()) { - dtkID newFront = mConnectMap[i][0]; - remainMassSprings.erase(newFront); - possibleBundle.insert(newFront); - bundleFront.push(newFront); - } - } - } - - if (possibleBundle.size() > 1) { - mConnectMasterMap[*(possibleBundle.begin())] = possibleBundle; - } - } -} - -size_t dtkPhysCore::AdhereMassSpring(dtkID from_id, dtkID to_id, double range) { - dtkCollisionDetectHierarchyKDOPS::Ptr hierarchy_points; - hierarchy_points = dtkCollisionDetectHierarchyKDOPS::New(K); - dtkPoints::Ptr pts = mMassSprings[from_id]->GetPoints(); - for (dtkID i = 0; i < pts->GetNumberOfPoints(); i++) { - dtkCollisionDetectPrimitive *primitive = - hierarchy_points->InsertSphere(pts, i); - primitive->mMajorID = from_id; - primitive->mMinorID = i; - primitive->mDetailIDs[0] = i; - primitive->SetExtend(range - mClothDepth); - } - hierarchy_points->AutoSetMaxLevel(); - hierarchy_points->Build(); - - vector intersectResults; - mStage->DoIntersect(dtkCollisionDetectStage::HierarchyPair( - mCollisionDetectHierarchies[to_id], hierarchy_points), - intersectResults, false, false); - - for (dtkID i = 0; i < intersectResults.size(); i++) { - dtkIntersectTest::IntersectResult::Ptr result = intersectResults[i]; - dtkCollisionDetectPrimitive *pri_1; - dtkCollisionDetectPrimitive *pri_2; - result->GetProperty(dtkIntersectTest::INTERSECT_PRIMITIVE_1, pri_1); - result->GetProperty(dtkIntersectTest::INTERSECT_PRIMITIVE_2, pri_2); - - AdherePointSet newset; - newset.dominate_pts = mMassSprings[pri_1->mMajorID]->GetPoints(); - newset.dominate_tri[0] = pri_1->mDetailIDs[0]; - newset.dominate_tri[1] = pri_1->mDetailIDs[1]; - newset.dominate_tri[2] = pri_1->mDetailIDs[2]; - newset.dominate_ID = pri_1->mMajorID; - newset.dominate_triID = pri_1->mMinorID; - map &countMap = mAdhereCounts[newset.dominate_ID]; - map::iterator triItr = countMap.find(newset.dominate_triID); - if (triItr != countMap.end()) { - triItr->second++; - } else { - countMap.insert(pair(newset.dominate_triID, 1)); - } - newset.slave_pts = mMassSprings[pri_2->mMajorID]->GetPoints(); - newset.slave_p = pri_2->mDetailIDs[0]; - newset.slave_ID = pri_2->mMajorID; - result->GetProperty(dtkIntersectTest::INTERSECT_WEIGHT_1, newset.uvw); - mAdherePointSets.push_back(newset); - } - - AdjustAdhereStatus(); - - return intersectResults.size(); -} - -void dtkPhysCore::AdjustAdhereStatus() { - for (dtkID i = 0; i < mAdherePointSets.size(); i++) { - dtkPhysCore::AdherePointSet &adherePointSet = mAdherePointSets[i]; - adherePointSet.slave_ratio = - 1.0 - - pow(0.5, 1.0 / (double)mAdhereCounts[adherePointSet.dominate_ID] - [adherePointSet.dominate_triID]); - } -} - -dtkPhysParticleSystem::Ptr -dtkPhysCore::CreateParticleSystem(dtkID id, double particleRadius, - double particleMass, - double particleLifetime) { - mParticleSystems[id] = dtkPhysParticleSystem::New( - particleRadius - mClothDepth, particleMass, particleLifetime); - - return mParticleSystems[id]; -} - -void dtkPhysCore::CreateObstacleForParticleSystem( - dtkID particlesystem_id, dtkID object_id, double viscosityCoef, - void (*custom_handle)( - const std::vector - &intersectResults, - void *pContext), - void *pContext) { - dtkID obstacleid = particlesystem_id * mPairOffset + object_id; - - dtkCollisionDetectHierarchyKDOPS::Ptr hierarchy_points; - hierarchy_points = dtkCollisionDetectHierarchyKDOPS::New(K); - - dtkPoints::Ptr pts = dtkPointsVector::New(1); - dtkCollisionDetectPrimitive *primitive = - hierarchy_points->InsertSphere(pts, 0); - primitive->mMajorID = obstacleid; - primitive->mMinorID = 0; - primitive->mDetailIDs[0] = 0; - primitive->SetExtend( - mParticleSystems[particlesystem_id]->GetParticleRadius()); - - hierarchy_points->AutoSetMaxLevel(); - hierarchy_points->Build(); - - ObstacleSet newset; - newset.pts = pts; - newset.particlesystem = mParticleSystems[particlesystem_id]; - newset.hierarchy_pair = dtkCollisionDetectStage::HierarchyPair( - mCollisionDetectHierarchies[object_id], hierarchy_points); - newset.viscosityCoef = viscosityCoef; - newset.custom_handle = custom_handle; - newset.pContext = pContext; - - mObstacleSets[obstacleid] = newset; -} - -void dtkPhysCore::DestroyMassSpring(dtkID id) { - mMassSprings.erase(id); - - mStage->RemoveHierarchy(mCollisionDetectHierarchies[id]); - mCollisionDetectHierarchies.erase(id); - - mCollisionDetectResponse->RemoveMassSpring(id); - - if (mNumberOfThreads >= 2) - Reallocate(); -} - -void dtkPhysCore::DestroyTriangleMassSpring(dtkID id) { - mMassSprings.erase(id); - - mStage->RemoveHierarchy(mCollisionDetectHierarchies[id]); - mCollisionDetectHierarchies.erase(id); - - mCollisionDetectResponse->RemoveMassSpring(id); - - mTriangleMeshes.erase(id); - - if (mNumberOfThreads >= 2) - Reallocate(); -} - -void dtkPhysCore::DestroyTetraMassSpring(dtkID id) { - mMassSprings.erase(id); - mTetraMassSprings.erase(id); - - dtkCollisionDetectHierarchy::Ptr hierarchy = mCollisionDetectHierarchies[id]; - mStage->RemoveHierarchy(hierarchy); - - vector deleteIDs; - for (std::map::iterator itr = - mCollisionDetectResponseSets.begin(); - itr != mCollisionDetectResponseSets.end(); itr++) { - if (itr->second.hierarchy_pair.first == hierarchy || - itr->second.hierarchy_pair.second == hierarchy) - deleteIDs.push_back(itr->first); - } - for (dtkID i = 0; i < deleteIDs.size(); i++) - mCollisionDetectResponseSets.erase(deleteIDs[i]); - mCollisionDetectHierarchies.erase(id); - - mCollisionDetectResponse->RemoveMassSpring(id); - - mTetraMeshes.erase(id); - mTriangleMeshes.erase(id); - - mStaticMeshEliminator->RemoveElimniateTarget(id); - - if (mNumberOfThreads >= 2) - Reallocate(); -} - -void dtkPhysCore::DestroyCollisionResponse(dtkID object1_id, dtkID object2_id) { - DestroyCollisionResponse(object1_id, SURFACE, object2_id, SURFACE); -} - -void dtkPhysCore::DestroyCollisionResponse(dtkID object1_id, - CollisionHierarchyType obj1_type, - dtkID object2_id, - CollisionHierarchyType obj2_type) { - dtkID response_id = object1_id * mPairOffset + object2_id; - - bool isInterior = false; - if (obj1_type == INTERIOR && obj2_type == THREADHEAD) { - isInterior = true; - } - - if (!isInterior) - mCollisionDetectResponseSets.erase(response_id); - else - mInternalCollisionDetectResponseSets.erase(response_id); - - if (mNumberOfThreads >= 2) - Reallocate(); -} - -void dtkPhysCore::DestroyCustomDetectTriangleArea(dtkID id) { - mStage->RemoveHierarchy(mCollisionDetectHierarchies[id]); - mCollisionDetectHierarchies.erase(id); -} - -void dtkPhysCore::DestroyParticleSystem(dtkID id) { - mParticleSystems.erase(id); -} - -void dtkPhysCore::DestroyObstacleForParticleSystem(dtkID particlesystem_id, - dtkID object_id) { - dtkID obstacleid = particlesystem_id * mPairOffset + object_id; - mObstacleSets.erase(obstacleid); -} - -void dtkPhysCore::DisconnectMassSpring(dtkID object1_id, dtkID object2_id) { - mMassSprings[object1_id]->AbandonTwins(); - mMassSprings[object2_id]->AbandonTwins(); -} - -void dtkPhysCore::DetachAllMassSpring() { mAdherePointSets.clear(); } - -void dtkPhysCore::EliminateTriangles(dtkID id, size_t originalFaceNum, - std::vector &collisionFace, - bool oldMethod) { - mStaticMeshEliminator->EliminateTriangles(id, originalFaceNum, collisionFace, - oldMethod); -} - -void dtkPhysCore::RegisterDevice(dtkID deviceLabel) { - mDeviceLabels[deviceLabel] = std::vector(); -} - -void dtkPhysCore::LabelObjectAsDevice(dtkID objectID, dtkID deviceLabel) { - mDeviceLabels[deviceLabel].push_back(objectID); - mMassSprings[objectID]->SetUnderControl(true); - mMassSprings[objectID]->RegisterLabel(deviceLabel); -} - -const dtkT3 &dtkPhysCore::GetDeviceForceFeedback(dtkID deviceLabel) { - return mDeviceForceFeedbacks[deviceLabel]; -} - -dtkPoints::Ptr dtkPhysCore::GetThreadPoints(dtkID id) { - return mThreadPoints[id]; -} -} // namespace dtk diff --git a/src/include/dtkGraphicsKernel.h.bak b/src/include/dtkGraphicsKernel.h.bak deleted file mode 100644 index d3e8817..0000000 --- a/src/include/dtkGraphicsKernel.h.bak +++ /dev/null @@ -1,286 +0,0 @@ -#ifndef DTK_GRAPHICSKERNEL_H -#define DTK_GRAPHICSKERNEL_H - -#include "dtkConfig.h" - -#include "CGAL/Exact_predicates_inexact_constructions_kernel.h" -#include -#include - -#include "dtkSign.h" -#include "dtkTx.h" - -namespace dtk { -template class dtkInterval { -public: - /* - typedef CGAL::Exact_predicates_inexact_constructions_kernel K; - typedef K::FT Float; - */ - - dtkInterval() { - mLowerBound = true; - mUpperBound = true; - mLower = 0; - mUpper = 0; - } - - dtkInterval(const Float &l, const Float &u, bool lb = true, bool ub = true) { - mLowerBound = lb; - mUpperBound = ub; - mLower = l; - mUpper = u; - } - - const Float &Lower() const { return mLower; } - - const Float &Upper() const { return mUpper; } - - bool IsLowerClosed() const { return mLowerBound; } - - bool IsUpperClosed() const { return mUpperBound; } - - const Float &operator[](const int &n) const { - switch (n) { - case 0: - return mLower; - case 1: - return mUpper; - default: // OUT_OF_RANGE - assert(false); - return mLower; - } - } - - Float &operator[](const int &n) { - return const_cast(static_cast(*this)[n]); - } - - bool Contain(const Float &v) const { - if (mLower > mUpper) - return false; - - if (v < mLower /*|| ( mLowerBound && v != mLower )*/) { - return false; - } else if (v > mUpper /*|| ( mUpperBound && v != mUpper )*/) { - return false; - } else - return true; - } - - void Extend(const Float &extend) { - if (extend < mLower) - mLower = extend; - - if (extend > mUpper) - mUpper = extend; - } - -public: - bool mLowerBound; - bool mUpperBound; - Float mLower; - Float mUpper; -}; - -template -inline std::ostream &operator<<(std::ostream &stream, - const dtkInterval &interval) { - if (interval.mLowerBound) - stream << "[ "; - else - stream << "( "; - - stream << interval.mLower << ", " << interval.mUpper << " "; - - if (interval.mUpperBound) - stream << "]"; - else - stream << ")"; - - return stream; -} - -class dtkDiscreteOrientationPolytope { -public: - typedef CGAL::Exact_predicates_inexact_constructions_kernel K; - typedef K::FT Float; - typedef CGAL::Vector_3 Vector3; - const static Vector3 mPredefinedAxis[13]; - -public: - dtkDiscreteOrientationPolytope(size_t half_k) { - mHalfK = half_k; - mIntervals.resize(mHalfK); - } - - ~dtkDiscreteOrientationPolytope() {} - - const Float &operator[](const int &n) const { - int major = n / 2; - int minor = n - major * 2; - - assert(((size_t)major) < mHalfK); - - return mIntervals[major][minor]; - } - - Float &operator[](const int &n) { - return const_cast( - static_cast(*this)[n]); - } - - size_t mHalfK; - std::vector> mIntervals; -}; - -inline std::ostream &operator<<(std::ostream &stream, - const dtkDiscreteOrientationPolytope &kdop) { - stream << "KDOP{ "; - for (size_t i = 0; i < kdop.mHalfK; i++) { - stream << kdop.mIntervals[i] << " "; - } - stream << "}"; - - return stream; -} - -class dtkGraphicsKernel { -public: - typedef dtkGraphicsKernel Type; - typedef CGAL::Exact_predicates_inexact_constructions_kernel K; - - typedef K::FT Float; - - typedef CGAL::Object Object; - - typedef dtkInterval Interval; - typedef dtkDiscreteOrientationPolytope KDOP; - - // 2D objects - typedef CGAL::Point_2 Point2; - typedef CGAL::Vector_2 Vector2; - typedef CGAL::Segment_2 Segment2; - typedef CGAL::Triangle_2 Triangle2; - typedef CGAL::Circle_2 Circle2; - - // 3D objects - typedef CGAL::Point_3 Point3; - typedef CGAL::Vector_3 Vector3; - typedef CGAL::Ray_3 Ray3; - typedef CGAL::Line_3 Line3; - typedef CGAL::Segment_3 Segment3; - typedef CGAL::Plane_3 Plane3; - typedef CGAL::Circle_3 Circle3; - typedef CGAL::Sphere_3 Sphere3; - typedef CGAL::Triangle_3 Triangle3; - typedef CGAL::Tetrahedron_3 Tetrahedron3; - typedef CGAL::Bbox_3 BBox3; - - typedef CGAL::Polyhedron_3 Polyhedron3; - - // basic operations. -public: - static bool IsZero(const Type::Float &v); - static float ToFloat(const Type::Float &v); - static dtkFloat3 ToFloat3(const Type::Point3 &pt); - static dtkFloat3 ToFloat3(const Type::Vector3 &pt); - - template static bool Assign(T &tgt, const Type::Object &obj) { - return CGAL::assign(tgt, obj); - } - - static Type::Float SquaredDistance(const Type::Point3 &pt, - const Type::Plane3 &plane); - static Type::Float SquaredDistance(const Type::Plane3 &plane, - const Type::Point3 &pt); - static Type::Float SquaredDistance(const Type::Point3 &pt, - const Type::Line3 &line); - static Type::Float SquaredDistance(const Type::Line3 &line, - const Type::Point3 &pt); - - static Type::Float SquaredArea(const Type::Point3 &p0, const Type::Point3 &p1, - const Type::Point3 &p2); - - static Type::Vector3 CrossProduct(const Type::Vector3 &lhs, - const Type::Vector3 &rhs); - static Type::Float DotProduct(const Type::Vector3 &lhs, - const Type::Vector3 &rhs); - static Type::Float DotProduct(const Type::Vector2 &lhs, - const Type::Vector2 &rhs); - - static Type::Vector3 Normalize(const Type::Vector3 &v); - static Type::Vector2 Normalize(const Type::Vector2 &v); - - static Type::Float Sqrt(const Type::Float &val); - - static Type::Point3 Midpoint(const Type::Point3 &p0, const Type::Point3 &p1); - - static Type::Point3 Centroid(const Type::Point3 &p0, const Type::Point3 &p1, - const Type::Point3 &p2); - static Type::Point3 Centroid(const Type::Point3 &p0, const Type::Point3 &p1, - const Type::Point3 &p2, const Type::Point3 &p3); - static Type::Point3 Centroid(const Type::Tetrahedron3 &tetra); - static Type::Point3 Centroid(const Type::Triangle3 &triangle); - - // Get Bounding box - static Type::BBox3 BoundingBox(const Type::Triangle3 &triangle); - static Type::BBox3 BoundingBox(const Type::Sphere3 &sphere); - static Type::BBox3 BoundingBox(const Type::Tetrahedron3 &tetra); - - // Circumcenter - static Type::Point3 Circumcenter(const Type::Triangle3 &triangle); - static Type::Point3 Circumcenter(const Type::Tetrahedron3 &tetra); - - // Barycenter Weight - static dtkDouble3 BarycentricWeight(const Type::Point3 &p0, - const Type::Point3 &p1, - const Type::Point3 &p2, - const Type::Point3 &p3); - static Type::Float Length(const Type::Vector3 &vec); - - // predicates. -public: - // return dtkSign::NEGATIVE if in right-hand order; - // dtkSign::POSITIVE if in left-hand order; - // dtkSign::ZERO if is degenerated. - static dtkSign Orient2D(const Type::Point2 &a, const Type::Point2 &b, - const Type::Point2 &p); - - static dtkSign Orient3D(const Type::Point3 &a, const Type::Point3 &b, - const Type::Point3 &p); - static dtkSign Orient3D(const Type::Point3 &a, const Type::Point3 &b, - const Type::Point3 &c, const Type::Point3 &p); - static dtkSign Orient3D(const Type::Plane3 &plane, const Type::Point3 &p); - - static dtkSign InSphere(const Type::Point3 &a, const Type::Point3 &b, - const Type::Point3 &c, const Type::Point3 &d, - const Type::Point3 &p); - - static bool IsDegenerate(const Type::Segment3 &seg); - static bool IsDegenerate(const Type::Tetrahedron3 &tetra); - - // dtkSign::POSITIVE, the point is in the bounded side - // dtkSign::NEGATIVE, the point is in the unbounded side - // dtkSign::ZERO, the point is on the boundary -public: - static dtkSign BoundedSide(const Type::Tetrahedron3 &tetra, - const Type::Point3 &pt); - static dtkSign BoundedSide(const Type::Sphere3 &sphere, - const Type::Point3 &pt); - -public: - static Type::Interval Merge(const Type::Interval &int_1, - const Type::Interval &int_2); - static Type::KDOP Merge(const Type::KDOP &kdop_1, const Type::KDOP &kdop_2); - - static void Merge(Type::Interval &int_r, const Type::Interval &int_1, - const Type::Interval &int_2); - static void Merge(Type::KDOP &kdop_r, const Type::KDOP &kdop_1, - const Type::KDOP &kdop_2); -}; - -typedef dtkGraphicsKernel GK; -}; // namespace dtk - -#endif // DTK_GRAPHICSKERNELCGAL_H diff --git a/src/include/dtkPhysMassSpring.h.bak b/src/include/dtkPhysMassSpring.h.bak deleted file mode 100644 index c8df523..0000000 --- a/src/include/dtkPhysMassSpring.h.bak +++ /dev/null @@ -1,219 +0,0 @@ -#ifndef DTK_PHYSMASSSPRING_H -#define DTK_PHYSMASSSPRING_H - -#ifdef DTK_CL -#include -#endif -#include "dtkPhysMassPoint.h" -#include "dtkPhysSpring.h" -#include "dtkPointsVector.h" -#include "dtkStaticTriangleMesh.h" -#include -#include -#include -#include - -using namespace std; - -#define NWITEMS 256 -namespace dtk { -class dtkPhysMassSpring : public boost::noncopyable { -public: - typedef std::shared_ptr Ptr; - - static Ptr - New(double defaultMass = 2, double defaultK = 2000, double defaultDamp = 5880, - double defaultPointDamp = 1.0, double defaultPointResistence = 2.5, - dtkDouble3 defaultGravityAccel = dtkDouble3( - 0, 0, 0)) // K = k0 * cross section area, damp is additional damp - { - return Ptr(new dtkPhysMassSpring(defaultMass, defaultK, defaultDamp, - defaultPointDamp, defaultPointResistence, - defaultGravityAccel)); - } - -public: - virtual ~dtkPhysMassSpring(); - dtkPhysMassSpring(double defaultMass, double defaultK, double defaultDamp, - double defaultPointDamp, - double defaultPointResistence = 2.5, - dtkDouble3 defaultGravityAccel = dtkDouble3(0, 0, 0)); - void SetPoints(dtkPoints::Ptr points); - dtkPoints::Ptr GetPoints(); - - const GK::Point3 &GetPoint(dtkID id) const; - void SetPoint(dtkID id, const GK::Point3 &coord); - - dtkPhysMassPoint *GetMassPoint(dtkID id) { return mMassPoints[id]; }; - dtkID GetNumberOfMassPoints() { return mMassPoints.size(); } - dtkID AddMassPoint(dtkID id, const double &mass = 1.0, - const dtkT3 &vel = dtkT3(0, 0, 0), - double pointDamp = 1.0, double pointResistence = 2.5, - dtkDouble3 defaultGravityAccel = dtkDouble3(0, 0, 0)); - - dtkPhysSpring *GetSpring(dtkID id) { return mSprings[id]; }; - dtkID GetNumberOfSprings() { return mSprings.size(); } - dtkID AddSpring(dtkID p1, dtkID p2, const double &stiff = 0, - const double &damp = 0); - void DeleteSpring(dtkID p1, dtkID p2); - - void SetSpringStiffness(int id, double newK); - void SetSpringDamp(int id, double newDamp); - void SetPointMass(int id, double newMass); - - bool Update(double timeslice, ItrMethod method = Euler, - bool limitDeformation = false); - bool Update_s(double timeslice, ItrMethod method = Euler, - bool limitDeformation = false); - virtual bool PreUpdate(double timeslice, ItrMethod method = Euler, - dtkID iteration = 0); - virtual bool PostUpdate(ItrMethod method = Euler, dtkID iteration = 0); - - bool Update_iteration(double timeslice, ItrMethod method = Euler, - dtkID iteration = 0, bool limitDeformation = false); - bool ApplyImpulse(double timeslice); - - bool UpdateStrings(double timeslice, ItrMethod method = Euler, - dtkID iteration = 0, bool limitDeformation = false); - bool UpdateMassPoints(double timeslice, ItrMethod method = Euler, - dtkID iteration = 0); - - void SetTriangleMesh(dtkStaticTriangleMesh::Ptr newTriangleMesh); - - size_t FindTwins(Ptr, double distance); - void AbandonTwins(); - - void RegisterLabel(dtkID label); - - void TransportForce(double timeslice); - - const dtkT3 &GetTransportForce(dtkID label) { - return mTransportForces[label]; - } - - void ConvertImpulseToForce(double timeslice); - - const dtkT3 &GetImpulseForce() { return mImpulseForce; } - - bool IsUnderControl() { return mUnderControl; } - - void SetUnderControl(bool underControl) { mUnderControl = underControl; } - - dtkPhysSpring *GetSpringByPoints(dtkID2); - -#ifdef DTK_CL - /** - * OpenCL related initialisations. - * Set up Context, Device list, Command Queue, Memory buffers - * Build CL kernel program executable - * @return 1 on success and 0 on failure - */ - bool Update_mt(double timeslice, ItrMethod method = Euler, - bool limitDeformation = false); - int SetupCL(); - int RunCLKernels(double timeslice, dtkID iteration); - bool Open(const char *fileName); - void InitialCLArray(); - void UpdateCopyToCLArray(); - void UpdateCopyBack(); - void CheckCLStatus(cl_int, std::string); - int WaitForEventAndRelease(cl_event *); - void UseMultiThread(const char *fileName); -#endif - -#ifdef DTK_DEBUG -public: - std::vector> mAltitudeSpringForces; - std::vector> mTotalSpringForces; -#endif - -protected: - dtkPoints::Ptr mPts; - std::vector mMassPoints; - std::vector mSprings; - - std::map - mEdgeMap; // map from spring specified by dtkID2 to spring id - - double mDefaultMass; - double mDefaultStiff; - double mDefaultDamp; - double mDefaultPointDamp; - double mDefaultPointResistence; - dtkDouble3 mDefaultGravityAccel; - - bool mUnderControl; - - std::vector mLabels; - std::map> mTransportForces; - dtkT3 mImpulseForce; - -#ifdef DTK_CL - bool mUseMultiThread; - // OpenCL - cl_float mTimesliceData; - cl_int mIterationData; - cl_int mNumberOfMassPoints; - - cl_int *mSpringVertexIDData; - cl_mem mSpringVertexIDBuffer; - cl_float *mSpringOriLengthData; - cl_mem mSpringOriLengthBuffer; - cl_float *mSpringStiffData; - cl_mem mSpringStiffBuffer; - cl_float *mSpringDampData; - cl_mem mSpringDampBuffer; - - cl_float *mMPPosData; - cl_mem mMPPosBuffer; - cl_float *mMPVelData; - cl_mem mMPVelBuffer; - cl_float *mMPAccelData; - cl_mem mMPAccelBuffer; - cl_float *mMPForceAccumData; - cl_mem mMPForceAccumBuffer; - cl_float *mMPGravityData; - cl_mem mMPGravityBuffer; - cl_float *mMPMassData; - cl_mem mMPMassBuffer; - cl_float *mMPPosBufferData; - cl_mem mMPPosBufferBuffer; - cl_float *mMPVelBufferData; - cl_mem mMPVelBufferBuffer; - cl_float *mMPAccelBufferData; - cl_mem mMPAccelBufferBuffer; - cl_float *mMPForceDecoratorData; - cl_mem mMPForceDecoratorBuffer; - cl_bool *mMPActiveData; - cl_mem mMPActiveBuffer; - - // new spring calculate - cl_int *mMPAdjacentVertexIDsData; - cl_mem mMPAdjacentVertexIDsBuffer; - cl_int *mMPNumberOfAdjacentVertexData; - cl_mem mMPNumberOfAdjacentVertexBuffer; - cl_int *mMPStartAdjacentVertexIDData; - cl_mem mMPStartAdjacentVertexIDBuffer; - - cl_int *mMPAdjacentSpringIDsData; - cl_mem mMPAdjacentSpringIDsBuffer; - - size_t maxWorkGroupSize; - size_t *maxWorkItemSizes; - - cl_context context; - cl_platform_id platform; - cl_device_id device; - - cl_command_queue queue; - cl_program program; - cl_kernel kernelSpring; - cl_kernel kernelMP; - size_t global_work_size; - size_t work_size; - std::string source_; -#endif -}; -} // namespace dtk - -#endif From dea02654d881a3b20bf762d7d1015317eef485b4 Mon Sep 17 00:00:00 2001 From: "Zone.N" Date: Fri, 10 Nov 2023 11:12:24 +0800 Subject: [PATCH 4/5] refactor: remove unused codes Signed-off-by: Zone.N --- src/include/{dtkArray.h => dtkArray_will_del.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/include/{dtkArray.h => dtkArray_will_del.h} (100%) diff --git a/src/include/dtkArray.h b/src/include/dtkArray_will_del.h similarity index 100% rename from src/include/dtkArray.h rename to src/include/dtkArray_will_del.h From aab61c9fb9e15dbc91ae079e94074dbe9fecc634 Mon Sep 17 00:00:00 2001 From: "Zone.N" Date: Fri, 10 Nov 2023 11:12:38 +0800 Subject: [PATCH 5/5] refactor: remove unused codes Signed-off-by: Zone.N --- src/include/dtk.h | 2 +- src/include/{dtkExports.h => dtkExports_will_del.h} | 0 src/include/{dtkImports.h => dtkImports_will_del.h} | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename src/include/{dtkExports.h => dtkExports_will_del.h} (100%) rename src/include/{dtkImports.h => dtkImports_will_del.h} (100%) diff --git a/src/include/dtk.h b/src/include/dtk.h index 7e6389f..092f0c2 100644 --- a/src/include/dtk.h +++ b/src/include/dtk.h @@ -175,7 +175,7 @@ #include "dtkUtility.h" // #include "dtkVolume.h" -#include "dtkArray.h" +#include "dtkArray_will_del.h" // Common #ifdef DTK_CUDA diff --git a/src/include/dtkExports.h b/src/include/dtkExports_will_del.h similarity index 100% rename from src/include/dtkExports.h rename to src/include/dtkExports_will_del.h diff --git a/src/include/dtkImports.h b/src/include/dtkImports_will_del.h similarity index 100% rename from src/include/dtkImports.h rename to src/include/dtkImports_will_del.h