Skip to content

Commit

Permalink
Add tuple3d centroid op
Browse files Browse the repository at this point in the history
  • Loading branch information
rimadoma committed Jul 12, 2016
1 parent ea6ea79 commit 400eb51
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@
<groupId>org.scijava</groupId>
<artifactId>scripting-javascript</artifactId>
</dependency>
<dependency>
<groupId>org.scijava</groupId>
<artifactId>vecmath</artifactId>
</dependency>

<!-- Third party dependencies -->
<dependency>
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/net/imagej/ops/geom/CentroidTuple3D.java
Original file line number Diff line number Diff line change
@@ -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<Collection<? extends Tuple3d>, Vector3d>
implements Ops.Geometric.Centroid {
@Override
public Vector3d compute1(final Collection<? extends Tuple3d> 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;
}
}
9 changes: 9 additions & 0 deletions src/main/java/net/imagej/ops/geom/GeomNamespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
import net.imglib2.util.Pair;

import org.scijava.plugin.Plugin;
import org.scijava.vecmath.Tuple3d;
import org.scijava.vecmath.Vector3d;

import java.util.Collection;

/**
* Namespace for Geom.
Expand Down Expand Up @@ -221,6 +225,11 @@ public RealLocalizable centroid(final Mesh in) {
return result;
}

@OpMethod(op = net.imagej.ops.geom.CentroidTuple3D.class)
public Vector3d centroid(final Collection<? extends Tuple3d> 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 Polygon in) {
final DoubleType result =
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java
Original file line number Diff line number Diff line change
@@ -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<Vector3d> 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<Vector3d> 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<Vector3d> 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);
}
}

0 comments on commit 400eb51

Please sign in to comment.