1#include "NeighborTriangleQuery.h"
3#include "Topology/SparseOctree.h"
5#include "Collision/CollisionDetectionBroadPhase.h"
9 IMPLEMENT_TCLASS(NeighborTriangleQuery, TDataType)
11 template<typename TDataType>
12 NeighborTriangleQuery<TDataType>::NeighborTriangleQuery()
15 mBroadPhaseCD = std::make_shared<CollisionDetectionBroadPhase<TDataType>>();
18 template<typename TDataType>
19 NeighborTriangleQuery<TDataType>::~NeighborTriangleQuery()
23 template<typename Real, typename Coord>
24 __global__ void NTQ_SetupAABB(
25 DArray<AABB> boundingBox,
26 DArray<Coord> position,
29 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
30 if (pId >= position.size()) return;
33 Coord p = position[pId];
37 boundingBox[pId] = box;
40 template<typename Coord>
41 __global__ void NTQ_SetupAABB(
42 DArray<AABB> boundingBox,
44 DArray<TopologyModule::Triangle> tIndex)
46 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
47 if (tId >= boundingBox.size()) return;
50 TopologyModule::Triangle index = tIndex[tId];
52 Coord v0 = vertex[index[0]];
53 Coord v1 = vertex[index[1]];
54 Coord v2 = vertex[index[2]];
56 box.v0 = minimum(v0, minimum(v1, v2));
57 box.v1 = maximum(v0, maximum(v1, v2));
59 boundingBox[tId] = box;
62 template<typename Coord>
63 __global__ void NTQ_Narrow_Count(
65 DArray<Coord> position,
67 DArray<TopologyModule::Triangle> triangles,
71 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
72 if (tId >= position.size()) return;
75 List<int>& nbrIds_i = nbr[tId];
76 int nbSize = nbrIds_i.size();
77 //printf("CNT_OKKKK nbSize: %d\n", nbSize);
78 for (int ne = 0; ne < nbSize; ne++)
81 Point3D point(position[tId]);
82 Triangle3D t3d_n(vertex[triangles[j][0]], vertex[triangles[j][1]], vertex[triangles[j][2]]);
83 Real p_dis_t = abs(point.distance(t3d_n));
84 if (p_dis_t< radius && p_dis_t > EPSILON)
92 template<typename Coord>
93 __global__ void NTQ_Narrow_Set(
95 DArrayList<int> nbr_out,
96 DArray<Coord> position,
98 DArray<TopologyModule::Triangle> triangles,
101 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
102 if (tId >= position.size()) return;
105 List<int>& nbrIds_i = nbr[tId];
106 int nbSize = nbrIds_i.size(); //int nbSize = nbr.getNeighborSize(tId);
107 List<int>& nbrOutIds_i = nbr_out[tId];
108 for (int ne = 0; ne < nbSize; ne++)
110 int j = nbrIds_i[ne];
112 Point3D point(position[tId]);
113 Triangle3D t3d_n(vertex[triangles[j][0]], vertex[triangles[j][1]], vertex[triangles[j][2]]);
114 Real proj_dist = abs(point.distance(t3d_n));
116 if (proj_dist < radius && proj_dist > EPSILON)
118 nbrOutIds_i.insert(j);
123 template<typename TDataType>
124 void NeighborTriangleQuery<TDataType>::compute()
126 int pNum = this->inPosition()->size();
127 if (pNum == 0) return;
129 if (mQueryAABB.size() != pNum) {
130 mQueryAABB.resize(pNum);
133 auto ts = this->inTriangleSet()->constDataPtr();
134 auto& triVertex = ts->getPoints();
135 auto& triIndex = ts->getTriangles();
137 int tNum = triIndex.size();
138 if (tNum == 0) return;
139 if (mQueriedAABB.size() != tNum) {
140 mQueriedAABB.resize(tNum);
146 this->inPosition()->constData(),
147 this->inRadius()->getValue());
155 Real radius = this->inRadius()->getValue();
157 mBroadPhaseCD->varGridSizeLimit()->setValue(2 * radius);
158 mBroadPhaseCD->inSource()->assign(mQueryAABB);
160 if (this->inTriangleSet()->isModified()) {
161 mBroadPhaseCD->inTarget()->assign(mQueriedAABB);
165 auto type = this->varSpatial()->getDataPtr()->currentKey();
170 mBroadPhaseCD->varAccelerationStructure()->setCurrentKey(CollisionDetectionBroadPhase<TDataType>::BVH);
172 case Spatial::OCTREE:
173 mBroadPhaseCD->varAccelerationStructure()->setCurrentKey(CollisionDetectionBroadPhase<TDataType>::Octree);
179 mBroadPhaseCD->update();
181 auto& nbr = mBroadPhaseCD->outContactList()->getData();
183 if (this->outNeighborIds()->size() != pNum)
185 this->outNeighborIds()->allocate();
189 auto& nbrIds = this->outNeighborIds()->getData();
190 nbrIds.resize(nbrNum);
193 //this->outNeighborIds()->getData().resize(p_num);
195 auto& nbrIds = this->outNeighborIds()->getData();
201 int sum1 = nbr.elementSize();
204 //printf("one %d %d\n", nbrNum.size(), p_num);
208 this->inPosition()->getData(),
212 this->inRadius()->getData());
214 nbrIds.resize(nbrNum);
216 int sum = mReduce.accumulate(nbrNum.begin(), nbrNum.size());
223 this->inPosition()->getData(),
226 this->inRadius()->getData());
233 DEFINE_CLASS(NeighborTriangleQuery);