Skip to content

Commit

Permalink
Add missing device functions to primal (#1422)
Browse files Browse the repository at this point in the history
* Add missing device functions to primal
* Make dimension a static constexpr function
  • Loading branch information
adayton1 authored Sep 24, 2024
1 parent 842a624 commit e80b31a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/axom/primal/geometry/Point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ AXOM_HOST_DEVICE bool operator==(const Point<T, NDIMS>& lhs,
* \brief Inequality comparison operator for points
*/
template <typename T, int NDIMS>
bool operator!=(const Point<T, NDIMS>& lhs, const Point<T, NDIMS>& rhs);
AXOM_HOST_DEVICE bool operator!=(const Point<T, NDIMS>& lhs,
const Point<T, NDIMS>& rhs);

/*!
* \brief Overloaded output operator for points
Expand Down Expand Up @@ -113,7 +114,8 @@ class Point
* \return d the dimension of the point.
* \post d >= 1.
*/
static int dimension() { return NDIMS; };
AXOM_HOST_DEVICE
static constexpr int dimension() { return NDIMS; };

/// \name Overloaded [] operator methods
///@{
Expand Down
15 changes: 11 additions & 4 deletions src/axom/primal/geometry/Segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ class Segment;
* \brief Equality comparison operator for Segment
*/
template <typename T, int NDIMS>
bool operator==(const Segment<T, NDIMS>& lhs, const Segment<T, NDIMS>& rhs);
AXOM_HOST_DEVICE bool operator==(const Segment<T, NDIMS>& lhs,
const Segment<T, NDIMS>& rhs);

/*!
* \brief Inequality comparison operator for Segment
*/
template <typename T, int NDIMS>
bool operator!=(const Segment<T, NDIMS>& lhs, const Segment<T, NDIMS>& rhs);
AXOM_HOST_DEVICE bool operator!=(const Segment<T, NDIMS>& lhs,
const Segment<T, NDIMS>& rhs);

/*!
* \brief Overloaded output operator for Segment
Expand Down Expand Up @@ -130,22 +132,26 @@ class Segment
/*!
* \brief Returns the length of the segment
*/
double length() const { return VectorType(m_source, m_target).norm(); }
AXOM_HOST_DEVICE double length() const
{
return VectorType(m_source, m_target).norm();
}

/*!
* \brief Returns a vector normal to the segment
*
* \note Only available in 2D
*/
template <int TDIM = NDIMS>
typename std::enable_if<TDIM == 2, VectorType>::type normal() const
AXOM_HOST_DEVICE typename std::enable_if<TDIM == 2, VectorType>::type normal() const
{
return VectorType {m_target[1] - m_source[1], m_source[0] - m_target[0]};
}

/*!
* \brief Equality comparison operator for segments
*/
AXOM_HOST_DEVICE
friend inline bool operator==(const Segment& lhs, const Segment& rhs)
{
return lhs.m_source == rhs.m_source && lhs.m_target == rhs.m_target;
Expand All @@ -154,6 +160,7 @@ class Segment
/*!
* \brief Inequality operator for segments
*/
AXOM_HOST_DEVICE
friend inline bool operator!=(const Segment& lhs, const Segment& rhs)
{
return !(lhs == rhs);
Expand Down
20 changes: 12 additions & 8 deletions src/axom/primal/geometry/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ AXOM_HOST_DEVICE Vector<T, NDIMS> operator-(const Point<T, NDIMS>& h,
* \return C resulting vector from unary negation.
*/
template <typename T, int NDIMS>
Vector<T, NDIMS> operator-(const Vector<T, NDIMS>& vec1);
AXOM_HOST_DEVICE Vector<T, NDIMS> operator-(const Vector<T, NDIMS>& vec1);

/*!
* \brief Scalar multiplication of vector; Scalar on rhs.
Expand Down Expand Up @@ -131,7 +131,8 @@ AXOM_HOST_DEVICE Vector<T, NDIMS> operator*(const T scalar,
* \pre scalar != 0.0
*/
template <typename T, int NDIMS>
Vector<T, NDIMS> operator/(const Vector<T, NDIMS>& vec, const T scalar);
AXOM_HOST_DEVICE Vector<T, NDIMS> operator/(const Vector<T, NDIMS>& vec,
const T scalar);

/*!
* \brief Overloaded output operator for vectors
Expand Down Expand Up @@ -229,7 +230,7 @@ class Vector
* \post d >= 1.
*/
AXOM_HOST_DEVICE
int dimension() const { return NDIMS; };
static constexpr int dimension() { return NDIMS; };

/*!
* \brief Access operator for individual components.
Expand Down Expand Up @@ -273,6 +274,7 @@ class Vector
/*!
* \brief Inequality operator for points
*/
AXOM_HOST_DEVICE
friend bool operator!=(const Vector& lhs, const Vector& rhs)
{
return !(lhs == rhs);
Expand Down Expand Up @@ -413,6 +415,7 @@ class Vector
* \param [in] z the z--coordinate of the vector. Default is 0.0.
* \return v a Vector instance with the given coordinates.
*/
AXOM_HOST_DEVICE
static Vector make_vector(const T& x, const T& y, const T& z = 0.0);

private:
Expand Down Expand Up @@ -656,7 +659,8 @@ AXOM_HOST_DEVICE Point<T, NDIMS> operator-(const Point<T, NDIMS>& P,

//------------------------------------------------------------------------------
template <typename T, int NDIMS>
inline Vector<T, NDIMS> operator/(const Vector<T, NDIMS>& vec, const T scalar)
AXOM_HOST_DEVICE inline Vector<T, NDIMS> operator/(const Vector<T, NDIMS>& vec,
const T scalar)
{
Vector<T, NDIMS> result(vec);
result /= scalar;
Expand All @@ -683,7 +687,7 @@ AXOM_HOST_DEVICE Vector<T, NDIMS> operator-(const Point<T, NDIMS>& h,

//------------------------------------------------------------------------------
template <typename T, int NDIMS>
inline Vector<T, NDIMS> operator-(const Vector<T, NDIMS>& vec1)
AXOM_HOST_DEVICE inline Vector<T, NDIMS> operator-(const Vector<T, NDIMS>& vec1)
{
Vector<T, NDIMS> result(vec1);
result.negate();
Expand All @@ -700,9 +704,9 @@ std::ostream& operator<<(std::ostream& os, const Vector<T, NDIMS>& vec)

//------------------------------------------------------------------------------
template <typename T, int NDIMS>
inline Vector<T, NDIMS> Vector<T, NDIMS>::make_vector(const T& x,
const T& y,
const T& z)
AXOM_HOST_DEVICE inline Vector<T, NDIMS> Vector<T, NDIMS>::make_vector(const T& x,
const T& y,
const T& z)
{
T tmp_array[3] = {x, y, z};
return Vector(tmp_array, NDIMS);
Expand Down

0 comments on commit e80b31a

Please sign in to comment.