Skip to content

Commit 8bd0ef1

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 8bd0ef1

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

Source/EbsdLib/Orientation/Rodrigues.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ class Rodrigues : public OrientationBase<T, 4>
5353
l() = length;
5454
}
5555

56+
static std::optional<Rodrigues<T>> From3Component(T x, T y, T z)
57+
{
58+
const T length = sqrtf(x * x + y * y + z * z);
59+
if(length == 0.0)
60+
{
61+
return {};
62+
}
63+
return Rodrigues<T>(x/length, y/length, z/length, length);
64+
}
65+
5666
/**
5767
* @brief Constructs a Rodrigues orientation from the 4 components
5868
* @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)