Code Monkey home page Code Monkey logo

Comments (5)

mitjap avatar mitjap commented on May 24, 2024 1

This is an example file with all triangle data. Surface is a regular grid mesh.

In leaf function I always return false for this example so that I can count all visits. Epsilon is set to 0 for reproducibility. If you set this to std::numeric_limits<float>::epsilon() + 1ulp traversal works as expected. For now I'll add a small epsilon value until better solution is found.

bvh-issue60.cpp
data.txt
mesh-preview.png

from bvh.

mitjap avatar mitjap commented on May 24, 2024 1

Thank you for fast response and for taking the time to fix the problem.

from bvh.

madmann91 avatar madmann91 commented on May 24, 2024

Here are a couple of things you could try:

  • Adding an epsilon to your ray direction/origin,
  • Avoiding rays that are very small,
  • Using the robust node intersector (IsRobust = true),

If none of those things help, please send me a small reproducing example, and I'll have a look at it.

from bvh.

madmann91 avatar madmann91 commented on May 24, 2024

TLDR:
Use master with the robust intersector.

Long version:

So I did a little investigation and that's what I found:

  • You are not setting up the scene properly. Primitives are connected in the wrong way, creating degenerate triangles.
  • I fixed that by changing the order in which coordinates are loaded from the stream, and validated it by exporting the result as OBJ and viewing it in Blender.
  • With that, the robust intersector was producing fewer traversal steps, but still too much in my opinion, so I dug into the code and fixed a bug where the inverse of the ray direction was computed via safe_inverse instead of a direct inverse.
  • The fast intersector returns wrong results here. That's expected. After all, it's using FMA to compute (pos - ray.org) / ray.dir as pos * ray.inv_dir + inv_org_dir (where inv_org_dir = -ray.org * ray.inv_dir). This means that if the direction is zero, then ray.inv_dir == inf and inv_org_dir == -inf and therefore the FMA returns NaN. To solve that problem, in fast mode, I don't compute the inverse exactly, but instead use FLT_MAX as an inverse if the direction is too close to zero. This explains why when the direction is small, the fast intersector returns incorrect results.

from bvh.

madmann91 avatar madmann91 commented on May 24, 2024

Regarding generating an OBJ file and making sure the geometry is correct, here's the relevant code (modified version of your code):

std::fstream stream("data.txt", std::fstream::in);

while (stream) {
    Vec3 a, b, c;
    
    // WRONG ORDER:
    //stream >> a[0] >> c[1] >> c[2]
    //       >> b[0] >> b[1] >> b[2]
    //       >> c[0] >> a[1] >> a[2];
    
    // CORRECT ORDER:
    stream >> a[0] >> a[1] >> a[2]
           >> b[0] >> b[1] >> b[2]
           >> c[0] >> c[1] >> c[2];

    if (stream)
        precomputed_tris.emplace_back(a, b, c);
}

std::ofstream s("data.obj");
int i = 0;
for (auto& precomp_tri : precomputed_tris) {
    auto tri = precomp_tri.convert_to_tri();
    s << "v " << tri.p0[0] << " " << tri.p0[1] << " " << tri.p0[2] << '\n';
    s << "v " << tri.p1[0] << " " << tri.p1[1] << " " << tri.p1[2] << '\n';
    s << "v " << tri.p2[0] << " " << tri.p2[1] << " " << tri.p2[2] << '\n';
    s << "f -1 -2 -3\n";
}

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.