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

Razakhel / RaZ / 8100413393

29 Feb 2024 05:51PM UTC coverage: 79.209% (+0.03%) from 79.184%
8100413393

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

53 of 56 new or added lines in 3 files covered. (94.64%)

9 existing lines in 2 files now uncovered.

7955 of 10043 relevant lines covered (79.21%)

2104.73 hits per line

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

97.67
/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
  for (std::size_t depthIndex = 0; depthIndex < m_depth; ++depthIndex) {
46✔
32
    for (std::size_t heightIndex = 0; heightIndex < m_height; ++heightIndex) {
374✔
33
      for (std::size_t widthIndex = 0; widthIndex < m_width; ++widthIndex) {
3,282✔
34
        const Vec3f rayPos = m_area.getMinPosition() + Vec3f(static_cast<float>(widthIndex) * widthStep,
2,948✔
35
                                                             static_cast<float>(heightIndex) * heightStep,
5,896✔
36
                                                             static_cast<float>(depthIndex) * depthStep);
5,896✔
37
        float& distance = m_distanceField[computeIndex(widthIndex, heightIndex, depthIndex)];
2,948✔
38

39
        for (const Vec3f& rayDir : MathUtils::computeFibonacciSpherePoints(sampleCount)) {
79,012✔
40
          RayHit hit {};
76,064✔
41

42
          if (!m_bvh->query(Ray(rayPos, rayDir), &hit))
76,064✔
43
            continue;
62,557✔
44

45
          if (rayDir.dot(hit.normal) > 0.f)
13,507✔
46
            hit.distance = -hit.distance;
6,603✔
47

48
          if (std::abs(hit.distance) < std::abs(distance))
13,507✔
49
            distance = hit.distance;
3,717✔
50
        }
2,948✔
51
      }
52
    }
53
  }
54
}
6✔
55

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

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

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

71
  return slices;
2✔
NEW
72
}
×
73

74
} // 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