Skip to content

Commit fb03366

Browse files
gselzerctrueden
authored andcommitted
WIP: port linalg namespace
TODO: ensure passing tests
1 parent 43c0e55 commit fb03366

3 files changed

Lines changed: 295 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*-
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package net.imagej.ops.linalg.rotate;
30+
31+
import org.joml.AxisAngle4d;
32+
import org.joml.AxisAngle4f;
33+
import org.joml.Quaterniond;
34+
import org.joml.Quaterniondc;
35+
import org.joml.Quaternionf;
36+
import org.joml.Quaternionfc;
37+
import org.joml.Vector3d;
38+
import org.joml.Vector3f;
39+
import org.scijava.ops.OpField;
40+
import org.scijava.ops.core.OpCollection;
41+
import org.scijava.ops.core.computer.BiComputer;
42+
import org.scijava.param.Parameter;
43+
import org.scijava.plugin.Plugin;
44+
import org.scijava.struct.ItemIO;
45+
46+
/**
47+
* Rotates the vector by the quaternion.
48+
*
49+
* @author Richard Domander (Royal Veterinary College, London)
50+
* @author Gabriel Selzer
51+
*/
52+
@Plugin(type = OpCollection.class)
53+
public class Rotations {
54+
55+
@OpField(names = "linalg.rotate")
56+
@Parameter(key = "inVector")
57+
@Parameter(key = "quaternion")
58+
@Parameter(key = "vDot", type = ItemIO.BOTH)
59+
public final BiComputer<Vector3d, Quaterniondc, Vector3d> rotate3d = (v, q, vDot) -> {
60+
vDot.set(v);
61+
vDot.rotate(q);
62+
};
63+
64+
@OpField(names = "linalg.rotate")
65+
@Parameter(key = "inVector")
66+
@Parameter(key = "axisAngle")
67+
@Parameter(key = "vDot", type = ItemIO.BOTH)
68+
public final BiComputer<Vector3d, AxisAngle4d, Vector3d> rotate3dAxisAngle = (v, aa, vDot) -> rotate3d.compute(v,
69+
new Quaterniond(aa), vDot);
70+
71+
@OpField(names = "linalg.rotate")
72+
@Parameter(key = "inVector")
73+
@Parameter(key = "quaternion")
74+
@Parameter(key = "vDot", type = ItemIO.BOTH)
75+
public final BiComputer<Vector3f, Quaternionfc, Vector3f> rotate3f = (v, q, vDot) -> {
76+
vDot.set(v);
77+
vDot.rotate(q);
78+
};
79+
80+
@OpField(names = "linalg.rotate")
81+
@Parameter(key = "inVector")
82+
@Parameter(key = "axisAngle")
83+
@Parameter(key = "vDot", type = ItemIO.BOTH)
84+
public final BiComputer<Vector3f, AxisAngle4f, Vector3f> rotate3fAxisAngle = (v, aa, vDot) -> rotate3f.compute(v,
85+
new Quaternionf(aa), vDot);
86+
87+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*-
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.linalg.rotate;
31+
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNotSame;
34+
import static org.junit.Assert.assertSame;
35+
36+
import net.imagej.ops.AbstractOpTest;
37+
38+
import org.joml.AxisAngle4d;
39+
import org.joml.Quaterniond;
40+
import org.joml.Quaterniondc;
41+
import org.joml.Vector3d;
42+
import org.junit.Test;
43+
44+
/**
45+
* Tests for {@link Rotate3d}.
46+
*
47+
* @author Richard Domander (Royal Veterinary College, London)
48+
*/
49+
public class Rotate3dTest extends AbstractOpTest {
50+
51+
private static final Quaterniondc IDENTITY = new Quaterniond(1, 0, 0, 0);
52+
53+
@Test
54+
public void testAxisAngle() {
55+
final Vector3d xAxis = new Vector3d(1, 0, 0);
56+
final Vector3d in = new Vector3d(xAxis);
57+
final AxisAngle4d axisAngle = new AxisAngle4d(Math.PI / 2.0, 0, 0, 1);
58+
final Vector3d expected = xAxis.rotate(new Quaterniond(axisAngle));
59+
60+
final Vector3d result = (Vector3d) ops.run("linalg.rotate", in, axisAngle);
61+
62+
assertEquals("Rotation is incorrect", expected, result);
63+
}
64+
65+
@Test
66+
public void testCalculate() {
67+
final Vector3d xAxis = new Vector3d(1, 0, 0);
68+
final Vector3d in = new Vector3d(xAxis);
69+
70+
final Vector3d result = (Vector3d) ops.run("linalg.rotate", in, IDENTITY);
71+
72+
assertNotSame("Op should create a new object for output", in, result);
73+
assertEquals("Rotation is incorrect", xAxis, result);
74+
}
75+
76+
@Test
77+
public void testCompute() {
78+
final Vector3d origin = new Vector3d();
79+
final Vector3d xAxis = new Vector3d(1, 0, 0);
80+
final Vector3d in = new Vector3d(xAxis);
81+
final Vector3d out = new Vector3d(origin);
82+
83+
final Vector3d result = (Vector3d) ops.run("linalg.rotate", in, IDENTITY, out);
84+
85+
assertSame("Op should not create a new object for output", out, result);
86+
assertEquals("Rotation is incorrect", xAxis, out);
87+
}
88+
89+
// TODO: X -> Inplace transformers
90+
// @Test
91+
// public void testMutate() {
92+
// final Vector3d xAxis = new Vector3d(1, 0, 0);
93+
// final Vector3d in = new Vector3d(xAxis);
94+
// final Quaterniond q = new Quaterniond(new AxisAngle4d(Math.PI / 2.0, 0, 0,
95+
// 1));
96+
// final Vector3d expected = xAxis.rotate(q);
97+
//
98+
// final Vector3d result = ops.linalg().rotate1(in, q);
99+
//
100+
// assertSame("Mutate should operate on the input object", in, result);
101+
// assertEquals("Rotation is incorrect", expected, result);
102+
// }
103+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*-
2+
* #%L
3+
* ImageJ software for multidimensional image processing and analysis.
4+
* %%
5+
* Copyright (C) 2014 - 2018 ImageJ developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package net.imagej.ops.linalg.rotate;
31+
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNotSame;
34+
import static org.junit.Assert.assertSame;
35+
36+
import net.imagej.ops.AbstractOpTest;
37+
38+
import org.joml.AxisAngle4f;
39+
import org.joml.Quaternionf;
40+
import org.joml.Quaternionfc;
41+
import org.joml.Vector3f;
42+
import org.junit.Test;
43+
44+
/**
45+
* Tests for {@link Rotate3f}.
46+
*
47+
* @author Richard Domander (Royal Veterinary College, London)
48+
*/
49+
public class Rotate3fTest extends AbstractOpTest {
50+
51+
private static final Quaternionfc IDENTITY = new Quaternionf(1, 0, 0, 0);
52+
53+
@Test
54+
public void testAxisAngle() {
55+
final Vector3f xAxis = new Vector3f(1, 0, 0);
56+
final Vector3f in = new Vector3f(xAxis);
57+
final AxisAngle4f axisAngle = new AxisAngle4f((float) (Math.PI / 2.0), 0, 0,
58+
1);
59+
final Vector3f expected = xAxis.rotate(new Quaternionf(axisAngle));
60+
61+
final Vector3f result = (Vector3f) ops.run("linalg.rotate", in, axisAngle);
62+
63+
assertEquals("Rotation is incorrect", expected, result);
64+
}
65+
66+
@Test
67+
public void testCalculate() {
68+
final Vector3f xAxis = new Vector3f(1, 0, 0);
69+
final Vector3f in = new Vector3f(xAxis);
70+
71+
final Vector3f result = (Vector3f) ops.run("linalg.rotate", in, IDENTITY);
72+
73+
assertNotSame("Op should create a new object for output", in, result);
74+
assertEquals("Rotation is incorrect", xAxis, result);
75+
}
76+
77+
@Test
78+
public void testCompute() {
79+
final Vector3f origin = new Vector3f();
80+
final Vector3f xAxis = new Vector3f(1, 0, 0);
81+
final Vector3f in = new Vector3f(xAxis);
82+
final Vector3f out = new Vector3f(origin);
83+
84+
final Vector3f result = (Vector3f) ops.run("linalg.rotate", in, IDENTITY, out);
85+
86+
assertSame("Op should not create a new object for output", out, result);
87+
assertEquals("Rotation is incorrect", xAxis, out);
88+
}
89+
90+
//TODO: X -> Inplaces transformers
91+
// @Test
92+
// public void testMutate() {
93+
// final Vector3f xAxis = new Vector3f(1, 0, 0);
94+
// final Vector3f in = new Vector3f(xAxis);
95+
// final Quaternionf q = new Quaternionf(new AxisAngle4f((float) (Math.PI /
96+
// 2.0), 0, 0, 1));
97+
// final Vector3f expected = xAxis.rotate(q);
98+
//
99+
// final Vector3f result = ops.linalg().rotate1(in, q);
100+
//
101+
// assertSame("Mutate should operate on the input object", in, result);
102+
// assertEquals("Rotation is incorrect", expected, result);
103+
// }
104+
105+
}

0 commit comments

Comments
 (0)