Skip to content

Commit d620359

Browse files
committed
Add convenience methods for mapping ROI coordinates to Paths
See https://forum.image.sc/t/axon-tracing-on-imagej-using-snt-plugin/120601/3
1 parent d2d1e24 commit d620359

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

‎src/main/java/sc/fiji/snt/Path.java‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,61 @@ protected int indexNearestTo(final double x, final double y, final double z) {
15171517
return indexNearestTo(x, y, z, Double.MAX_VALUE);
15181518
}
15191519

1520+
/**
1521+
* Returns the index of the node nearest to the specified position in pixel
1522+
* (unscaled) coordinates. This is useful when matching ROI positions (which
1523+
* are always in pixel coordinates) to path nodes. Canvas offsets are handled
1524+
* automatically.
1525+
*
1526+
* @param x the x-coordinate in pixel units
1527+
* @param y the y-coordinate in pixel units
1528+
* @param z the z-coordinate in pixel units (0-based slice index)
1529+
* @param within the search radius in pixel units. Only nodes within this
1530+
* distance are considered. Use {@code Double.MAX_VALUE} to always
1531+
* return the closest node regardless of distance.
1532+
* @return the index of the closest node, or -1 if no node was found within
1533+
* the specified radius
1534+
* @see #indexNearestTo(double, double, double, double)
1535+
* @see #getUnscaledNodes()
1536+
*/
1537+
public int indexNearestToUnscaled(final double x, final double y, final double z,
1538+
final double within)
1539+
{
1540+
if (nodes.isEmpty()) throw new IllegalArgumentException(
1541+
"indexNearestToUnscaled called on a Path of size() = 0");
1542+
1543+
double minimumDistanceSquared = within * within;
1544+
int indexOfMinimum = -1;
1545+
1546+
for (int i = 0; i < nodes.size(); ++i) {
1547+
final double diff_x = x - getXUnscaledDouble(i);
1548+
final double diff_y = y - getYUnscaledDouble(i);
1549+
final double diff_z = z - getZUnscaledDouble(i);
1550+
final double thisDistanceSquared = diff_x * diff_x + diff_y * diff_y +
1551+
diff_z * diff_z;
1552+
if (thisDistanceSquared < minimumDistanceSquared) {
1553+
indexOfMinimum = i;
1554+
minimumDistanceSquared = thisDistanceSquared;
1555+
}
1556+
}
1557+
1558+
return indexOfMinimum;
1559+
}
1560+
1561+
/**
1562+
* Returns the index of the node nearest to the specified position in pixel
1563+
* (unscaled) coordinates, searching across all nodes regardless of distance.
1564+
*
1565+
* @param x the x-coordinate in pixel units
1566+
* @param y the y-coordinate in pixel units
1567+
* @param z the z-coordinate in pixel units (0-based slice index)
1568+
* @return the index of the closest node
1569+
* @see #indexNearestToUnscaled(double, double, double, double)
1570+
*/
1571+
public int indexNearestToUnscaled(final double x, final double y, final double z) {
1572+
return indexNearestToUnscaled(x, y, z, Double.MAX_VALUE);
1573+
}
1574+
15201575
/**
15211576
* Gets the position of the node tagged as 'editable', if any.
15221577
*

0 commit comments

Comments
 (0)