Compare commits

...

3 Commits

  1. 4
      .gitignore
  2. 3
      .vscode/settings.json
  3. 59
      CMakeLists.txt
  4. 2
      include/device/Nurbs/bvh.cuh
  5. 57
      include/device/Nurbs/loop_detection.cuh
  6. 324
      include/device/Nurbs/nurbs_surface.cuh
  7. 6
      include/device/srf_mesh.cuh
  8. 8
      include/device/warmup.cuh
  9. 15
      include/utils.h
  10. 0
      readme.md
  11. 23
      src/device/Nurbs/bvh.cu
  12. 26
      src/device/Nurbs/loop_detection.cu
  13. 1
      src/device/Nurbs/nurbs_common.cu
  14. 2
      src/device/Nurbs/nurbs_curve.cu
  15. 1514
      src/device/Nurbs/nurbs_surface.cu
  16. 2
      src/device/device_utils.cu
  17. 22
      src/device/srf_mesh.cu
  18. 12
      src/device/warmup.cu
  19. 28
      src/utils.cpp
  20. 391
      tests/main.cpp
  21. 317
      tests/main_bak.cpp

4
.gitignore

@ -41,4 +41,6 @@
*.app
cmake-build-debug/
.idea/
build/
.idea/
.cache/

3
.vscode/settings.json

@ -0,0 +1,3 @@
{
"cmake.generator": "Ninja"
}

59
CMakeLists.txt

