-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rimadoma
committed
Jul 12, 2016
1 parent
ea6ea79
commit 400eb51
Showing
4 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/test/java/net/imagej/ops/geom/CentroidTuple3DTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |