Skip to content

Commit

Permalink
Modified Ray class
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq committed Aug 8, 2024
1 parent f1980b9 commit 5873abc
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 42 deletions.
142 changes: 105 additions & 37 deletions src/simplnx/Common/Ray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace nx::core
* simplify and better describe values used in GeometryMath. The rays initial alignment
* is assumed to be with the Y-axis.
*/
template <typename T, bool CacheEndV = false>
template <typename T>
class Ray
{
public:
Expand Down Expand Up @@ -46,10 +46,6 @@ class Ray
, m_Angle(std::move(ang))
, m_Length(length)
{
if constexpr(CacheEndV)
{
m_Endpoint = calculateEndpoint();
}
}

/**
Expand Down Expand Up @@ -107,21 +103,16 @@ class Ray
* @brief Sets a new origin point.
* @param origin
*/
void setOrigin(const PointType& origin)
virtual void setOrigin(const PointType& origin)
{
m_Origin = origin;

if constexpr(CacheEndV)
{
m_Endpoint = calculateEndpoint();
}
}

/**
* @brief Sets a new Euler angle.
* @param ang
*/
void setEuler(const ZXZEulerType& ang)
virtual void setEuler(const ZXZEulerType& ang)
{
m_Angle = ang;
}
Expand All @@ -139,37 +130,19 @@ class Ray
* @brief Sets a new ray length.
* @param length
*/
void setLength(LengthType length)
virtual void setLength(LengthType length)
{
m_Length = length;

if constexpr(CacheEndV)
{
m_Endpoint = calculateEndpoint();
}
}

/**
* @brief Returns the end point determined by the origin point, Euler angle, and length.
* Based on the assumption the point is initially aligned with the global axis' and the that this vector specifically aligned with the local y-axis.
* @return PointType
*/
PointType getEndPoint() const
{
if constexpr(CacheEndV)
{
return m_Endpoint;
}
else
{
return calculateEndpoint();
}
}

template <bool E = CacheEndV, class = std::enable_if_t<E, bool>>
const PointType& getEndPointRef() const
virtual PointType getEndPoint() const
{
return m_Endpoint;
return calculateEndpoint();
}

/**
Expand Down Expand Up @@ -235,14 +208,10 @@ class Ray
}

protected:
private:
PointType m_Origin;
ZXZEulerType m_Angle;
LengthType m_Length;

// Optional caching for speed
PointType m_Endpoint = {};

PointType calculateEndpoint() const
{
const auto sin1 = std::sin(m_Angle[0]);
Expand All @@ -261,4 +230,103 @@ class Ray
return m_Origin + (localXRotationVec * m_Length) + (localYRotationVec * m_Length) + (localZRotationVec * m_Length);
}
};

template <typename T>
class CachedRay : public Ray<T>
{
public:
/**
* @brief Creates a Ray from an origin point, ZXZEuler angle, and target length.
* @param origin
* @param ang
* @param length
*/
CachedRay(const Ray<T>::PointType& origin, Ray<T>::ZXZEulerType ang, Ray<T>::LengthType length)

Check failure on line 244 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'

Check failure on line 244 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::ZXZEulerType'

Check failure on line 244 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::LengthType'

Check failure on line 244 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'

Check failure on line 244 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'

Check failure on line 244 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::ZXZEulerType'

Check failure on line 244 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::LengthType'

Check failure on line 244 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'
: Ray<T>(origin, std::move(ang), length)
{
m_Endpoint = this->calculateEndpoint();
}

/**
* @brief Copy constructor
* @param other
*/
CachedRay(const CachedRay& other)
: m_Endpoint(other.m_Endpoint)
, Ray<T>(other)
{
}

/**
* @brief Move constructor
* @param other
*/
CachedRay(CachedRay&& other) noexcept
: m_Endpoint(std::move(other.m_Endpoint))
, Ray<T>(std::move(other))
{
}

~CachedRay() = default;

/**
* @brief Copy assignment operator
* @param rhs
* @return CachedRay&
*/
CachedRay& operator=(const CachedRay& rhs)
{
this->m_Origin = rhs.m_Origin;
this->m_Angle = rhs.m_Angle;
this->m_Length = rhs.m_Length;
m_Endpoint = rhs.m_Endpoint;
return *this;
}

/**
* @brief Move assignment operator
* @param rhs
* @return Ray&
*/
CachedRay& operator=(CachedRay&& rhs) noexcept
{
this->m_Origin = std::move(rhs.m_Origin);
this->m_Angle = std::move(rhs.m_Angle);
this->m_Length = std::move(rhs.m_Length);
m_Endpoint = std::move(rhs.m_Endpoint);
return *this;
}

void setOrigin(const Ray<T>::PointType& origin) override

Check failure on line 300 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'

Check failure on line 300 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'
{
this->m_Origin = origin;
m_Endpoint = Ray<T>::calculateEndpoint();
}

void setLength(Ray<T>::LengthType length) override

Check failure on line 306 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::LengthType'

Check failure on line 306 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::LengthType'
{
this->m_Length = length;
m_Endpoint = this->calculateEndpoint();
}

void setEuler(const Ray<T>::ZXZEulerType& ang) override

Check failure on line 312 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::ZXZEulerType'

Check failure on line 312 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::ZXZEulerType'
{
this->m_Angle = ang;
m_Endpoint = this->calculateEndpoint();
}

const Ray<T>::PointType& getEndPointRef() const

Check failure on line 318 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'

Check failure on line 318 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'
{
return m_Endpoint;
}

Ray<T>::PointType getEndPoint() const override

Check failure on line 323 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'

Check failure on line 323 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'
{
return m_Endpoint;
}

private:
// Optional caching for speed
Ray<T>::PointType m_Endpoint = {};

Check failure on line 330 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (macos-12)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'

Check failure on line 330 in src/simplnx/Common/Ray.hpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, clang++-14)

