From 75a7b791b39c8a19dc7cd513ea179c10fc64a30a Mon Sep 17 00:00:00 2001 From: forty-twoo <1013417276@qq.com> Date: Sat, 26 Nov 2022 13:53:41 +0800 Subject: [PATCH] add some files --- CMakeLists.txt | 4 ++- include/bvh.hpp | 22 ++++++++++---- include/mathdef.hpp | 4 --- src/bvh.cpp | 2 ++ src/intersection.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 2 +- 6 files changed, 90 insertions(+), 12 deletions(-) delete mode 100644 include/mathdef.hpp create mode 100644 src/intersection.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a67cd1..b594557 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,8 @@ file(GLOB_RECURSE HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp) file(GLOB_RECURSE THRDPARTY_FILES ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/*) + +#used for IDE, eg.Visual Studio source_group("Header Files" FILES ${HEADER_FILES}) source_group("3rdparty" FILES ${THRDPARTY_FILES}) @@ -32,4 +34,4 @@ source_group("3rdparty" FILES ${THRDPARTY_FILES}) add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEADER_FILES} ${THRDPARTY_FILES} ) -target_link_libraries(${PROJECT_NAME} PUBLIC igl::glfw cln ginac) +target_link_libraries(${PROJECT_NAME} PUBLIC igl::glfw) diff --git a/include/bvh.hpp b/include/bvh.hpp index df50ee9..cd42266 100644 --- a/include/bvh.hpp +++ b/include/bvh.hpp @@ -23,21 +23,31 @@ typedef struct BVH_AABB_Node AABB bound; //包围盒边界 std::vector> param; //此包围盒对应的每个参数的范围 std::vector childPtr; //把每个孩子结点指针存储到容器里 + BVH_AABB *myBVH; //指向自己的BVH结构 } BVH_AABB_Node, *BVH_AABB_NodePtr; class BVH_AABB { public: - unsigned int Hlevel; //递归层数 - unsigned int BranchNum; //每个结点的分支数 [BranchNum=2:曲线 || BranchNum=4:曲面] - double sampleNum; //采样点数--越大包围盒构造越准确 - BVH_AABB_NodePtr bvh_aabb_node = NULL; // bvh树的根指针 + unsigned int Hlevel; //递归层数 + unsigned int BranchNum; //每个结点的分支数 [BranchNum=2:曲线 || BranchNum=4:曲面] + tinynurbs::RationalCurve *NurbsCurvePtr = NULL; //指向该BVH对应的NurbsCurve的指针针 + tinynurbs::RationalSurface *NurbsSurfacePtr = NULL; //指向该BVH对应的NurbsSurface的指针针 + double sampleNum; //采样点数--越大包围盒构造越准确 + BVH_AABB_NodePtr bvh_aabb_node = NULL; // bvh树的根指针 public: - BVH_AABB(unsigned int Hlevel_, unsigned int BranchNum_, double sampleNum_) : Hlevel(Hlevel_), BranchNum(BranchNum_), sampleNum(sampleNum_) + BVH_AABB(unsigned int Hlevel_, unsigned int BranchNum_, double sampleNum_, tinynurbs::RationalCurve NurbsCurve_) : Hlevel(Hlevel_), BranchNum(BranchNum_), sampleNum(sampleNum_) { bvh_aabb_node = new BVH_AABB_Node; - std::cout << "BVH_AABB constructed correct!\n"; + NurbsCurvePtr = new tinynurbs::RationalCurve(NurbsCurve_); + std::cout << "BVH_AABB constructed correct, this is a bvh of NurbsCurve!\n"; + }; + BVH_AABB(unsigned int Hlevel_, unsigned int BranchNum_, double sampleNum_, tinynurbs::RationalSurface NurbsSurface_) : Hlevel(Hlevel_), BranchNum(BranchNum_), sampleNum(sampleNum_) + { + bvh_aabb_node = new BVH_AABB_Node; + NurbsSurfacePtr = new tinynurbs::RationalSurface(NurbsSurface_); + std::cout << "BVH_AABB constructed correct, this is a bvh of NurbsSurface!\n"; }; BVH_AABB_Node *GetNode() const { diff --git a/include/mathdef.hpp b/include/mathdef.hpp deleted file mode 100644 index a24d68e..0000000 --- a/include/mathdef.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef MATHDEF_H_ -#define MATHDEF_H_ - -#endif \ No newline at end of file diff --git a/src/bvh.cpp b/src/bvh.cpp index 5bc816d..f0042ea 100644 --- a/src/bvh.cpp +++ b/src/bvh.cpp @@ -28,6 +28,7 @@ AABB BVH_AABB::UpdateBound(const AABB &bound, const glm::vec3 &p) BVH_AABB_NodePtr BVH_AABB::Build_NurbsCurve(tinynurbs::RationalCurve &curve, BVH_AABB_NodePtr &curNode, int curLevel, double t, double tstep) { + curNode->myBVH = this; if (curLevel + 1 == Hlevel) { for (double i = t; i < t + tstep; i += tstep / sampleNum) @@ -58,6 +59,7 @@ BVH_AABB_NodePtr BVH_AABB::Build_NurbsCurve(tinynurbs::RationalCurve &cu } BVH_AABB_NodePtr BVH_AABB::Build_NurbsSurface(tinynurbs::RationalSurface &surface, BVH_AABB_NodePtr &curNode, int curLevel, double u, double v, double ustep, double vstep) { + curNode->myBVH = this; if (curLevel == Hlevel) { for (double i = u; i < u + ustep; i++) diff --git a/src/intersection.cpp b/src/intersection.cpp new file mode 100644 index 0000000..3ddb503 --- /dev/null +++ b/src/intersection.cpp @@ -0,0 +1,68 @@ +#include "bvh.cpp" +#include +#include + +bool CurveCurveBVHIntersect(BVH_AABB_NodePtr &BoxPtr1, BVH_AABB_NodePtr &BoxPtr2, std::vector> &IstNodePtr) +{ + AABB box1 = BoxPtr1->bound; + AABB box2 = BoxPtr2->bound; + + auto LineIntersect = [](double x1, double x2, double x1_, double x2_) + { + bool LIntersect = false; + if ((x2 <= x2_ && x2 >= x1_) || (x1_ <= x2 && x1_ >= x1)) + LIntersect = true; + return LIntersect; + }; + auto BoxIntersect = [&]() + { + double x1 = box1.Bmin.x, x1_ = box2.Bmin.x; + double x2 = box1.Bmax.x, x2_ = box2.Bmax.x; + double y1 = box1.Bmin.y, y1_ = box2.Bmin.y; + double y2 = box1.Bmax.y, y2_ = box2.Bmax.y; + double z1 = box1.Bmin.z, z1_ = box2.Bmin.z; + double z2 = box1.Bmax.z, z2_ = box2.Bmax.z; + if (LineIntersect(x1, x2, x1_, x2_) && LineIntersect(y1, y2, y1_, y2_) && LineIntersect(z1, z2, z1_, z2_)) + return true; + else + false; + }; + + int biggerbox = 1; + if (glm::distance(box1.Bmax, box1.Bmin) < glm::distance(box2.Bmax, box2.Bmin)) + biggerbox = 2; + + if (BoxIntersect()) + { + if (biggerbox == 1) + { + if (BoxPtr1->childPtr.size() == 0) + { + std::pair pi = std::make_pair(BoxPtr1, BoxPtr2); + IstNodePtr.push_back(pi); + return true; + } + for (auto it : BoxPtr1->childPtr) + { + CurveCurveBVHIntersect(it, BoxPtr2, IstNodePtr) + } + } + else + { + if (BoxPtr2->childPtr.size() == 0) + { + std::pair pi = std::make_pair(BoxPtr2, BoxPtr1); + IstNodePtr.push_back(pi); + return true; + } + for (auto it : BoxPtr2->childPtr) + { + CurveCurveBVHIntersect(BoxPtr1, it, IstNodePtr); + } + } + } + else + { + return false; + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7e84688..6b360f1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ int main(int argc, char *argv[]) crv.weights = {1, 1, 1, 1, 1}; ShowCurve_Igl(viewer, crv, 100); - BVH_AABB bvh_curve(8, 2, 100); + BVH_AABB bvh_curve(8, 2, 10, crv); double tstep = *(crv.knots.end() - 1); bvh_curve.Build_NurbsCurve(crv, bvh_curve.bvh_aabb_node, 0, 0, tstep); ShowBVHNode(viewer, bvh_curve.bvh_aabb_node);