Skip to content

Commit fe6711d

Browse files
committed
BUG: Fix Rodrigues 3 arg construtor to static with std::optional return
Signed-off-by: Michael Jackson <mike.jackson@bluequartz.net>
1 parent b9c7b2e commit fe6711d

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

Source/EbsdLib/Orientation/Rodrigues.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "EbsdLib/Orientation/OrientationFwd.hpp"
66

77
#include <array>
8+
#include <optional>
89

910
namespace ebsdlib
1011
{
@@ -53,6 +54,16 @@ class Rodrigues : public OrientationBase<T, 4>
5354
l() = length;
5455
}
5556

57+
static std::optional<Rodrigues<T>> From3Component(T x, T y, T z)
58+
{
59+
const T length = sqrtf(x * x + y * y + z * z);
60+
if(length == 0.0)
61+
{
62+
return {};
63+
}
64+
return Rodrigues<T>(x / length, y / length, z / length, length);
65+
}
66+
5667
/**
5768
* @brief Constructs a Rodrigues orientation from the 4 components
5869
* @param x

Source/Test/OrientationTest.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,49 @@ TEST_CASE("ebsdlib::Orientations::StereographicConsistencyCheck", "[EbsdLib][Ori
576576
GENERATE_TEST_METHOD(Stereographic, Homochoric, Cubochoric, Stereographic);
577577
GENERATE_TEST_METHOD(Stereographic, Cubochoric, Homochoric, Stereographic);
578578
}
579+
580+
TEST_CASE("ebsdlib::Orientations::RodriguesFrom3Component", "[EbsdLib][Orientations]")
581+
{
582+
SECTION("Zero vector returns empty optional")
583+
{
584+
auto result = ebsdlib::RodriguesDType::From3Component(0.0, 0.0, 0.0);
585+
REQUIRE_FALSE(result.has_value());
586+
}
587+
588+
SECTION("Unit vector along X-axis produces valid Rodrigues")
589+
{
590+
auto result = ebsdlib::RodriguesDType::From3Component(1.0, 0.0, 0.0);
591+
REQUIRE(result.has_value());
592+
REQUIRE(result->x() == Approx(1.0));
593+
REQUIRE(result->y() == Approx(0.0));
594+
REQUIRE(result->z() == Approx(0.0));
595+
REQUIRE(result->l() == Approx(1.0));
596+
auto validity = result->isValid();
597+
REQUIRE(validity.result == 1);
598+
}
599+
600+
SECTION("Non-unit vector normalizes axis and stores computed length")
601+
{
602+
// From3Component normalizes x, y, z and sets l = ||(x,y,z)||
603+
auto result = ebsdlib::RodriguesDType::From3Component(3.0, 4.0, 0.0);
604+
REQUIRE(result.has_value());
605+
REQUIRE(result->x() == Approx(0.6));
606+
REQUIRE(result->y() == Approx(0.8));
607+
REQUIRE(result->z() == Approx(0.0));
608+
REQUIRE(result->l() == Approx(5.0));
609+
auto validity = result->isValid();
610+
REQUIRE(validity.result == 1);
611+
}
612+
613+
SECTION("Negative component vector produces valid length")
614+
{
615+
auto result = ebsdlib::RodriguesDType::From3Component(0.0, -1.0, 0.0);
616+
REQUIRE(result.has_value());
617+
REQUIRE(result->x() == Approx(0.0));
618+
REQUIRE(result->y() == Approx(-1.0));
619+
REQUIRE(result->z() == Approx(0.0));
620+
REQUIRE(result->l() == Approx(1.0));
621+
auto validity = result->isValid();
622+
REQUIRE(validity.result == 1);
623+
}
624+
}

0 commit comments

Comments
 (0)