missing 'typename' prior to dependent type name 'Ray<T>::PointType'
};
} // namespace nx::core
10 changes: 5 additions & 5 deletions src/simplnx/Utilities/Math/GeometryMath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ bool DoesRayIntersectBox(const nx::core::Ray<T>& ray, const nx::core::BoundingBo
* @return bool
*/
template <typename T>
bool DoesRayIntersectBox(const nx::core::Ray<T, true>& ray, const nx::core::BoundingBox3D<T>& bounds)
bool DoesRayIntersectBox(const nx::core::CachedRay<T>& ray, const nx::core::BoundingBox3D<T>& bounds)
{
const auto& origin = ray.getOriginRef();
const auto& min = bounds.getMinPoint();
Expand Down Expand Up @@ -468,7 +468,7 @@ char RayCrossesTriangle(const Ray<T>& ray, const Point3D<T>& p0, const Point3D<T
* @return bool
*/
template <typename T>
char RayCrossesTriangle(const Ray<T, true>& ray, const Point3D<T>& p0, const Point3D<T>& p1, const Point3D<T>& p2)
char RayCrossesTriangle(const CachedRay<T>& ray, const Point3D<T>& p0, const Point3D<T>& p1, const Point3D<T>& p2)
{
T vol0 = FindTetrahedronVolume(ray.getOrigin(), p0, p1, ray.getEndPointRef());
T vol1 = FindTetrahedronVolume(ray.getOrigin(), p1, p2, ray.getEndPointRef());
Expand Down Expand Up @@ -552,7 +552,7 @@ char RayIntersectsPlane(const Ray<T>& ray, const Point3D<T>& p0, const Point3D<T
* @return bool
*/
template <typename T>
char RayIntersectsPlane(const Ray<T, true>& ray, const Point3D<T>& p0, const Point3D<T>& p1, const Point3D<T>& p2)
char RayIntersectsPlane(const CachedRay<T>& ray, const Point3D<T>& p0, const Point3D<T>& p1, const Point3D<T>& p2)
{
Vec3<T> normal = FindPlaneNormalVector(p0, p1, p2);
T d = FindPlaneCoefficients(p0, normal);
Expand Down Expand Up @@ -639,7 +639,7 @@ char RayIntersectsTriangle(const Ray<T>& ray, const nx::core::Point3D<T>& p0, co
* @return char
*/
template <typename T>
char RayIntersectsTriangle(const Ray<T, true>& ray, const nx::core::Point3D<T>& p0, const nx::core::Point3D<T>& p1, const nx::core::Point3D<T>& p2)
char RayIntersectsTriangle(const CachedRay<T>& ray, const nx::core::Point3D<T>& p0, const nx::core::Point3D<T>& p1, const nx::core::Point3D<T>& p2)
{
char code = RayIntersectsPlane(ray, p0, p1, p2);

Expand Down Expand Up @@ -713,7 +713,7 @@ char IsPointInPolyhedron(const nx::core::TriangleGeom& triangleGeomRef, const st
eulerAngles[1] = w * std::sin(t);

// Generate and add ray to point to find other end [optimized version with lifetime caching]
Ray<T, true> ray(point, ZXZEuler(eulerAngles.data()), radius);
CachedRay<T> ray(point, ZXZEuler(eulerAngles.data()), radius);

bool doNextCheck = false;
for(usize face = 0; face < numFaces; face++)
Expand Down

0 comments on commit 5873abc

Please sign in to comment.