More cleanups and reorganizations.

This commit is contained in:
Relintai 2023-12-18 21:36:03 +01:00
parent a3d45fddd8
commit b551dc7ec1
15 changed files with 0 additions and 1506 deletions

View File

@ -1,19 +0,0 @@
#!/usr/bin/env python
Import("env")
env.core_sources = []
env.add_source_files(env.core_sources, "*.cpp")
env.add_source_files(env.core_sources, "./math/*.cpp")
env.add_source_files(env.core_sources, "./containers/*.cpp")
env.add_source_files(env.core_sources, "./log/*.cpp")
env.add_source_files(env.core_sources, "./os/*.cpp")
env.add_source_files(env.core_sources, "./image/*.cpp")
env.add_source_files(env.core_sources, "./threading/*.cpp")
env.add_source_files(env.core_sources, "./settings/*.cpp")
env.add_source_files(env.core_sources, "./nodes/*.cpp")
# Build it all as a library
lib = env.add_library("core", env.core_sources)
env.Prepend(LIBS=[lib])

View File

@ -1,722 +0,0 @@
/*************************************************************************/
/* triangle_mesh.cpp */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/*************************************************************************/
#include "triangle_mesh.h"
#include "core/containers/sort_array.h"
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) {
if (p_depth > max_depth) {
max_depth = p_depth;
}
if (p_size == 1) {
return p_bb[p_from] - p_bvh;
} else if (p_size == 0) {
return -1;
}
AABB aabb;
aabb = p_bb[p_from]->aabb;
for (int i = 1; i < p_size; i++) {
aabb.merge_with(p_bb[p_from + i]->aabb);
}
int li = aabb.get_longest_axis_index();
switch (li) {
case Vector3::AXIS_X: {
SortArray<BVH *, BVHCmpX> sort_x;
sort_x.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
//sort_x.sort(&p_bb[p_from],p_size);
} break;
case Vector3::AXIS_Y: {
SortArray<BVH *, BVHCmpY> sort_y;
sort_y.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
//sort_y.sort(&p_bb[p_from],p_size);
} break;
case Vector3::AXIS_Z: {
SortArray<BVH *, BVHCmpZ> sort_z;
sort_z.nth_element(0, p_size, p_size / 2, &p_bb[p_from]);
//sort_z.sort(&p_bb[p_from],p_size);
} break;
}
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);
int index = max_alloc++;
BVH *_new = &p_bvh[index];
_new->aabb = aabb;
_new->center = aabb.position + aabb.size * 0.5f;
_new->face_index = -1;
_new->left = left;
_new->right = right;
return index;
}
void TriangleMesh::get_indices(PoolVector<int> *r_triangles_indices) const {
if (!valid) {
return;
}
const int triangles_num = triangles.size();
// Parse vertices indices
PoolVector<Triangle>::Read triangles_read = triangles.read();
r_triangles_indices->resize(triangles_num * 3);
PoolVector<int>::Write r_indices_write = r_triangles_indices->write();
for (int i = 0; i < triangles_num; ++i) {
r_indices_write[3 * i + 0] = triangles_read[i].indices[0];
r_indices_write[3 * i + 1] = triangles_read[i].indices[1];
r_indices_write[3 * i + 2] = triangles_read[i].indices[2];
}
}
void TriangleMesh::create(const PoolVector<Vector3> &p_faces) {
valid = false;
int fc = p_faces.size();
ERR_FAIL_COND(!fc || ((fc % 3) != 0));
fc /= 3;
triangles.resize(fc);
bvh.resize(fc * 3); //will never be larger than this (todo make better)
PoolVector<BVH>::Write bw = bvh.write();
{
//create faces and indices and base bvh
//except for the Set for repeated triangles, everything
//goes in-place.
PoolVector<Vector3>::Read r = p_faces.read();
PoolVector<Triangle>::Write w = triangles.write();
RBMap<Vector3, int> db;
for (int i = 0; i < fc; i++) {
Triangle &f = w[i];
const Vector3 *v = &r[i * 3];
for (int j = 0; j < 3; j++) {
int vidx = -1;
Vector3 vs = v[j].snapped(Vector3(0.0001, 0.0001, 0.0001));
RBMap<Vector3, int>::Element *E = db.find(vs);
if (E) {
vidx = E->get();
} else {
vidx = db.size();
db[vs] = vidx;
}
f.indices[j] = vidx;
if (j == 0) {
bw[i].aabb.position = vs;
} else {
bw[i].aabb.expand_to(vs);
}
}
f.normal = Face3(r[i * 3 + 0], r[i * 3 + 1], r[i * 3 + 2]).get_plane().get_normal();
bw[i].left = -1;
bw[i].right = -1;
bw[i].face_index = i;
bw[i].center = bw[i].aabb.position + bw[i].aabb.size * 0.5f;
}
vertices.resize(db.size());
PoolVector<Vector3>::Write vw = vertices.write();
for (RBMap<Vector3, int>::Element *E = db.front(); E; E = E->next()) {
vw[E->get()] = E->key();
}
}
PoolVector<BVH *> bwptrs;
bwptrs.resize(fc);
PoolVector<BVH *>::Write bwp = bwptrs.write();
for (int i = 0; i < fc; i++) {
bwp[i] = &bw[i];
}
max_depth = 0;
int max_alloc = fc;
_create_bvh(bw.ptr(), bwp.ptr(), 0, fc, 1, max_depth, max_alloc);
bw.release(); //clearup
bvh.resize(max_alloc); //resize back
valid = true;
}
Vector3 TriangleMesh::get_area_normal(const AABB &p_aabb) const {
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
enum {
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,
};
int n_count = 0;
Vector3 n;
int level = 0;
PoolVector<Triangle>::Read trianglesr = triangles.read();
PoolVector<Vector3>::Read verticesr = vertices.read();
PoolVector<BVH>::Read bvhr = bvh.read();
const Triangle *triangleptr = trianglesr.ptr();
int pos = bvh.size() - 1;
const BVH *bvhptr = bvhr.ptr();
stack[0] = pos;
while (true) {
uint32_t node = stack[level] & NODE_IDX_MASK;
const BVH &b = bvhptr[node];
bool done = false;
switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
bool valid = b.aabb.intersects(p_aabb);
if (!valid) {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
if (b.face_index >= 0) {
const Triangle &s = triangleptr[b.face_index];
n += s.normal;
n_count++;
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
}
}
continue;
}
case VISIT_LEFT_BIT: {
stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.left | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_RIGHT_BIT: {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.right | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_DONE_BIT: {
if (level == 0) {
done = true;
break;
} else {
level--;
}
continue;
}
}
if (done) {
break;
}
}
if (n_count > 0) {
n /= n_count;
}
return n;
}
bool TriangleMesh::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const {
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
enum {
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,
};
Vector3 n = (p_end - p_begin).normalized();
real_t d = 1e10;
bool inters = false;
int level = 0;
PoolVector<Triangle>::Read trianglesr = triangles.read();
PoolVector<Vector3>::Read verticesr = vertices.read();
PoolVector<BVH>::Read bvhr = bvh.read();
const Triangle *triangleptr = trianglesr.ptr();
const Vector3 *vertexptr = verticesr.ptr();
int pos = bvh.size() - 1;
const BVH *bvhptr = bvhr.ptr();
stack[0] = pos;
while (true) {
uint32_t node = stack[level] & NODE_IDX_MASK;
const BVH &b = bvhptr[node];
bool done = false;
switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
bool valid = b.aabb.intersects_segment(p_begin, p_end);
//bool valid = b.aabb.intersects(ray_aabb);
if (!valid) {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
if (b.face_index >= 0) {
const Triangle &s = triangleptr[b.face_index];
Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
Vector3 res;
if (f3.intersects_segment(p_begin, p_end, &res)) {
real_t nd = n.dot(res);
if (nd < d) {
d = nd;
r_point = res;
r_normal = f3.get_plane().get_normal();
inters = true;
}
}
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
}
}
continue;
}
case VISIT_LEFT_BIT: {
stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.left | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_RIGHT_BIT: {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.right | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_DONE_BIT: {
if (level == 0) {
done = true;
break;
} else {
level--;
}
continue;
}
}
if (done) {
break;
}
}
if (inters) {
if (n.dot(r_normal) > 0) {
r_normal = -r_normal;
}
}
return inters;
}
bool TriangleMesh::intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const {
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
enum {
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,
};
Vector3 n = p_dir;
real_t d = 1e20;
bool inters = false;
int level = 0;
PoolVector<Triangle>::Read trianglesr = triangles.read();
PoolVector<Vector3>::Read verticesr = vertices.read();
PoolVector<BVH>::Read bvhr = bvh.read();
const Triangle *triangleptr = trianglesr.ptr();
const Vector3 *vertexptr = verticesr.ptr();
int pos = bvh.size() - 1;
const BVH *bvhptr = bvhr.ptr();
stack[0] = pos;
while (true) {
uint32_t node = stack[level] & NODE_IDX_MASK;
const BVH &b = bvhptr[node];
bool done = false;
switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
bool valid = b.aabb.intersects_ray(p_begin, p_dir);
if (!valid) {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
if (b.face_index >= 0) {
const Triangle &s = triangleptr[b.face_index];
Face3 f3(vertexptr[s.indices[0]], vertexptr[s.indices[1]], vertexptr[s.indices[2]]);
Vector3 res;
if (f3.intersects_ray(p_begin, p_dir, &res)) {
real_t nd = n.dot(res);
if (nd < d) {
d = nd;
r_point = res;
r_normal = f3.get_plane().get_normal();
inters = true;
}
}
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
}
}
continue;
}
case VISIT_LEFT_BIT: {
stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.left | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_RIGHT_BIT: {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.right | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_DONE_BIT: {
if (level == 0) {
done = true;
break;
} else {
level--;
}
continue;
}
}
if (done) {
break;
}
}
if (inters) {
if (n.dot(r_normal) > 0) {
r_normal = -r_normal;
}
}
return inters;
}
bool TriangleMesh::intersect_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const {
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
//p_fully_inside = true;
enum {
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,
};
int level = 0;
PoolVector<Triangle>::Read trianglesr = triangles.read();
PoolVector<Vector3>::Read verticesr = vertices.read();
PoolVector<BVH>::Read bvhr = bvh.read();
const Triangle *triangleptr = trianglesr.ptr();
const Vector3 *vertexptr = verticesr.ptr();
int pos = bvh.size() - 1;
const BVH *bvhptr = bvhr.ptr();
stack[0] = pos;
while (true) {
uint32_t node = stack[level] & NODE_IDX_MASK;
const BVH &b = bvhptr[node];
bool done = false;
switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
bool valid = b.aabb.intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count);
if (!valid) {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
if (b.face_index >= 0) {
const Triangle &s = triangleptr[b.face_index];
for (int j = 0; j < 3; ++j) {
const Vector3 &point = vertexptr[s.indices[j]];
const Vector3 &next_point = vertexptr[s.indices[(j + 1) % 3]];
Vector3 res;
bool over = true;
for (int i = 0; i < p_plane_count; i++) {
const Plane &p = p_planes[i];
if (p.intersects_segment(point, next_point, &res)) {
bool inisde = true;
for (int k = 0; k < p_plane_count; k++) {
if (k == i) {
continue;
}
const Plane &pp = p_planes[k];
if (pp.is_point_over(res)) {
inisde = false;
break;
}
}
if (inisde) {
return true;
}
}
if (p.is_point_over(point)) {
over = false;
break;
}
}
if (over) {
return true;
}
}
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
}
}
continue;
}
case VISIT_LEFT_BIT: {
stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.left | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_RIGHT_BIT: {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.right | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_DONE_BIT: {
if (level == 0) {
done = true;
break;
} else {
level--;
}
continue;
}
}
if (done) {
break;
}
}
return false;
}
bool TriangleMesh::inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale) const {
uint32_t *stack = (uint32_t *)alloca(sizeof(int) * max_depth);
enum {
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,
};
int level = 0;
PoolVector<Triangle>::Read trianglesr = triangles.read();
PoolVector<Vector3>::Read verticesr = vertices.read();
PoolVector<BVH>::Read bvhr = bvh.read();
Transform scale(Basis().scaled(p_scale));
const Triangle *triangleptr = trianglesr.ptr();
const Vector3 *vertexptr = verticesr.ptr();
int pos = bvh.size() - 1;
const BVH *bvhptr = bvhr.ptr();
stack[0] = pos;
while (true) {
uint32_t node = stack[level] & NODE_IDX_MASK;
const BVH &b = bvhptr[node];
bool done = false;
switch (stack[level] >> VISITED_BIT_SHIFT) {
case TEST_AABB_BIT: {
bool intersects = scale.xform(b.aabb).intersects_convex_shape(p_planes, p_plane_count, p_points, p_point_count);
if (!intersects) {
return false;
}
bool inside = scale.xform(b.aabb).inside_convex_shape(p_planes, p_plane_count);
if (inside) {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
if (b.face_index >= 0) {
const Triangle &s = triangleptr[b.face_index];
for (int j = 0; j < 3; ++j) {
Vector3 point = scale.xform(vertexptr[s.indices[j]]);
for (int i = 0; i < p_plane_count; i++) {
const Plane &p = p_planes[i];
if (p.is_point_over(point)) {
return false;
}
}
}
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
} else {
stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
}
}
continue;
}
case VISIT_LEFT_BIT: {
stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.left | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_RIGHT_BIT: {
stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
stack[level + 1] = b.right | TEST_AABB_BIT;
level++;
continue;
}
case VISIT_DONE_BIT: {
if (level == 0) {
done = true;
break;
} else {
level--;
}
continue;
}
}
if (done) {
break;
}
}
return true;
}
bool TriangleMesh::is_valid() const {
return valid;
}
PoolVector<Face3> TriangleMesh::get_faces() const {
if (!valid) {
return PoolVector<Face3>();
}
PoolVector<Face3> faces;
int ts = triangles.size();
faces.resize(triangles.size());
PoolVector<Face3>::Write w = faces.write();
PoolVector<Triangle>::Read r = triangles.read();
PoolVector<Vector3>::Read rv = vertices.read();
for (int i = 0; i < ts; i++) {
for (int j = 0; j < 3; j++) {
w[i].vertex[j] = rv[r[i].indices[j]];
}
}
w.release();
return faces;
}
TriangleMesh::TriangleMesh() {
valid = false;
max_depth = 0;
}

View File

@ -1,100 +0,0 @@
#ifndef TRIANGLE_MESH_H
#define TRIANGLE_MESH_H
/*************************************************************************/
/* triangle_mesh.h */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/*************************************************************************/
#include "core/math/face3.h"
#include "core/object/reference.h"
class TriangleMesh : public Reference {
GDCLASS(TriangleMesh, Reference);
public:
struct Triangle {
Vector3 normal;
int indices[3];
};
private:
PoolVector<Triangle> triangles;
PoolVector<Vector3> vertices;
struct BVH {
AABB aabb;
Vector3 center; //used for sorting
int left;
int right;
int face_index;
};
struct BVHCmpX {
bool operator()(const BVH *p_left, const BVH *p_right) const {
return p_left->center.x < p_right->center.x;
}
};
struct BVHCmpY {
bool operator()(const BVH *p_left, const BVH *p_right) const {
return p_left->center.y < p_right->center.y;
}
};
struct BVHCmpZ {
bool operator()(const BVH *p_left, const BVH *p_right) const {
return p_left->center.z < p_right->center.z;
}
};
int _create_bvh(BVH *p_bvh, BVH **p_bb, int p_from, int p_size, int p_depth, int &max_depth, int &max_alloc);
PoolVector<BVH> bvh;
int max_depth;
bool valid;
public:
bool is_valid() const;
bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const;
bool intersect_ray(const Vector3 &p_begin, const Vector3 &p_dir, Vector3 &r_point, Vector3 &r_normal) const;
bool intersect_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const;
bool inside_convex_shape(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, Vector3 p_scale = Vector3(1, 1, 1)) const;
Vector3 get_area_normal(const AABB &p_aabb) const;
PoolVector<Face3> get_faces() const;
const PoolVector<Triangle> &get_triangles() const { return triangles; }
const PoolVector<Vector3> &get_vertices() const { return vertices; }
void get_indices(PoolVector<int> *r_triangles_indices) const;
void create(const PoolVector<Vector3> &p_faces);
TriangleMesh();
};
#endif // TRIANGLE_MESH_H

View File

@ -1,210 +0,0 @@
/*************************************************************************/
/* triangulate.cpp */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/*************************************************************************/
#include "triangulate.h"
real_t Triangulate::get_area(const Vector<Vector2> &contour) {
int n = contour.size();
const Vector2 *c = &contour[0];
real_t A = 0.0;
for (int p = n - 1, q = 0; q < n; p = q++) {
A += c[p].cross(c[q]);
}
return A * 0.5f;
}
/*
* `is_inside_triangle` decides if a point P is inside the triangle
* defined by A, B, C.
*/
bool Triangulate::is_inside_triangle(real_t Ax, real_t Ay,
real_t Bx, real_t By,
real_t Cx, real_t Cy,
real_t Px, real_t Py,
bool include_edges) {
real_t ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
real_t cCROSSap, bCROSScp, aCROSSbp;
ax = Cx - Bx;
ay = Cy - By;
bx = Ax - Cx;
by = Ay - Cy;
cx = Bx - Ax;
cy = By - Ay;
apx = Px - Ax;
apy = Py - Ay;
bpx = Px - Bx;
bpy = Py - By;
cpx = Px - Cx;
cpy = Py - Cy;
aCROSSbp = ax * bpy - ay * bpx;
cCROSSap = cx * apy - cy * apx;
bCROSScp = bx * cpy - by * cpx;
if (include_edges) {
return ((aCROSSbp > 0) && (bCROSScp > 0) && (cCROSSap > 0));
} else {
return ((aCROSSbp >= 0) && (bCROSScp >= 0) && (cCROSSap >= 0));
}
}
bool Triangulate::snip(const Vector<Vector2> &p_contour, int u, int v, int w, int n, const Vector<int> &V, bool relaxed) {
int p;
real_t Ax, Ay, Bx, By, Cx, Cy, Px, Py;
const Vector2 *contour = &p_contour[0];
Ax = contour[V[u]].x;
Ay = contour[V[u]].y;
Bx = contour[V[v]].x;
By = contour[V[v]].y;
Cx = contour[V[w]].x;
Cy = contour[V[w]].y;
// It can happen that the triangulation ends up with three aligned vertices to deal with.
// In this scenario, making the check below strict may reject the possibility of
// forming a last triangle with these aligned vertices, preventing the triangulatiom
// from completing.
// To avoid that we allow zero-area triangles if all else failed.
float threshold = relaxed ? -CMP_EPSILON : CMP_EPSILON;
if (threshold > (((Bx - Ax) * (Cy - Ay)) - ((By - Ay) * (Cx - Ax)))) {
return false;
}
for (p = 0; p < n; p++) {
if ((p == u) || (p == v) || (p == w)) {
continue;
}
Px = contour[V[p]].x;
Py = contour[V[p]].y;
if (is_inside_triangle(Ax, Ay, Bx, By, Cx, Cy, Px, Py, relaxed)) {
return false;
}
}
return true;
}
bool Triangulate::triangulate(const Vector<Vector2> &contour, Vector<int> &result) {
/* allocate and initialize list of Vertices in polygon */
int n = contour.size();
if (n < 3) {
return false;
}
Vector<int> V;
V.resize(n);
/* we want a counter-clockwise polygon in V */
if (0 < get_area(contour)) {
for (int v = 0; v < n; v++) {
V.write[v] = v;
}
} else {
for (int v = 0; v < n; v++) {
V.write[v] = (n - 1) - v;
}
}
bool relaxed = false;
int nv = n;
/* remove nv-2 Vertices, creating 1 triangle every time */
int count = 2 * nv; /* error detection */
for (int v = nv - 1; nv > 2;) {
/* if we loop, it is probably a non-simple polygon */
if (0 >= (count--)) {
if (relaxed) {
//** Triangulate: ERROR - probable bad polygon!
return false;
} else {
// There may be aligned vertices that the strict
// checks prevent from triangulating. In this situation
// we are better off adding flat triangles than
// failing, so we relax the checks and try one last
// round.
// Only relaxing the constraints as a last resort avoids
// degenerate triangles when they aren't necessary.
count = 2 * nv;
relaxed = true;
}
}
/* three consecutive vertices in current polygon, <u,v,w> */
int u = v;
if (nv <= u) {
u = 0; /* previous */
}
v = u + 1;
if (nv <= v) {
v = 0; /* new v */
}
int w = v + 1;
if (nv <= w) {
w = 0; /* next */
}
if (snip(contour, u, v, w, nv, V, relaxed)) {
int a, b, c, s, t;
/* true names of the vertices */
a = V[u];
b = V[v];
c = V[w];
/* output Triangle */
result.push_back(a);
result.push_back(b);
result.push_back(c);
/* remove v from remaining polygon */
for (s = v, t = v + 1; t < nv; s++, t++) {
V.write[s] = V[t];
}
nv--;
/* reset error detection counter */
count = 2 * nv;
}
}
return true;
}

View File

@ -1,64 +0,0 @@
#ifndef TRIANGULATE_H
#define TRIANGULATE_H
/*************************************************************************/
/* triangulate.h */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/*************************************************************************/
#include "core/math/vector2.h"
#include "core/containers/vector.h"
/*
http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
*/
class Triangulate {
public:
// triangulate a contour/polygon, places results in STL vector
// as series of triangles.
static bool triangulate(const Vector<Vector2> &contour, Vector<int> &result);
// compute area of a contour/polygon
static real_t get_area(const Vector<Vector2> &contour);
// decide if point Px/Py is inside triangle defined by
// (Ax,Ay) (Bx,By) (Cx,Cy)
static bool is_inside_triangle(real_t Ax, real_t Ay,
real_t Bx, real_t By,
real_t Cx, real_t Cy,
real_t Px, real_t Py,
bool include_edges);
private:
static bool snip(const Vector<Vector2> &p_contour, int u, int v, int w, int n, const Vector<int> &V, bool relaxed);
};
#endif

View File

@ -1,122 +0,0 @@
#include "node.h"
#include "node_tree.h"
bool Node::is_in_tree() const {
return _in_tree;
}
Node *Node::get_parent() {
return _parent;
}
void Node::set_parent(Node *parent) {
if (_parent == parent) {
return;
}
if (_parent) {
notification(NOTIFICATION_UNPARENTED);
}
_parent = parent;
if (_parent) {
notification(NOTIFICATION_PARENTED);
}
}
int Node::get_child_count() {
return _children.size();
}
Node *Node::get_child(int index) {
return _children[index];
}
void Node::add_child(Node *child) {
ERR_FAIL_COND(!child);
ERR_FAIL_COND(child->get_parent());
_children.push_back(child);
child->set_parent(this);
if (_in_tree) {
child->set_tree(_tree);
child->notification(NOTIFICATION_EXIT_TREE);
}
notification(NOTIFICATION_CHILD_ADDED);
}
void Node::remove_child_index(int index) {
Node *c = _children[index];
_children.remove_keep_order(index);
c->set_parent(nullptr);
notification(NOTIFICATION_CHILD_REMOVED);
}
void Node::remove_child(Node *child) {
ERR_FAIL_COND(!child);
for (int i = 0; i < _children.size(); ++i) {
Node *c = _children[i];
if (c == child) {
_children.remove_keep_order(i);
child->set_parent(nullptr);
notification(NOTIFICATION_CHILD_REMOVED);
return;
}
}
}
NodeTree *Node::get_tree() {
return _tree;
}
void Node::set_tree(NodeTree *tree) {
if (_tree) {
_in_tree = false;
_notification(NOTIFICATION_EXIT_TREE);
}
_tree = tree;
if (_tree) {
_in_tree = true;
}
for (int i = 0; i < _children.size(); ++i) {
_children[i]->set_tree(tree);
}
if (_tree) {
_notification(NOTIFICATION_ENTER_TREE);
}
}
void Node::notification(const int what) {
_notification(what);
for (int i = 0; i < _children.size(); ++i) {
_children[i]->notification(what);
}
}
void Node::_notification(const int what) {
}
Node::Node() :
Object() {
_in_tree = false;
_parent = nullptr;
_tree = nullptr;
}
Node::~Node() {
for (int i = 0; i < _children.size(); ++i) {
delete _children[i];
}
_children.clear();
}

View File

@ -1,52 +0,0 @@
#ifndef NODE_H
#define NODE_H
#include "core/object.h"
#include "core/containers/vector.h"
class NodeTree;
class Node : public Object {
RCPP_OBJECT(Node, Object);
public:
enum {
NOTIFICATION_ENTER_TREE = 0,
NOTIFICATION_EXIT_TREE = 1,
NOTIFICATION_PARENTED = 2,
NOTIFICATION_UNPARENTED = 3,
NOTIFICATION_CHILD_ADDED = 4,
NOTIFICATION_CHILD_REMOVED = 5,
NOTIFICATION_CHILD_MOVED = 6,
NOTIFICATION_UPDATE = 7,
NOTIFICATION_TREE_WRITE_LOCKED = 8,
};
bool is_in_tree() const;
Node *get_parent();
void set_parent(Node *parent);
int get_child_count();
Node *get_child(int index);
void add_child(Node *child);
void remove_child_index(int index);
void remove_child(Node *child);
NodeTree *get_tree();
void set_tree(NodeTree *tree);
virtual void notification(const int what);
virtual void _notification(const int what);
Node();
~Node();
protected:
bool _in_tree;
Node * _parent;
Vector<Node *> _children;
NodeTree *_tree;
};
#endif

View File

@ -1,60 +0,0 @@
#include "node_tree.h"
#include "node.h"
Node *NodeTree::get_root() {
return _root_node;
}
void NodeTree::set_root(Node *root) {
if (_root_node) {
_root_node->set_tree(nullptr);
}
_root_node = root;
if (_root_node) {
_root_node->set_tree(this);
}
}
void NodeTree::update() {
if (!_root_node) {
return;
}
_root_node->notification(Node::NOTIFICATION_UPDATE);
if (_write_lock_requested) {
_rw_lock.write_lock();
_root_node->notification(Node::NOTIFICATION_TREE_WRITE_LOCKED);
_rw_lock.write_unlock();
_write_lock_requested = false;
}
}
void NodeTree::_send_update() {
if (_root_node) {
_root_node->notification(Node::NOTIFICATION_UPDATE);
}
}
float NodeTree::get_update_delta_time() {
return 0;
}
NodeTree::NodeTree() :
Object() {
_root_node = nullptr;
_update_interval = 0;
}
NodeTree::~NodeTree() {
if (_root_node) {
delete _root_node;
_root_node = nullptr;
}
}

View File

@ -1,35 +0,0 @@
#ifndef NODE_TREE_H
#define NODE_TREE_H
#include "core/object.h"
#include "core/threading/rw_lock.h"
class Node;
class NodeTree : public Object {
RCPP_OBJECT(NodeTree, Object);
public:
Node *get_root();
virtual void set_root(Node *root);
virtual void update();
void request_write_lock();
virtual float get_update_delta_time();
NodeTree();
~NodeTree();
protected:
virtual void _send_update();
Node *_root_node;
float _update_interval;
bool _write_lock_requested;
RWLock _rw_lock;
};
#endif

View File

@ -1,26 +0,0 @@
Copyright (c) 2013-2016, tinydir authors:
- Cong Xu
- Lautis Sun
- Baudouin Feildel
- Andargor <andargor@yahoo.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,79 +0,0 @@
TinyDir
=======
[![Build Status](https://travis-ci.org/cxong/tinydir.svg?branch=master)](https://travis-ci.org/cxong/tinydir)
[![Release](http://img.shields.io/github/release/cxong/tinydir.svg)](https://github.com/cxong/tinydir/releases/latest)
Lightweight, portable and easy to integrate C directory and file reader. TinyDir wraps dirent for POSIX and FindFirstFile for Windows.
Windows unicode is supported by defining `UNICODE` and `_UNICODE` before including `tinydir.h`.
Example
=======
There are two methods. Error checking omitted:
```C
tinydir_dir dir;
tinydir_open(&dir, "/path/to/dir");
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
printf("%s", file.name);
if (file.is_dir)
{
printf("/");
}
printf("\n");
tinydir_next(&dir);
}
tinydir_close(&dir);
```
```C
tinydir_dir dir;
int i;
tinydir_open_sorted(&dir, "/path/to/dir");
for (i = 0; i < dir.n_files; i++)
{
tinydir_file file;
tinydir_readfile_n(&dir, &file, i);
printf("%s", file.name);
if (file.is_dir)
{
printf("/");
}
printf("\n");
}
tinydir_close(&dir);
```
See the `/samples` folder for more examples, including an interactive command-line directory navigator.
Language
========
ANSI C, or C90.
Platforms
=========
POSIX and Windows supported. Open to the possibility of supporting other platforms.
License
=======
Simplified BSD; if you use tinydir you can comply by including `tinydir.h` or `COPYING` somewhere in your package.
Known Limitations
=================
- Limited path and filename sizes
- [Possible race condition bug if folder being read has changing content](https://github.com/cxong/tinydir/issues/13)

View File

@ -1,17 +0,0 @@
{
"name": "tinydir",
"description": "Lightweight, portable and easy to integrate C directory and file reader",
"license": "BSD-2-Clause",
"keywords": [
"dir",
"directory",
"file",
"reader",
"filesystem"
],
"src": [
"tinydir.h"
],
"version": "1.2.4",
"repo": "cxong/tinydir"
}