/
collide.cpp
196 lines (163 loc) · 5.88 KB
/
collide.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "../common/tutorial/tutorial.h"
#include "../common/tutorial/benchmark_render.h"
#include "../common/tutorial/statistics.h"
#include <set>
#include "../../common/sys/mutex.h"
#include "../common/core/ray.h"
#include "../../kernels/geometry/triangle_triangle_intersector.h"
#include "clothModel.h"
namespace embree
{
void updateScene ();
// RTCDevice g_device = nullptr;
RTCScene g_scene = nullptr;
using SurfacePoint = std::pair<unsigned,unsigned>;
using Collision = std::pair<SurfacePoint, SurfacePoint>;
using Collisions = std::vector<Collision>;
Collisions sim_collisions;
// bool use_user_geometry = false;
std::vector<std::unique_ptr<collide2::Mesh>> meshes;
unsigned int clothID;
bool benchmark = false;
int numPhi = 45;
int numTheta = 2*numPhi;
size_t NX = 50;
size_t NZ = 50;
SpinLock mutex;
bool pause = false;
bool intersect_triangle_triangle (unsigned geomID0, unsigned primID0, unsigned geomID1, unsigned primID1)
{
//CSTAT(bvh_collide_prim_intersections1++);
/* special culling for scene intersection with itself */
if (geomID0 == geomID1 && primID0 == primID1) {
return false;
}
//CSTAT(bvh_collide_prim_intersections2++);
auto mesh0 = meshes[geomID0].get ();
auto mesh1 = meshes[geomID1].get ();
auto const & tri0 = (Triangle&) mesh0->tris_[primID0];
auto const & tri1 = (Triangle&) mesh1->tris_[primID1];
if (geomID0 == geomID1)
{
/* ignore intersection with topological neighbors */
const vint4 t0(tri0.v0,tri0.v1,tri0.v2,tri0.v2);
if (any(vint4(tri1.v0) == t0)) return false;
if (any(vint4(tri1.v1) == t0)) return false;
if (any(vint4(tri1.v2) == t0)) return false;
}
//CSTAT(bvh_collide_prim_intersections3++);
const Vec3fa a0 = mesh0->x_[tri0.v0];
const Vec3fa a1 = mesh0->x_[tri0.v1];
const Vec3fa a2 = mesh0->x_[tri0.v2];
const Vec3fa b0 = mesh1->x_[tri1.v0];
const Vec3fa b1 = mesh1->x_[tri1.v1];
const Vec3fa b2 = mesh1->x_[tri1.v2];
return isa::TriangleTriangleIntersector::intersect_triangle_triangle(a0,a1,a2,b0,b1,b2);
}
void CollideFunc (void* userPtr, RTCCollision* collisions, unsigned int num_collisions)
{
for (size_t i=0; i<num_collisions;)
{
bool intersect = intersect_triangle_triangle(collisions[i].geomID0,collisions[i].primID0,
collisions[i].geomID1,collisions[i].primID1);
if (intersect) i++;
else collisions[i] = collisions[--num_collisions];
}
if (num_collisions == 0)
return;
Lock<SpinLock> lock(mutex);
for (size_t i=0; i<num_collisions; i++)
{
const unsigned geomID0 = collisions[i].geomID0;
const unsigned primID0 = collisions[i].primID0;
const unsigned geomID1 = collisions[i].geomID1;
const unsigned primID1 = collisions[i].primID1;
static_cast<Collisions*>(userPtr)->push_back(std::make_pair(std::make_pair(geomID0,primID0),std::make_pair(geomID1,primID1)));
}
}
void triangle_bounds_func(const struct RTCBoundsFunctionArguments* args)
{
void* ptr = args->geometryUserPtr;
unsigned geomID = (unsigned) (size_t) ptr;
auto const & mesh = *meshes[geomID];
BBox3fa bounds = empty;
bounds.extend(mesh.x_[mesh.tris_[args->primID].v0]);
bounds.extend(mesh.x_[mesh.tris_[args->primID].v1]);
bounds.extend(mesh.x_[mesh.tris_[args->primID].v2]);
*(BBox3fa*) args->bounds_o = bounds;
}
void triangle_intersect_func(const RTCIntersectFunctionNArguments* args)
{
void* ptr = args->geometryUserPtr;
::Ray* ray = (::Ray*)args->rayhit;
unsigned int primID = args->primID;
unsigned geomID = (unsigned) (size_t) ptr;
auto const & mesh = *meshes[geomID];
auto & v0 = mesh.x_[mesh.tris_[args->primID].v0];
auto & v1 = mesh.x_[mesh.tris_[args->primID].v1];
auto & v2 = mesh.x_[mesh.tris_[args->primID].v2];
auto e1 = v0-v1;
auto e2 = v2-v0;
auto Ng = cross(e1,e2);
/* calculate denominator */
auto O = Vec3fa(ray->org);
auto D = Vec3fa(ray->dir);
auto C = v0 - O;
auto R = cross(D,C);
float den = dot(Ng,D);
float rcpDen = rcp(den);
/* perform edge tests */
float u = dot(R,e2)*rcpDen;
float v = dot(R,e1)*rcpDen;
/* perform backface culling */
bool valid = (den != 0.0f) & (u >= 0.0f) & (v >= 0.0f) & (u+v<=1.0f);
if (likely(!valid)) return;
/* perform depth test */
float t = dot(Vec3fa(Ng),C)*rcpDen;
valid &= (t > ray->tnear()) & (t < ray->tfar);
if (likely(!valid)) return;
/* update hit */
ray->tfar = t;
ray->u = u;
ray->v = v;
ray->geomID = geomID;
ray->primID = primID;
ray->Ng = Ng;
}
struct Tutorial : public TutorialApplication
{
Tutorial()
: TutorialApplication("collide",FEATURE_RTCORE)
{
registerOption("benchmark-collision", [] (Ref<ParseStream> cin, const FileName& path) {
benchmark = true;
}, "--benchmark-collision: benchmarks collision detection");
registerOption("complexity", [] (Ref<ParseStream> cin, const FileName& path) {
int N = cin->getInt();
numPhi = N;
numTheta = 2*numPhi;
NX = N;
NZ = N;
}, "--complexity: sets grid and sphere geometric complexity");
camera.from = Vec3fa(-2.5f,2.5f,-2.5f);
camera.to = Vec3fa(0.0f,0.0f,0.0f);
}
#if defined(USE_GLFW)
void keypressed(int key) override
{
if (key == 32 /* */) initializeClothPositions ((collide2::ClothModel &) (*meshes[clothID]));
if (key == 80 /*p*/) { pause = !pause; }
if (pause == true && key == 78 /*n*/) { updateScene (); }
else TutorialApplication::keypressed(key);
}
#endif
};
}
int main(int argc, char** argv) {
if (embree::TutorialBenchmark::benchmark(argc, argv)) {
return embree::TutorialBenchmark(embree::renderBenchFunc<embree::Tutorial>).main(argc, argv, "collide");
}
return embree::Tutorial().main(argc,argv);
}