2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* triangle_mesh.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2015-04-18 19:38:54 +02:00
|
|
|
#include "triangle_mesh.h"
|
|
|
|
#include "sort.h"
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int TriangleMesh::_create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_depth > max_depth) {
|
|
|
|
max_depth = p_depth;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_size == 1) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return p_bb[p_from] - p_bvh;
|
|
|
|
} else if (p_size == 0) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Rect3 aabb;
|
2017-03-05 16:44:50 +01:00
|
|
|
aabb = p_bb[p_from]->aabb;
|
|
|
|
for (int i = 1; i < p_size; i++) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
aabb.merge_with(p_bb[p_from + i]->aabb);
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int li = aabb.get_longest_axis_index();
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (li) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
case Vector3::AXIS_X: {
|
2017-03-05 16:44:50 +01:00
|
|
|
SortArray<BVH *, BVHCmpX> sort_x;
|
|
|
|
sort_x.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
|
2015-04-18 19:38:54 +02:00
|
|
|
//sort_x.sort(&p_bb[p_from],p_size);
|
|
|
|
} break;
|
|
|
|
case Vector3::AXIS_Y: {
|
2017-03-05 16:44:50 +01:00
|
|
|
SortArray<BVH *, BVHCmpY> sort_y;
|
|
|
|
sort_y.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
|
2015-04-18 19:38:54 +02:00
|
|
|
//sort_y.sort(&p_bb[p_from],p_size);
|
|
|
|
} break;
|
|
|
|
case Vector3::AXIS_Z: {
|
2017-03-05 16:44:50 +01:00
|
|
|
SortArray<BVH *, BVHCmpZ> sort_z;
|
|
|
|
sort_z.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
|
2015-04-18 19:38:54 +02:00
|
|
|
//sort_z.sort(&p_bb[p_from],p_size);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int left = _create_bvh(p_bvh, p_bb, p_from, p_size / 2, p_depth + 1, max_depth, max_alloc);
|
|
|
|
int right = _create_bvh(p_bvh, p_bb, p_from + p_size / 2, p_size - p_size / 2, p_depth + 1, max_depth, max_alloc);
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int index = max_alloc++;
|
2015-04-18 19:38:54 +02:00
|
|
|
BVH *_new = &p_bvh[index];
|
2017-03-05 16:44:50 +01:00
|
|
|
_new->aabb = aabb;
|
2017-06-06 20:33:51 +02:00
|
|
|
_new->center = aabb.position + aabb.size * 0.5;
|
2017-03-05 16:44:50 +01:00
|
|
|
_new->face_index = -1;
|
|
|
|
_new->left = left;
|
|
|
|
_new->right = right;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void TriangleMesh::create(const PoolVector<Vector3> &p_faces) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
valid = false;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int fc = p_faces.size();
|
|
|
|
ERR_FAIL_COND(!fc || ((fc % 3) != 0));
|
|
|
|
fc /= 3;
|
2015-04-18 19:38:54 +02:00
|
|
|
triangles.resize(fc);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bvh.resize(fc * 3); //will never be larger than this (todo make better)
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<BVH>::Write bw = bvh.write();
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//create faces and indices and base bvh
|
|
|
|
//except for the Set for repeated triangles, everything
|
|
|
|
//goes in-place.
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3>::Read r = p_faces.read();
|
|
|
|
PoolVector<Triangle>::Write w = triangles.write();
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<Vector3, int> db;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < fc; i++) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Triangle &f = w[i];
|
|
|
|
const Vector3 *v = &r[i * 3];
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int j = 0; j < 3; j++) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int vidx = -1;
|
2017-06-30 20:47:17 +02:00
|
|
|
Vector3 vs = v[j].snapped(Vector3(0.0001, 0.0001, 0.0001));
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<Vector3, int>::Element *E = db.find(vs);
|
2015-04-18 19:38:54 +02:00
|
|
|
if (E) {
|
2017-03-05 16:44:50 +01:00
|
|
|
vidx = E->get();
|
2015-04-18 19:38:54 +02:00
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
vidx = db.size();
|
|
|
|
db[vs] = vidx;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
f.indices[j] = vidx;
|
|
|
|
if (j == 0)
|
2017-06-06 20:33:51 +02:00
|
|
|
bw[i].aabb.position = vs;
|
2015-04-18 19:38:54 +02:00
|
|
|
else
|
|
|
|
bw[i].aabb.expand_to(vs);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
f.normal = Face3(r[i * 3 + 0], r[i * 3 + 1], r[i * 3 + 2]).get_plane().get_normal();
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bw[i].left = -1;
|
|
|
|
bw[i].right = -1;
|
|
|
|
bw[i].face_index = i;
|
2017-06-06 20:33:51 +02:00
|
|
|
bw[i].center = bw[i].aabb.position + bw[i].aabb.size * 0.5;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
vertices.resize(db.size());
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Vector3>::Write vw = vertices.write();
|
2017-03-05 16:44:50 +01:00
|
|
|
for (Map<Vector3, int>::Element *E = db.front(); E; E = E->next()) {
|
|
|
|
vw[E->get()] = E->key();
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
PoolVector<BVH *> bwptrs;
|
2015-04-18 19:38:54 +02:00
|
|
|
bwptrs.resize(fc);
|
2017-03-05 16:44:50 +01:00
|
|
|
PoolVector<BVH *>::Write bwp = bwptrs.write();
|
|
|
|
for (int i = 0; i < fc; i++) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bwp[i] = &bw[i];
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
max_depth = 0;
|
|
|
|
int max_alloc = fc;
|
|
|
|
int max = _create_bvh(bw.ptr(), bwp.ptr(), 0, fc, 1, max_depth, max_alloc);
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bw = PoolVector<BVH>::Write(); //clearup
|
2015-04-18 19:38:54 +02:00
|
|
|
bvh.resize(max_alloc); //resize back
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
valid = true;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 TriangleMesh::get_area_normal(const Rect3 &p_aabb) const {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
enum {
|
2017-03-05 16:44:50 +01:00
|
|
|
TEST_AABB_BIT = 0,
|
|
|
|
VISIT_LEFT_BIT = 1,
|
|
|
|
VISIT_RIGHT_BIT = 2,
|
|
|
|
VISIT_DONE_BIT = 3,
|
|
|
|
VISITED_BIT_SHIFT = 29,
|
|
|
|
NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
|
|
|
|
VISITED_BIT_MASK = ~NODE_IDX_MASK,
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int n_count = 0;
|
2015-04-18 19:38:54 +02:00
|
|
|
Vector3 n;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int level = 0;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Triangle>::Read trianglesr = triangles.read();
|
2017-03-05 16:44:50 +01:00
|
|
|
PoolVector<Vector3>::Read verticesr = vertices.read();
|
|
|
|
PoolVector<BVH>::Read bvhr = bvh.read();
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Triangle *triangleptr = trianglesr.ptr();
|
|
|
|
int pos = bvh.size() - 1;
|
2015-04-18 19:38:54 +02:00
|
|
|
const BVH *bvhptr = bvhr.ptr();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[0] = pos;
|
|
|
|
while (true) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t node = stack[level] & NODE_IDX_MASK;
|
|
|
|
const BVH &b = bvhptr[node];
|
|
|
|
bool done = false;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (stack[level] >> VISITED_BIT_SHIFT) {
|
2015-04-18 19:38:54 +02:00
|
|
|
case TEST_AABB_BIT: {
|
|
|
|
|
|
|
|
bool valid = b.aabb.intersects(p_aabb);
|
|
|
|
if (!valid) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (b.face_index >= 0) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Triangle &s = triangleptr[b.face_index];
|
|
|
|
n += s.normal;
|
2015-04-18 19:38:54 +02:00
|
|
|
n_count++;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
case VISIT_LEFT_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
|
|
|
|
stack[level + 1] = b.left | TEST_AABB_BIT;
|
2015-04-18 19:38:54 +02:00
|
|
|
level++;
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
case VISIT_RIGHT_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
|
|
|
|
stack[level + 1] = b.right | TEST_AABB_BIT;
|
2015-04-18 19:38:54 +02:00
|
|
|
level++;
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
case VISIT_DONE_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (level == 0) {
|
|
|
|
done = true;
|
2015-04-18 19:38:54 +02:00
|
|
|
break;
|
|
|
|
} else
|
|
|
|
level--;
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (n_count > 0)
|
|
|
|
n /= n_count;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
enum {
|
2017-03-05 16:44:50 +01:00
|
|
|
TEST_AABB_BIT = 0,
|
|
|
|
VISIT_LEFT_BIT = 1,
|
|
|
|
VISIT_RIGHT_BIT = 2,
|
|
|
|
VISIT_DONE_BIT = 3,
|
|
|
|
VISITED_BIT_SHIFT = 29,
|
|
|
|
NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
|
|
|
|
VISITED_BIT_MASK = ~NODE_IDX_MASK,
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 n = (p_end - p_begin).normalized();
|
|
|
|
real_t d = 1e10;
|
|
|
|
bool inters = false;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int level = 0;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Triangle>::Read trianglesr = triangles.read();
|
2017-03-05 16:44:50 +01:00
|
|
|
PoolVector<Vector3>::Read verticesr = vertices.read();
|
|
|
|
PoolVector<BVH>::Read bvhr = bvh.read();
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Triangle *triangleptr = trianglesr.ptr();
|
|
|
|
const Vector3 *vertexptr = verticesr.ptr();
|
|
|
|
int pos = bvh.size() - 1;
|
2015-04-18 19:38:54 +02:00
|
|
|
const BVH *bvhptr = bvhr.ptr();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[0] = pos;
|
|
|
|
while (true) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t node = stack[level] & NODE_IDX_MASK;
|
|
|
|
const BVH &b = bvhptr[node];
|
|
|
|
bool done = false;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (stack[level] >> VISITED_BIT_SHIFT) {
|
2015-04-18 19:38:54 +02:00
|
|
|
case TEST_AABB_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool valid = b.aabb.intersects_segment(p_begin, p_end);
|
2017-01-14 12:26:56 +01:00
|
|
|
//bool valid = b.aabb.intersects(ray_aabb);
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (b.face_index >= 0) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Triangle &s = triangleptr[b.face_index];
|
|
|
|
Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
Vector3 res;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (f3.intersects_segment(p_begin, p_end, &res)) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t nd = n.dot(res);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (nd < d) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
d = nd;
|
|
|
|
r_point = res;
|
|
|
|
r_normal = f3.get_plane().get_normal();
|
|
|
|
inters = true;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
case VISIT_LEFT_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
|
|
|
|
stack[level + 1] = b.left | TEST_AABB_BIT;
|
2015-04-18 19:38:54 +02:00
|
|
|
level++;
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
case VISIT_RIGHT_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
|
|
|
|
stack[level + 1] = b.right | TEST_AABB_BIT;
|
2015-04-18 19:38:54 +02:00
|
|
|
level++;
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
case VISIT_DONE_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (level == 0) {
|
|
|
|
done = true;
|
2015-04-18 19:38:54 +02:00
|
|
|
break;
|
|
|
|
} else
|
|
|
|
level--;
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inters) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (n.dot(r_normal) > 0)
|
|
|
|
r_normal = -r_normal;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return inters;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
enum {
|
2017-03-05 16:44:50 +01:00
|
|
|
TEST_AABB_BIT = 0,
|
|
|
|
VISIT_LEFT_BIT = 1,
|
|
|
|
VISIT_RIGHT_BIT = 2,
|
|
|
|
VISIT_DONE_BIT = 3,
|
|
|
|
VISITED_BIT_SHIFT = 29,
|
|
|
|
NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
|
|
|
|
VISITED_BIT_MASK = ~NODE_IDX_MASK,
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector3 n = p_dir;
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t d = 1e20;
|
|
|
|
bool inters = false;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int level = 0;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Triangle>::Read trianglesr = triangles.read();
|
2017-03-05 16:44:50 +01:00
|
|
|
PoolVector<Vector3>::Read verticesr = vertices.read();
|
|
|
|
PoolVector<BVH>::Read bvhr = bvh.read();
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Triangle *triangleptr = trianglesr.ptr();
|
|
|
|
const Vector3 *vertexptr = verticesr.ptr();
|
|
|
|
int pos = bvh.size() - 1;
|
2015-04-18 19:38:54 +02:00
|
|
|
const BVH *bvhptr = bvhr.ptr();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[0] = pos;
|
|
|
|
while (true) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint32_t node = stack[level] & NODE_IDX_MASK;
|
|
|
|
const BVH &b = bvhptr[node];
|
|
|
|
bool done = false;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (stack[level] >> VISITED_BIT_SHIFT) {
|
2015-04-18 19:38:54 +02:00
|
|
|
case TEST_AABB_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool valid = b.aabb.intersects_ray(p_begin, p_dir);
|
2015-04-18 19:38:54 +02:00
|
|
|
if (!valid) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (b.face_index >= 0) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Triangle &s = triangleptr[b.face_index];
|
|
|
|
Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
Vector3 res;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (f3.intersects_ray(p_begin, p_dir, &res)) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-01-14 21:35:39 +01:00
|
|
|
real_t nd = n.dot(res);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (nd < d) {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
d = nd;
|
|
|
|
r_point = res;
|
|
|
|
r_normal = f3.get_plane().get_normal();
|
|
|
|
inters = true;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
case VISIT_LEFT_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
|
|
|
|
stack[level + 1] = b.left | TEST_AABB_BIT;
|
2015-04-18 19:38:54 +02:00
|
|
|
level++;
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
case VISIT_RIGHT_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
|
|
|
|
stack[level + 1] = b.right | TEST_AABB_BIT;
|
2015-04-18 19:38:54 +02:00
|
|
|
level++;
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
case VISIT_DONE_BIT: {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (level == 0) {
|
|
|
|
done = true;
|
2015-04-18 19:38:54 +02:00
|
|
|
break;
|
|
|
|
} else
|
|
|
|
level--;
|
2017-01-15 22:15:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inters) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (n.dot(r_normal) > 0)
|
|
|
|
r_normal = -r_normal;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return inters;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TriangleMesh::is_valid() const {
|
|
|
|
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Face3> TriangleMesh::get_faces() const {
|
2015-04-18 19:38:54 +02:00
|
|
|
|
|
|
|
if (!valid)
|
2017-01-07 22:25:37 +01:00
|
|
|
return PoolVector<Face3>();
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Face3> faces;
|
2015-04-18 19:38:54 +02:00
|
|
|
int ts = triangles.size();
|
|
|
|
faces.resize(triangles.size());
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
PoolVector<Face3>::Write w = faces.write();
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<Triangle>::Read r = triangles.read();
|
|
|
|
PoolVector<Vector3>::Read rv = vertices.read();
|
2015-04-18 19:38:54 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < ts; i++) {
|
|
|
|
for (int j = 0; j < 3; j++) {
|
|
|
|
w[i].vertex[j] = rv[r[i].indices[j]];
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
w = PoolVector<Face3>::Write();
|
2015-04-18 19:38:54 +02:00
|
|
|
return faces;
|
|
|
|
}
|
|
|
|
|
|
|
|
TriangleMesh::TriangleMesh() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
valid = false;
|
|
|
|
max_depth = 0;
|
2015-04-18 19:38:54 +02:00
|
|
|
}
|