Class Extent
- All Implemented Interfaces:
Serializable,Comparable<Extent>
When describing a 2D entity, the depth (Z-axis value) is always 1.
Each size corresponds to the perpendicular Cartesian axes i.e. X, Y and Z axes.
This class is immutable. No operation will modify existing state.
The area (width by height) is pre-calculated to make certain operations like calculating
offsets (which may need to be called frequently) efficient. However, for sizes producing very
large areas that exceed the bounds of an int these need to be handled separately and
carefully. These cases can be checked for by calling areaXY().
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleananyDimensionIsLargerThan(Extent other) Returns true if any dimension in this extent is larger than the corresponding dimension inotherextent.intareaXY()Size in X multiplied by size in Y.doubleDerives an aspect-ratio, by dividing the X-dimension by the Y-dimension value.asTuple()Exposes the extent as a tuple.doubleCalculates the area freshly by multiplying the x-size with y-size, as doubles.longCalculates the volume of theExtentwhen considered as a box.intLikecalculateVolume()but uses anintto calculate the volume.intbooleancontains(int x, int y, int z) Is a point contained within the extent?booleancontains(BoundingBox box) Isboxentirely contained within the extent?booleanIs a point of typePoint2icontained within the extent in the XY plane?booleanIs a point of typePoint3dcontained within the extent?booleancontains(ReadableTuple3i point) Is a point of typeReadableTuple3icontained within the extent?booleancontainsX(double value) Is a value contained within the extent on the X-axis?booleancontainsX(int value) Is a value contained within the extent on the X-axis?booleancontainsY(double value) Is a value contained within the extent on the Y-axis?booleancontainsY(int value) Is a value contained within the extent on the Y-axis?booleancontainsZ(double value) Is a value contained within the extent on the Z-axis?booleancontainsZ(int value) Is a value contained within the extent on the Z-axis?static ExtentCreates from aReadableTuple3irepresenting the sizes in each dimension, where the tuple is not used internally.static ExtentCreates from aReadableTuple3irepresenting the sizes in each dimension, where the tuple is used internally.Creates a newExtentwith each dimension decreased by one.duplicateChangeX(int xToAssign) Creates a copy of the currentExtentwith the value for the X-dimension changed.duplicateChangeY(int yToAssign) Creates a copy of the currentExtentwith the value for the Y-dimension changed.duplicateChangeZ(int zToAssign) Creates a copy of the currentExtentwith the value for the Z-dimension changed.booleanbooleanequalsIgnoreZ(Extent other) Checks for equality with another extent ignoring any differences in the Z dimension.flattenZ()Collapses the Z dimension.growBy(int toAdd) Creates a newExtentwithtoAddsize added to each dimension.growBy(ReadableTuple3i toAdd) Creates a newExtentwithtoAddsize added to each respective dimension.inthashCode()intersectWith(Extent other) Intersects this extent with another (i.e. takes the smaller value in each dimension).booleanisEmpty()Does the extent contain zero voxels?<E extends Exception>
voiditerateOverXY(OffsettedScalarTwoDimensionalConsumer<E> pointConsumer) Calls processor once for each x and y-values in the range.voiditerateOverXY(PointTwoDimensionalConsumer pointConsumer) Calls processor once for each x and y-values in the range.<E extends Exception>
voiditerateOverXYOffset(CheckedIntConsumer<E> offsetConsumer) Calls processor once for each x and y-values but only passing an offset.voiditerateOverXYOffset(OffsettedPointTwoDimensionalConsumer pointConsumer) Calls processor once for each x and y-values in the range.voiditerateOverXYWithShift(Point2i shift, PointTwoDimensionalConsumer pointConsumer) Calls processor once for each x and y-values in the range, with a shift added.voiditerateOverYXOffset(OffsettedPointTwoDimensionalConsumer pointConsumer) Calls processor once for each x and y-values in the range.<E extends Exception>
voiditerateOverZ(CheckedIntConsumer<E> indexConsumer) Calls processor once for each z-value in the range.booleaniterateOverZUntil(IntPredicate indexPredicate) Calls processor once for each z-value in the range unlessindexPredicatereturns false.An extent that contains the minimum of two extents for each dimension respectively.final intoffset(int x, int y) Calculates a XY-offset of a point in a buffer whose dimensions are this extent.final intoffset(int x, int y, int z) Calculates a XYZ-offset of a point in a buffer whose dimensions are this extent.final intCalculates a XY-offset of a point in a buffer whose dimensions are this extent.final intoffset(ReadableTuple3i point) Calculates a XYZ-offset of a point in a buffer whose dimensions are this extent.final intoffsetSlice(ReadableTuple3i point) Calculates a XY-offset of a point in a buffer whose dimensions are this extent.scaleXYBy(double scaleFactor, boolean round) Scales all dimensions by a scaling-factor.scaleXYBy(ScaleFactor scaleFactor, boolean round) Scales the X- and Y- dimensions by a scaling-factor.shrinkBy(ReadableTuple3i toSubtract) Creates a newExtentwithtoSubtractsize subtracted from each respective dimension.Streams over the range of z values.int[]toArray()Derives an three-element array with each dimension in the extent.toString()intvalueByDimension(int dimensionIndex) The size in the dimension identified bydimensionIndex.intvalueByDimension(Axis axis) The size in the dimension identified byaxis.intx()The size in the X dimension.inty()The size in the Y dimension.intz()The size in the Z dimension.
-
Constructor Details
-
Extent
public Extent(int x, int y) Creates with with only X and Y dimensions.The z-dimension is assigned a value of
1.- Parameters:
x- size along the X-axis dimension.y- size along the Y-axis dimension.
-
Extent
public Extent(int x, int y, int z) Creates with X and Y and Z dimensions.- Parameters:
x- size along the X-axis dimension.y- size along the Y-axis dimension.z- size along the Z-axis dimension.
-
-
Method Details
-
createFromTupleDuplicate
Creates from aReadableTuple3irepresenting the sizes in each dimension, where the tuple is not used internally.The tuple is not used internally, with its values being copied in the constructor.
- Parameters:
tuple- a tuple with the extent size's for each dimension.- Returns:
- a newly created extent - that doesn't reuse
tupleinternally.
-
createFromTupleReuse
Creates from aReadableTuple3irepresenting the sizes in each dimension, where the tuple is used internally.- Parameters:
tuple- a tuple with the extent size's for each dimension.- Returns:
- a newly created extent - that reuses
tupleinternally.
-
areaXY
public int areaXY()Size in X multiplied by size in Y.This may be convenient for calculating offsets and for iterations.
Note that for very large sizes (e.g. the sizes of whole-slide images) an int may be insufficiently large to capture the area. Consider instead using
calculateAreaXYAsDouble()in these cases.- Returns:
- the X-size multiplied by the Y-size.
- Throws:
AnchorFriendlyRuntimeException- if the area is too large to be expressed as anint.
-
calculateAreaXYAsDouble
public double calculateAreaXYAsDouble()Calculates the area freshly by multiplying the x-size with y-size, as doubles.This is useful if the area would otherwise be too large to be represented as an int.
Any previously calculated area is ignored.
- Returns:
- the area calculated as a double.
-
calculateVolume
public long calculateVolume()Calculates the volume of theExtentwhen considered as a box.This is is the size in the X, Y and Z dimensions multiplied together.
- Returns:
- the volume in voxels.
-
calculateVolumeAsInt
public int calculateVolumeAsInt()LikecalculateVolume()but uses anintto calculate the volume.A
AnchorFriendlyRuntimeExceptionis thrown if an overflow occurs.- Returns:
- the volume in voxels.
-
isEmpty
public boolean isEmpty()Does the extent contain zero voxels?- Returns:
- true if any dimension has size 0.
-
x
public int x()The size in the X dimension.- Returns:
- the size.
-
y
public int y()The size in the Y dimension.- Returns:
- the size.
-
z
public int z()The size in the Z dimension.- Returns:
- the size.
-
valueByDimension
public int valueByDimension(int dimensionIndex) The size in the dimension identified bydimensionIndex.- Parameters:
dimensionIndex- the dimension to return a size for, as perReadableTuple3i#valueByDimension(int).- Returns:
- the size.
-
valueByDimension
The size in the dimension identified byaxis.- Parameters:
axis- the dimension to return a size for, as perReadableTuple3i#valueByDimension(Axis).- Returns:
- the size.
-
asTuple
Exposes the extent as a tuple.Importantly, this class is designed to be immutable, so this tuple should be treated as read-only, and never modified.
- Returns:
- the extent's width, height, depth as a tuple.
-
hashCode
public int hashCode() -
equals
-
equalsIgnoreZ
Checks for equality with another extent ignoring any differences in the Z dimension.- Parameters:
other- the extent to check for equality with.- Returns:
- true if the current extent is identical to
objignoring the Z dimension.
-
toString
-
offset
public final int offset(int x, int y) Calculates a XY-offset of a point in a buffer whose dimensions are this extent.- Parameters:
x- the value in the X-dimension for the point.y- the value in the Y-dimension for the point.- Returns:
- the offset, pertaining only to all dimensions.
-
offset
public final int offset(int x, int y, int z) Calculates a XYZ-offset of a point in a buffer whose dimensions are this extent.To be computationally efficient, this does not actively check that
areaXYis non-negative. It is the responsibility of the caller of this function to otherwise check this, or infer it from context.- Parameters:
x- the value in the X-dimension for the point.y- the value in the Y-dimension for the point.z- the value in the Z-dimension for the point.- Returns:
- the offset, pertaining only to all dimensions.
-
offset
Calculates a XYZ-offset of a point in a buffer whose dimensions are this extent.To be computationally efficient, this does not actively check that
areaXYis non-negative. It is the responsibility of the caller of this function to otherwise check this, or infer it from context.- Parameters:
point- the point to calculate an offset for.- Returns:
- the offset, pertaining only to all dimensions.
-
offset
Calculates a XY-offset of a point in a buffer whose dimensions are this extent.To be computationally efficient, this does not actively check that
areaXYis non-negative. It is the responsibility of the caller of this function to otherwise check this, or infer it from context.- Parameters:
point- the point to calculate an offset for.- Returns:
- the offset, pertaining only to the X and Y dimensions.
-
offsetSlice
Calculates a XY-offset of a point in a buffer whose dimensions are this extent.To be computationally efficient, this does not actively check that
areaXYis non-negative. It is the responsibility of the caller of this function to otherwise check this, or infer it from context.- Parameters:
point- the point to calculate an offset for.- Returns:
- the offset, pertaining only to the X and Y dimensions.
-
duplicateChangeX
Creates a copy of the currentExtentwith the value for the X-dimension changed.- Parameters:
xToAssign- the value to assign for the x-dimension.- Returns:
- the copy, with a changed x-value.
-
duplicateChangeY
Creates a copy of the currentExtentwith the value for the Y-dimension changed.- Parameters:
yToAssign- the value to assign for the y-dimension.- Returns:
- the copy, with a changed y-value.
-
duplicateChangeZ
Creates a copy of the currentExtentwith the value for the Z-dimension changed.- Parameters:
zToAssign- the value to assign for the z-dimension.- Returns:
- the copy, with a changed z-value.
-
containsX
public boolean containsX(double value) Is a value contained within the extent on the X-axis?- Parameters:
value- the value to check.- Returns:
- true iff the value is non-negative and less than the size of the extent in the X-axis.
-
containsY
public boolean containsY(double value) Is a value contained within the extent on the Y-axis?- Parameters:
value- the value to check.- Returns:
- true iff the value is non-negative and less than the size of the extent in the Y-axis.
-
containsZ
public boolean containsZ(double value) Is a value contained within the extent on the Z-axis?- Parameters:
value- the value to check.- Returns:
- true iff the value is non-negative and less than the size of the extent in the Z-axis.
-
containsX
public boolean containsX(int value) Is a value contained within the extent on the X-axis?- Parameters:
value- the value to check.- Returns:
- true iff the value is non-negative and less than the size of the extent in the X-axis.
-
containsY
public boolean containsY(int value) Is a value contained within the extent on the Y-axis?- Parameters:
value- the value to check.- Returns:
- true iff the value is non-negative and less than the size of the extent in the Y-axis.
-
containsZ
public boolean containsZ(int value) Is a value contained within the extent on the Z-axis?- Parameters:
value- the value to check.- Returns:
- true iff the value is non-negative and less than the size of the extent in the Z-axis.
-
contains
Is a point of typePoint2icontained within the extent in the XY plane?The z-dimension is ignored.
- Parameters:
point- the point to check.- Returns:
- true iff the point exists within the plane.
-
contains
Is a point of typePoint3dcontained within the extent?- Parameters:
point- the point to check.- Returns:
- true iff the point exists within the extent, considering all axes.
-
contains
Is a point of typeReadableTuple3icontained within the extent?- Parameters:
point- the point to check.- Returns:
- true iff the point exists within the extent, considering all axes.
-
contains
public boolean contains(int x, int y, int z) Is a point contained within the extent?- Parameters:
x- the value of the point on the x-axis.y- the value of the point on the y-axis.z- the value of the point on the z-axis.- Returns:
- true iff the point exists within the extent, considering all axes.
-
contains
Isboxentirely contained within the extent?- Parameters:
box- the bounding-box to check.- Returns:
- true iff only describes space contained in the current extent.
-
scaleXYBy
Scales the X- and Y- dimensions by a scaling-factor.- Parameters:
scaleFactor- the scaling-factor to multiply the respective X and Y dimension values by.round- if true, each dimension is rounded to the nearest whole number. If false, it is ceiled upwards to the nearest number.- Returns:
- a new
Extentwhose X and Y values are scaled versions of the current values, and Z value is unchanged.
-
scaleXYBy
Scales all dimensions by a scaling-factor.- Parameters:
scaleFactor- the scaling-factor to multiply the respective dimension values by.round- if true, each dimension is rounded to the nearest whole number. If false, it is ceiled upwards to the nearest number.- Returns:
- a new
Extentwhose dimension values are scaled versions of the current values, with a minimum of 1.
-
createMinusOne
Creates a newExtentwith each dimension decreased by one.- Returns:
- the new extent.
-
growBy
Creates a newExtentwithtoAddsize added to each dimension.- Parameters:
toAdd- the number of voxels to add to all dimensions.- Returns:
- a newly created
Extentgrown as per above.
-
growBy
Creates a newExtentwithtoAddsize added to each respective dimension.- Parameters:
toAdd- the number of voxels to add to each dimension.- Returns:
- a newly created
Extentgrown as per above.
-
shrinkBy
Creates a newExtentwithtoSubtractsize subtracted from each respective dimension.- Parameters:
toSubtract- the number of voxels to subtract from each dimension.- Returns:
- a newly created
Extentshrunk as per above.
-
intersectWith
Intersects this extent with another (i.e. takes the smaller value in each dimension).- Parameters:
other- the other.- Returns:
- a newly-created extent that is the intersection of this and another.
-
flattenZ
Collapses the Z dimension.- Returns:
- a new otherwise identical extent bit with size of of 1 in Z-dimension.
-
anyDimensionIsLargerThan
Returns true if any dimension in this extent is larger than the corresponding dimension inotherextent.- Parameters:
other- extent to compare to.- Returns:
- true or false (if all dimensions or less than or equal to their corresponding
dimension in
other).
-
iterateOverXY
public <E extends Exception> void iterateOverXY(OffsettedScalarTwoDimensionalConsumer<E> pointConsumer) throws E Calls processor once for each x and y-values in the range.This occurs in ascending order (x-dimension increments first, y-dimension increments second).
- Type Parameters:
E- a checked-exception thatindexConsumermay throw.- Parameters:
pointConsumer- called for each point.- Throws:
E- ifindexConsumerthrows this exception.
-
iterateOverXY
Calls processor once for each x and y-values in the range.This occurs in ascending order (x-dimension increments first, y-dimension increments second).
- Parameters:
pointConsumer- called for each point.
-
iterateOverXYWithShift
Calls processor once for each x and y-values in the range, with a shift added.This occurs in ascending order (x-dimension increments first, y-dimension increments second).
- Parameters:
shift- a shift added to each point, so the effective iteration occurs over @extent + shift.pointConsumer- called for each point.
-
iterateOverXYOffset
public <E extends Exception> void iterateOverXYOffset(CheckedIntConsumer<E> offsetConsumer) throws E Calls processor once for each x and y-values but only passing an offset.This occurs in ascending order (x-dimension increments first, y-dimension increments second).
- Type Parameters:
E- a checked-exception thatoffsetConsumermay throw.- Parameters:
offsetConsumer- called for each point with the offset.- Throws:
E- ifindexConsumerthrows this exception.AnchorFriendlyRuntimeException- if the area is too large to be expressed as anint.
-
iterateOverXYOffset
Calls processor once for each x and y-values in the range.This occurs in ascending order (x-dimension increments first, y-dimension increments second).
- Parameters:
pointConsumer- called for each point.
-
iterateOverYXOffset
Calls processor once for each x and y-values in the range.This occurs in ascending order (x-dimension increments first, y-dimension increments second).
- Parameters:
pointConsumer- called for each point.
-
iterateOverZ
Calls processor once for each z-value in the range.This occurs sequentially from 0 (inclusive) to
z()(exclusive).- Type Parameters:
E- a checked-exception thatindexConsumermay throw- Parameters:
indexConsumer- called for each index (z-value)- Throws:
E- ifindexConsumerthrows this exception
-
iterateOverZUntil
Calls processor once for each z-value in the range unlessindexPredicatereturns false.This occurs sequentially from 0 (inclusive) to
z()(exclusive).As soon as the
indexPredicatereturns false, the iteration stops.- Parameters:
indexPredicate- called for each index (z-value)- Returns:
- true if
indexPredicatealways returned true for every slice, false otherwise.
-
streamOverZ
Streams over the range of z values.The values range from 0 (inclusive) to
z()(exclusive).- Returns:
- the stream.
-
toArray
public int[] toArray()Derives an three-element array with each dimension in the extent.- Returns:
- a newly created three-element array with respectively extents for the x, y and z dimensions.
-
minimum
An extent that contains the minimum of two extents for each dimension respectively.This is an immutable operation.
- Parameters:
extent- the other extent to find a minimum with.- Returns:
- a newly created extent.
-
aspectRatioXY
public double aspectRatioXY()Derives an aspect-ratio, by dividing the X-dimension by the Y-dimension value.Note that the Z-dimension is irrelevant to this calculation.
- Returns:
- The X-dimension divided by the Y-dimension.
-
compareTo
- Specified by:
compareToin interfaceComparable<Extent>
-