Coder Social home page Coder Social logo

Instances about bvh HOT 8 CLOSED

madmann91 avatar madmann91 commented on May 18, 2024
Instances

from bvh.

Comments (8)

madmann91 avatar madmann91 commented on May 18, 2024 1

You can update the bounding boxes of every node in a for loop like so:

// It's important to process the nodes in reverse order, as this makes
// sure that children are processed before their parents.
for (auto& node : std::ranges::reverse_view { bvh.nodes }) {
  if (node.is_leaf()) {
    // refit node according to contents
    ...
  } else {
    auto& left  = bvh.nodes[node.index.first_id];
    auto& right = bvh.nodes[node.index.first_id + 1];
    node.set_bbox(left.get_bbox().extend(right.get_bbox()));
  }
}

I should probably add a method for this. Anyway, this method is very fast but should not be run in parallel as it expects the children to have been processed before the parents. The method used in the previous version of the repository works in parallel but is only faster when the BVH is extremely large.

from bvh.

madmann91 avatar madmann91 commented on May 18, 2024 1

Yes, they are still supported. The interface is a bit lower-level in the new version, so you only need an axis-aligned bounding box and a center point for each primitive you need to insert in the BVH. For you, this means something like:

std::vector<BBox> bboxes(obbs.size());
std::vector<Vec3> centers(obbs.size());
executor.for_each(0, tris.size(), [&] (size_t begin, size_t end) {
    for (size_t i = begin; i < end; ++i) {
        bboxes[i]  = get_box(obbs[i]);
        centers[i] = obbs[i].pos;
    }
});

typename bvh::v2::DefaultBuilder<Node>::Config config;
config.quality = bvh::v2::DefaultBuilder<Node>::Quality::High;
auto bvh = bvh::v2::DefaultBuilder<Node>::build(thread_pool, bboxes, centers, config);

You can get the AABB of an OBB this way (you do not need to transform the 8 points of a 3D OBB):

// Assuming the corners are obtained by multiplying the matrix with (+-1, +-1, +-1)
struct OBB {
    Mat3x3 mat;
    Vec3 pos;
};

inline Box3 get_bbox(const OBB& obb) {
    auto abs_mat = abs(obb.mat);
    auto half_extents = abs_mat * Vec3 { 1, 1, 1 };
    return Box3 { obb.pos - half_extents, obb.pos + half_extents };
};

Thanks, that works! But what about updates/refitting? In version 1 I could use the HierarchyRefitter, but I see no equivalent in version 2. I create the bvh like you describe:

bvh = ::bvh::v2::DefaultBuilder<pragma::bvh::Node>::build(GetThreadPool(), bboxes, centers, config);

After creation I need to update the bboxes/centers every frame, but I see no way to access those after the bvh has been built. Can the ReinsertionOptimizer be used for refitting somehow?

::bvh::v2::ReinsertionOptimizer<Node>::optimize(GetThreadPool(), bvh);

There is now a refitting method which allows you to refit the BVH serially (since #74 has been merged). I might add a parallel version later.

from bvh.

madmann91 avatar madmann91 commented on May 18, 2024

Hello! You may take a look at the example on custom primitives. An instance is just a primitive like any other, which itself contains a BVH and a matrix. Before intersecting the instance, you transform the ray by the inverse of the matrix. That's basically it.

from bvh.

Silverlan avatar Silverlan commented on May 18, 2024

Hello! You may take a look at the example on custom primitives. An instance is just a primitive like any other, which itself contains a BVH and a matrix. Before intersecting the instance, you transform the ray by the inverse of the matrix. That's basically it.

Are custom primitives still supported? I noticed that the example for them is no longer available, and I need a BVH with OBBs as primitives.

from bvh.

madmann91 avatar madmann91 commented on May 18, 2024

Yes, they are still supported. The interface is a bit lower-level in the new version, so you only need an axis-aligned bounding box and a center point for each primitive you need to insert in the BVH. For you, this means something like:

std::vector<BBox> bboxes(obbs.size());
std::vector<Vec3> centers(obbs.size());
executor.for_each(0, tris.size(), [&] (size_t begin, size_t end) {
    for (size_t i = begin; i < end; ++i) {
        bboxes[i]  = get_box(obbs[i]);
        centers[i] = obbs[i].pos;
    }
});

typename bvh::v2::DefaultBuilder<Node>::Config config;
config.quality = bvh::v2::DefaultBuilder<Node>::Quality::High;
auto bvh = bvh::v2::DefaultBuilder<Node>::build(thread_pool, bboxes, centers, config);

You can get the AABB of an OBB this way (you do not need to transform the 8 points of a 3D OBB):

// Assuming the corners are obtained by multiplying the matrix with (+-1, +-1, +-1)
struct OBB {
    Mat3x3 mat;
    Vec3 pos;
};

inline Box3 get_bbox(const OBB& obb) {
    auto abs_mat = abs(obb.mat);
    auto half_extents = abs_mat * Vec3 { 1, 1, 1 };
    return Box3 { obb.pos - half_extents, obb.pos + half_extents };
};

from bvh.

Silverlan avatar Silverlan commented on May 18, 2024

Yes, they are still supported. The interface is a bit lower-level in the new version, so you only need an axis-aligned bounding box and a center point for each primitive you need to insert in the BVH. For you, this means something like:

std::vector<BBox> bboxes(obbs.size());
std::vector<Vec3> centers(obbs.size());
executor.for_each(0, tris.size(), [&] (size_t begin, size_t end) {
    for (size_t i = begin; i < end; ++i) {
        bboxes[i]  = get_box(obbs[i]);
        centers[i] = obbs[i].pos;
    }
});

typename bvh::v2::DefaultBuilder<Node>::Config config;
config.quality = bvh::v2::DefaultBuilder<Node>::Quality::High;
auto bvh = bvh::v2::DefaultBuilder<Node>::build(thread_pool, bboxes, centers, config);

You can get the AABB of an OBB this way (you do not need to transform the 8 points of a 3D OBB):

// Assuming the corners are obtained by multiplying the matrix with (+-1, +-1, +-1)
struct OBB {
    Mat3x3 mat;
    Vec3 pos;
};

inline Box3 get_bbox(const OBB& obb) {
    auto abs_mat = abs(obb.mat);
    auto half_extents = abs_mat * Vec3 { 1, 1, 1 };
    return Box3 { obb.pos - half_extents, obb.pos + half_extents };
};

Thanks, that works! But what about updates/refitting? In version 1 I could use the HierarchyRefitter, but I see no equivalent in version 2. I create the bvh like you describe:

bvh = ::bvh::v2::DefaultBuilder<pragma::bvh::Node>::build(GetThreadPool(), bboxes, centers, config);

After creation I need to update the bboxes/centers every frame, but I see no way to access those after the bvh has been built. Can the ReinsertionOptimizer be used for refitting somehow?

::bvh::v2::ReinsertionOptimizer<Node>::optimize(GetThreadPool(), bvh);

from bvh.

tigrazone avatar tigrazone commented on May 18, 2024

I need example for use in vulkan, for example.
Bvh can be create on CPU but usage on GPU

from bvh.

madmann91 avatar madmann91 commented on May 18, 2024

I need example for use in vulkan, for example. Bvh can be create on CPU but usage on GPU

Just look at the traversal code in bvh.h. You can also look at https://madmann91.github.io/2021/01/06/bvhs-part-2.html for an introduction to BVH traversal. This article contains implementation code that you can just port to your target language (GLSL/HLSL).

from bvh.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.