Skip to content

BUG: Validate sampling, resampling and geometry count values - #1964

Open
joeykleingers wants to merge 7 commits into
BlueQuartzSoftware:developfrom
joeykleingers:bug/c14-sampling-resampling
Open

joeykleingers wants to merge 7 commits into
BlueQuartzSoftware:developfrom
joeykleingers:bug/c14-sampling-resampling

Conversation

@joeykleingers

@joeykleingers joeykleingers commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Sampling, resampling, scan-path and geometry-creation filters now reject spacing, resolution, sigma, origin, angle, count and dimension values outside their domain in preflight, instead of looping forever, dividing by zero, converting NaN or infinite values to integers, or wrapping signed counts to huge unsigned sizes. Values that depend on the final geometry (resampled layouts, hull grids, edge sample counts, partition indices, interpolation kernels) are checked when the filter executes, before any conversion or allocation. 79 files changed, +4961 / −299 lines.

Dependency: stacked on #1952 (BUG: Validate geometry transform values and rotation axes), which adds the shared GeometryUtilities::ValidateFiniteVector() and ValidatePositiveFiniteVector() used here. Until #1952 merges, the diff against develop also shows its commits; review the last three commits. This pull request stays a draft until then.

1. Spatial sampling and resampling values

  • src/simplnx/Utilities/ImageProcessing/ImageGeometryResample.{hpp,cpp} — ValidateResamplingParameters(), ComputeResampledImageLayout() and checked resampling axis maps
  • src/Plugins/SimplnxCore/src/SimplnxCore/Filters/*Filter.cpp — per-filter preflight checks
  • test/ImageGeometryResampleTest.cpp — core tests for the shared resampling code

Preflight rejects active values outside their domain:

  • Create AM Scan Paths: Hatch Spacing finite and > 0; Hatch Rotation Angle must give finite radians.
  • Point Sample Edge Geometry: Sampling Spacing finite and > 0.
  • Resample Image Geom: New Spacing and Scale Factor finite and > 0 in the selected mode.
  • Approximate Point Cloud Hull: Grid Resolution finite and > 0; Minimum Number of Empty Neighbors in 0 through 26.
  • Uncertain Regular Grid Sample Surface Mesh: Spacing finite and > 0; Origin and Uncertainty finite.
  • Regular Grid Sample Surface Mesh: Origin finite when creating the grid.
  • Interpolate Point Cloud To Regular Grid: Gaussian Sigmas finite with a positive, finite float32 square (Gaussian only).
  • Compute Coordinate Threshold: sphere center finite and radius finite and > 0.
  • Extract Feature Boundaries 2D: Custom Z Value finite when selected.

Inactive options are not checked or read. Resampled spacing, dimensions and tuple products, the hull's voxel bounds and grid product, and each edge's sample count depend on the final geometry. Preflight warns and uses unit placeholder dimensions and spacing when the current geometry gives a layout that cannot be represented; execution recomputes the layout from the final metadata and returns an error before converting or allocating.

The hull check covers the grid with the one-voxel empty border that #1910/#1911 added. The border is added in integer arithmetic after the check, so it is kept for voxel indices beyond 2^53, where float64 cannot represent ±1.

2. Geometry and sampling counts

  • src/simplnx/Utilities/GeometryUtilities.{hpp,cpp} — ValidateImageDimensions() (positive dimensions with a representable product)
  • src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/PartitionGeometry*.cpp — final-grid checks and checked coordinate-to-partition conversion
  • src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/InterpolatePointCloudToRegularGrid.{hpp,cpp} — ComputeKernelLayout()

Preflight rejects:

  • Partition Geometry (Basic, Advanced, Bounding Box): negative Number Of Cells Per Axis values, which were cast to usize before the <= 0 check, products that overflow, and partition IDs that do not fit int32 from Starting Feature ID. Existing Partition Grid mode ignores the counts.
  • Create Geometry (Image), Create Image Geometry, Resample Image Geom (Exact Dimensions) and Resample Rect Grid To Image Geom (Dimensions): zero dimensions and dimension products that overflow.
  • Feature Face Curvature: Neighborhood Ring Count below 1.
  • Point Sample Triangle Geometry: negative Number of Sample Points. Zero is valid and creates an empty vertex geometry.
  • Interpolate Point Cloud To Regular Grid: nonfinite Kernel Size.

Checked during execution:

  • An existing partition grid: its final origin, spacing, product and ID range.
  • Each coordinate-to-partition-index conversion; a NaN or out-of-range quotient is an error instead of an undefined integer conversion. Masked vertices are skipped before conversion.
  • The interpolation kernel's radius, size and storage, from the final spacing; preflight warns when the current spacing gives a kernel that cannot be represented.

3. Read Image Stack resampling

  • src/Plugins/NXImageProcessing/src/NXImageProcessing/Filters/ReadImageStackFilter.cpp

Preflight checks only the selected resampling option, before reading slice metadata: Exact 2D Dimensions must be at least 1 with a representable product, and Scaling must be finite and at least 1 percent, through the shared resampling checks. Exact 2D Dimensions is no longer read when another mode is selected.

Preflight is the one place these parameters are checked. The algorithms state the properties they rely on as @pre conditions and check only values that preflight cannot know.

Changed behavior

  • New preflight errors for the values above. New codes are -86100 to -86114, -86124 to -86137, and -86150 to -86174 except -86151, -86159, -86160 and -86164. Existing codes are kept for conditions they already reported: Partition Geometry zero counts -3012 to -3014, Resample Rect Grid To Image Geom negative dimensions -7362 to -7364, Read Image Stack Scaling below 1 percent -23508, finite negative Kernel Size -11000. A Kernel Size of −infinity now reports -86163 instead of -11000.
  • Results that depend on the final geometry fail when the filter executes, after a preflight warning where preflight can see them.
  • Point Sample Triangle Geometry with zero sample points returns the empty output without drawing samples.
  • Uniform interpolation no longer checks the inactive Gaussian Sigmas.
  • Partition Geometry's in-memory path for RectGrid and node geometries makes one extra serial pass over the coordinates to check their conversion before the parallel workers run.
  • Output for every previously valid value is unchanged.

Family entries fixed

This pull request fixes these entries of #1873; the issue stays open for its other parameter classes:

Entry Fix
CreateAMScanPathsFilter hatch spacing (CreateAMScanPaths.cpp:190) and rotation angle (:421) Section 1
PointSampleEdgeGeometryFilter sampling spacing (PointSampleEdgeGeometry.cpp:73) Section 1
ResampleImageGeomFilter spacing and exact dimensions (ImageGeometryResample.cpp:260) Sections 1–2
ApproximatePointCloudHullFilter grid resolution (ApproximatePointCloudHullFilter.cpp:86) and empty-neighbor maximum (ApproximatePointCloudHull.cpp:178) Section 1
UncertainRegularGridSampleSurfaceMeshFilter spacing, origin and uncertainty (:138) Section 1
InterpolatePointCloudToRegularGridFilter Gaussian sigmas (:172) and kernel size (InterpolatePointCloudToRegularGrid.cpp:417) Sections 1–2
ComputeCoordinateThresholdFilter sphere (:165) Section 1
ExtractFeatureBoundaries2DFilter custom Z (ExtractFeatureBoundaries2D.cpp:265) Section 1
RegularGridSampleSurfaceMeshFilter origin (:200) Section 1
PartitionGeometryFilter cell counts (the count part of the :550 entry) Section 2
ResampleRectGridToImageGeomFilter dimensions (:113) Section 2
CreateGeometryFilter / CreateImageGeometryFilter dimensions (the dimension part of the CreateImageGeometryAction.cpp:58 entry) Section 2
FeatureFaceCurvatureFilter ring count (:58) Section 2
PointSampleTriangleGeometryFilter sample count (PointSampleTriangleGeometry.cpp:64) Section 2
ReadImageStackFilter exact 2D dimensions (:302) Section 3

Not part of this pull request: the Partition Geometry bounding-box and Compute Coordinate Threshold rectangle relations, and the Read Image Stack origin and spacing overrides (later pull requests for #1873); cancellation in the Create AM Scan Paths hatch loop (#1876); and results that overflow float32 from finite values (#1874). Until the bounding-box entry is fixed, the execution check of the partition grid's origin and spacing is what rejects a nonfinite bounding box.

Test plan

Related Issue

Related to #1873

@joeykleingers joeykleingers self-assigned this Nov 12, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@joeykleingers

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@joeykleingers

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

The axis-angle rotation matrix normalized its axis in float32 without a
check. A zero axis produced NaN, a large finite axis such as
{0,0,1e20} overflowed the squared norm, and a tiny axis such as
{1e-22,0,0} underflowed it, so valid rotations produced wrong matrices.
A vector shorter than four values was read out of bounds.

- Add ImageRotationUtilities::ValidateRotationAxisAngle(): four values,
  a finite nonzero axis and a finite angle, each with its own error.
- GenerateRotationTransformationMatrix() keeps its signature. It scales
  the axis by its largest component and normalizes it in float64; the
  angle arithmetic is unchanged. Invalid input returns an all-NaN matrix
  instead of undefined behavior.
- RotateSampleRefFrame validates the Axis-Angle value in preflight and
  at algorithm entry. Zero or nonfinite axes now report the axis or
  angle error instead of the non-principal-rotation error, and large
  axes such as {0,0,1e20} at 90 degrees are accepted.

Matrices for ordinary axes are unchanged to within 2e-6.

Part of BlueQuartzSoftware#1873.

Signed-off-by: Joey Kleingers <joey.kleingers@bluequartz.net>
Image Geometry creation and transform filters accepted zero, negative,
NaN or infinite spacing, origins, scales and cell lengths, and negative
padding, producing invalid geometry, division by zero or unsigned wrap.

- Add GeometryUtilities::ValidateFiniteVector(),
  ValidatePositiveFiniteVector() and ComputePaddedDimensions().
- CreateGeometry, CreateImageGeometry and SetImageGeomOriginScaling
  require positive finite spacing and finite origins; PartitionGeometry
  checks Advanced Cell Length and Partition Grid Origin before its
  geometry branch, so RectGrid and node geometries are covered too.
- ApplyTransformationToGeometry validates the selected Translation,
  Scale or Rotation in preflight and at algorithm entry through
  ValidateTransformationParameters(). Image Geometry scale must be
  positive; node geometries keep zero and negative scale.
- PadImageGeometry rejects negative padding and computes padded
  dimensions with widened, checked arithmetic in preflight and at
  algorithm entry.
- Results that depend on existing geometry metadata (scaled spacing,
  translated, padded or centered origins, padded dimensions) are
  checked when the filter executes, before the geometry changes;
  preflight only warns, because the metadata can still change upstream.
  UpdateImageGeomAction gains an opt-in check for this.
- UpdateImageGeomAction::clone() now keeps the Center Origin flag, and
  Center Origin places negative or mixed-sign origins at the geometry's
  center as documented.

Apart from the two Center Origin corrections above, output for every
previously valid value with a representable result is unchanged.

Part of BlueQuartzSoftware#1873.

Signed-off-by: Joey Kleingers <joey.kleingers@bluequartz.net>
…eflight

Filter preflight is the one place a filter's inputs are checked; the
algorithm classes do not repeat those checks.

- ApplyTransformationToGeometry, PadImageGeometry and
  RotateSampleRefFrame no longer re-validate Translation, Scale,
  Rotation, padding signs or the rotation axis-angle at algorithm entry.
  ValidateTransformationParameters() and the padding sign check now live
  with their filters' preflight.
- PadImageGeometry still checks, when it executes, the padded dimensions
  and origin it computes from the final geometry, because those can
  differ from what preflight sees.
- Tests that called the algorithms directly to reach these errors now
  run the filters. Value checks keep their codes; wrong-length vectors
  now fail parameter validation (-1, naming the expected size), and a
  malformed inactive vector is accepted.

Signed-off-by: Joey Kleingers <joey.kleingers@bluequartz.net>
- The Set Image Geom Origin Scaling documentation names the Set Spacing
  and Set Origin options by their on-screen labels.
- ImageRotationUtilities.hpp includes <string_view> for its public
  validation function instead of relying on a transitive include.

Signed-off-by: Joey Kleingers <joey.kleingers@bluequartz.net>
Sampling, resampling and scan-path filters accepted zero, negative,
NaN or infinite spacing, resolutions, sigmas, origins, angles and
coordinates, which led to division by zero, loops that never end,
float-to-integer conversions of nonfinite values and NaN output.

- Preflight rejects invalid active values in Create AM Scan Paths,
  Point Sample Edge Geometry, Resample Image Geom, Approximate Point
  Cloud Hull, Uncertain Regular Grid Sample Surface Mesh, Interpolate
  Point Cloud To Regular Grid (Gaussian sigmas), Compute Coordinate
  Threshold (sphere), Extract Feature Boundaries 2D (custom Z) and
  Regular Grid Sample Surface Mesh (origin). Inactive options are not
  checked.
- Values derived from the final geometry (resampled spacing and
  dimensions, hull grid bounds, edge sample counts, resampling axis
  maps) are checked when the filter executes, before any conversion or
  allocation; preflight warns and uses safe placeholders when the
  current geometry gives an unrepresentable layout.
- Add ValidateResamplingParameters() and ComputeResampledImageLayout()
  for the shared resampling code.

Output for every previously valid value is unchanged.

Part of BlueQuartzSoftware#1873.

Signed-off-by: Joey Kleingers <joey.kleingers@bluequartz.net>
Geometry-creation, partitioning, resampling, curvature, point-sampling
and interpolation filters accepted zero or negative counts, signed
values that wrapped to huge unsigned dimensions, nonfinite kernel
sizes and dimension products that overflow, which led to empty or
corrupt geometries, division by zero, huge allocations and
float-to-integer conversions that are undefined behavior.

- Preflight rejects negative Partition Geometry counts, partition
  products and ID ranges that cannot be represented (Basic, Advanced
  and Bounding Box modes), zero dimensions in Create Geometry (Image),
  Create Image Geometry, Resample Image Geom (Exact Dimensions) and
  Resample Rect Grid To Image Geom, Neighborhood Ring Count below 1 in
  Feature Face Curvature, negative Number of Sample Points in Point
  Sample Triangle Geometry and nonfinite Kernel Size in Interpolate
  Point Cloud To Regular Grid. Inactive options are not checked.
- Values derived from final geometry are checked when the filter
  executes, before conversion or allocation: an existing partition
  grid's metadata, product and ID range, each coordinate-to-partition
  index conversion, and the interpolation kernel radius, size and
  storage. Preflight warns when the current geometry gives an
  unrepresentable kernel.
- Add GeometryUtilities::ValidateImageDimensions() for the shared
  positive-dimension check.
- A zero sample count now returns an empty vertex geometry without
  drawing samples; masked vertices are skipped before their
  coordinates are converted.

Output for every previously valid value is unchanged.

Part of BlueQuartzSoftware#1873.

Signed-off-by: Joey Kleingers <joey.kleingers@bluequartz.net>
Read Image Stack accepted zero Exact 2D Dimensions and nonfinite
Scaling, and read the Exact 2D Dimensions vector even when another
resampling mode was selected. Zero dimensions produced infinite
spacing in the resampled slices.

- Preflight checks only the active resampling option, before reading
  slice metadata or creating output actions: Exact 2D Dimensions must
  be at least 1 with a representable product, and Scaling must be
  finite and at least 1 percent. These use the shared resampling
  checks.
- Inactive resampling options are no longer read or checked.

Output for every previously valid value is unchanged.

Part of BlueQuartzSoftware#1873.

Signed-off-by: Joey Kleingers <joey.kleingers@bluequartz.net>
@joeykleingers
joeykleingers force-pushed the bug/c14-sampling-resampling branch from 8e77db8 to 93e1900 Compare November 14, 2026 15:13
@joeykleingers
joeykleingers marked this pull request as ready for review November 14, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant