-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathVisCheck.cpp
More file actions
141 lines (122 loc) · 4.71 KB
/
Copy pathVisCheck.cpp
File metadata and controls
141 lines (122 loc) · 4.71 KB
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
#include "VisCheck.h"
#include <cmath>
#include <algorithm>
#include <limits>
#include <iostream>
const size_t LEAF_THRESHOLD = 4;
VisCheck::VisCheck(const std::string& optimizedGeometryFile) {
if (!geometry.LoadFromFile(optimizedGeometryFile)) {
std::cerr << "Failed to load optimized file: " << optimizedGeometryFile << std::endl;
}
for (const auto& mesh : geometry.meshes) {
bvhNodes.push_back(BuildBVH(mesh));
}
}
std::unique_ptr<BVHNode> VisCheck::BuildBVH(const std::vector<TriangleCombined>& tris) {
auto node = std::make_unique<BVHNode>();
if (tris.empty()) return node;
AABB bounds = tris[0].ComputeAABB();
for (size_t i = 1; i < tris.size(); ++i) {
AABB triAABB = tris[i].ComputeAABB();
bounds.min.x = std::min(bounds.min.x, triAABB.min.x);
bounds.min.y = std::min(bounds.min.y, triAABB.min.y);
bounds.min.z = std::min(bounds.min.z, triAABB.min.z);
bounds.max.x = std::max(bounds.max.x, triAABB.max.x);
bounds.max.y = std::max(bounds.max.y, triAABB.max.y);
bounds.max.z = std::max(bounds.max.z, triAABB.max.z);
}
node->bounds = bounds;
if (tris.size() <= LEAF_THRESHOLD) {
node->triangles = tris;
return node;
}
Vector3 diff = bounds.max - bounds.min;
int axis = (diff.x > diff.y && diff.x > diff.z) ? 0 : ((diff.y > diff.z) ? 1 : 2);
std::vector<TriangleCombined> sortedTris = tris;
std::sort(sortedTris.begin(), sortedTris.end(), [axis](const TriangleCombined& a, const TriangleCombined& b) {
AABB aabbA = a.ComputeAABB();
AABB aabbB = b.ComputeAABB();
float centerA, centerB;
if (axis == 0) {
centerA = (aabbA.min.x + aabbA.max.x) / 2.0f;
centerB = (aabbB.min.x + aabbB.max.x) / 2.0f;
}
else if (axis == 1) {
centerA = (aabbA.min.y + aabbA.max.y) / 2.0f;
centerB = (aabbB.min.y + aabbB.max.y) / 2.0f;
}
else {
centerA = (aabbA.min.z + aabbA.max.z) / 2.0f;
centerB = (aabbB.min.z + aabbB.max.z) / 2.0f;
}
return centerA < centerB;
});
size_t mid = sortedTris.size() / 2;
std::vector<TriangleCombined> leftTris(sortedTris.begin(), sortedTris.begin() + mid);
std::vector<TriangleCombined> rightTris(sortedTris.begin() + mid, sortedTris.end());
node->left = BuildBVH(leftTris);
node->right = BuildBVH(rightTris);
return node;
}
bool VisCheck::IntersectBVH(const BVHNode* node, const Vector3& rayOrigin, const Vector3& rayDir, float maxDistance, float& hitDistance) {
if (!node->bounds.RayIntersects(rayOrigin, rayDir)) {
return false;
}
bool hit = false;
if (node->IsLeaf()) {
for (const auto& tri : node->triangles) {
float t;
if (RayIntersectsTriangle(rayOrigin, rayDir, tri, t)) {
if (t < maxDistance && t < hitDistance) {
hitDistance = t;
hit = true;
}
}
}
}
else {
if (node->left) {
hit |= IntersectBVH(node->left.get(), rayOrigin, rayDir, maxDistance, hitDistance);
}
if (node->right) {
hit |= IntersectBVH(node->right.get(), rayOrigin, rayDir, maxDistance, hitDistance);
}
}
return hit;
}
bool VisCheck::IsPointVisible(const Vector3& point1, const Vector3& point2)
{
Vector3 rayDir = { point2.x - point1.x, point2.y - point1.y, point2.z - point1.z };
float distance = std::sqrt(rayDir.dot(rayDir));
rayDir = { rayDir.x / distance, rayDir.y / distance, rayDir.z / distance };
float hitDistance = std::numeric_limits<float>::max();
for (const auto& bvhRoot : bvhNodes) {
if (IntersectBVH(bvhRoot.get(), point1, rayDir, distance, hitDistance)) {
if (hitDistance < distance) {
return false;
}
}
}
return true;
}
bool VisCheck::RayIntersectsTriangle(const Vector3& rayOrigin, const Vector3& rayDir, const TriangleCombined& triangle, float& t)
{
const float EPSILON = 1e-7f;
Vector3 edge1 = triangle.v1 - triangle.v0;
Vector3 edge2 = triangle.v2 - triangle.v0;
Vector3 h = rayDir.cross(edge2);
float a = edge1.dot(h);
if (a > -EPSILON && a < EPSILON)
return false;
float f = 1.0f / a;
Vector3 s = rayOrigin - triangle.v0;
float u = f * s.dot(h);
if (u < 0.0f || u > 1.0f)
return false;
Vector3 q = s.cross(edge1);
float v = f * rayDir.dot(q);
if (v < 0.0f || u + v > 1.0f)
return false;
t = f * edge2.dot(q);
return (t > EPSILON);
}