Skip to content

Commit

Permalink
ENH/BUG: Additional Origin Centering Options and BoundingBox API Fix (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq authored Jan 3, 2024
1 parent 4994adb commit 62df764
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Parameters SetImageGeomOriginScalingFilter::parameters() const
params.insertSeparator(Parameters::Separator{"Input Parameters"});
params.insert(std::make_unique<GeometrySelectionParameter>(k_ImageGeomPath_Key, "Image Geometry", "Path to the target ImageGeom", DataPath(), std::set{IGeometry::Type::Image}));
params.insertLinkableParameter(std::make_unique<BoolParameter>(k_ChangeOrigin_Key, "Set Origin", "Specifies if the origin should be changed", true));
params.insert(
std::make_unique<BoolParameter>(k_CenterOrigin_Key, "Put Origin at the Center of Geometry", "Specifies if the origin should be aligned with the corner (false) or center (true)", false));
params.insert(std::make_unique<VectorFloat64Parameter>(k_Origin_Key, "Origin (Physical Units)", "Specifies the new origin values in physical units.", std::vector<float64>{0.0, 0.0, 0.0},
std::vector<std::string>{"X", "Y", "Z"}));

Expand All @@ -62,6 +64,8 @@ Parameters SetImageGeomOriginScalingFilter::parameters() const
std::vector<std::string>{"X", "Y", "Z"}));

params.linkParameters(k_ChangeOrigin_Key, k_Origin_Key, std::make_any<bool>(true));
params.linkParameters(k_ChangeOrigin_Key, k_CenterOrigin_Key, std::make_any<bool>(true));

