From f9cd535fdb50ba372c803648dce82f1ef370a4c5 Mon Sep 17 00:00:00 2001 From: rimadoma Date: Tue, 12 Jul 2016 17:12:56 +0200 Subject: [PATCH 1/5] Add tuple3d centroid op --- pom.xml | 4 ++ .../net/imagej/ops/geom/CentroidTuple3D.java | 32 ++++++++++ .../net/imagej/ops/geom/GeomNamespace.java | 9 +++ .../imagej/ops/geom/CentroidTuple3DTest.java | 59 +++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 src/main/java/net/imagej/ops/geom/CentroidTuple3D.java create mode 100644 src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java diff --git a/pom.xml b/pom.xml index 5909e9d219..18441a27c5 100644 --- a/pom.xml +++ b/pom.xml @@ -260,6 +260,10 @@ org.scijava scripting-javascript + + org.scijava + vecmath + diff --git a/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java b/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java new file mode 100644 index 0000000000..32a9770c71 --- /dev/null +++ b/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java @@ -0,0 +1,32 @@ +package net.imagej.ops.geom; + +import net.imagej.ops.Ops; +import net.imagej.ops.special.function.AbstractUnaryFunctionOp; +import org.scijava.plugin.Plugin; +import org.scijava.vecmath.Tuple3d; +import org.scijava.vecmath.Vector3d; + +import java.util.Collection; + +/** + * Calculates the centroid (geometrical centre) of the given tuples + * + * @author Richard Domander (Royal Veterinary College, London) + */ +@Plugin(type = Ops.Geometric.Centroid.class) +public class CentroidTuple3D extends AbstractUnaryFunctionOp, Vector3d> + implements Ops.Geometric.Centroid { + @Override + public Vector3d compute1(final Collection input) { + final int vectors = input.size(); + if (vectors == 0) { + return new Vector3d(Double.NaN, Double.NaN, Double.NaN); + } + + final Vector3d sum = new Vector3d(0.0, 0.0, 0.0); + input.stream().forEach(sum::add); + sum.scale(1.0 / vectors); + + return sum; + } +} diff --git a/src/main/java/net/imagej/ops/geom/GeomNamespace.java b/src/main/java/net/imagej/ops/geom/GeomNamespace.java index 621fc28fbf..c58fbcd7fe 100644 --- a/src/main/java/net/imagej/ops/geom/GeomNamespace.java +++ b/src/main/java/net/imagej/ops/geom/GeomNamespace.java @@ -51,6 +51,10 @@ import org.apache.commons.math3.linear.RealMatrix; import org.scijava.plugin.Plugin; +import org.scijava.vecmath.Tuple3d; +import org.scijava.vecmath.Vector3d; + +import java.util.Collection; /** * Namespace for Geom. @@ -228,6 +232,11 @@ public RealLocalizable centroid(final Mesh in) { return result; } + @OpMethod(op = net.imagej.ops.geom.CentroidTuple3D.class) + public Vector3d centroid(final Collection in) { + return (Vector3d) ops().run(net.imagej.ops.Ops.Geometric.Centroid.class, in); + } + @OpMethod(op = net.imagej.ops.geom.geom2d.DefaultCircularity.class) public DoubleType circularity(final Polygon2D in) { final DoubleType result = diff --git a/src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java b/src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java new file mode 100644 index 0000000000..fa67648311 --- /dev/null +++ b/src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java @@ -0,0 +1,59 @@ +package net.imagej.ops.geom; + +import net.imagej.ops.AbstractOpTest; +import org.junit.Test; +import org.scijava.vecmath.Vector3d; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.DoubleStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Unit tests for {@link CentroidTuple3D} + * + * @author Richard Domander (Royal Veterinary College, London) + */ +public class CentroidTuple3DTest extends AbstractOpTest { + @Test + public void testCompute1EmptyCollection() throws Exception { + final List emptyList = Collections.emptyList(); + + final Vector3d result = (Vector3d) ops.run(CentroidTuple3D.class, emptyList); + + assertTrue("Coordinates should all be NaN", + DoubleStream.of(result.x, result.y, result.z).allMatch(Double::isNaN)); + } + + @Test + public void testCompute1SingleVector() throws Exception { + final Vector3d vector = new Vector3d(1.0, 2.0, 3.0); + final List vectors = Collections.singletonList(vector); + + final Vector3d result = (Vector3d) ops.run(CentroidTuple3D.class, vectors); + + assertEquals("Incorrect centroid vector", vector, result); + } + + @Test + public void testCompute1() throws Exception { + final Vector3d expected = new Vector3d(0.5, 0.5, 0.5); + final List cubeVectors = Arrays.asList( + new Vector3d(0.0, 0.0, 0.0), + new Vector3d(1.0, 0.0, 0.0), + new Vector3d(1.0, 1.0, 0.0), + new Vector3d(0.0, 1.0, 0.0), + new Vector3d(0.0, 0.0, 1.0), + new Vector3d(1.0, 0.0, 1.0), + new Vector3d(1.0, 1.0, 1.0), + new Vector3d(0.0, 1.0, 1.0) + ); + + final Vector3d result = (Vector3d) ops.run(CentroidTuple3D.class, cubeVectors); + + assertEquals("Incorrect centroid vector", expected, result); + } +} \ No newline at end of file From ef011ea6a3e7fd2bfb4c9c61a7694b828e73eb74 Mon Sep 17 00:00:00 2001 From: Richard Domander Date: Tue, 19 Jun 2018 18:08:19 +0100 Subject: [PATCH 2/5] Conform to new API This commit also rebased branch --- src/main/java/net/imagej/ops/geom/CentroidTuple3D.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java b/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java index 32a9770c71..6cb9de120e 100644 --- a/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java +++ b/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java @@ -17,7 +17,7 @@ public class CentroidTuple3D extends AbstractUnaryFunctionOp, Vector3d> implements Ops.Geometric.Centroid { @Override - public Vector3d compute1(final Collection input) { + public Vector3d calculate(final Collection input) { final int vectors = input.size(); if (vectors == 0) { return new Vector3d(Double.NaN, Double.NaN, Double.NaN); From 3d91beba2d99ec4a8e91ac52ec401ea6f3a6dc4f Mon Sep 17 00:00:00 2001 From: Richard Domander Date: Wed, 20 Jun 2018 15:58:15 +0100 Subject: [PATCH 3/5] Switch to JOML Switch from org.scijava.vecmath to org.joml.joml. --- pom.xml | 8 +-- .../net/imagej/ops/geom/CentroidTuple3D.java | 32 --------- .../net/imagej/ops/geom/CentroidVector3d.java | 37 +++++++++++ .../net/imagej/ops/geom/GeomNamespace.java | 10 ++- .../imagej/ops/geom/CentroidTuple3DTest.java | 59 ----------------- .../imagej/ops/geom/CentroidVector3dTest.java | 66 +++++++++++++++++++ 6 files changed, 111 insertions(+), 101 deletions(-) delete mode 100644 src/main/java/net/imagej/ops/geom/CentroidTuple3D.java create mode 100644 src/main/java/net/imagej/ops/geom/CentroidVector3d.java delete mode 100644 src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java create mode 100644 src/test/java/net/imagej/ops/geom/CentroidVector3dTest.java diff --git a/pom.xml b/pom.xml index 18441a27c5..0ddc75d505 100644 --- a/pom.xml +++ b/pom.xml @@ -260,16 +260,16 @@ org.scijava scripting-javascript - - org.scijava - vecmath - org.apache.commons commons-math3 + + org.joml + joml + diff --git a/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java b/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java deleted file mode 100644 index 6cb9de120e..0000000000 --- a/src/main/java/net/imagej/ops/geom/CentroidTuple3D.java +++ /dev/null @@ -1,32 +0,0 @@ -package net.imagej.ops.geom; - -import net.imagej.ops.Ops; -import net.imagej.ops.special.function.AbstractUnaryFunctionOp; -import org.scijava.plugin.Plugin; -import org.scijava.vecmath.Tuple3d; -import org.scijava.vecmath.Vector3d; - -import java.util.Collection; - -/** - * Calculates the centroid (geometrical centre) of the given tuples - * - * @author Richard Domander (Royal Veterinary College, London) - */ -@Plugin(type = Ops.Geometric.Centroid.class) -public class CentroidTuple3D extends AbstractUnaryFunctionOp, Vector3d> - implements Ops.Geometric.Centroid { - @Override - public Vector3d calculate(final Collection input) { - final int vectors = input.size(); - if (vectors == 0) { - return new Vector3d(Double.NaN, Double.NaN, Double.NaN); - } - - final Vector3d sum = new Vector3d(0.0, 0.0, 0.0); - input.stream().forEach(sum::add); - sum.scale(1.0 / vectors); - - return sum; - } -} diff --git a/src/main/java/net/imagej/ops/geom/CentroidVector3d.java b/src/main/java/net/imagej/ops/geom/CentroidVector3d.java new file mode 100644 index 0000000000..77b3924c84 --- /dev/null +++ b/src/main/java/net/imagej/ops/geom/CentroidVector3d.java @@ -0,0 +1,37 @@ + +package net.imagej.ops.geom; + +import java.util.Iterator; + +import net.imagej.ops.Ops; +import net.imagej.ops.special.function.AbstractUnaryFunctionOp; + +import org.joml.Vector3d; +import org.joml.Vector3dc; +import org.scijava.plugin.Plugin; + +/** + * Calculates the centroid (geometrical centre) of the vectors + * + * @author Richard Domander (Royal Veterinary College, London) + */ +@Plugin(type = Ops.Geometric.Centroid.class) +public class CentroidVector3d extends + AbstractUnaryFunctionOp, Vector3d> implements + Ops.Geometric.Centroid +{ + + @Override + public Vector3d calculate(final Iterable input) { + final Iterator iterator = input.iterator(); + final Vector3d sum = new Vector3d(); + long count = 0; + while (iterator.hasNext()) { + final Vector3dc v = iterator.next(); + sum.add(v); + count++; + } + sum.div(count); + return sum; + } +} diff --git a/src/main/java/net/imagej/ops/geom/GeomNamespace.java b/src/main/java/net/imagej/ops/geom/GeomNamespace.java index c58fbcd7fe..5a0c6314f1 100644 --- a/src/main/java/net/imagej/ops/geom/GeomNamespace.java +++ b/src/main/java/net/imagej/ops/geom/GeomNamespace.java @@ -50,11 +50,9 @@ import net.imglib2.util.Pair; import org.apache.commons.math3.linear.RealMatrix; +import org.joml.Vector3d; +import org.joml.Vector3dc; import org.scijava.plugin.Plugin; -import org.scijava.vecmath.Tuple3d; -import org.scijava.vecmath.Vector3d; - -import java.util.Collection; /** * Namespace for Geom. @@ -232,8 +230,8 @@ public RealLocalizable centroid(final Mesh in) { return result; } - @OpMethod(op = net.imagej.ops.geom.CentroidTuple3D.class) - public Vector3d centroid(final Collection in) { + @OpMethod(op = net.imagej.ops.geom.CentroidVector3d.class) + public Vector3d centroid(final Iterable in) { return (Vector3d) ops().run(net.imagej.ops.Ops.Geometric.Centroid.class, in); } diff --git a/src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java b/src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java deleted file mode 100644 index fa67648311..0000000000 --- a/src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package net.imagej.ops.geom; - -import net.imagej.ops.AbstractOpTest; -import org.junit.Test; -import org.scijava.vecmath.Vector3d; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.stream.DoubleStream; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * Unit tests for {@link CentroidTuple3D} - * - * @author Richard Domander (Royal Veterinary College, London) - */ -public class CentroidTuple3DTest extends AbstractOpTest { - @Test - public void testCompute1EmptyCollection() throws Exception { - final List emptyList = Collections.emptyList(); - - final Vector3d result = (Vector3d) ops.run(CentroidTuple3D.class, emptyList); - - assertTrue("Coordinates should all be NaN", - DoubleStream.of(result.x, result.y, result.z).allMatch(Double::isNaN)); - } - - @Test - public void testCompute1SingleVector() throws Exception { - final Vector3d vector = new Vector3d(1.0, 2.0, 3.0); - final List vectors = Collections.singletonList(vector); - - final Vector3d result = (Vector3d) ops.run(CentroidTuple3D.class, vectors); - - assertEquals("Incorrect centroid vector", vector, result); - } - - @Test - public void testCompute1() throws Exception { - final Vector3d expected = new Vector3d(0.5, 0.5, 0.5); - final List cubeVectors = Arrays.asList( - new Vector3d(0.0, 0.0, 0.0), - new Vector3d(1.0, 0.0, 0.0), - new Vector3d(1.0, 1.0, 0.0), - new Vector3d(0.0, 1.0, 0.0), - new Vector3d(0.0, 0.0, 1.0), - new Vector3d(1.0, 0.0, 1.0), - new Vector3d(1.0, 1.0, 1.0), - new Vector3d(0.0, 1.0, 1.0) - ); - - final Vector3d result = (Vector3d) ops.run(CentroidTuple3D.class, cubeVectors); - - assertEquals("Incorrect centroid vector", expected, result); - } -} \ No newline at end of file diff --git a/src/test/java/net/imagej/ops/geom/CentroidVector3dTest.java b/src/test/java/net/imagej/ops/geom/CentroidVector3dTest.java new file mode 100644 index 0000000000..1aa12e803a --- /dev/null +++ b/src/test/java/net/imagej/ops/geom/CentroidVector3dTest.java @@ -0,0 +1,66 @@ + +package net.imagej.ops.geom; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.DoubleStream; + +import net.imagej.ops.AbstractOpTest; + +import org.joml.Vector3d; +import org.junit.Test; + +/** + * Unit tests for {@link CentroidVector3d} + * + * @author Richard Domander (Royal Veterinary College, London) + */ +public class CentroidVector3dTest extends AbstractOpTest { + + @Test + public void testCalculate() { + final Vector3d expected = new Vector3d(0.5, 0.5, 0.5); + //@formatter:off + final List cubeVectors = Arrays.asList( + new Vector3d(0.0, 0.0, 0.0), + new Vector3d(1.0, 0.0, 0.0), + new Vector3d(1.0, 1.0, 0.0), + new Vector3d(0.0, 1.0, 0.0), + new Vector3d(0.0, 0.0, 1.0), + new Vector3d(1.0, 0.0, 1.0), + new Vector3d(1.0, 1.0, 1.0), + new Vector3d(0.0, 1.0, 1.0) + ); + //@formatter:on + + final Vector3d result = (Vector3d) ops.run(CentroidVector3d.class, + cubeVectors); + + assertEquals("Incorrect centroid vector", expected, result); + } + + @Test + public void testCalculateEmptyCollection() { + final List emptyList = Collections.emptyList(); + + final Vector3d result = (Vector3d) ops.run(CentroidVector3d.class, + emptyList); + + assertTrue("Coordinates should all be NaN", DoubleStream.of(result.x, + result.y, result.z).allMatch(Double::isNaN)); + } + + @Test + public void testCalculateSingleVector() { + final Vector3d vector = new Vector3d(1.0, 2.0, 3.0); + final List vectors = Collections.singletonList(vector); + + final Vector3d result = (Vector3d) ops.run(CentroidVector3d.class, vectors); + + assertEquals("Incorrect centroid vector", vector, result); + } +} From a7c617dff60a842bf3161b66dc76e5b88b8df288 Mon Sep 17 00:00:00 2001 From: Richard Domander Date: Wed, 20 Jun 2018 16:09:35 +0100 Subject: [PATCH 4/5] Remove unused dependency --- pom.xml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pom.xml b/pom.xml index 0ddc75d505..55a706ecbd 100644 --- a/pom.xml +++ b/pom.xml @@ -298,10 +298,5 @@ ij test - - net.imglib2 - imglib2-ij - test - From 4e61cfaaf27e25a9500b44f267741c1c80fa1940 Mon Sep 17 00:00:00 2001 From: Richard Domander Date: Wed, 20 Jun 2018 16:13:19 +0100 Subject: [PATCH 5/5] Add missing dependency --- pom.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pom.xml b/pom.xml index 55a706ecbd..55145f51f4 100644 --- a/pom.xml +++ b/pom.xml @@ -270,6 +270,10 @@ org.joml joml + + gov.nist.math + jama +