@ -1,47 +1,38 @@
cmake_minimum_required(VERSION 3.21)
project(NurbsPerformer LANGUAGES CXX CUDA)
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PROJECT_SOURCES
tests/main.cpp
src/utils.cpp include/utils.h
src/device/aabb.cu include/device/aabb.cuh
src/device/vec.cu include/device/vec.cuh
src/device/device_utils.cu include/device/device_utils.cuh
src/device/Nurbs/nurbs_common.cu include/device/Nurbs/nurbs_common.cuh
src/device/Nurbs/nurbs_surface.cu include/device/Nurbs/nurbs_surface.cuh
src/device/Nurbs/nurbs_curve.cu include/device/Nurbs/nurbs_curve.cuh
src/device/Nurbs/bvh.cu include/device/Nurbs/bvh.cuh
src/device/Nurbs/loop_detection.cu include/device/Nurbs/loop_detection.cuh src/device/srf_mesh.cu include/device/srf_mesh.cuh)
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler=\"/Zc:__cplusplus\"")
add_executable(NurbsPerformer ${PROJECT_SOURCES})
file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.cu")
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp" "include/*.cuh")
#add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
#add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
message(STATUS "Sources: ${SOURCES}")
message(STATUS "Headers: ${HEADERS}")
#
#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#
#add_library(NurbsPerformer ${PROJECT_SOURCES})
# add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
# add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
####### exe or lib #######
# CUDA_PATH
# linux
#include_directories("/usr/local/device-11.8/targets/x86_64-linux/include")
# windows
include_directories("$ENV{CUDA_PATH}/include")
include_directories("E:/CLib/glm")
include_directories(include)
include_directories("E:/CLib/tinynurbs/include")
add_executable(NurbsPerformer tests/main.cpp ${SOURCES} ${HEADERS})
#MESSAGE("CUDA PATH::: $ENV{CUDA_PATH}")
#MESSAGE("CUDA PATH::: $ENV{LD_LIBRARY_PATH}")
#MESSAGE("CUDA PATH::: $ENV{CPATH}")
#
# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# #
# add_library(NurbsPerformer ${SOURCES} ${HEADERS})
set_target_properties(NurbsPerformer PROPERTIES
CUDA_SEPARABLE_COMPILATION ON)
include_directories(include "$ENV{CUDA_PATH}/include" "E:/CLib/glm" "E:/CLib/tinynurbs/include")
set_target_properties(NurbsPerformer PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
# find_package(CUDA REQUIRED)
# target_link_libraries(NurbsPerformer ${CUDA_LIBRARIES})
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES 70 75 80)
endif(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES 70 75 80)
endif(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
target_link_libraries(NurbsPerformer ${CUDA_LIBRARIES})

2
include/device/Nurbs/bvh.cuh

@ -37,7 +37,7 @@ public:
};
__global__ void
g_buildBvh(const float *k, int level, const float *evaluatedPoints, float lastKnot_u, float lastKnot_v,
g_buildBvh(const float *k, int level, const glm::vec3 *evaluatedPoints, float lastKnot_u, float lastKnot_v,
int sampleCnt_u, int sampleCnt_v, BVHNode *BVH);
#endif //NURBSEVALUATOR_BVH_CUH

57
include/device/Nurbs/loop_detection.cuh

@ -4,39 +4,50 @@
#include "device/srf_mesh.cuh"
#include "map"
#include "set"
#include <cuda_runtime.h>
class LoopDetection {
public:
SrfMesh *d_s_srfMesh;
SrfMesh *d_f_srfMesh;
SrfMesh *d_s_srfMesh;
SrfMesh *d_f_srfMesh;
float s_paramRegionSize_u;
float s_paramRegionSize_v;
int edgeSampleCnt;
float s_paramRegionSize_u;
float s_paramRegionSize_v;
glm::vec<2, int> *d_selectedPointsIdx;
glm::vec2 *d_vectorFields;
int *d_rotationNumbers;
int edgeSampleCnt;
LoopDetection(glm::vec3 *d_s_eval, glm::vec3 *d_f_eval, glm::vec3 *d_s_tan_u,
glm::vec3 *d_f_tan_u, glm::vec3 *d_s_tan_v,
glm::vec3 *d_f_tan_v, glm::vec3 *d_s_norm, glm::vec3 *d_f_norm,
int edgeSampleCnt, float s_paramRegionSize_u,
float s_paramRegionSize_v);
glm::vec<2, int> *d_selectedPointsIdx;
glm::vec2 *d_vectorFields;
int *d_rotationNumbers;
// LoopDetection(SrfMesh *h_s_srfMesh, SrfMesh *h_f_srfMesh, int
// edgeSampleCnt, float s_paramRegionSize_u,
// float s_paramRegionSize_v);
LoopDetection(glm::vec3 *s_eval, glm::vec3 *f_eval, glm::vec3 *s_tan_u, glm::vec3 *f_tan_u, glm::vec3 *s_tan_v,
glm::vec3 *f_tan_v, glm::vec3 *s_norm, glm::vec3 *f_norm, int edgeSampleCnt,
float s_paramRegionSize_u, float s_paramRegionSize_v);
__host__ void h_initOrientedDisAndVecFields(
const std::map<std::pair<int, int>, std::set<std::pair<int, int>>>
&intersectBoxPairs);
__host__ void h_initOrientedDisAndVecFields(
const std::map<std::pair<int, int>, std::set<std::pair<int, int>>> &intersectBoxPairs);
__host__ void h_getRotationNumber();
__host__ void h_getRotationNumber();
~LoopDetection();
~LoopDetection();
};
__global__ void
g_initOrientedDisAndVecFields(SrfMesh *s_srfMesh, SrfMesh *f_srfMesh, int s_cellCnt, glm::vec<2, int> *s_cells,
glm::vec<2, int> **f_cells, const int *f_cellCnt, glm::vec<2, int> *selectedPointsIdx,
glm::vec2 *vectorFields);
__global__ void g_initOrientedDisAndVecFields(
SrfMesh *s_srfMesh, SrfMesh *f_srfMesh, int s_cellCnt,
glm::vec<2, int> *s_cells, glm::vec<2, int> **f_cells, const int *f_cellCnt,
glm::vec<2, int> *selectedPointsIdx, glm::vec2 *vectorFields);
__global__ void
g_getRotationNumber(SrfMesh *s_srfMesh, float paramRegionSize_u, float paramRegionSize_v, glm::vec2 *vectorFields,
int *rotationNumbers);
__global__ void g_getRotationNumber(SrfMesh *s_srfMesh, float paramRegionSize_u,
float paramRegionSize_v,
glm::vec2 *vectorFields,
int *rotationNumbers);
#endif //NURBSPERFORMER_LOOP_DETECTION_CUH
#endif // NURBSPERFORMER_LOOP_DETECTION_CUH

324
include/device/Nurbs/nurbs_surface.cuh

@ -5,10 +5,12 @@
#ifndef NURBSEVALUATOR_NURBS_SURFACE_CUH
#define NURBSEVALUATOR_NURBS_SURFACE_CUH
#include <vector>
#include "cuda_runtime.h"
#include "bvh.cuh"
#include "cuda_runtime.h"
#include "device/srf_mesh.cuh"
#include "glm/glm.hpp"
#include <vector>
typedef std::vector<glm::vec3> LinePoints3;
typedef std::vector<LinePoints3> MeshPoints3;
@ -17,149 +19,175 @@ typedef std::pair<int, int> idx2;
typedef std::pair<idx2, idx2> boxPair;
namespace NurbsSurface {
// const int POINT_SIZE = 4;
/**
* 曲线计算的核函数
* @param d_pointSize 点的大小(3: [x, y, z] | 4:[x, y, z, w])
*/
__global__ static void
g_evaluate(float *res, const float *d_nTexture_u, const float *d_nTexture_v, const float *d_points,
int d_pointsCnt_u, int d_pointsCnt_v, int d_pointSize, float d_lastKnot_u, float d_lastKnot_v,
int d_sampleCnt_u, int d_sampleCnt_v);
__global__ static void
g_derivative(float *derivatives, float *normals, const float *derTexture_u, const float *derTexture_v,
const float *nTexture_u, const float *nTexture_v, const float *d_points, int d_pointsCnt_u,
int d_pointsCnt_v, int d_pointSize, float d_lastKnot_u, float d_lastKnot_v, int d_sampleCnt_u,
int d_sampleCnt_v);
__global__ static void
g_curvature(const float *derivatives, int sampleCnt_u, int sampleCnt_v, float lastKnot_u, float lastKnot_v,
float *ms, float *k);
class Surface {
private:
MeshPoints4 controlPoints;
float *d_points;
std::vector<float> knots_u;
std::vector<float> knots_v;
float *d_knots_u;
float *d_knots_v;
bool recordTime;
float *d_nTexture_u; // u方向指向度为p时的device中的nurbs基函数矩阵
float *d_nTexture_v; // v方向指向度为p时的device中的nurbs基函数矩阵
float *d_nTexture1_u; // u方向指向度为p-1时的device中的nurbs基函数矩阵
float *d_nTexture1_v; // v方向指向度为p-1时的device中的nurbs基函数矩阵
float *d_evaluationRes; // evaluation结果
float *d_derivatives; // 一阶导计算结果
float *d_normals;
float *d_k; // 最大曲率
__host__ void recursiveGetRayBVHIntersection(glm::vec3 dir, glm::vec3 startPoint, int idx,
std::vector<BVHNode> &intersectionLeafNodes);
public:
float *h_evaluations;
float *h_derivatives;
float *h_normals;
BVH bvh;
BVH gauss_map;
/**
* 构造函数
* @param controlPoints 控制点矩阵[pointsCnt_u][pointsCnt_v][3]
* @param knots_u u方向knots
* @param knots_v v方向knots
*/
__host__ explicit Surface(MeshPoints4 controlPoints, std::vector<float> knots_u, std::vector<float> knots_v);
/**
* 供外部CPU程序使用的、负责调用gpu并行计算采样点xyz值的方法
* @param sampleCnt_u u方向采样数目
* @param sampleCnt_v v方向采样数目
* @return 由 map 组成的vector{<<u, v>, {x, y, z}>}
*/
__host__ std::vector<std::vector<glm::vec3>> getEvaluateVec(int sampleCnt_u, int sampleCnt_v) const;
__host__ std::vector<MeshPoints3> getDerivativeVec(int sampleCnt_u, int sampleCnt_v) const;
/**
* 供外部CPU程序使用的、负责调用gpu并行计算采样点xyz值的方法
* @param sampleCnt_u u方向采样数目
* @param sampleCnt_v v方向采样数目
* @return 表示为vertices形式的采样点值数组
*/
__host__ void evaluate(int sampleCnt_u, int sampleCnt_v);
/**
* 供外部CPU程序使用的、负责调用gpu并行计算切向量的方法
*/
__host__ void derivative(int sampleCnt_u, int sampleCnt_v);
/**
* 供外部CPU程序使用的、负责调用gpu并行计算二阶导的方法
*/
__host__ void curvature(int sampleCnt_u, int sampleCnt_v);
/**
* 供外部CPU程序使用的、负责调用gpu并行计算BVH的方法
*/
__host__ void buildBVH(int layerCnt, bool useK = false);
/**
* 供外部CPU程序使用的、负责调用gpu并行计算Gauss Map Tree的方法
*/
__host__ void buildGaussMap(int layerCnt);
/**
* 供外部CPU程序使用的、负责调用递归函数求解BVH与ray交点的方法
*/
__host__ std::vector<BVHNode> rayBVHIntersection(glm::vec3 dir, glm::vec3 startPoint);
void setRecordTime(bool r);
~Surface();
};
__host__ void recursiveGetOverlapLeafNodes(const BVH &bvh1, const BVH &bvh2, int idx1, int idx2,
std::vector<std::pair<int, int>> &pairs);
__host__ std::vector<boxPair> getOverlappedLeafNodes(const BVH &bvh1, const BVH &bvh2);
/**
* 判断两个曲面的Gauss Map在指定的、各自的参数范围内有没有交
* @param idxRange_u1 第一个gauss map的参数u范围对应的u方向上的采样网格的格子下标范围
* @param idxRange_v1 第一个gauss map的参数v范围对应的u方向上的采样网格的格子下标范围
* @param idxRange_u2 第而个gauss map的参数u范围对应的u方向上的采样网格的格子下标范围
* @param idxRange_v2 第二个gauss map的参数v范围对应的u方向上的采样网格的格子下标范围
* @return true:gauss map的包围盒有交集,说明gauss map<可能>有重合;false: gauss map一定没有重合
*/
__host__ bool isGaussMapsOverlapped(const BVH &gm1, const BVH &gm2, std::pair<int, int> idxRange_u1,
std::pair<int, int> idxRange_v1, std::pair<int, int> idxRange_u2,
std::pair<int, int> idxRange_v2);
/**
* 判断两个曲面的Gauss Map在指定的、各自的参数范围内有没有交
* @param range_u1 第一个gauss map的参数u范围
* @param range_v1 第一个gauss map的参数v范围
* @param range_u2 第二个gauss map的参数u范围
* @param range_v2 第二个gauss map的参数v范围
* @param paramRange_u1 第一个gauss map的参数u定义域
* @param paramRange_v1 第一个gauss map的参数v定义域
* @param paramRange_u2 第二个gauss map的参数u定义域
* @param paramRange_v2 第二个gauss map的参数v定义域
* @return true:gauss map的包围盒有交集,说明gauss map<可能>有重合;false: gauss map一定没有重合
*/
__host__ bool isGaussMapsOverlapped(const BVH &gm1, const BVH &gm2, std::pair<float, float> range_u1,
std::pair<float, float> range_v1, std::pair<float, float> range_u2,
std::pair<float, float> range_v2, std::pair<float, float> paramRange_u1,
std::pair<float, float> paramRange_v1,
std::pair<float, float> paramRange_u2,
std::pair<float, float> paramRange_v2);
}
#endif //NURBSEVALUATOR_NURBS_SURFACE_CUH
/**
* 曲线计算的核函数
* @param d_pointSize 点的大小(3: [x, y, z] | 4:[x, y, z, w])
*/
__global__ static void g_evaluate(glm::vec3 *res, const float *d_nTexture_u,
const float *d_nTexture_v,
const glm::vec4 *d_points, int d_pointsCnt_u,
int d_pointsCnt_v, float d_lastKnot_u,
float d_lastKnot_v, int d_sampleCnt_u,
int d_sampleCnt_v);
__global__ static void g_derivative(
glm::vec3 *derivatives_u, glm::vec3 *derivatives_v, glm::vec3 *normals,
const float *derTexture_u, const float *derTexture_v,
const float *nTexture_u, const float *nTexture_v, const glm::vec4 *d_points,
int d_pointsCnt_u, int d_pointsCnt_v, int d_sampleCnt_u, int d_sampleCnt_v);
__global__ static void g_curvature(const glm::vec3 *derivatives_u,
const glm::vec3 *derivatives_v,
int sampleCnt_u, int sampleCnt_v,
float lastKnot_u, float lastKnot_v,
float *ms, float *k);
class Surface {
private:
MeshPoints4 controlPoints;
glm::vec4 *d_points; // 控制点
std::vector<float> knots_u;
std::vector<float> knots_v;
float *d_knots_u;
float *d_knots_v;
bool recordTime;
int layerCnt, edgeSampleCnt_u, edgeSampleCnt_v;
float *d_nTexture_u; // u方向指向度为p时的device中的nurbs基函数矩阵
float *d_nTexture_v; // v方向指向度为p时的device中的nurbs基函数矩阵
float *d_nTexture1_u; // u方向指向度为p-1时的device中的nurbs基函数矩阵
float *d_nTexture1_v; // v方向指向度为p-1时的device中的nurbs基函数矩阵
glm::vec3 *d_evaluations; // evaluation结果
glm::vec3 *d_derivatives_u; // 一阶导计算结果
glm::vec3 *d_derivatives_v; // 一阶导计算结果
glm::vec3 *d_normals;
float *d_k; // 最大曲率
__host__ void
recursiveGetRayBVHIntersection(glm::vec3 dir, glm::vec3 startPoint, int idx,
std::vector<BVHNode> &intersectionLeafNodes);
public:
glm::vec3 *h_evaluations;
glm::vec3 *h_derivatives_u;
glm::vec3 *h_derivatives_v;
glm::vec3 *h_normals;
BVH bvh;
BVH gauss_map;
/**
* 构造函数
* @param controlPoints 控制点矩阵[pointsCnt_u][pointsCnt_v][3]
* @param knots_u u方向knots
* @param knots_v v方向knots
*/
__host__ explicit Surface(MeshPoints4 controlPoints,
std::vector<float> knots_u,
std::vector<float> knots_v, int layerCnt);
/**
* 供外部CPU程序使用的、负责调用gpu并行计算采样点xyz值的方法
* @param sampleCnt_u u方向采样数目
* @param sampleCnt_v v方向采样数目
* @return 由 map 组成的vector{<<u, v>, {x, y, z}>}
*/
__host__ std::vector<std::vector<glm::vec3>> getEvaluateVec() const;
__host__ std::vector<MeshPoints3> getDerivativeVec() const;
/**
* 供外部CPU程序使用的、负责调用gpu并行计算采样点xyz值的方法
* @param sampleCnt_u u方向采样数目
* @param sampleCnt_v v方向采样数目
* @return 表示为vertices形式的采样点值数组
*/
__host__ void evaluate();
/**
* 供外部CPU程序使用的、负责调用gpu并行计算切向量的方法
*/
__host__ void derivative();
/**
* 供外部CPU程序使用的、负责调用gpu并行计算二阶导的方法
*/
__host__ void curvature();
/**
* 供外部CPU程序使用的、负责调用gpu并行计算BVH的方法
*/
__host__ void buildBVH(bool useK = false);
/**
* 供外部CPU程序使用的、负责调用gpu并行计算Gauss Map Tree的方法
*/
__host__ void buildGaussMap();
/**
* 供外部CPU程序使用的、负责调用递归函数求解BVH与ray交点的方法
*/
__host__ std::vector<BVHNode> rayBVHIntersection(glm::vec3 dir,
glm::vec3 startPoint);
/**
* 获取曲面对应的Mesh对象,Mesh中的每一个成员都指向gpu
* @return
*/
__host__ SrfMesh *getDMesh();
void setRecordTime(bool r);
~Surface();
};
__host__ void
recursiveGetOverlapLeafNodes(const BVH &bvh1, const BVH &bvh2, int idx1,
int idx2, std::vector<std::pair<int, int>> &pairs);
__host__ std::vector<boxPair> getOverlappedLeafNodes(const BVH &bvh1,
const BVH &bvh2);
/**
* 判断两个曲面的Gauss Map在指定的、各自的参数范围内有没有交
* @param idxRange_u1 第一个gauss
* map的参数u范围对应的u方向上的采样网格的格子下标范围
* @param idxRange_v1 第一个gauss
* map的参数v范围对应的u方向上的采样网格的格子下标范围
* @param idxRange_u2 第而个gauss
* map的参数u范围对应的u方向上的采样网格的格子下标范围
* @param idxRange_v2 第二个gauss
* map的参数v范围对应的u方向上的采样网格的格子下标范围
* @return true:gauss map的包围盒有交集,说明gauss map<可能>有重合;false: gauss
* map一定没有重合
*/
__host__ bool isGaussMapsOverlapped(const BVH &gm1, const BVH &gm2,
std::pair<int, int> idxRange_u1,
std::pair<int, int> idxRange_v1,
std::pair<int, int> idxRange_u2,
std::pair<int, int> idxRange_v2);
/**
* 判断两个曲面的Gauss Map在指定的、各自的参数范围内有没有交
* @param range_u1 第一个gauss map的参数u范围
* @param range_v1 第一个gauss map的参数v范围
* @param range_u2 第二个gauss map的参数u范围
* @param range_v2 第二个gauss map的参数v范围
* @param paramRange_u1 第一个gauss map的参数u定义域
* @param paramRange_v1 第一个gauss map的参数v定义域
* @param paramRange_u2 第二个gauss map的参数u定义域
* @param paramRange_v2 第二个gauss map的参数v定义域
* @return true:gauss map的包围盒有交集,说明gauss map<可能>有重合;false: gauss
* map一定没有重合
*/
__host__ bool isGaussMapsOverlapped(const BVH &gm1, const BVH &gm2,
std::pair<float, float> range_u1,
std::pair<float, float> range_v1,
std::pair<float, float> range_u2,
std::pair<float, float> range_v2,
std::pair<float, float> paramRange_u1,
std::pair<float, float> paramRange_v1,
std::pair<float, float> paramRange_u2,
std::pair<float, float> paramRange_v2);
} // namespace NurbsSurface
#endif // NURBSEVALUATOR_NURBS_SURFACE_CUH

