BVH class - Work in progress
The BVH
class is used to create a bounding volume hierarchy for the terrain mesh. It contains the necessary attributes to describe the BVH, such as the triangles, the BVH tree, the precomputed triangles, and the primitive ids. It also contains methods to compute the intersection altitude of a ray with the terrain mesh which is used to find the altitude of the trees in the terrain mesh.
Unfortunaly this class is not usable right now, it is still a work in progress. The following code snippet shows the current implementation of the BVH class but there might be an error in how the triangles are being added to the BVH tree, in particular maked_triangles
might not be the correct triangles to add to the BVH tree for our use case.
class BVH {
using Scalar = float;
using Vec3 = bvh::v2::Vec<Scalar, 3>;
using BBox = bvh::v2::BBox<Scalar, 3>;
using Tri = bvh::v2::Tri<Scalar, 3>;
using Node = bvh::v2::Node<Scalar, 3>;
using Bvh = bvh::v2::Bvh<Node>;
using Ray = bvh::v2::Ray<Scalar, 3>;
using PrecomputedTri = bvh::v2::PrecomputedTri<Scalar>;
private:
std::vector<Tri> M_tris;
Bvh M_bvh;
std::vector<PrecomputedTri> M_precomputed_tris;
std::vector<size_t> M_prim_ids;
static constexpr bool should_permute = true;
public:
BVH() = default;
BVH(const Mesh &terrainMesh) {
auto marked_triangles = markedtriangles(terrainMesh, "terrain");
for (auto &triangle : marked_triangles) {
auto &p0 = triangle.get().point(0);
auto &p1 = triangle.get().point(1);
auto &p2 = triangle.get().point(2);
M_tris.push_back(
{{static_cast<float>(p0.x()), static_cast<float>(p0.y()),
static_cast<float>(p0.z())},
{static_cast<float>(p1.x()), static_cast<float>(p1.y()),
static_cast<float>(p1.z())},
{static_cast<float>(p2.x()), static_cast<float>(p2.y()),
static_cast<float>(p2.z())}});
}
....
};
double intersectionAltitude(double x, double y) const {
auto ray = Ray{
Vec3(x, y, 1000), // Ray origin
Vec3(0., 0., -1.), // Ray direction
0., // Minimum intersection distance
2000 // Maximum intersection distance
};
....}
};