|
| 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.math; |
| 31 | + |
| 32 | +import net.imglib2.type.numeric.NumericType; |
| 33 | +import net.imglib2.type.numeric.RealType; |
| 34 | + |
| 35 | +import org.scijava.core.Priority; |
| 36 | +import org.scijava.ops.OpField; |
| 37 | +import org.scijava.ops.core.OpCollection; |
| 38 | +import org.scijava.ops.core.computer.BiComputer; |
| 39 | +import org.scijava.ops.core.computer.Computer3; |
| 40 | +import org.scijava.param.Parameter; |
| 41 | +import org.scijava.plugin.Plugin; |
| 42 | +import org.scijava.struct.ItemIO; |
| 43 | + |
| 44 | +/** |
| 45 | + * Binary Ops of the {@code math} namespace which operate on {@link RealType}s. |
| 46 | + * |
| 47 | + * TODO: Can these be static? |
| 48 | + * |
| 49 | + * @author Leon Yang |
| 50 | + */ |
| 51 | +@Plugin(type = OpCollection.class) |
| 52 | +public class BinaryNumericTypeMath <T extends NumericType<T>> { |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets the real component of an output real number to the addition of the |
| 56 | + * real components of two input real numbers. |
| 57 | + */ |
| 58 | + @OpField(names = "math.add", priority = Priority.HIGH) |
| 59 | + @Parameter(key = "input1") |
| 60 | + @Parameter(key = "input2") |
| 61 | + @Parameter(key = "sum", type = ItemIO.BOTH) |
| 62 | + public final BiComputer<T, T, T> adder = (input1, input2, output) -> { |
| 63 | + output.set(input1); |
| 64 | + output.add(input2); |
| 65 | + }; |
| 66 | + |
| 67 | + /** |
| 68 | + * Sets the real component of an output real number to the division of the |
| 69 | + * real component of two input real numbers. |
| 70 | + */ |
| 71 | + @OpField(names = "math.divide", priority = Priority.HIGH) |
| 72 | + @Parameter(key = "input1") |
| 73 | + @Parameter(key = "input2") |
| 74 | + @Parameter(key = "divideByZeroValue") |
| 75 | + @Parameter(key = "result", type = ItemIO.BOTH) |
| 76 | + public final Computer3<T, T, T, T> divider = (input1, input2, dbzVal, output) -> { |
| 77 | + try { |
| 78 | + output.set(input1); |
| 79 | + output.div(input2); |
| 80 | + } catch(Exception e) { |
| 81 | + output.set(dbzVal); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + /** |
| 86 | + * Sets the real component of an output real number to the multiplication of |
| 87 | + * the real component of two input real numbers. |
| 88 | + */ |
| 89 | + @OpField(names = "math.multiply", priority = Priority.HIGH) |
| 90 | + @Parameter(key = "input1") |
| 91 | + @Parameter(key = "input2") |
| 92 | + @Parameter(key = "result", type = ItemIO.BOTH) |
| 93 | + public final BiComputer<T, T, T> multiplier = (input1, input2, output) -> { |
| 94 | + output.set(input1); |
| 95 | + output.mul(input2); |
| 96 | + }; |
| 97 | + |
| 98 | + /* |
| 99 | + * Sets the real component of an output real number to the subtraction between |
| 100 | + * the real component of two input real numbers. |
| 101 | + */ |
| 102 | + @OpField(names = "math.subtract", priority = Priority.HIGH) |
| 103 | + @Parameter(key = "input1") |
| 104 | + @Parameter(key = "input2") |
| 105 | + @Parameter(key = "result", type = ItemIO.BOTH) |
| 106 | + public final BiComputer<T, T, T> subtracter = (input1, input2, output) -> { |
| 107 | + output.set(input1); |
| 108 | + output.sub(input2); |
| 109 | + }; |
| 110 | + |
| 111 | +} |
0 commit comments