feat: add CUDA raytracing benchmark and refactor VoxRaytracer::RayData to use DataAllocator for host/device memory management.

This commit is contained in:
AndreaRigoni
2026-03-04 17:47:18 +00:00
parent eb76521060
commit adedbcc37c
12 changed files with 1013 additions and 364 deletions

View File

@@ -39,48 +39,48 @@ namespace uLib {
////////////////////////////////////////////////////////////////////////////////
void VoxRaytracer::RayData::AddElement(Id_t id, float L) {
if (m_Count >= m_Data.size()) {
size_t new_size = m_Data.size() == 0 ? 128 : m_Data.size() * 2;
m_Data.resize(new_size);
}
Element el = {id, L};
m_Data.push_back(el);
m_Data[m_Count] = el;
m_Count++;
m_TotalLength += L;
}
void VoxRaytracer::RayData::AppendRay(const VoxRaytracer::RayData &in) {
if (unlikely(!in.m_Data.size())) {
if (unlikely(in.m_Count == 0)) {
std::cout << "Warinig: PoCA on exit border!\n";
return;
} else if (unlikely(!m_Data.size())) {
m_Data = in.m_Data;
} else if (unlikely(m_Count == 0)) {
m_Data.resize(in.m_Count);
for (size_t i = 0; i < in.m_Count; ++i) {
m_Data[i] = in.m_Data[i];
}
m_Count = in.m_Count;
m_TotalLength = in.m_TotalLength;
std::cout << "Warinig: PoCA on entrance border!\n";
return;
} else {
// Opzione 1) un voxel in piu' //
if (in.m_Data.size() > 0) {
m_Data.insert(m_Data.end(), in.m_Data.begin(), in.m_Data.end());
if (in.m_Count > 0) {
if (m_Count + in.m_Count > m_Data.size()) {
m_Data.resize(m_Count + in.m_Count);
}
for (size_t i = 0; i < in.m_Count; ++i) {
m_Data[m_Count + i] = in.m_Data[i];
}
m_Count += in.m_Count;
}
// Opzione 2) merge dei voxel nel poca.
// RayData::Element &e1 = m_Data.back();
// const RayData::Element &e2 = in.m_Data.front();
// if(e1.vox_id == e2.vox_id)
// {
// m_Data.reserve(m_Data.size() + in.m_Data.size() - 1);
// e1.L += e2.L; //fix//
// m_Data.insert(m_Data.end(), in.m_Data.begin()+1,
// in.m_Data.end());
// }
// else {
// m_Data.reserve(m_Data.size() + in.m_Data.size());
// m_Data.insert(m_Data.end(), in.m_Data.begin(),
// in.m_Data.end());
// }
m_TotalLength += in.m_TotalLength;
}
}
void VoxRaytracer::RayData::PrintSelf(std::ostream &o) {
o << "Ray: total lenght " << m_TotalLength << "\n";
std::vector<Element>::iterator it;
for (it = m_Data.begin(); it < m_Data.end(); ++it)
o << "[ " << (*it).vox_id << ", " << (*it).L << "] \n";
for (size_t i = 0; i < m_Count; ++i)
o << "[ " << m_Data[i].vox_id << ", " << m_Data[i].L << "] \n";
}
////////////////////////////////////////////////////////////////////////////////
@@ -144,14 +144,21 @@ VoxRaytracer::RayData
VoxRaytracer::TraceBetweenPoints(const HPoint3f &in,
const HPoint3f &out) const {
RayData ray;
// get the local points and the direction vector
// local to image means in the normalized voxel space where the size
// of the voxel is 1 in all dimensions
Vector4f pt1 = m_Image->GetLocalPoint(in);
Vector4f pt2 = m_Image->GetLocalPoint(out);
Vector4f s = pt2 - pt1;
// l is the total length of the ray in normalized voxel space
float l = s.head(3).norm();
// L is the length of the ray between two grid lines in grid
Vector3f L(l / s(0), l / s(1), l / s(2));
// Vector3f scale; // FIXXX
// Vector3f scale; // TODO: FIX Scaling
// scale << (m_Image->GetWorldMatrix() * Vector4f(1,0,0,0)).norm(),
// (m_Image->GetWorldMatrix() * Vector4f(0,1,0,0)).norm(),
// (m_Image->GetWorldMatrix() * Vector4f(0,0,1,0)).norm();
@@ -174,21 +181,23 @@ VoxRaytracer::TraceBetweenPoints(const HPoint3f &in,
float d;
while (l > 0) {
// find which is the minimum of the offsets to the next grid line
// it will be also the actual normalized voxel ray length
d = offset.minCoeff(&id);
// see if the voxel is inside the grid (we are still inside image)
if (m_Image->IsInsideGrid(vid)) {
// add the voxel to the ray with mapping id and length scaled
ray.AddElement(m_Image->Map(vid), d * m_scale(id));
}
// nan check //
// if(unlikely(!isFinite(d * scale(id)))) {
// std:: cout << "NAN in raytracer\n";
// exit(1);
// }
// move to the next voxel
vid(id) += (int)fast_sign(s(id));
// update the remaining length
l -= d;
// update the offsets
offset.array() -= d;
offset(id) = fmin(L(id), l);
}