Commit fae8409c authored by Haocheng LIU's avatar Haocheng LIU

Add float version operations for vtkm::Math Pi()

This commit also removes the old static_cast<vtkm::float32>vtkm::Pi() usages and
fix serveral conversion warnings.
parent 7836fff2
# Add float version operations for vtkm::Math Pi()
Now PI related functions are evalulated at compile time as constexpr functions.
It also removes the old static_cast<T>vtkm::Pi() usages with
template ones and fix serveral conversion warnings.
......@@ -210,13 +210,12 @@ void mouseMove(int x, int y)
if (mouse_state == 0)
{
vtkm::Float32 pideg = static_cast<vtkm::Float32>(vtkm::Pi_2());
Quaternion newRotX;
newRotX.setEulerAngles(-0.2f * dx * pideg / 180.0f, 0.0f, 0.0f);
newRotX.setEulerAngles(-0.2f * dx * vtkm::Pi_180f(), 0.0f, 0.0f);
qrot.mul(newRotX);
Quaternion newRotY;
newRotY.setEulerAngles(0.0f, 0.0f, -0.2f * dy * pideg / 180.0f);
newRotY.setEulerAngles(0.0f, 0.0f, -0.2f * dy * vtkm::Pi_180f());
qrot.mul(newRotY);
}
lastx = x;
......
......@@ -155,13 +155,12 @@ void mouseMove(int x, int y)
if (mouse_state == 0)
{
vtkm::Float32 pi = static_cast<float>(vtkm::Pi());
Quaternion newRotX;
newRotX.setEulerAngles(-0.2f * dx * pi / 180.0f, 0.0f, 0.0f);
newRotX.setEulerAngles(-0.2f * dx * vtkm::Pi_180f(), 0.0f, 0.0f);
qrot.mul(newRotX);
Quaternion newRotY;
newRotY.setEulerAngles(0.0f, 0.0f, -0.2f * dy * pi / 180.0f);
newRotY.setEulerAngles(0.0f, 0.0f, -0.2f * dy * vtkm::Pi_180f());
qrot.mul(newRotY);
}
lastx = x;
......
......@@ -233,13 +233,12 @@ void mouseMove(int x, int y)
if (mouse_state == 0)
{
vtkm::Float32 pi = static_cast<float>(vtkm::Pi());
Quaternion newRotX;
newRotX.setEulerAngles(-0.2f * dx * pi / 180.0f, 0.0f, 0.0f);
newRotX.setEulerAngles(-0.2f * dx * vtkm::Pi_180f(), 0.0f, 0.0f);
qrot.mul(newRotX);
Quaternion newRotY;
newRotY.setEulerAngles(0.0f, 0.0f, -0.2f * dy * pi / 180.0f);
newRotY.setEulerAngles(0.0f, 0.0f, -0.2f * dy * vtkm::Pi_180f());
qrot.mul(newRotY);
}
lastx = x;
......
......@@ -204,13 +204,12 @@ void mouseMove(int x, int y)
if (mouse_state == 0)
{
vtkm::Float32 pi = static_cast<float>(vtkm::Pi());
Quaternion newRotX;
newRotX.setEulerAngles(-0.2f * dx * pi / 180.0f, 0.0f, 0.0f);
newRotX.setEulerAngles(-0.2f * dx * vtkm::Pi_180f(), 0.0f, 0.0f);
qrot.mul(newRotX);
Quaternion newRotY;
newRotY.setEulerAngles(0.0f, 0.0f, -0.2f * dy * pi / 180.0f);
newRotY.setEulerAngles(0.0f, 0.0f, -0.2f * dy * vtkm::Pi_180f());
qrot.mul(newRotY);
}
lastx = x;
......
......@@ -53,53 +53,162 @@ namespace vtkm
{
//-----------------------------------------------------------------------------
/// Returns the constant 2 times Pi.
namespace detail
{
template <typename T>
struct FloatingPointReturnType
{
using ctype = typename vtkm::VecTraits<T>::ComponentType;
using representable_as_float_type = std::integral_constant<bool,
((sizeof(ctype) < sizeof(float)) || std::is_same<ctype, vtkm::Float32>::value)>;
using Type = typename std::conditional<representable_as_float_type::value,
vtkm::Float32,
vtkm::Float64>::type;
};
} // namespace detail
/// Returns the constant 2 times Pi in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 TwoPif()
{
return 6.28318530717958647692528676655900576f;
}
/// Returns the constant Pi in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pif()
{
return 3.14159265358979323846264338327950288f;
}
/// Returns the constant Pi halves in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pi_2f()
{
return 1.57079632679489661923132169163975144f;
}
/// Returns the constant Pi thirds in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pi_3f()
{
return 1.04719755119659774615421446109316762f;
}
/// Returns the constant Pi fourths in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pi_4f()
{
return 0.78539816339744830961566084581987572f;
}
/// Returns the constant Pi one hundred and eightieth in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pi_180f()
{
return 0.01745329251994329547437168059786927f;
}
/// Returns the constant 2 times Pi in float64.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 TwoPi()
{
return 6.28318530717958647692528676655900576;
}
/// Returns the constant Pi.
/// Returns the constant Pi in float64.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi()
{
return 3.14159265358979323846264338327950288;
}
/// Returns the constant Pi halves.
/// Returns the constant Pi halves in float64.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi_2()
{
return 1.57079632679489661923132169163975144;
}
/// Returns the constant Pi thirds.
/// Returns the constant Pi thirds in float64.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi_3()
{
return 1.04719755119659774615421446109316762;
}
/// Returns the constant Pi fourths.
/// Returns the constant Pi fourths in float64.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi_4()
{
return 0.78539816339744830961566084581987572;
}
namespace detail
/// Returns the constant Pi one hundred and eightieth in float64.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi_180()
{
return 0.01745329251994329547437168059786927;
}
/// Returns the constant 2 times Pi.
///
template <typename T>
struct FloatingPointReturnType
{
using ctype = typename vtkm::VecTraits<T>::ComponentType;
using representable_as_float_type = std::integral_constant<bool,
((sizeof(ctype) < sizeof(float)) || std::is_same<ctype, vtkm::Float32>::value)>;
using Type = typename std::conditional<representable_as_float_type::value,
vtkm::Float32,
vtkm::Float64>::type;
};
} // namespace detail
static constexpr inline VTKM_EXEC_CONT auto TwoPi() -> typename detail::FloatingPointReturnType<T>::Type
{
using FT = typename detail::FloatingPointReturnType<T>::Type;
using RAFT = typename detail::FloatingPointReturnType<T>::representable_as_float_type;
return static_cast<FT>(RAFT::value ? TwoPif() : TwoPi());
}
/// Returns the constant Pi.
///
template <typename T>
static constexpr inline VTKM_EXEC_CONT auto Pi() -> typename detail::FloatingPointReturnType<T>::Type
{
using FT = typename detail::FloatingPointReturnType<T>::Type;
using RAFT = typename detail::FloatingPointReturnType<T>::representable_as_float_type;
return static_cast<FT>(RAFT::value ? Pif() : Pi());
}
/// Returns the constant Pi halves.
///
template <typename T>
static constexpr inline VTKM_EXEC_CONT auto Pi_2() -> typename detail::FloatingPointReturnType<T>::Type
{
using FT = typename detail::FloatingPointReturnType<T>::Type;
using RAFT = typename detail::FloatingPointReturnType<T>::representable_as_float_type;
return static_cast<FT>(RAFT::value ? Pi_2f() : Pi_2());
}
/// Returns the constant Pi thirds.
///
template <typename T>
static constexpr inline VTKM_EXEC_CONT auto Pi_3() -> typename detail::FloatingPointReturnType<T>::Type
{
using FT = typename detail::FloatingPointReturnType<T>::Type;
using RAFT = typename detail::FloatingPointReturnType<T>::representable_as_float_type;
return static_cast<FT>(RAFT::value ? Pi_3f() : Pi_3());
}
/// Returns the constant Pi fourths.
///
template <typename T>
static constexpr inline VTKM_EXEC_CONT auto Pi_4() -> typename detail::FloatingPointReturnType<T>::Type
{
using FT = typename detail::FloatingPointReturnType<T>::Type;
using RAFT = typename detail::FloatingPointReturnType<T>::representable_as_float_type;
return static_cast<FT>(RAFT::value ? Pi_4f() : Pi_4());
}
/// Returns the constant Pi one hundred and eightieth.
///
template <typename T>
static constexpr inline VTKM_EXEC_CONT auto Pi_180() -> typename detail::FloatingPointReturnType<T>::Type
{
using FT = typename detail::FloatingPointReturnType<T>::Type;
using RAFT = typename detail::FloatingPointReturnType<T>::representable_as_float_type;
return static_cast<FT>(RAFT::value ? Pi_180f() : Pi_180());
}
/// Compute the sine of \p x.
///
......
......@@ -189,6 +189,16 @@ static inline VTKM_EXEC_CONT vtkm::Float64 {0}(vtkm::Float64 x, vtkm::Float64 y)
return {1};
}}
'''.format(vtkmname, expression)
def unary_pi_related_function_no_vec(vtkmname):
return '''template <typename T>
static constexpr inline VTKM_EXEC_CONT auto {0}() -> typename detail::FloatingPointReturnType<T>::Type
{{
using FT = typename detail::FloatingPointReturnType<T>::Type;
using RAFT = typename detail::FloatingPointReturnType<T>::representable_as_float_type;
return static_cast<FT>(RAFT::value ? {0}f() : {0}());
}}
'''.format(vtkmname)
)\
$extend(unary_math_function)\
$extend(unary_math_function_no_vec)\
......@@ -196,59 +206,133 @@ $extend(unary_Vec_function)\
$extend(unary_template_function_no_vec)\
$extend(binary_math_function)\
$extend(binary_template_function)\
$extend(unary_pi_related_function_no_vec)\
\
// clang-format off
namespace vtkm
{
//-----------------------------------------------------------------------------
/// Returns the constant 2 times Pi.
namespace detail
{
template <typename T>
struct FloatingPointReturnType
{
using ctype = typename vtkm::VecTraits<T>::ComponentType;
using representable_as_float_type = std::integral_constant<bool,
((sizeof(ctype) < sizeof(float)) || std::is_same<ctype, vtkm::Float32>::value)>;
using Type = typename std::conditional<representable_as_float_type::value,
vtkm::Float32,
vtkm::Float64>::type;
};
} // namespace detail
/// Returns the constant 2 times Pi in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 TwoPif()
{
return 6.28318530717958647692528676655900576f;
}
/// Returns the constant Pi in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pif()
{
return 3.14159265358979323846264338327950288f;
}
/// Returns the constant Pi halves in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pi_2f()
{
return 1.57079632679489661923132169163975144f;
}
/// Returns the constant Pi thirds in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pi_3f()
{
return 1.04719755119659774615421446109316762f;
}
/// Returns the constant Pi fourths in float32.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pi_4f()
{
return 0.78539816339744830961566084581987572f;
}
/// Returns the constant Pi one hundred and eightieth in float32.
///
static inline VTKM_EXEC_CONT vtkm::Float64 TwoPi()
static constexpr inline VTKM_EXEC_CONT vtkm::Float32 Pi_180f()
{
return 0.01745329251994329547437168059786927f;
}
/// Returns the constant 2 times Pi in float64.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 TwoPi()
{
return 6.28318530717958647692528676655900576;
}
/// Returns the constant Pi.
/// Returns the constant Pi in float64.
///
static inline VTKM_EXEC_CONT vtkm::Float64 Pi()
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi()
{
return 3.14159265358979323846264338327950288;
}
/// Returns the constant Pi halves.
/// Returns the constant Pi halves in float64.
///
static inline VTKM_EXEC_CONT vtkm::Float64 Pi_2()
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi_2()
{
return 1.57079632679489661923132169163975144;
}
/// Returns the constant Pi thirds.
/// Returns the constant Pi thirds in float64.
///
static inline VTKM_EXEC_CONT vtkm::Float64 Pi_3()
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi_3()
{
return 1.04719755119659774615421446109316762;
}
/// Returns the constant Pi fourths.
/// Returns the constant Pi fourths in float64.
///
static inline VTKM_EXEC_CONT vtkm::Float64 Pi_4()
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi_4()
{
return 0.78539816339744830961566084581987572;
}
namespace detail
{
template <typename T>
struct FloatingPointReturnType
/// Returns the constant Pi one hundred and eightieth in float64.
///
static constexpr inline VTKM_EXEC_CONT vtkm::Float64 Pi_180()
{
using ctype = typename vtkm::VecTraits<T>::ComponentType;
using representable_as_float_type = std::integral_constant<bool,
((sizeof(ctype) < sizeof(float)) || std::is_same<ctype, vtkm::Float32>::value)>;
using Type = typename std::conditional<representable_as_float_type::value,
vtkm::Float32,
vtkm::Float64>::type;
};
} // namespace detail
return 0.01745329251994329547437168059786927;
}
/// Returns the constant 2 times Pi.
///
$unary_pi_related_function_no_vec('TwoPi')\
/// Returns the constant Pi.
///
$unary_pi_related_function_no_vec('Pi')\
/// Returns the constant Pi halves.
///
$unary_pi_related_function_no_vec('Pi_2')\
/// Returns the constant Pi thirds.
///
$unary_pi_related_function_no_vec('Pi_3')\
/// Returns the constant Pi fourths.
///
$unary_pi_related_function_no_vec('Pi_4')\
/// Returns the constant Pi one hundred and eightieth.
///
$unary_pi_related_function_no_vec('Pi_180')\
/// Compute the sine of \p x.
///
......
......@@ -156,7 +156,7 @@ template <typename T>
VTKM_EXEC_CONT vtkm::Matrix<T, 4, 4> Transform3DRotate(T angleDegrees,
const vtkm::Vec<T, 3>& axisOfRotation)
{
T angleRadians = static_cast<T>(vtkm::Pi() / 180) * angleDegrees;
T angleRadians = vtkm::Pi_180<T>() * angleDegrees;
const vtkm::Vec<T, 3> normAxis = vtkm::Normal(axisOfRotation);
T sinAngle = vtkm::Sin(angleRadians);
T cosAngle = vtkm::Cos(angleRadians);
......
......@@ -272,10 +272,9 @@ VTKM_EXEC typename FieldVecType::ComponentType CellInterpolate(
ParametricCoordType angle = vtkm::ATan2(pcoords[1] - 0.5f, pcoords[0] - 0.5f);
if (angle < 0)
{
angle += static_cast<ParametricCoordType>(2 * vtkm::Pi());
angle += 2 * vtkm::Pi<ParametricCoordType>();
}
const ParametricCoordType deltaAngle =
static_cast<ParametricCoordType>(2 * vtkm::Pi() / numPoints);
const ParametricCoordType deltaAngle = 2 * vtkm::Pi<ParametricCoordType>() / numPoints;
vtkm::IdComponent firstPointIndex =
static_cast<vtkm::IdComponent>(vtkm::Floor(angle / deltaAngle));
vtkm::IdComponent secondPointIndex = firstPointIndex + 1;
......
......@@ -286,8 +286,8 @@ inline vtkm::Vec<float, 3> MshToLab(const vtkm::Vec<float, 3>& msh)
VTKM_EXEC
inline float DivergingAngleDiff(float a1, float a2)
{
constexpr float f_pi = 3.141592653589793f;
constexpr float f_two_pi = 6.28318530717958647692528676655900576f;
constexpr float f_pi = vtkm::Pif();
constexpr float f_two_pi = vtkm::TwoPif();
float adiff = a1 - a2;
if (adiff < 0.0f)
adiff = -adiff;
......@@ -315,7 +315,7 @@ inline float DivergingAdjustHue(const vtkm::Vec<float, 3>& msh, float unsatM)
float hueSpin =
(msh[1] * vtkm::Sqrt(unsatM * unsatM - msh[0] * msh[0]) / (msh[0] * vtkm::Sin(msh[1])));
constexpr float one_third_pi = 0.33f * 3.141592653589793f;
constexpr float one_third_pi = vtkm::Pi_3f();
// Spin hue away from 0 except in purple hues.
if (msh[2] > -one_third_pi)
{
......@@ -609,7 +609,7 @@ vtkm::Vec<float, 3> ColorTableDiverging::MapThroughColorSpace(const vtkm::Vec<fl
detail::LabToMsh(lab2, msh2);
// If the endpoints are distinct saturated colors, then place white in between
// them.
constexpr float one_third_pi = 0.33f * 3.141592653589793f;
constexpr float one_third_pi = vtkm::Pi_3f();
if ((msh1[1] > 0.05f) && (msh2[1] > 0.05f) &&
(detail::DivergingAngleDiff(msh1[2], msh2[2]) > one_third_pi))
{
......
......@@ -212,7 +212,7 @@ void JacobianFor2DCell(const WorldCoordType &wCoords,
{
const vtkm::IdComponent numPoints = wCoords.GetNumberOfComponents();
vtkm::Vec<JacobianType,2> pc(pcoords[0], pcoords[1]);
JacobianType deltaAngle = static_cast<JacobianType>(2*vtkm::Pi()/numPoints);
JacobianType deltaAngle = 2*vtkm::Pi<JacobianType>()/numPoints;
jacobian = vtkm::Matrix<JacobianType,2,2>(0);
for (vtkm::IdComponent pointIndex = 0; pointIndex < numPoints; pointIndex++)
......
......@@ -40,7 +40,7 @@ vtkm::Matrix<vtkm::Float32, 4, 4> Camera::Camera3DStruct::CreateProjectionMatrix
vtkm::MatrixIdentity(matrix);
vtkm::Float32 AspectRatio = vtkm::Float32(width) / vtkm::Float32(height);
vtkm::Float32 fovRad = (this->FieldOfView * 3.1415926f) / 180.f;
vtkm::Float32 fovRad = this->FieldOfView * vtkm::Pi_180f();
fovRad = vtkm::Tan(fovRad * 0.5f);
vtkm::Float32 size = nearPlane * fovRad;
vtkm::Float32 left = -size * AspectRatio;
......
......@@ -87,7 +87,7 @@ void TextAnnotationBillboard::Render(const vtkm::rendering::Camera& camera,
viewportMatrix = vtkm::Transform3DScale(2.f / xs, 2.f / ys, 1.f);
}
MatrixType rotateMatrix = vtkm::Transform3DRotateZ(this->Angle * 3.14159265f / 180.f);
MatrixType rotateMatrix = vtkm::Transform3DRotateZ(this->Angle * vtkm::Pi_180f());
vtkm::Matrix<vtkm::Float32, 4, 4> fullTransformMatrix = vtkm::MatrixMultiply(
translateMatrix,
......
......@@ -76,8 +76,8 @@ public:
, Origin(origin)
, BoundingBox(boundingBox)
{
vtkm::Float32 thx = tanf((fovX * vtkm::Float32(vtkm::Pi()) / 180.f) * .5f);
vtkm::Float32 thy = tanf((fovY * vtkm::Float32(vtkm::Pi()) / 180.f) * .5f);
vtkm::Float32 thx = tanf((fovX * vtkm::Pi_180f()) * .5f);
vtkm::Float32 thy = tanf((fovY * vtkm::Pi_180f()) * .5f);
vtkm::Vec<vtkm::Float32, 3> ru = vtkm::Cross(look, up);
vtkm::Normalize(ru);
......@@ -290,8 +290,8 @@ public:
, Miny(miny)
, SubsetWidth(subsetWidth)
{
vtkm::Float32 thx = tanf((fovX * vtkm::Float32(vtkm::Pi()) / 180.f) * .5f);
vtkm::Float32 thy = tanf((fovY * vtkm::Float32(vtkm::Pi()) / 180.f) * .5f);
vtkm::Float32 thx = tanf((fovX * vtkm::Pi_180f()) * .5f);
vtkm::Float32 thy = tanf((fovY * vtkm::Pi_180f()) * .5f);
vtkm::Vec<vtkm::Float32, 3> ru = vtkm::Cross(look, up);
vtkm::Normalize(ru);
......@@ -534,7 +534,7 @@ void Camera::SetFieldOfView(const vtkm::Float32& degrees)
if (this->Width != this->Height)
{
vtkm::Float32 fovyRad = (newFOVY * static_cast<vtkm::Float32>(vtkm::Pi())) / 180.0f;
vtkm::Float32 fovyRad = newFOVY * vtkm::Pi_180f();
// Use the tan function to find the distance from the center of the image to the top (or
// bottom). (Actually, we are finding the ratio of this distance to the near plane distance,
......@@ -548,7 +548,7 @@ void Camera::SetFieldOfView(const vtkm::Float32& degrees)
// Now use the arctan function to get the proper field of view in the x direction.
vtkm::Float32 fovxRad = 2.0f * vtkm::ATan(horizontalDistance);
newFOVX = 180.0f * (fovxRad / static_cast<vtkm::Float32>(vtkm::Pi()));
newFOVX = fovxRad / vtkm::Pi_180f();
}
else
{
......@@ -1034,8 +1034,8 @@ void Camera::CreateDebugRayImp(vtkm::Vec<vtkm::Int32, 2> pixel, Ray<Precision>&
rays.MinDistance.GetPortalControl().Set(0, 0.f);
rays.HitIdx.GetPortalControl().Set(0, -2);
vtkm::Float32 thx = tanf((this->FovX * vtkm::Float32(vtkm::Pi()) / 180.f) * .5f);
vtkm::Float32 thy = tanf((this->FovY * vtkm::Float32(vtkm::Pi()) / 180.f) * .5f);
vtkm::Float32 thx = tanf((this->FovX * vtkm::Pi_180f()) * .5f);
vtkm::Float32 thy = tanf((this->FovY * vtkm::Pi_180f()) * .5f);
vtkm::Vec<vtkm::Float32, 3> ru = vtkm::Cross(this->Look, this->Up);
vtkm::Normalize(ru);
......
......@@ -140,6 +140,9 @@ struct ScalarFieldTests : public vtkm::exec::FunctorBase
{
// std::cout << "Testing Pi" << std::endl;
VTKM_MATH_ASSERT(test_equal(vtkm::Pi(), 3.14159265), "Pi not correct.");
VTKM_MATH_ASSERT(test_equal(vtkm::Pif(), 3.14159265f), "Pif not correct.");
VTKM_MATH_ASSERT(test_equal(vtkm::Pi<vtkm::Float64>(), 3.14159265),
"Pi template function not correct.");
}
VTKM_EXEC
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment