• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Razakhel / RaZ / 8116431801

01 Mar 2024 07:24PM UTC coverage: 79.236% (+0.06%) from 79.177%
8116431801

push

github

Razakhel
[Data/MeshDistanceField] Added an MDF structure

- This computes signed distances to the nearest mesh geometry within a 3D grid using a BVH

- This can be used for some algorithms, such as rendering ones that can use sphere marching through the 3D volume created from this field

55 of 58 new or added lines in 3 files covered. (94.83%)

11 existing lines in 3 files now uncovered.

7983 of 10075 relevant lines covered (79.24%)

2078.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.78
/src/RaZ/Data/MeshDistanceField.cpp
1
#include "RaZ/Data/BoundingVolumeHierarchy.hpp"
2
#include "RaZ/Data/Image.hpp"
3
#include "RaZ/Data/MeshDistanceField.hpp"
4
#include "RaZ/Math/MathUtils.hpp"
5
#include "RaZ/Utils/Ray.hpp"
6
#include "RaZ/Utils/Threading.hpp"
7

8
namespace Raz {
9

10
MeshDistanceField::MeshDistanceField(const AABB& area, unsigned int width, unsigned int height, unsigned int depth)
6✔
11
  : m_area{ area }, m_width{ width }, m_height{ height }, m_depth{ depth }, m_distanceField(width * height * depth, std::numeric_limits<float>::max()) {
6✔
12
  if (m_width < 2 || m_height < 2 || m_depth < 2)
6✔
13
    throw std::invalid_argument("[MeshDistanceField] The width, height & depth must all be equal to or greater than 2.");
2✔
14
}
8✔
15

16
float MeshDistanceField::getDistance(std::size_t widthIndex, std::size_t heightIndex, std::size_t depthIndex) const {
114✔
17
  return m_distanceField[computeIndex(widthIndex, heightIndex, depthIndex)];
114✔
18
}
19

20
void MeshDistanceField::compute(std::size_t sampleCount) {
7✔
21
  if (m_bvh == nullptr)
7✔
22
    throw std::runtime_error("[MeshDistanceField] Computing a mesh distance field requires having given a BVH.");
1✔
23

24
  std::fill(m_distanceField.begin(), m_distanceField.end(), std::numeric_limits<float>::max());
6✔
25

26
  const Vec3f areaExtents = m_area.getMaxPosition() - m_area.getMinPosition();
6✔
27
  const float widthStep   = areaExtents.x() / static_cast<float>(m_width - 1);
6✔
28
  const float heightStep  = areaExtents.y() / static_cast<float>(m_height - 1);
6✔
29
  const float depthStep   = areaExtents.z() / static_cast<float>(m_depth - 1);
6✔
30

31
  Threading::parallelize(0, m_depth, [this, widthStep, heightStep, depthStep, sampleCount] (const Threading::IndexRange& range) {
6✔
32
    for (std::size_t depthIndex = range.beginIndex; depthIndex < range.endIndex; ++depthIndex) {
68✔
33
      for (std::size_t heightIndex = 0; heightIndex < m_height; ++heightIndex) {
381✔
34
        for (std::size_t widthIndex = 0; widthIndex < m_width; ++widthIndex) {
3,263✔
35
          const Vec3f rayPos = m_area.getMinPosition() + Vec3f(static_cast<float>(widthIndex) * widthStep,
2,919✔
36
                                                               static_cast<float>(heightIndex) * heightStep,
5,840✔
37
                                                               static_cast<float>(depthIndex) * depthStep);
5,843✔
38
          float& distance = m_distanceField[computeIndex(widthIndex, heightIndex, depthIndex)];
2,918✔
39

40
          for (const Vec3f& rayDir : MathUtils::computeFibonacciSpherePoints(sampleCount)) {
78,685✔
41
            RayHit hit {};
75,655✔
42

43
            if (!m_bvh->query(Ray(rayPos, rayDir), &hit))
75,655✔
44
              continue;
62,304✔
45

46
            if (rayDir.dot(hit.normal) > 0.f)
13,499✔
47
              hit.distance = -hit.distance;
6,601✔
48

49
            if (std::abs(hit.distance) < std::abs(distance))
13,501✔
50
              distance = hit.distance;
3,716✔
51
          }
2,795✔
52
        }
53
      }
54
    }
55
  });
28✔
56
}
6✔
57

58
std::vector<Image> MeshDistanceField::recoverSlices() const {
2✔
59
  std::vector<Image> slices;
2✔
60
  slices.reserve(m_depth);
2✔
61

62
  for (std::size_t depthIndex = 0; depthIndex < m_depth; ++depthIndex) {
6✔
63
    Image& slice = slices.emplace_back(m_width, m_height, ImageColorspace::GRAY, ImageDataType::FLOAT);
4✔
64

65
    for (std::size_t heightIndex = 0; heightIndex < m_height; ++heightIndex) {
14✔
66
      for (std::size_t widthIndex = 0; widthIndex < m_width; ++widthIndex) {
42✔
67
        const float distance = getDistance(widthIndex, heightIndex, depthIndex);
32✔
68
        slice.setPixel(widthIndex, heightIndex, distance);
32✔
69
      }
70
    }
71
  }
72

73
  return slices;
2✔
NEW
74
}
×
75

76
} // namespace Raz
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc