Skip to content

Commit a0eabb0

Browse files
committed
ConvexPolytope and HyperPlane affine transforms in n dimensions.
1 parent 4b4b9c5 commit a0eabb0

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/main/java/net/imglib2/algorithm/kdtree/ConvexPolytope.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import java.util.Collection;
4040

4141
import net.imglib2.AbstractEuclideanSpace;
42-
import net.imglib2.realtransform.AffineTransform3D;
42+
import net.imglib2.realtransform.AffineGet;
4343
import net.imglib2.util.LinAlgHelpers;
4444

4545
public class ConvexPolytope extends AbstractEuclideanSpace
@@ -63,27 +63,29 @@ public ConvexPolytope( final HyperPlane... hyperplanes )
6363
}
6464

6565
/**
66-
* Apply an {@link AffineTransform3D} to a 3D {@link ConvexPolytope}.
66+
* Apply an {@link AffineGet affine transformation} to a {@link HyperPlane}.
6767
*
6868
* @param polytope
69-
* a 3D polytope.
69+
* a polytope.
7070
* @param transform
7171
* affine transformation to apply to the polytope.
7272
* @return the transformed polytope.
7373
*/
74-
public static ConvexPolytope transform( final ConvexPolytope polytope, final AffineTransform3D transform )
74+
public static ConvexPolytope transform( final ConvexPolytope polytope, final AffineGet transform )
7575
{
76-
assert polytope.numDimensions() == 3;
76+
assert polytope.numDimensions() == transform.numDimensions();
7777

78-
final double[] O = new double[ 3 ];
79-
final double[] tO = new double[ 3 ];
80-
final double[] tN = new double[ 3 ];
81-
final double[][] m = new double[3][3];
82-
for ( int r = 0; r < 3; ++r )
83-
for ( int c = 0; c < 3; ++c )
78+
final int n = transform.numDimensions();
79+
80+
final double[] O = new double[ n ];
81+
final double[] tO = new double[ n ];
82+
final double[] tN = new double[ n ];
83+
final double[][] m = new double[n][n];
84+
for ( int r = 0; r < n; ++r )
85+
for ( int c = 0; c < n; ++c )
8486
m[r][c] = transform.inverse().get( c, r );
8587

86-
final ArrayList< HyperPlane > transformedPlanes = new ArrayList< HyperPlane >();
88+
final ArrayList< HyperPlane > transformedPlanes = new ArrayList<>();
8789
for ( final HyperPlane plane : polytope.getHyperplanes() )
8890
{
8991
LinAlgHelpers.scale( plane.getNormal(), plane.getDistance(), O );

src/main/java/net/imglib2/algorithm/kdtree/HyperPlane.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
package net.imglib2.algorithm.kdtree;
3636

3737
import net.imglib2.AbstractEuclideanSpace;
38-
import net.imglib2.realtransform.AffineTransform3D;
38+
import net.imglib2.realtransform.AffineGet;
3939
import net.imglib2.util.LinAlgHelpers;
4040

4141
public class HyperPlane extends AbstractEuclideanSpace
@@ -72,28 +72,30 @@ public double getDistance()
7272
}
7373

7474
/**
75-
* Apply an {@link AffineTransform3D} to a 3D {@link HyperPlane}.
75+
* Apply an {@link AffineGet affine transformation} to a {@link HyperPlane}.
7676
*
7777
* @param plane
78-
* a 3D plane.
78+
* a plane.
7979
* @param transform
8080
* affine transformation to apply to the plane.
8181
* @return the transformed plane.
8282
*/
83-
public static HyperPlane transform( final HyperPlane plane, final AffineTransform3D transform )
83+
public static HyperPlane transform( final HyperPlane plane, final AffineGet transform )
8484
{
85-
assert plane.numDimensions() == 3;
85+
assert plane.numDimensions() == transform.numDimensions();
8686

87-
final double[] O = new double[ 3 ];
88-
final double[] tO = new double[ 3 ];
87+
final int n = transform.numDimensions();
88+
89+
final double[] O = new double[ n ];
90+
final double[] tO = new double[ n ];
8991
LinAlgHelpers.scale( plane.getNormal(), plane.getDistance(), O );
9092
transform.apply( O, tO );
9193

92-
final double[][] m = new double[ 3 ][ 3 ];
93-
for ( int r = 0; r < 3; ++r )
94-
for ( int c = 0; c < 3; ++c )
94+
final double[][] m = new double[ n ][ n ];
95+
for ( int r = 0; r < n; ++r )
96+
for ( int c = 0; c < n; ++c )
9597
m[ r ][ c ] = transform.inverse().get( c, r );
96-
final double[] tN = new double[ 3 ];
98+
final double[] tN = new double[ n ];
9799
LinAlgHelpers.mult( m, plane.getNormal(), tN );
98100
LinAlgHelpers.normalize( tN );
99101
final double td = LinAlgHelpers.dot( tN, tO );

0 commit comments

Comments
 (0)