Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CentroidTuple3d op #420

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
</dependency>
<dependency>
<groupId>org.joml</groupId>
<artifactId>joml</artifactId>
</dependency>
<dependency>
<groupId>gov.nist.math</groupId>
<artifactId>jama</artifactId>
</dependency>

<!-- Test scope dependencies -->
<dependency>
Expand Down Expand Up @@ -294,10 +302,5 @@
<artifactId>ij</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.imglib2</groupId>
<artifactId>imglib2-ij</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
37 changes: 37 additions & 0 deletions src/main/java/net/imagej/ops/geom/CentroidVector3d.java
Original file line number Diff line number Diff line change
@@ -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<Iterable<Vector3dc>, Vector3d> implements
Ops.Geometric.Centroid
{

@Override
public Vector3d calculate(final Iterable<Vector3dc> input) {
final Iterator<Vector3dc> 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;
}
}
7 changes: 7 additions & 0 deletions src/main/java/net/imagej/ops/geom/GeomNamespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
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;

/**
Expand Down Expand Up @@ -228,6 +230,11 @@ public RealLocalizable centroid(final Mesh in) {
return result;
}

@OpMethod(op = net.imagej.ops.geom.CentroidVector3d.class)
public Vector3d centroid(final Iterable<Vector3dc> 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 =
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/net/imagej/ops/geom/CentroidVector3dTest.java
Original file line number Diff line number Diff line change
@@ -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<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)
);
//@formatter:on

final Vector3d result = (Vector3d) ops.run(CentroidVector3d.class,
cubeVectors);

assertEquals("Incorrect centroid vector", expected, result);
}

@Test
public void testCalculateEmptyCollection() {
final List<Vector3d> 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<Vector3d> vectors = Collections.singletonList(vector);

final Vector3d result = (Vector3d) ops.run(CentroidVector3d.class, vectors);

assertEquals("Incorrect centroid vector", vector, result);
}
}