params.linkParameters(k_ChangeResolution_Key, k_Spacing_Key, std::make_any<bool>(true));
return params;
}
Expand All @@ -78,6 +82,7 @@ IFilter::PreflightResult SetImageGeomOriginScalingFilter::preflightImpl(const Da
{
auto imageGeomPath = filterArgs.value<DataPath>(k_ImageGeomPath_Key);
auto shouldChangeOrigin = filterArgs.value<bool>(k_ChangeOrigin_Key);
auto shouldCenterOrigin = filterArgs.value<bool>(k_CenterOrigin_Key);
auto shouldChangeResolution = filterArgs.value<bool>(k_ChangeResolution_Key);
auto origin = filterArgs.value<std::vector<float64>>(k_Origin_Key);
auto spacing = filterArgs.value<std::vector<float64>>(k_Spacing_Key);
Expand All @@ -92,16 +97,18 @@ IFilter::PreflightResult SetImageGeomOriginScalingFilter::preflightImpl(const Da

std::vector<PreflightValue> preflightUpdatedValues;

bool centerOrigin = false;
if(shouldChangeOrigin)
{
optOrigin = originVec;
centerOrigin = shouldCenterOrigin;
}
if(shouldChangeResolution)
{
spacingVec = spacingVec;
}

auto action = std::make_unique<UpdateImageGeomAction>(optOrigin, spacingVec, imageGeomPath);
auto action = std::make_unique<UpdateImageGeomAction>(optOrigin, spacingVec, imageGeomPath, centerOrigin);

resultOutputActions.value().appendAction(std::move(action));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SIMPLNXCORE_EXPORT SetImageGeomOriginScalingFilter : public IFilter
// Parameter Keys
static inline constexpr StringLiteral k_ImageGeomPath_Key = "image_geom";
static inline constexpr StringLiteral k_ChangeOrigin_Key = "change_origin";
static inline constexpr StringLiteral k_CenterOrigin_Key = "center_origin";
static inline constexpr StringLiteral k_ChangeResolution_Key = "change_resolution";
static inline constexpr StringLiteral k_Origin_Key = "origin";
static inline constexpr StringLiteral k_Spacing_Key = "spacing";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TEST_CASE("SimplnxCore::SetImageGeomOriginScalingFilter(Instantiate)", "[Simplnx
SIMPLNX_RESULT_REQUIRE_VALID(result.outputActions);
}

TEST_CASE("SimplnxCore::SetImageGeomOriginScalingFilter(Valid Parameters)", "[SimplnxCore][SetImageGeomOriginScalingFilter]")
TEST_CASE("SimplnxCore::SetImageGeomOriginScalingFilter: Valid Execution", "[SimplnxCore][SetImageGeomOriginScalingFilter]")
{
DataPath k_ImageGeomPath({Constants::k_SmallIN100, Constants::k_EbsdScanData, Constants::k_ImageGeometry});
bool k_ChangeOrigin = true;
Expand Down Expand Up @@ -57,3 +57,63 @@ TEST_CASE("SimplnxCore::SetImageGeomOriginScalingFilter(Valid Parameters)", "[Si
REQUIRE(imageGeom.getOrigin() == FloatVec3{7, 6, 5});
REQUIRE(imageGeom.getSpacing() == FloatVec3{2, 2, 2});
}

TEST_CASE("SimplnxCore::SetImageGeomOriginScalingFilter: 0,0,0 Central Origin", "[SimplnxCore][SetImageGeomOriginScalingFilter]")
{
DataPath k_ImageGeomPath({Constants::k_SmallIN100, Constants::k_EbsdScanData, Constants::k_ImageGeometry});
bool k_ChangeOrigin = true;
bool k_ChangeResolution = true;
std::vector<float64> k_Origin{0.0, 0.0, 0.0};
std::vector<float64> k_Spacing{2, 2, 2};

SetImageGeomOriginScalingFilter filter;
DataStructure dataStructure = UnitTest::CreateDataStructure();
Arguments args;

args.insert(SetImageGeomOriginScalingFilter::k_ImageGeomPath_Key, std::make_any<DataPath>(k_ImageGeomPath));
args.insert(SetImageGeomOriginScalingFilter::k_ChangeOrigin_Key, std::make_any<bool>(k_ChangeOrigin));
args.insert(SetImageGeomOriginScalingFilter::k_CenterOrigin_Key, std::make_any<bool>(true));
args.insert(SetImageGeomOriginScalingFilter::k_ChangeResolution_Key, std::make_any<bool>(k_ChangeResolution));
args.insert(SetImageGeomOriginScalingFilter::k_Origin_Key, std::make_any<std::vector<float64>>(k_Origin));
args.insert(SetImageGeomOriginScalingFilter::k_Spacing_Key, std::make_any<std::vector<float64>>(k_Spacing));

auto preflightResult = filter.preflight(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions);

auto result = filter.execute(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_VALID(result.result);

auto& imageGeom = dataStructure.getDataRefAs<ImageGeom>(k_ImageGeomPath);
REQUIRE(imageGeom.getBoundingBoxf().center() == Point3Df{0.0f, 0.0f, 0.0f});
REQUIRE(imageGeom.getSpacing() == FloatVec3{2, 2, 2});
}

TEST_CASE("SimplnxCore::SetImageGeomOriginScalingFilter: Custom Central Origin", "[SimplnxCore][SetImageGeomOriginScalingFilter]")
{
DataPath k_ImageGeomPath({Constants::k_SmallIN100, Constants::k_EbsdScanData, Constants::k_ImageGeometry});
bool k_ChangeOrigin = true;
bool k_ChangeResolution = true;
std::vector<float64> k_Origin{7.0, 6.0, 5.0};
std::vector<float64> k_Spacing{2, 2, 2};

SetImageGeomOriginScalingFilter filter;
DataStructure dataStructure = UnitTest::CreateDataStructure();
Arguments args;

args.insert(SetImageGeomOriginScalingFilter::k_ImageGeomPath_Key, std::make_any<DataPath>(k_ImageGeomPath));
args.insert(SetImageGeomOriginScalingFilter::k_ChangeOrigin_Key, std::make_any<bool>(k_ChangeOrigin));
args.insert(SetImageGeomOriginScalingFilter::k_CenterOrigin_Key, std::make_any<bool>(true));
args.insert(SetImageGeomOriginScalingFilter::k_ChangeResolution_Key, std::make_any<bool>(k_ChangeResolution));
args.insert(SetImageGeomOriginScalingFilter::k_Origin_Key, std::make_any<std::vector<float64>>(k_Origin));
args.insert(SetImageGeomOriginScalingFilter::k_Spacing_Key, std::make_any<std::vector<float64>>(k_Spacing));

auto preflightResult = filter.preflight(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions);

auto result = filter.execute(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_VALID(result.result);

auto& imageGeom = dataStructure.getDataRefAs<ImageGeom>(k_ImageGeomPath);
REQUIRE(imageGeom.getBoundingBoxf().center() == Point3Df{7.0, 6.0, 5.0});
REQUIRE(imageGeom.getSpacing() == FloatVec3{2, 2, 2});
}
2 changes: 1 addition & 1 deletion src/simplnx/Common/BoundingBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class BoundingBox
*/
PointType center() const
{
return {(m_Lower + sideLengths()) / static_cast<ValueType>(2.0)};
return {m_Lower + (sideLengths() / static_cast<ValueType>(2.0))};
}

/**
Expand Down
26 changes: 23 additions & 3 deletions src/simplnx/Filter/Actions/UpdateImageGeomAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ using namespace nx::core;

namespace nx::core
{
UpdateImageGeomAction::UpdateImageGeomAction(const std::optional<FloatVec3>& origin, const std::optional<FloatVec3>& spacing, const DataPath& path)
UpdateImageGeomAction::UpdateImageGeomAction(const std::optional<FloatVec3>& origin, const std::optional<FloatVec3>& spacing, const DataPath& path, bool centerOrigin)
: m_Origin(origin)
, m_Spacing(spacing)
, m_Path(path)
, m_CenterOrigin(centerOrigin)
{
}

Expand All @@ -26,13 +27,32 @@ Result<> UpdateImageGeomAction::apply(DataStructure& dataStructure, Mode mode) c
return MakeErrorResult(-6701, fmt::format("{}Unable to find ImageGeom at '{}'", prefix, path().toString()));
}

if(shouldUpdateSpacing())
{
image->setSpacing(m_Spacing.value());
}

if(shouldUpdateOrigin())
{
image->setOrigin(m_Origin.value());
}
if(shouldUpdateSpacing())

// This must be done last since spacing affects it and the origin may not be set resulting in invalid access
if(m_CenterOrigin)
{
image->setSpacing(m_Spacing.value());
Point3Df origin = image->getOrigin();
BoundingBox3Df bounds = image->getBoundingBoxf();
Point3Df centerPoint = bounds.center();
Point3Df minPoint = bounds.getMinPoint();

for(uint8 i = 0; i < 3; i++)
{
// absolute center - absolute min point = absolute offset
float32 offset = std::abs(centerPoint[i]) - std::abs(minPoint[i]);
// subtract absolute offset and save point into "new" origin
origin[i] -= offset;
}
image->setOrigin(origin);
}

return {};
Expand Down
3 changes: 2 additions & 1 deletion src/simplnx/Filter/Actions/UpdateImageGeomAction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SIMPLNX_EXPORT UpdateImageGeomAction : public IDataAction
public:
UpdateImageGeomAction() = delete;

UpdateImageGeomAction(const std::optional<FloatVec3>& origin, const std::optional<FloatVec3>& spacing, const DataPath& path);
UpdateImageGeomAction(const std::optional<FloatVec3>& origin, const std::optional<FloatVec3>& spacing, const DataPath& path, bool centerOrigin = false);

~UpdateImageGeomAction() noexcept override;

Expand Down Expand Up @@ -71,5 +71,6 @@ class SIMPLNX_EXPORT UpdateImageGeomAction : public IDataAction
std::optional<FloatVec3> m_Origin;
std::optional<FloatVec3> m_Spacing;
DataPath m_Path;
bool m_CenterOrigin;
};
} // namespace nx::core

0 comments on commit 62df764

Please sign in to comment.