6
include/device/srf_mesh.cuh

@ -5,12 +5,14 @@
class SrfMesh {
public:
bool inDev; // 告诉SerMesh各个指针是指向CPU的还是GPU,true:GPU
int edgeSampleCnt;
glm::vec3 *evaluationRes;
glm::vec3 *evaluations;
glm::vec3 *tangent_u;
glm::vec3 *tangent_v;
glm::vec3 *normal;
SrfMesh(glm::vec3 *evalRes, glm::vec3 *tan_u, glm::vec3 *tan_v, glm::vec3 *nor, int edgeSampleCnt);
SrfMesh(bool inDev, int edgeSampleCnt, glm::vec3 *eval = nullptr, glm::vec3 *tan_u = nullptr, glm::vec3 *tan_v = nullptr, glm::vec3 *nor= nullptr);
~SrfMesh();
};

8
include/device/warmup.cuh

@ -0,0 +1,8 @@
#pragma once
#include <cuda_runtime.h>
#include <device_types.h>
__global__ void g_dummyKernel();
void gpuWarmUp();

15
include/utils.h

@ -19,14 +19,13 @@ double get_time();
/**
* free的时候不会出错free
*
*
* 1.
* 2.
*/
void safeCudaFree(float *&p);
void safeCudaFree(BVHNode *&p);
template<typename T>
void safeCudaFree(T *&p) {
inline void safeCudaFree(T *&p) {
if (p != nullptr) {
cudaFree(p);
p = nullptr;
@ -34,16 +33,12 @@ void safeCudaFree(T *&p) {
}
template<typename T>
void safeFree(T *&p) {
inline void safeFree(T *&p) {
if (p != nullptr) {
free(p);
p = nullptr;
}
}
void safeFree(float *&p);
void safeFree(BVHNode *&p);
#endif //UNTITLED1_UTILS_H

0
README.md → readme.md

23
src/device/Nurbs/bvh.cu

@ -45,7 +45,7 @@ __host__ int h_getChildNodeIdx(int ix, int iy){
}
__global__ void
g_buildBvh(const float *k, int level, const float *evaluatedPoints, float lastKnot_u, float lastKnot_v,
g_buildBvh(const float *k, int level, const glm::vec3 *evaluatedPoints, float lastKnot_u, float lastKnot_v,
int sampleCnt_u, int sampleCnt_v, BVHNode *BVH) {
// 假设还是二维grid和二维的block
int ix = blockIdx.x * blockDim.x + threadIdx.x;
@ -67,12 +67,10 @@ g_buildBvh(const float *k, int level, const float *evaluatedPoints, float lastKn
int baseIdx = getStartIdxOfLayerN(level); // 当前层的第一个节点的坐标
int idx = baseIdx + bias;
int tmpIdx = (ix * sampleCnt_v + iy) * 3;
int tmpIdx = ix * sampleCnt_v + iy;
// if(idx == 75) printf("75: %g, %g, %g\n", evaluatedPoints[tmpIdx], evaluatedPoints[tmpIdx + 1], evaluatedPoints[tmpIdx + 2]);
if (step == 1) {
AABB bound(glm::vec3(evaluatedPoints[tmpIdx], evaluatedPoints[tmpIdx + 1],
evaluatedPoints[tmpIdx + 2])); // 最初只包含u,v点
AABB bound(evaluatedPoints[tmpIdx]); // 最初只包含u,v点
// 叶子节点
BVH[idx].u0 = u;
BVH[idx].u1 = u + step_u * singleStepVal_u;
@ -81,15 +79,12 @@ g_buildBvh(const float *k, int level, const float *evaluatedPoints, float lastKn
BVH[idx].idx_u = ix;
BVH[idx].idx_v = iy;
BVH[idx].level = level;
int tmpIdx1 = ((ix + 1) * sampleCnt_v + iy) * 3;
int tmpIdx2 = (ix * sampleCnt_v + iy + 1) * 3;
int tmpIdx3 = ((ix + 1) * sampleCnt_v + iy + 1) * 3;
bound = bound.Union(glm::vec3(evaluatedPoints[tmpIdx1], evaluatedPoints[tmpIdx1 + 1],
evaluatedPoints[tmpIdx1 + 2]));
bound = bound.Union(glm::vec3(evaluatedPoints[tmpIdx2], evaluatedPoints[tmpIdx2 + 1],
evaluatedPoints[tmpIdx2 + 2]));
bound = bound.Union(glm::vec3(evaluatedPoints[tmpIdx3], evaluatedPoints[tmpIdx3 + 1],
evaluatedPoints[tmpIdx3 + 2]));
int tmpIdx1 = (ix + 1) * sampleCnt_v + iy;
int tmpIdx2 = ix * sampleCnt_v + iy + 1;
int tmpIdx3 = (ix + 1) * sampleCnt_v + iy + 1;
bound = bound.Union(evaluatedPoints[tmpIdx1]);
bound = bound.Union(evaluatedPoints[tmpIdx2]);
bound = bound.Union(evaluatedPoints[tmpIdx3]);
if(k != nullptr) bound.expand(*k);
BVH[idx].bounds = bound;
BVH[idx].firstChild = -1;

26
src/device/Nurbs/loop_detection.cu

@ -4,27 +4,26 @@
#include "cuda_runtime.h"
#include "device/device_utils.cuh"
LoopDetection::LoopDetection(glm::vec3 *const s_eval, glm::vec3 *const f_eval, glm::vec3 *const s_tan_u,
glm::vec3 *const f_tan_u, glm::vec3 *const s_tan_v, glm::vec3 *const f_tan_v,
glm::vec3 *const s_norm, glm::vec3 *const f_norm, int edgeSampleCnt,
LoopDetection::LoopDetection(glm::vec3 *const d_s_eval, glm::vec3 *const d_f_eval, glm::vec3 *const d_s_tan_u,
glm::vec3 *const d_f_tan_u, glm::vec3 *const d_s_tan_v, glm::vec3 *const d_f_tan_v,
glm::vec3 *const d_s_norm, glm::vec3 *const d_f_norm, int edgeSampleCnt,
float s_paramRegionSize_u, float s_paramRegionSize_v) :
edgeSampleCnt(edgeSampleCnt),
s_paramRegionSize_u(s_paramRegionSize_u),
s_paramRegionSize_v(s_paramRegionSize_v) {
auto initSrfMesh = [](SrfMesh *&d_srfMesh, glm::vec3 *eval, glm::vec3 *tan_u, glm::vec3 *tan_v, glm::vec3 *norm,
int edgeSampleCnt) {
auto h_srfMesh = new SrfMesh(eval, tan_u, tan_v, norm, edgeSampleCnt);
auto h_srfMesh = new SrfMesh(false, edgeSampleCnt, eval, tan_u, tan_v, norm);
safeCudaFree(d_srfMesh);
auto srfMeshSize = sizeof(SrfMesh);
cudaMalloc((void **) &d_srfMesh, srfMeshSize);
cudaMemcpy(d_srfMesh, h_srfMesh, srfMeshSize, cudaMemcpyHostToDevice);
safeFree(h_srfMesh);
};
initSrfMesh(d_s_srfMesh, s_eval, s_tan_u, s_tan_v, s_norm, edgeSampleCnt);
initSrfMesh(d_f_srfMesh, f_eval, f_tan_u, f_tan_v, f_norm, edgeSampleCnt);
initSrfMesh(d_s_srfMesh, d_s_eval, d_s_tan_u, d_s_tan_v, d_s_norm, edgeSampleCnt);
initSrfMesh(d_f_srfMesh, d_f_eval, d_f_tan_u, d_f_tan_v, d_f_norm, edgeSampleCnt);
}
LoopDetection::~LoopDetection() {
safeCudaFree(d_selectedPointsIdx);
safeCudaFree(d_vectorFields);
@ -42,11 +41,11 @@ __host__ void LoopDetection::h_initOrientedDisAndVecFields(
auto h_selectedPointsIdx = new glm::vec<2, int>[sampleCnt];
std::copy(selectedPointsIdxVec.begin(), selectedPointsIdxVec.end(), h_selectedPointsIdx);
int selectedPtsIdxBytes = sizeof(glm::vec<2, int>) * sampleCnt;
cudaMalloc((void**)&d_selectedPointsIdx, selectedPtsIdxBytes);
cudaMalloc((void **) &d_selectedPointsIdx, selectedPtsIdxBytes);
cudaMemcpy(d_selectedPointsIdx, h_selectedPointsIdx, selectedPtsIdxBytes, cudaMemcpyHostToDevice);
safeCudaFree(d_vectorFields);
cudaMalloc((void**)&d_vectorFields, sizeof(glm::vec2) * sampleCnt);
cudaMalloc((void **) &d_vectorFields, sizeof(glm::vec2) * sampleCnt);
int mapCnt = intersectBoxPairs.size();
@ -104,15 +103,16 @@ __host__ void LoopDetection::h_initOrientedDisAndVecFields(
__host__ void LoopDetection::h_getRotationNumber() {
safeCudaFree(d_rotationNumbers);
cudaMalloc((void**)&d_rotationNumbers, sizeof(int) * edgeSampleCnt * edgeSampleCnt);
cudaMalloc((void **) &d_rotationNumbers, sizeof(int) * edgeSampleCnt * edgeSampleCnt);
dim3 block(32, 32);
dim3 grid((edgeSampleCnt + block.x - 1) / block.x, (edgeSampleCnt + block.y - 1) / block.y);
g_getRotationNumber<<<grid, block>>>(d_s_srfMesh, s_paramRegionSize_u, s_paramRegionSize_v, d_vectorFields,
d_rotationNumbers);
d_rotationNumbers);
}
__device__ const int neighborIdxes[4][2] = {{1, 0},
{0, 1},
{-1, 0},
@ -157,8 +157,8 @@ g_initOrientedDisAndVecFields(SrfMesh *s_srfMesh, SrfMesh *f_srfMesh, int s_cell
if (fIdx_u < 0 || fIdx_u >= f_edgeSampleCnt || fIdx_v < 0 ||
fIdx_v >= f_edgeSampleCnt)
continue;
auto dis = glm::distance(s_srfMesh->evaluationRes[idxI][idxJ],
f_srfMesh->evaluationRes[fIdx_u][fIdx_v]);
auto dis = glm::distance(s_srfMesh->evaluations[idxI][idxJ],
f_srfMesh->evaluations[fIdx_u][fIdx_v]);
if (dis < minDis) {
minDis = dis;
minDisFIdx_u = fIdx_u;

1
src/device/Nurbs/nurbs_common.cu

@ -46,7 +46,6 @@ __global__ void g_basisTexture(float *nTexture, float *nTexture1, const float *d
for (int i = 0; i < d_pointsCnt; i++) {
nTexture[idx * d_pointsCnt + i] = N_dp[d_degree * (d_knotsCnt - 1) + i];
nTexture1[idx * (d_pointsCnt + 1) + i] = N_dp[(d_degree - 1) * (d_knotsCnt - 1) + i];
// if(idx == 3)printf("nTexture1: %g ", nTexture1[idx * (d_pointsCnt + 1) + i]);
}
nTexture1[idx * (d_pointsCnt + 1) + d_pointsCnt] = N_dp[(d_degree - 1) * (d_knotsCnt - 1) +
d_pointsCnt]; // nTexture1多记录一列数据

2
src/device/Nurbs/nurbs_curve.cu

@ -188,6 +188,8 @@ NurbsCurve::Curve::evaluate(int sampleCnt) {
double time_cost_device;
if (recordTime) time_cost_device = get_time();
printf("there..\n");
g_basisTexture <<<gridBasis, blockBasis>>>(d_nTexture, d_nTexture1, d_knots, pointsCnt, knotsCnt, sampleCnt);
// cudaMemcpy(d_nTextureCpy, d_nTexture, nTextureBytes, cudaMemcpyDeviceToDevice); // 有同步功能
cudaDeviceSynchronize();

1514
src/device/Nurbs/nurbs_surface.cu

File diff suppressed because it is too large

2
src/device/device_utils.cu

@ -12,7 +12,7 @@ __device__ void d_safeFree(float *&p) {
}
__device__ bool d_floatEqual(float a, float b) {
return abs(a - b) < 0.00001;
return abs(a - b) < 0.000001;
}
__device__ void normalization(float &a, float &b, float &c) {

22
src/device/srf_mesh.cu

@ -1,4 +1,22 @@
#include "device/srf_mesh.cuh"
#include "utils.h"
SrfMesh::SrfMesh(bool inDev, int edgeSampleCnt, glm::vec3 *eval, glm::vec3 *tan_u, glm::vec3 *tan_v, glm::vec3 *nor)
:
inDev(inDev), evaluations(eval), tangent_u(tan_u), tangent_v(tan_v), normal(nor),
edgeSampleCnt(edgeSampleCnt) {}
SrfMesh::~SrfMesh() {
if (inDev) {
safeCudaFree(evaluations);
safeCudaFree(tangent_u);
safeCudaFree(tangent_v);
safeCudaFree(normal);
} else {
safeFree(evaluations);
safeFree(tangent_u);
safeFree(tangent_v);
safeFree(normal);
}
}
SrfMesh::SrfMesh(glm::vec3 *evalRes, glm::vec3 *tan_u, glm::vec3 *tan_v, glm::vec3 *nor, int edgeSampleCnt) :
evaluationRes(evalRes), tangent_u(tan_u), tangent_v(tan_v), normal(nor), edgeSampleCnt(edgeSampleCnt) {}

12
src/device/warmup.cu

@ -0,0 +1,12 @@
#pragma once
#include "device/warmup.cuh"
__global__ void g_dummyKernel() {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
}
void gpuWarmUp() {
g_dummyKernel<<<1, 1>>>();
cudaDeviceSynchronize();
}

28
src/utils.cpp

@ -30,31 +30,3 @@ double get_time() {
return t;
}
#endif
void safeCudaFree(float *&p) {
if (p != nullptr) {
cudaFree(p);
p = nullptr;
}
}
void safeCudaFree(BVHNode *&p){
if (p != nullptr) {
cudaFree(p);
p = nullptr;
}
}
//template<typename T>
void safeFree(float *&p) {
if (p != nullptr) {
free(p);
p = nullptr;
}
}
void safeFree(BVHNode *&p) {
if (p != nullptr) {
free(p);
p = nullptr;
}
}

391
tests/main.cpp

@ -1,97 +1,316 @@
#include <cstdio>
#include "device/Nurbs/loop_detection.cuh"
#include "device/Nurbs/nurbs_curve.cuh"
#include "device/Nurbs/nurbs_surface.cuh"
#include "device/Nurbs/loop_detection.cuh"
#include "device/warmup.cuh"
#include <cstdio>
#include <iostream>
int main() {
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{-1, 0, 0, 0.3}, {0, 1, 6, 0.8}, {1, 0, 4, 0.5}, {2, 0.5, 3, 0.8}, {3, 3, 1, 0.6}, {4, -5, 0, 0.7}},
// {{-2, 1, 1.2, 0.2}, {1, 2, 3, 0.3}, {2, 2, 3, 0.6}, {-1, -0.3, 2, 0.4}, {-1, 2, 0, 0.9}, {7, -8, 2, 0.3}},
// {{-3.4, 2, 3, 0.8}, {2, 3, 0, 0.6}, {4, 3, 7, 0.3}, {-2, 0, -0.2, 0.4}, {1, 1.7, 5, 0.6}, {9, -10.3, 6, 0.7}},
// {{-1.5, 3.2, 1, 0.5}, {2.6, 7, -2, 0.7}, {5, 0.8, 4.2, 0.8}, {-4, 1, 4, 0.7}, {2.1, 4, -2, 0.3}, {11, -6, 4, 0.6}},
// {{-0.2, 2, 0, 0.7}, {5, 3, 2, 0.4}, {5, 1.5, 1.4, 0.6}, {-3, 2, 5, 0.8}, {0.8, 1.3, 0, 0.5}, {15, -2, 0.9, 0.6}},
// {{3, 1.4, -1, 0.4}, {6, 2, 4, 0.6}, {-1, 0, -2, 0.4}, {0, 2.8, 2, 0.6}, {-0.5, 2, 1.2, 0.9}, {7, -3, -2, 0.3}},},
// {0, 0, 0, 0.1, 0.5, 0.8, 1, 1, 1},
// {0, 0, 0, 0.2, 0.7, 0.8, 1, 1, 1});
// NurbsSurface::Evaluator nurbsSurfaceEvaluator({
// {{-1, 0, 0, 1}, {0, 1, 6, 1}, {1, 0, 4, 1}, {2, 0.5, 3, 1}, {3, 3, 1, 1}, {4, -5, 0, 1}},
// {{-2, 1, 1.2, 1}, {1, 2, 3, 1}, {2, 2, 3, 1}, {-1, -0.3, 2, 1}, {-1, 2, 0, 1}, {7, -8, 2, 1}},
// {{-3.4, 2, 3, 1}, {2, 3, 0, 1}, {4, 3, 7, 1}, {-2, 0, -0.2, 1}, {1, 1.7, 5, 1}, {9, -10.3, 6, 1}},
// {{-1.5, 3.2, 1, 1}, {2.6, 7, -2, 1}, {5, 0.8, 4.2, 1}, {-4, 1, 4, 1}, {2.1, 4, -2, 1}, {11, -6, 4, 1}},
// {{-0.2, 2, 0, 1}, {5, 3, 2, 1}, {5, 1.5, 1.4, 1}, {-3, 2, 5, 1}, {0.8, 1.3, 0, 1}, {15, -2, 0.9, 1}},
// {{3, 1.4, -1, 1}, {6, 2, 4, 1}, {-1, 0, -2, 1}, {0, 2.8, 2, 1}, {-0.5, 2, 1.2, 1}, {7, -3, -2, 1}},},
// {0, 0, 0, 0.1, 0.5, 0.8, 1, 1, 1},
// {0, 0, 0, 0.2, 0.7, 0.8, 1, 1, 1});
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{-0.57, -0.48, -0.31, 1}, {-0.17, -0.49, -0.43, 1}, {0.3, -0.53, -0.27, 1}, {0.66, -0.42, -0.36, 1}},
// {{-0.53, -0.15, -0.03, 1}, {-0.21, -0.12, 0.09, 1}, {0.10, -0.18, 0.03, 1}, {0.49, -0.15, -0.03, 1}},
// {{-0.61, 0.22, 0.09, 1}, {-0.21, 0.20, 0.09, 1}, {0.13, 0.20, 0.12, 1}, {0.49, 0.19, -0.03, 1}},
// {{-0.52, 0.51, 0.13, 1}, {-0.20, 0.32, 0.36, 1}, {0.18, 0.29, 0.28, 1}, {0.51, 0.53, 0.12, 1}}
// }, {0, 0, 0, 0, 1, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 1, 1});
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{2, -2, -2, 1}, {-2.5, -2.2, -1.5, 3}, {-2, -2, -0.5, 4}, {-2, -2, 1.5, 2}},
// {{2, -1, -2, 2}, {-2.5, -1.2, -1.5, 1}, {-2, -1, -0.5, 1}, {-2, -1, 1.5, 1}},
// {{3, 1.2, -2, 9}, {-2.5, 1.2, -1.5, 1}, {-2, 1.5, -0.5, 7}, {-2, 1.5, 1.5, 4}},
// {{2, 2, -2, 1}, {-2.5, 2, -1.5, 1}, {-2, 2.5, -0.5, 7}, {-2, 2, 1.5, 1}}
// }, {0, 0, 0, 0, 1, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 1, 1});
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{-0.5, -0.5, -0.2, 1}, {-0.2, -0.5, -0.1, 1}, {0.2, -0.5, -0.1, 1}, {0.5, -0.5, -0.2, 1}},
// {{-0.5, -0.2, -0.1, 1}, {-0.2, -0.2, 0., 1}, {0.2, -0.2, 0., 1}, {0.5, -0.2, -0.1, 1}},
// {{-0.5, 0.2, -0.1, 1}, {-0.2, 0.2, 0., 1}, {0.2, 0.2, 0., 1}, {0.5, 0.2, -0.1, 1}},
// {{-0.5, 0.5, -0.2, 1}, {-0.2, 0.5, -0.1, 1}, {0.2, 0.5, -0.1, 1}, {0.5, 0.5, -0.2, 1}}
// }, {0, 0, 0, 0, 1, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 1, 1});
NurbsSurface::Surface nurbsSurfaceEvaluator({
{{0, 0, 0.2, 1.1}, {0, 0.2, 0.3, 0.9}, {0, 0.4, 0.25, 1.1}, {0, 0.6, 0.12, 0.9}, {0, 0.8, 0.22, 1}, {0, 1, 0.12, 1}},
{{0.2, 0, 0.22, 1}, {0.2, 0.2, -0.3, 1}, {0.2, 0.4, 0.4, 1}, {0.2, 0.6, -0.1, 1.1}, {0.2, 0.8, -0.1, 1}, {0.2, 1, 0.2, 0.9}},
{{0.4, 0, 0.18, 0.9}, {0.4, 0.2, -0.3, 1}, {0.4, 0.4, 0.4, 1.1}, {0.4, 0.6, -0.1, 1}, {0.4, 0.8, -0.1, 0.9}, {0.4, 1, 0.23, 1}},
{{0.6, 0, 0.2, 0.9}, {0.6, 0.2, 0.4, 0.9}, {0.6, 0.4, 0.4, 1}, {0.6, 0.6, 0.4, 0.9}, {0.6, 0.8, 0.4, 1}, {0.6, 1, 0.2, 1}},
{{0.8, 0, 0.19, 1.1}, {0.8, 0.2, -0.4, 1}, {0.8, 0.4, 0.4, 1.1}, {0.8, 0.6, -0.27, 1}, {0.8, 0.8, -0.27, 1}, {0.8, 1, 0.2, 0.9}},
{{1, 0, 0.2, 1.1}, {1, 0.2, 0.12, 1}, {1, 0.4, 0.22, 1}, {1, 0.6, 0.32, 1}, {1, 0.8, 0.22, 1.2}, {1, 1, 0.12, 1.1}}
}, {0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 1, 1, 1, 1, 1});
nurbsSurfaceEvaluator.setRecordTime(true);
// nurbsSurfaceEvaluator.buildGaussMap(6); // 构建曲面的Gauss map
// nurbsSurfaceEvaluator.gauss_map.printQuadTree(); // 打印整棵Gauss map树
// auto overlappedLeafNodes = NurbsSurface::getOverlappedLeafNodes(nurbsSurfaceEvaluator.gauss_map,
// nurbsSurfaceEvaluator.gauss_map);
// printf("overlapped leafNodes cnt: %llu\n", overlappedLeafNodes.size());
//判定两个Surface构造的GaussMap在指定u、v范围内是否有重合
// auto isRegionallyOverlapped = NurbsSurface::isGaussMapsOverlapped(nurbsSurfaceEvaluator.gauss_map,
// nurbsSurfaceEvaluator.gauss_map, {0.2, 0.3},
// {0.2, 0.3}, {0.28, 0.38}, {0.28, 0.38}, {0, 1},
// {0, 1}, {0, 1}, {0, 1});
// printf("is guass map overlapped: %d\n", isRegionallyOverlapped);
int level = 8;
int edgeSampleCnt = pow(2, level - 1) + 1;
nurbsSurfaceEvaluator.evaluate(edgeSampleCnt, edgeSampleCnt);
nurbsSurfaceEvaluator.derivative(edgeSampleCnt, edgeSampleCnt);
// 如果buildBVH需要曲率K,这里需要再先: nurbsSurfaceEvaluator.curvature(edgeSampleCnt, edgeSampleCnt);
// 同时buildBVH传入第二个参数
nurbsSurfaceEvaluator.buildBVH(level); // 构建曲面的BVH
auto derInfo = nurbsSurfaceEvaluator.getDerivativeVec(edgeSampleCnt, edgeSampleCnt); // 求值
int level = 10;
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{-1, 0, 0,
// 0.3}, {0, 1,
// 6, 0.8}, {1, 0,
// 4, 0.5}, {2,
// 0.5, 3, 0.8}, {3,
// 3, 1, 0.6}, {4,
// -5, 0, 0.7}},
// {{-2, 1, 1.2,
// 0.2}, {1, 2, 3,
// 0.3}, {2, 2, 3,
// 0.6}, {-1, -0.3,
// 2, 0.4}, {-1,
// 2, 0, 0.9}, {7,
// -8, 2, 0.3}},
// {{-3.4, 2, 3,
// 0.8}, {2, 3,
// 0, 0.6}, {4, 3,
// 7, 0.3}, {-2, 0,
// -0.2, 0.4},
// {1, 1.7, 5,
// 0.6}, {9, -10.3,
// 6, 0.7}},
// {{-1.5, 3.2, 1,
// 0.5}, {2.6, 7,
// -2, 0.7}, {5,
// 0.8, 4.2, 0.8},
// {-4, 1, 4,
// 0.7}, {2.1, 4,
// -2, 0.3}, {11,
// -6, 4, 0.6}},
// {{-0.2, 2, 0,
// 0.7}, {5, 3,
// 2, 0.4},
// {5, 1.5, 1.4,
// 0.6}, {-3, 2, 5,
// 0.8}, {0.8, 1.3,
// 0, 0.5}, {15,
// -2, 0.9, 0.6}},
// {{3, 1.4, -1,
// 0.4}, {6, 2, 4,
// 0.6}, {-1, 0, -2,
// 0.4}, {0, 2.8,
// 2, 0.6}, {-0.5,
// 2, 1.2, 0.9},
// {7, -3, -2,
// 0.3}},},
// {0, 0, 0, 0.1, 0.5, 0.8,
// 1, 1, 1}, {0, 0, 0, 0.2,
// 0.7, 0.8, 1, 1, 1});
// NurbsSurface::Evaluator nurbsSurfaceEvaluator({
// {{-1, 0, 0,
// 1}, {0, 1, 6,
// 1}, {1, 0, 4,
// 1}, {2, 0.5, 3,
// 1}, {3, 3,
// 1, 1}, {4, -5,
// 0, 1}},
// {{-2, 1, 1.2,
// 1}, {1, 2, 3,
// 1}, {2, 2, 3,
// 1}, {-1, -0.3,
// 2, 1}, {-1, 2,
// 0, 1}, {7, -8,
// 2, 1}},
// {{-3.4, 2, 3,
// 1}, {2, 3, 0,
// 1}, {4, 3, 7,
// 1}, {-2, 0,
// -0.2, 1},
// {1, 1.7, 5, 1},
// {9, -10.3, 6,
// 1}},
// {{-1.5, 3.2, 1,
// 1}, {2.6, 7, -2,
// 1}, {5, 0.8, 4.2,
// 1}, {-4, 1, 4,
// 1}, {2.1, 4,
// -2, 1}, {11, -6,
// 4, 1}},
// {{-0.2, 2, 0,
// 1}, {5, 3, 2,
// 1}, {5, 1.5, 1.4,
// 1}, {-3, 2, 5,
// 1}, {0.8, 1.3,
// 0, 1}, {15, -2,
// 0.9, 1}},
// {{3, 1.4, -1,
// 1}, {6, 2, 4,
// 1}, {-1, 0, -2,
// 1}, {0, 2.8, 2,
// 1}, {-0.5,
// 2, 1.2, 1}, {7,
// -3, -2, 1}},},
// {0, 0, 0, 0.1, 0.5, 0.8,
// 1, 1, 1}, {0, 0, 0, 0.2,
// 0.7, 0.8, 1, 1, 1});
printf("==============================\n");
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{-0.57, -0.48,
// -0.31, 1}, {-0.17,
// -0.49, -0.43, 1},
// {0.3, -0.53, -0.27,
// 1}, {0.66, -0.42,
// -0.36, 1}},
// {{-0.53, -0.15,
// -0.03, 1}, {-0.21,
// -0.12, 0.09, 1},
// {0.10, -0.18, 0.03,
// 1}, {0.49, -0.15,
// -0.03, 1}},
// {{-0.61, 0.22, 0.09,
// 1}, {-0.21, 0.20,
// 0.09, 1}, {0.13,
// 0.20, 0.12, 1},
// {0.49, 0.19, -0.03,
// 1}},
// {{-0.52, 0.51, 0.13,
// 1}, {-0.20, 0.32,
// 0.36, 1}, {0.18,
// 0.29, 0.28, 1},
// {0.51, 0.53, 0.12,
// 1}}
// }, {0, 0, 0, 0, 1, 1, 1, 1},
// {0, 0, 0, 0, 1, 1, 1, 1});
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{2, -2, -2, 1},
// {-2.5, -2.2, -1.5,
// 3}, {-2, -2, -0.5,
// 4}, {-2, -2, 1.5,
// 2}},
// {{2, -1, -2, 2},
// {-2.5, -1.2, -1.5,
// 1}, {-2, -1, -0.5,
// 1}, {-2, -1, 1.5,
// 1}},
// {{3, 1.2, -2, 9},
// {-2.5, 1.2, -1.5,
// 1}, {-2, 1.5, -0.5,
// 7}, {-2, 1.5, 1.5,
// 4}},
// {{2, 2, -2, 1},
// {-2.5, 2, -1.5,
// 1}, {-2, 2.5, -0.5,
// 7}, {-2, 2, 1.5,
// 1}}
// }, {0, 0, 0, 0, 1, 1, 1, 1},
// {0, 0, 0, 0, 1, 1, 1, 1});
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{-0.5, -0.5,
// -0.2, 1},
// {-0.2, -0.5,
// -0.1, 1},
// {0.2, -0.5,
// -0.1, 1},
// {0.5, -0.5,
// -0.2, 1}},
// {{-0.5, -0.2,
// -0.1, 1},
// {-0.2, -0.2,
// 0., 1}, {0.2,
// -0.2, 0., 1},
// {0.5, -0.2,
// -0.1, 1}},
// {{-0.5, 0.2,
// -0.1, 1},
// {-0.2, 0.2,
// 0., 1}, {0.2,
// 0.2, 0., 1},
// {0.5, 0.2,
// -0.1, 1}},
// {{-0.5, 0.5,
// -0.2, 1},
// {-0.2, 0.5,
// -0.1, 1},
// {0.2, 0.5,
// -0.1, 1},
// {0.5, 0.5,
// -0.2, 1}}
// }, {0, 0, 0, 0, 1, 1,
// 1, 1}, {0, 0, 0, 0,
// 1, 1, 1, 1});
NurbsSurface::Surface nurbsSurfaceEvaluator(
{{{0, 0, 0.2, 1.1},
{0, 0.2, 0.3, 0.9},
{0, 0.4, 0.25, 1.1},
{0, 0.6, 0.12, 0.9},
{0, 0.8, 0.22, 1},
{0, 1, 0.12, 1}},
{{0.2, 0, 0.22, 1},
{0.2, 0.2, -0.3, 1},
{0.2, 0.4, 0.4, 1},
{0.2, 0.6, -0.1, 1.1},
{0.2, 0.8, -0.1, 1},
{0.2, 1, 0.2, 0.9}},
{{0.4, 0, 0.18, 0.9},
{0.4, 0.2, -0.3, 1},
{0.4, 0.4, 0.4, 1.1},
{0.4, 0.6, -0.1, 1},
{0.4, 0.8, -0.1, 0.9},
{0.4, 1, 0.23, 1}},
{{0.6, 0, 0.2, 0.9},
{0.6, 0.2, 0.4, 0.9},
{0.6, 0.4, 0.4, 1},
{0.6, 0.6, 0.4, 0.9},
{0.6, 0.8, 0.4, 1},
{0.6, 1, 0.2, 1}},
{{0.8, 0, 0.19, 1.1},
{0.8, 0.2, -0.4, 1},
{0.8, 0.4, 0.4, 1.1},
{0.8, 0.6, -0.27, 1},
{0.8, 0.8, -0.27, 1},
{0.8, 1, 0.2, 0.9}},
{{1, 0, 0.2, 1.1},
{1, 0.2, 0.12, 1},
{1, 0.4, 0.22, 1},
{1, 0.6, 0.32, 1},
{1, 0.8, 0.22, 1.2},
{1, 1, 0.12, 1.1}}},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, level);
nurbsSurfaceEvaluator.setRecordTime(true);
NurbsCurve::Curve nurbsCurveEvaluator(
{{-1, 0, 0, 0.3},
{0, 1, 6, 0.4},
{1, 0, 4, 0.5},
{2, 0.5, 3, 0.4},
{3, 3, 1, 0.5},
{4, -5, 0, 0.7}},
{0, 0, 0, 0.1, 0.5, 0.8, 1, 1, 1});
nurbsCurveEvaluator.setRecordTime(true);
// nurbsSurfaceEvaluator.buildGaussMap(6); // 构建曲面的Gauss map
// nurbsSurfaceEvaluator.gauss_map.printQuadTree(); // 打印整棵Gauss map树
// auto overlappedLeafNodes =
// NurbsSurface::getOverlappedLeafNodes(nurbsSurfaceEvaluator.gauss_map,
// nurbsSurfaceEvaluator.gauss_map);
// printf("overlapped leafNodes cnt: %llu\n", overlappedLeafNodes.size());
// 判定两个Surface构造的GaussMap在指定u、v范围内是否有重合
// auto isRegionallyOverlapped =
// NurbsSurface::isGaussMapsOverlapped(nurbsSurfaceEvaluator.gauss_map,
// nurbsSurfaceEvaluator.gauss_map,
// {0.2,
// 0.3},
// {0.2,
// 0.3},
// {0.28,
// 0.38},
// {0.28,
// 0.38},
// {0,
// 1},
// {0,
// 1},
// {0,
// 1},
// {0,
// 1});
// printf("is guass map overlapped: %d\n", isRegionallyOverlapped);
gpuWarmUp();
auto res = nurbsCurveEvaluator.evaluate(11);
for (auto &re: res) {
printf("%f, %f, %f\n", re[0], re[1], re[2]);
}
printf("\n");
int edgeSampleCnt = pow(2, level - 1) + 1;
nurbsSurfaceEvaluator.evaluate();
nurbsSurfaceEvaluator.derivative();
// 如果buildBVH需要曲率K,这里需要再先:
// nurbsSurfaceEvaluator.curvature(edgeSampleCnt, edgeSampleCnt);
// 同时buildBVH传入第二个参数
nurbsSurfaceEvaluator.buildBVH(false); // 构建曲面的BVH
auto derInfo = nurbsSurfaceEvaluator.getDerivativeVec(); // 求值
return 0;
printf("==============================\n");
//
// NurbsCurve::Curve nurbsCurveEvaluator(
// {{0.721068, 0, 0, 1},
// {1.05152, 0, 0, 1},
// {0.346353, 0, 0, 1},
// {0.351813, 0, 0, 1},
// {0.375972, 0, 0, 1},
// {0.363015, 0, 0, 1},
// {0.368758, 0, 0, 1},
// {0.368222, 0, 0, 1},
// {0.371302, 0, 0, 1},
// {0.374657, 0, 0, 1},
// {0.377866, 0, 0, 1},
// {0.380762, 0, 0, 1},
// {0.382451, 0, 0, 1},
// {0.383466, 0, 0, 1},
// {0.383948, 0, 0, 1},
// {0.384242, 0, 0, 1},
// {0.384156, 0, 0, 1},
// {0.383659, 0, 0, 1},
// {0.382616, 0, 0, 1},
// {0.381428, 0, 0, 1},
// {0.379223, 0, 0, 1},
// {0.379492, 0, 0, 1},
// {0.372487, 0, 0, 1},
// {0.384305, 0, 0, 1},
// {0.357789, 0, 0, 1},
// {0.34936, 0, 0, 1},
// {1.0537, 0, 0, 1},
// {0.717109, 0, 0, 1}},
// {0, 0, 0, 0, 0.103448, 0.137931, 0.172414, 0.206897, 0.241379,
// 0.275862, 0.310345, 0.344828, 0.37931, 0.413793, 0.448276,
// 0.482759, 0.517241, 0.551724, 0.586207, 0.62069, 0.655172,
// 0.689655, 0.724138, 0.758621, 0.793103, 0.827586, 0.862069,
// 0.896552, 1, 1, 1, 1});
// nurbsCurveEvaluator.setRecordTime(true);
//
// auto res = nurbsCurveEvaluator.evaluate(30);
// for (auto &re: res) {
// printf("%f, %f, %f\n", re[0], re[1], re[2]);
// }
// printf("\n");
return 0;
}

317
tests/main_bak.cpp

@ -0,0 +1,317 @@
#include "device/Nurbs/loop_detection.cuh"
#include "device/Nurbs/nurbs_curve.cuh"
#include "device/Nurbs/nurbs_surface.cuh"
#include <cstdio>
#include <iostream>
void test();
int main() {
int level = 5;
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{-1, 0, 0,
// 0.3}, {0, 1,
// 6, 0.8}, {1, 0,
// 4, 0.5}, {2,
// 0.5, 3, 0.8}, {3,
// 3, 1, 0.6}, {4,
// -5, 0, 0.7}},
// {{-2, 1, 1.2,
// 0.2}, {1, 2, 3,
// 0.3}, {2, 2, 3,
// 0.6}, {-1, -0.3,
// 2, 0.4}, {-1,
// 2, 0, 0.9}, {7,
// -8, 2, 0.3}},
// {{-3.4, 2, 3,
// 0.8}, {2, 3,
// 0, 0.6}, {4, 3,
// 7, 0.3}, {-2, 0,
// -0.2, 0.4},
// {1, 1.7, 5,
// 0.6}, {9, -10.3,
// 6, 0.7}},
// {{-1.5, 3.2, 1,
// 0.5}, {2.6, 7,
// -2, 0.7}, {5,
// 0.8, 4.2, 0.8},
// {-4, 1, 4,
// 0.7}, {2.1, 4,
// -2, 0.3}, {11,
// -6, 4, 0.6}},
// {{-0.2, 2, 0,
// 0.7}, {5, 3,
// 2, 0.4},
// {5, 1.5, 1.4,
// 0.6}, {-3, 2, 5,
// 0.8}, {0.8, 1.3,
// 0, 0.5}, {15,
// -2, 0.9, 0.6}},
// {{3, 1.4, -1,
// 0.4}, {6, 2, 4,
// 0.6}, {-1, 0, -2,
// 0.4}, {0, 2.8,
// 2, 0.6}, {-0.5,
// 2, 1.2, 0.9},
// {7, -3, -2,
// 0.3}},},
// {0, 0, 0, 0.1, 0.5, 0.8,
// 1, 1, 1}, {0, 0, 0, 0.2,
// 0.7, 0.8, 1, 1, 1});
// NurbsSurface::Evaluator nurbsSurfaceEvaluator({
// {{-1, 0, 0,
// 1}, {0, 1, 6,
// 1}, {1, 0, 4,
// 1}, {2, 0.5, 3,
// 1}, {3, 3,
// 1, 1}, {4, -5,
// 0, 1}},
// {{-2, 1, 1.2,
// 1}, {1, 2, 3,
// 1}, {2, 2, 3,
// 1}, {-1, -0.3,
// 2, 1}, {-1, 2,
// 0, 1}, {7, -8,
// 2, 1}},
// {{-3.4, 2, 3,
// 1}, {2, 3, 0,
// 1}, {4, 3, 7,
// 1}, {-2, 0,
// -0.2, 1},
// {1, 1.7, 5, 1},
// {9, -10.3, 6,
// 1}},
// {{-1.5, 3.2, 1,
// 1}, {2.6, 7, -2,
// 1}, {5, 0.8, 4.2,
// 1}, {-4, 1, 4,
// 1}, {2.1, 4,
// -2, 1}, {11, -6,
// 4, 1}},
// {{-0.2, 2, 0,
// 1}, {5, 3, 2,
// 1}, {5, 1.5, 1.4,
// 1}, {-3, 2, 5,
// 1}, {0.8, 1.3,
// 0, 1}, {15, -2,
// 0.9, 1}},
// {{3, 1.4, -1,
// 1}, {6, 2, 4,
// 1}, {-1, 0, -2,
// 1}, {0, 2.8, 2,
// 1}, {-0.5,
// 2, 1.2, 1}, {7,
// -3, -2, 1}},},
// {0, 0, 0, 0.1, 0.5, 0.8,
// 1, 1, 1}, {0, 0, 0, 0.2,
// 0.7, 0.8, 1, 1, 1});
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{-0.57, -0.48,
// -0.31, 1}, {-0.17,
// -0.49, -0.43, 1},
// {0.3, -0.53, -0.27,
// 1}, {0.66, -0.42,
// -0.36, 1}},
// {{-0.53, -0.15,
// -0.03, 1}, {-0.21,
// -0.12, 0.09, 1},
// {0.10, -0.18, 0.03,
// 1}, {0.49, -0.15,
// -0.03, 1}},
// {{-0.61, 0.22, 0.09,
// 1}, {-0.21, 0.20,
// 0.09, 1}, {0.13,
// 0.20, 0.12, 1},
// {0.49, 0.19, -0.03,
// 1}},
// {{-0.52, 0.51, 0.13,
// 1}, {-0.20, 0.32,
// 0.36, 1}, {0.18,
// 0.29, 0.28, 1},
// {0.51, 0.53, 0.12,
// 1}}
// }, {0, 0, 0, 0, 1, 1, 1, 1},
// {0, 0, 0, 0, 1, 1, 1, 1});
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{2, -2, -2, 1},
// {-2.5, -2.2, -1.5,
// 3}, {-2, -2, -0.5,
// 4}, {-2, -2, 1.5,
// 2}},
// {{2, -1, -2, 2},
// {-2.5, -1.2, -1.5,
// 1}, {-2, -1, -0.5,
// 1}, {-2, -1, 1.5,
// 1}},
// {{3, 1.2, -2, 9},
// {-2.5, 1.2, -1.5,
// 1}, {-2, 1.5, -0.5,
// 7}, {-2, 1.5, 1.5,
// 4}},
// {{2, 2, -2, 1},
// {-2.5, 2, -1.5,
// 1}, {-2, 2.5, -0.5,
// 7}, {-2, 2, 1.5,
// 1}}
// }, {0, 0, 0, 0, 1, 1, 1, 1},
// {0, 0, 0, 0, 1, 1, 1, 1});
// NurbsSurface::Surface nurbsSurfaceEvaluator({
// {{-0.5, -0.5,
// -0.2, 1},
// {-0.2, -0.5,
// -0.1, 1},
// {0.2, -0.5,
// -0.1, 1},
// {0.5, -0.5,
// -0.2, 1}},
// {{-0.5, -0.2,
// -0.1, 1},
// {-0.2, -0.2,
// 0., 1}, {0.2,
// -0.2, 0., 1},
// {0.5, -0.2,
// -0.1, 1}},
// {{-0.5, 0.2,
// -0.1, 1},
// {-0.2, 0.2,
// 0., 1}, {0.2,
// 0.2, 0., 1},
// {0.5, 0.2,
// -0.1, 1}},
// {{-0.5, 0.5,
// -0.2, 1},
// {-0.2, 0.5,
// -0.1, 1},
// {0.2, 0.5,
// -0.1, 1},
// {0.5, 0.5,
// -0.2, 1}}
// }, {0, 0, 0, 0, 1, 1,
// 1, 1}, {0, 0, 0, 0,
// 1, 1, 1, 1});
NurbsSurface::Surface nurbsSurfaceEvaluator(
{{{0, 0, 0.2, 1.1},
{0, 0.2, 0.3, 0.9},
{0, 0.4, 0.25, 1.1},
{0, 0.6, 0.12, 0.9},
{0, 0.8, 0.22, 1},
{0, 1, 0.12, 1}},
{{0.2, 0, 0.22, 1},
{0.2, 0.2, -0.3, 1},
{0.2, 0.4, 0.4, 1},
{0.2, 0.6, -0.1, 1.1},
{0.2, 0.8, -0.1, 1},
{0.2, 1, 0.2, 0.9}},
{{0.4, 0, 0.18, 0.9},
{0.4, 0.2, -0.3, 1},
{0.4, 0.4, 0.4, 1.1},
{0.4, 0.6, -0.1, 1},
{0.4, 0.8, -0.1, 0.9},
{0.4, 1, 0.23, 1}},
{{0.6, 0, 0.2, 0.9},
{0.6, 0.2, 0.4, 0.9},
{0.6, 0.4, 0.4, 1},
{0.6, 0.6, 0.4, 0.9},
{0.6, 0.8, 0.4, 1},
{0.6, 1, 0.2, 1}},
{{0.8, 0, 0.19, 1.1},
{0.8, 0.2, -0.4, 1},
{0.8, 0.4, 0.4, 1.1},
{0.8, 0.6, -0.27, 1},
{0.8, 0.8, -0.27, 1},
{0.8, 1, 0.2, 0.9}},
{{1, 0, 0.2, 1.1},
{1, 0.2, 0.12, 1},
{1, 0.4, 0.22, 1},
{1, 0.6, 0.32, 1},
{1, 0.8, 0.22, 1.2},
{1, 1, 0.12, 1.1}}},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, level);
nurbsSurfaceEvaluator.setRecordTime(true);
// nurbsSurfaceEvaluator.buildGaussMap(6); // 构建曲面的Gauss map
// nurbsSurfaceEvaluator.gauss_map.printQuadTree(); // 打印整棵Gauss map树
// auto overlappedLeafNodes =
// NurbsSurface::getOverlappedLeafNodes(nurbsSurfaceEvaluator.gauss_map,
// nurbsSurfaceEvaluator.gauss_map);
// printf("overlapped leafNodes cnt: %llu\n", overlappedLeafNodes.size());
// 判定两个Surface构造的GaussMap在指定u、v范围内是否有重合
// auto isRegionallyOverlapped =
// NurbsSurface::isGaussMapsOverlapped(nurbsSurfaceEvaluator.gauss_map,
// nurbsSurfaceEvaluator.gauss_map,
// {0.2,
// 0.3},
// {0.2,
// 0.3},
// {0.28,
// 0.38},
// {0.28,
// 0.38},
// {0,
// 1},
// {0,
// 1},
// {0,
// 1},
// {0,
// 1});
// printf("is guass map overlapped: %d\n", isRegionallyOverlapped);
int edgeSampleCnt = pow(2, level - 1) + 1;
nurbsSurfaceEvaluator.evaluate();
nurbsSurfaceEvaluator.derivative();
// 如果buildBVH需要曲率K,这里需要再先:
// nurbsSurfaceEvaluator.curvature(edgeSampleCnt, edgeSampleCnt);
// 同时buildBVH传入第二个参数
nurbsSurfaceEvaluator.buildBVH(false); // 构建曲面的BVH
auto derInfo = nurbsSurfaceEvaluator.getDerivativeVec(); // 求值
printf("==============================\n");
//
// NurbsCurve::Curve nurbsCurveEvaluator(
// {{0.721068, 0, 0, 1},
// {1.05152, 0, 0, 1},
// {0.346353, 0, 0, 1},
// {0.351813, 0, 0, 1},
// {0.375972, 0, 0, 1},
// {0.363015, 0, 0, 1},
// {0.368758, 0, 0, 1},
// {0.368222, 0, 0, 1},
// {0.371302, 0, 0, 1},
// {0.374657, 0, 0, 1},
// {0.377866, 0, 0, 1},
// {0.380762, 0, 0, 1},
// {0.382451, 0, 0, 1},
// {0.383466, 0, 0, 1},
// {0.383948, 0, 0, 1},
// {0.384242, 0, 0, 1},
// {0.384156, 0, 0, 1},
// {0.383659, 0, 0, 1},
// {0.382616, 0, 0, 1},
// {0.381428, 0, 0, 1},
// {0.379223, 0, 0, 1},
// {0.379492, 0, 0, 1},
// {0.372487, 0, 0, 1},
// {0.384305, 0, 0, 1},
// {0.357789, 0, 0, 1},
// {0.34936, 0, 0, 1},
// {1.0537, 0, 0, 1},
// {0.717109, 0, 0, 1}},
// {0, 0, 0, 0, 0.103448, 0.137931, 0.172414, 0.206897, 0.241379,
// 0.275862, 0.310345, 0.344828, 0.37931, 0.413793, 0.448276,
// 0.482759, 0.517241, 0.551724, 0.586207, 0.62069, 0.655172,
// 0.689655, 0.724138, 0.758621, 0.793103, 0.827586, 0.862069,
// 0.896552, 1, 1, 1, 1});
// nurbsCurveEvaluator.setRecordTime(true);
//
// auto res = nurbsCurveEvaluator.evaluate(30);
// for (auto &re: res) {
// printf("%f, %f, %f\n", re[0], re[1], re[2]);
// }
// printf("\n");
test();
return 0;
}
Loading…
Cancel
Save