Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions include/vsg/maths/quat.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ namespace vsg

void set(value_type angle_radians, const t_vec3<value_type>& axis)
{
const value_type epsilon = 1e-7;
//@todo epsilon might have to be made value_type dependent
Comment thread
cmeyer42 marked this conversation as resolved.
static constexpr value_type epsilon = static_cast<value_type>(1e-7);
// avoid implicit conversions.
// a performance optimization for the sin / cos calls below which otherwise operate in double precision.
static constexpr value_type one = static_cast<value_type>(1.0);
static constexpr value_type half = static_cast<value_type>(0.5);

value_type len = length(axis);
if (len < epsilon)
{
Expand All @@ -102,9 +108,9 @@ namespace vsg
return;
}

value_type inversenorm = 1.0 / len;
value_type coshalfangle = cos(0.5 * angle_radians);
value_type sinhalfangle = sin(0.5 * angle_radians);
value_type inversenorm = one / len;
value_type coshalfangle = cos(half * angle_radians);
value_type sinhalfangle = sin(half * angle_radians);

x = axis.x * sinhalfangle * inversenorm;
y = axis.y * sinhalfangle * inversenorm;
Expand All @@ -114,7 +120,12 @@ namespace vsg

void set(const t_vec3<value_type>& from, const t_vec3<value_type>& to)
{
const value_type epsilon = 1e-7;
//@todo epsilon might have to be made value_type dependent
static constexpr value_type epsilon = static_cast<value_type>(1e-7);
// avoid implicit conversions.
// a performance optimization for the sin / cos calls below which otherwise operate in double precision.
static constexpr value_type one = static_cast<value_type>(1.0);
static constexpr value_type half = static_cast<value_type>(0.5);

value_type dot_pd = vsg::dot(from, to);
value_type div = std::sqrt(length2(from) * length2(to));
Expand All @@ -132,17 +143,21 @@ namespace vsg

double angle_radians = acos(dot_pd / div);

value_type inversenorm = 1.0 / len;
value_type coshalfangle = cos(0.5 * angle_radians);
value_type sinhalfangle = sin(0.5 * angle_radians);
value_type inversenorm = one / len;
value_type coshalfangle = cos(half * angle_radians);
value_type sinhalfangle = sin(half * angle_radians);

x = axis.x * sinhalfangle * inversenorm;
y = axis.y * sinhalfangle * inversenorm;
z = axis.z * sinhalfangle * inversenorm;
w = coshalfangle;
}

explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0 || value[2] != 0.0 || value[3] != 0.0; }
explicit operator bool() const noexcept
{
static constexpr value_type zero = static_cast<value_type>(0.0);
return value[0] != zero || value[1] != zero || value[2] != zero || value[3] != zero;
}
};

using quat = t_quat<float>; /// float quaternion
Expand Down Expand Up @@ -275,7 +290,7 @@ namespace vsg
T one(1.0);

T cosomega = dot(from, to);
if (cosomega < 0.0)
if (cosomega < static_cast<T>(0.0))
{
cosomega = -cosomega;
to.x = -to.x;
Expand Down
Loading