Skip to content

Commit

Permalink
Rename to primitive reference
Browse files Browse the repository at this point in the history
  • Loading branch information
pshriwise committed Jan 17, 2024
1 parent 81ef877 commit bd63a72
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 50 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ src/geometry/closest.cpp
src/error.cpp
src/mesh_manager_interface.cpp
src/ray_tracing_interface.cpp
src/triangle_ref.cpp
src/triangle_intersect.cpp
src/util/str_utils.cpp
src/xdg.cpp
)
Expand Down
4 changes: 2 additions & 2 deletions include/xdg/geometry_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace xdg
{

struct MeshManager; // Forward declaration
struct TriangleRef; // Forward declaration
struct PrimitiveRef; // Forward declaration

struct GeometryUserData {
MeshID surface_id {ID_NONE}; //! ID of the surface this geometry data is associated with
MeshManager* mesh_manager {nullptr}; //! Pointer to the mesh manager for this geometry
TriangleRef* tri_ref_buffer {nullptr}; //! Pointer to the triangles in the geometry
PrimitiveRef* prim_ref_buffer {nullptr}; //! Pointer to the mesh primitives in the geometry
double box_bump; //! Bump distance for the bounding boxes in this geometry
};

Expand Down
7 changes: 2 additions & 5 deletions include/xdg/triangle_ref.h → include/xdg/primitive_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@

namespace xdg {

struct TriangleRef; // Forward declaration

// TODO: could be a more generic primitive ref?
struct TriangleRef {
MeshID triangle_id {ID_NONE};
struct PrimitiveRef {
MeshID primitive_id {ID_NONE};
Sense sense {Sense::UNSET};
};

Expand Down
4 changes: 2 additions & 2 deletions include/xdg/ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct RTCDHit : RTCHit {
}

// data members
const TriangleRef* tri_ref {nullptr}; //!< Pointer to the triangle reference for this hit
const PrimitiveRef* primitive_ref {nullptr}; //!< Pointer to the primitive reference for this hit
Vec3da dNg; //!< Double precision version of the primitive normal
};

Expand Down Expand Up @@ -133,7 +133,7 @@ struct RTCDPointQuery : RTCPointQuery {
unsigned int primID = RTC_INVALID_GEOMETRY_ID; //<! ID of the nearest primitive
unsigned int geomID = RTC_INVALID_GEOMETRY_ID; //<! ID of the nearest geometry
double dblx, dbly, dblz; //<! Double precision version of the query location
const TriangleRef* tri_ref {nullptr}; //!< Pointer to the triangle reference for this hit
const PrimitiveRef* primitive_ref {nullptr}; //!< Pointer to the primitive reference for this hit
double dradius; //!< Double precision version of the query distance
};

Expand Down
6 changes: 3 additions & 3 deletions include/xdg/ray_tracing_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "xdg/constants.h"
#include "xdg/embree_interface.h"
#include "xdg/mesh_manager_interface.h"
#include "xdg/triangle_ref.h"
#include "xdg/primitive_ref.h"
#include "xdg/geometry_data.h"

namespace xdg
Expand Down Expand Up @@ -50,7 +50,7 @@ class RayTracer {
void closest(MeshID volume,
const Position& origin,
double& dist,
TriangleRef& triangle);
PrimitiveRef& triangle);

void closest(MeshID volume,
const Position& origin,
Expand Down Expand Up @@ -87,7 +87,7 @@ class RayTracer {
double numerical_precision_ {1e-3};

// storage
std::unordered_map<MeshID, std::vector<TriangleRef>> triangle_storage_map_;
std::unordered_map<MeshID, std::vector<PrimitiveRef>> primitive_ref_storage_;
};

} // namespace xdg
Expand Down
12 changes: 8 additions & 4 deletions include/xdg/xdg.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class XDG {
ray_tracing_interface_->register_all_volumes(mesh_manager_);
}

// Geometric Queries
MeshID find_volume(const Position& point,
const Direction& direction) const;

// Geometric Measurements
double measure_volume(MeshID volume) const;
double measure_surface_area(MeshID surface) const;
Expand All @@ -34,8 +38,8 @@ class XDG {
}

// Accessors
const RayTracer* ray_tracing_interface() const {
return ray_tracing_interface_.get();
const std::shared_ptr<RayTracer> ray_tracing_interface() const {
return ray_tracing_interface_;
}

const MeshManager* mesh_manager() const {
Expand All @@ -44,8 +48,8 @@ class XDG {

// Private methods
private:
double _triangle_volume_contribution(const TriangleRef& triangle) const;
double _triangle_area_contribution(const TriangleRef& triangle) const;
double _triangle_volume_contribution(const PrimitiveRef& triangle) const;
double _triangle_area_contribution(const PrimitiveRef& triangle) const;

private:
const std::shared_ptr<RayTracer> ray_tracing_interface_ {std::make_shared<RayTracer>()};
Expand Down
30 changes: 15 additions & 15 deletions src/ray_tracing_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "xdg/geometry_data.h"
#include "xdg/ray_tracing_interface.h"
#include "xdg/ray.h"
#include "xdg/triangle_ref.h"
#include "xdg/primitive_ref.h"

namespace xdg {

Expand Down Expand Up @@ -34,8 +34,8 @@ RayTracer::register_volume(const std::shared_ptr<MeshManager> mesh_manager,
{
// allocate storage for this volume
auto volume_elements = mesh_manager->get_volume_elements(volume_id);
this->triangle_storage_map_[volume_id].resize(volume_elements.size());
auto& triangle_storage = this->triangle_storage_map_[volume_id];
this->primitive_ref_storage_[volume_id].resize(volume_elements.size());
auto& triangle_storage = this->primitive_ref_storage_[volume_id];

auto volume_surfaces = mesh_manager->get_volume_surfaces(volume_id);
int storage_offset {0};
Expand All @@ -49,9 +49,9 @@ RayTracer::register_volume(const std::shared_ptr<MeshManager> mesh_manager,

auto surface_elements = mesh_manager->get_surface_elements(surface_id);
for (int i = 0; i < surface_elements.size(); ++i) {
auto& triangle_ref = triangle_storage[i + storage_offset];
triangle_ref.triangle_id = surface_elements[i];
triangle_ref.sense = triangle_sense;
auto& primitive_ref = triangle_storage[i + storage_offset];
primitive_ref.primitive_id = surface_elements[i];
primitive_ref.sense = triangle_sense;
}
storage_offset += surface_elements.size();
}
Expand All @@ -61,7 +61,7 @@ RayTracer::register_volume(const std::shared_ptr<MeshManager> mesh_manager,
rtcSetSceneBuildQuality(volume_scene, RTC_BUILD_QUALITY_HIGH);
this->volume_to_scene_map_[volume_id] = volume_scene;

TriangleRef* tri_ref_ptr = triangle_storage.data();
PrimitiveRef* tri_ref_ptr = triangle_storage.data();

// compute the bounding box of the volume
auto volume_bounding_box = mesh_manager->volume_bounding_box(volume_id);
Expand All @@ -87,13 +87,13 @@ RayTracer::register_volume(const std::shared_ptr<MeshManager> mesh_manager,
GeometryUserData surface_data;
surface_data.surface_id = surface;
surface_data.mesh_manager = mesh_manager.get();
surface_data.tri_ref_buffer = tri_ref_ptr + buffer_start;
surface_data.prim_ref_buffer = tri_ref_ptr + buffer_start;
user_data_map_[surface_geometry] = surface_data;

rtcSetGeometryUserData(surface_geometry, &user_data_map_[surface_geometry]);

for (int i = 0; i < surface_triangles.size(); ++i) {
auto& triangle_ref = surface_data.tri_ref_buffer[i];
auto& triangle_ref = surface_data.prim_ref_buffer[i];
// triangle_ref.embree_surface = embree_surface;
}
buffer_start += surface_triangles.size();
Expand Down Expand Up @@ -175,7 +175,7 @@ RayTracer::ray_fire(MeshID volume,
void RayTracer::closest(MeshID volume,
const Position& point,
double& distance,
TriangleRef& triangle_ref)
PrimitiveRef& triangle_ref)
{
RTCScene scene = volume_to_scene_map_[volume];

Expand All @@ -193,14 +193,14 @@ void RayTracer::closest(MeshID volume,
}

distance = query.dradius;
triangle_ref = *query.tri_ref;
triangle_ref = *query.primitive_ref;
}

void RayTracer::closest(MeshID volume,
const Position& point,
double& distance)
{
TriangleRef triangle_ref;
PrimitiveRef triangle_ref;
closest(volume, point, distance, triangle_ref);
}

Expand Down Expand Up @@ -252,14 +252,14 @@ Direction RayTracer::get_normal(MeshID surface,
point_query.set_point(point);
point_query.set_radius(INFTY);

TriangleRef triangle_ref;
closest(surface_vols.first, point, dist, triangle_ref);
PrimitiveRef primitive_ref;
closest(surface_vols.first, point, dist, primitive_ref);

// TODO: bring this back when we have a better way to handle this
// if (geom_data.surface_id != surface) {
// fatal_error("Point {} was closest to surface {}, not surface {}, in volume {}.", point, geom_data.surface_id, surface, surface_vols.first);
// }
element = triangle_ref.triangle_id;
element = primitive_ref.primitive_id;
}

// return the normal of the selected triangle
Expand Down
27 changes: 14 additions & 13 deletions src/triangle_ref.cpp → src/triangle_intersect.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <algorithm> // for find

#include "xdg/geometry/closest.h"
#include "xdg/triangle_ref.h"
#include "xdg/primitive_ref.h"
#include "xdg/geometry_data.h"
#include "xdg/geometry/plucker.h"
#include "xdg/ray.h"
Expand Down Expand Up @@ -38,8 +38,8 @@ void TriangleBoundsFunc(RTCBoundsFunctionArguments* args)
const GeometryUserData* user_data = (const GeometryUserData*)args->geometryUserPtr;
const MeshManager* mesh_manager = user_data->mesh_manager;

const TriangleRef& tri_ref = user_data->tri_ref_buffer[args->primID];
BoundingBox bounds = mesh_manager->element_bounding_box(tri_ref.triangle_id);
const PrimitiveRef& primitive_ref = user_data->prim_ref_buffer[args->primID];
BoundingBox bounds = mesh_manager->element_bounding_box(primitive_ref.primitive_id);

args->bounds_o->lower_x = bounds.min_x - user_data->box_bump;
args->bounds_o->lower_y = bounds.min_y - user_data->box_bump;
Expand All @@ -53,9 +53,9 @@ void TriangleIntersectionFunc(RTCIntersectFunctionNArguments* args) {
const GeometryUserData* user_data = (const GeometryUserData*)args->geometryUserPtr;
const MeshManager* mesh_manager = user_data->mesh_manager;

const TriangleRef& tri_ref = user_data->tri_ref_buffer[args->primID];
const PrimitiveRef& primitive_ref = user_data->prim_ref_buffer[args->primID];

auto vertices = mesh_manager->triangle_vertices(tri_ref.triangle_id);
auto vertices = mesh_manager->triangle_vertices(primitive_ref.primitive_id);

RTCDRayHit* rayhit = (RTCDRayHit*)args->rayhit;
RTCDRay& ray = rayhit->ray;
Expand All @@ -72,9 +72,10 @@ void TriangleIntersectionFunc(RTCIntersectFunctionNArguments* args) {

if (plucker_dist > rayhit->ray.dtfar) return;

Direction normal = mesh_manager->triangle_normal(tri_ref.triangle_id);
Direction normal = mesh_manager->triangle_normal(primitive_ref.primitive_id);
// if this is a normal ray fire, flip the normal as needed
if (tri_ref.sense == Sense::REVERSE && rayhit->ray.rf_type != RayFireType::FIND_VOLUME) normal = -normal;
if (primitive_ref.sense == Sense::REVERSE && rayhit->ray.rf_type != RayFireType::FIND_VOLUME)
normal = -normal;

if (rayhit->ray.rf_type == RayFireType::VOLUME) {
if (orientation_cull(rayhit->ray.ddir, normal, rayhit->ray.orientation)) return;
Expand All @@ -89,7 +90,7 @@ void TriangleIntersectionFunc(RTCIntersectFunctionNArguments* args) {
// set the hit information
rayhit->hit.geomID = args->geomID;
rayhit->hit.primID = args->primID;
rayhit->hit.tri_ref = &tri_ref;
rayhit->hit.primitive_ref = &primitive_ref;

rayhit->hit.dNg = normal;
}
Expand All @@ -101,8 +102,8 @@ bool TriangleClosestFunc(RTCPointQueryFunctionArguments* args) {

const MeshManager* mesh_manager = user_data->mesh_manager;

const TriangleRef& tri_ref = user_data->tri_ref_buffer[args->primID];
auto vertices = mesh_manager->triangle_vertices(tri_ref.triangle_id);
const PrimitiveRef& primitive_ref = user_data->prim_ref_buffer[args->primID];
auto vertices = mesh_manager->triangle_vertices(primitive_ref.primitive_id);

RTCDPointQuery* query = (RTCDPointQuery*) args->query;
Position p {query->dblx, query->dbly, query->dblz};
Expand All @@ -113,7 +114,7 @@ bool TriangleClosestFunc(RTCPointQueryFunctionArguments* args) {
if ( dist < query->dradius) {
query->radius = dist;
query->dradius = dist;
query->tri_ref = &tri_ref;
query->primitive_ref = &primitive_ref;
query->primID = args->primID;
query->geomID = args->geomID;
return true;
Expand All @@ -125,9 +126,9 @@ bool TriangleClosestFunc(RTCPointQueryFunctionArguments* args) {
void TriangleOcclusionFunc(RTCOccludedFunctionNArguments* args) {
const GeometryUserData* user_data = (const GeometryUserData*) args->geometryUserPtr;
const MeshManager* mesh_manager = user_data->mesh_manager;
const TriangleRef& tri_ref = user_data->tri_ref_buffer[args->primID];
const PrimitiveRef& primitive_ref = user_data->prim_ref_buffer[args->primID];

auto vertices = mesh_manager->triangle_vertices(tri_ref.triangle_id);
auto vertices = mesh_manager->triangle_vertices(primitive_ref.primitive_id);

// get the double precision ray from the args
RTCDRay* ray = (RTCDRay*) args->ray;
Expand Down
12 changes: 12 additions & 0 deletions src/xdg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
#include "xdg/geometry/measure.h"
namespace xdg {

MeshID XDG::find_volume(const Position& point,
const Direction& direction) const
{
for (auto volume : mesh_manager()->volumes()) {
if (ray_tracing_interface()->point_in_volume(volume, point, &direction)) {
return volume;
}
}
return ID_NONE;
}


double XDG::measure_volume(MeshID volume) const
{
double volume_total {0.0};
Expand Down
10 changes: 5 additions & 5 deletions tests/test_normal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ TEST_CASE("Test Get Normal")

// move the point closer to the positive x surface
origin = {4.0, 0.0, 0.0};
TriangleRef triangle_ref;
rti->closest(volume, origin, nearest_distance, triangle_ref);
PrimitiveRef primitive_ref;
rti->closest(volume, origin, nearest_distance, primitive_ref);
REQUIRE_THAT(nearest_distance, Catch::Matchers::WithinAbs(1.0, 1e-6));

MeshID surface {mm->surfaces()[3]};

// call for the normal w/o a triangle, it should be the same as the returned triangle from the closest call
Direction normal = rti->get_normal(surface, origin);
REQUIRE(normal == mm->triangle_normal(triangle_ref.triangle_id));
REQUIRE(normal == mm->triangle_normal(primitive_ref.primitive_id));

// move the origin, but pass the triangle
// This should result in the same normal as well b/c the triangle is used intead of a call to 'closest'
origin = {-2.0, 0.0, 0.0};
std::vector<MeshID> exclude_primitives {triangle_ref.triangle_id};
std::vector<MeshID> exclude_primitives {primitive_ref.primitive_id};
normal = rti->get_normal(surface, origin, &exclude_primitives);
REQUIRE(normal == mm->triangle_normal(triangle_ref.triangle_id));
REQUIRE(normal == mm->triangle_normal(primitive_ref.primitive_id));
}

0 comments on commit bd63a72

Please sign in to comment.