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

Replace List<Number> with Range #682

Open
wants to merge 2 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
95 changes: 95 additions & 0 deletions core/src/main/java/edu/wpi/grip/core/Range.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package edu.wpi.grip.core;

import static com.google.common.base.Preconditions.checkArgument;

/**
* Holds the lower and upper bounds of a range of numbers.
*/
public class Range {

private double min;
private double max;

/**
* Creates a new range with both bounds equal to zero.
*/
public Range() {
this(0, 0);
}

/**
* Creates a new range with the given bounds.
*
* @param min the lower end of the range
* @param max the upper end of the range
*
* @throws IllegalArgumentException if min > max
*/
public Range(double min, double max) {
checkArgument(min <= max, "Min must be <= max");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably be more usable if, instead of throwing checkArgument exceptions, this just did the intelligent thing and set min and max to the smallest and the largest, respectively.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I would agree with that.

this.min = min;
this.max = max;
}

/**
* Creates a new range with the given bounds. This is equivalent to
* {@link #Range(double, double) new Range(min, max)} .
*
* @param min the lower end of the range
* @param max the upper end of the range
*
* @return a new range for the given bounds
*
* @throws IllegalArgumentException if min > max
*/
public static Range of(double min, double max) {
return new Range(min, max);
}

/**
* Sets the lower end of the range.
*
* @param min the new lower end of the range
*
* @throws IllegalArgumentException if min > max
*/
public void setMin(double min) {
checkArgument(min <= max, "Min must be <= max");
this.min = min;
}

/**
* Sets the upper end of the range.
*
* @param max the new upper end of the range
*
* @throws IllegalArgumentException if max < min
*/
public void setMax(double max) {
checkArgument(max >= min, "Max must be >= min");
this.max = max;
}

/**
* Gets the lower bound of the range.
*
* @return the lower bound of the range
*/
public double getMin() {
return min;
}

/**
* Gets the upper bound of the range.
*
* @return the upper bound of the range
*/
public double getMax() {
return max;
}

public String toString() {
return String.format("Range(%f, %f)", min, max);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.wpi.grip.core.Operation;
import edu.wpi.grip.core.OperationDescription;
import edu.wpi.grip.core.Range;
import edu.wpi.grip.core.sockets.InputSocket;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.core.sockets.SocketHint;
Expand Down Expand Up @@ -60,8 +61,8 @@ public class FilterContoursOperation implements Operation {
private final SocketHint<Number> maxHeightHint =
SocketHints.Inputs.createNumberSpinnerSocketHint("Max Height", 1000, 0, Integer.MAX_VALUE);

private final SocketHint<List<Number>> solidityHint =
SocketHints.Inputs.createNumberListRangeSocketHint("Solidity", 0, 100);
private final SocketHint<Range> solidityHint =
SocketHints.Inputs.createNumberRangeSocketHint("Solidity", 0, 100);

private final SocketHint<Number> minVertexHint =
SocketHints.Inputs.createNumberSpinnerSocketHint("Min Vertices", 0, 0, Integer.MAX_VALUE);
Expand All @@ -84,7 +85,7 @@ public class FilterContoursOperation implements Operation {
private final InputSocket<Number> maxWidthSocket;
private final InputSocket<Number> minHeightSocket;
private final InputSocket<Number> maxHeightSocket;
private final InputSocket<List<Number>> soliditySocket;
private final InputSocket<Range> soliditySocket;
private final InputSocket<Number> minVertexSocket;
private final InputSocket<Number> maxVertexSocket;
private final InputSocket<Number> minRatioSocket;
Expand Down Expand Up @@ -146,8 +147,8 @@ public void perform() {
final double maxWidth = maxWidthSocket.getValue().get().doubleValue();
final double minHeight = minHeightSocket.getValue().get().doubleValue();
final double maxHeight = maxHeightSocket.getValue().get().doubleValue();
final double minSolidity = soliditySocket.getValue().get().get(0).doubleValue();
final double maxSolidity = soliditySocket.getValue().get().get(1).doubleValue();
final double minSolidity = soliditySocket.getValue().get().getMin();
final double maxSolidity = soliditySocket.getValue().get().getMax();
final double minVertexCount = minVertexSocket.getValue().get().doubleValue();
final double maxVertexCount = maxVertexSocket.getValue().get().doubleValue();
final double minRatio = minRatioSocket.getValue().get().doubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.wpi.grip.core.Operation;
import edu.wpi.grip.core.OperationDescription;
import edu.wpi.grip.core.Range;
import edu.wpi.grip.core.sockets.InputSocket;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.core.sockets.SocketHint;
Expand Down Expand Up @@ -33,8 +34,8 @@ public class FilterLinesOperation implements Operation {
private final SocketHint<Number> minLengthHint = SocketHints.Inputs
.createNumberSpinnerSocketHint("Min Length", 20);

private final SocketHint<List<Number>> angleHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Angle", 0, 360);
private final SocketHint<Range> angleHint = SocketHints.Inputs
.createNumberRangeSocketHint("Angle", 0, 360);

private final SocketHint<LinesReport> outputHint =
new SocketHint.Builder<>(LinesReport.class)
Expand All @@ -43,7 +44,7 @@ public class FilterLinesOperation implements Operation {

private final InputSocket<LinesReport> inputSocket;
private final InputSocket<Number> minLengthSocket;
private final InputSocket<List<Number>> angleSocket;
private final InputSocket<Range> angleSocket;

private final OutputSocket<LinesReport> linesOutputSocket;

Expand Down Expand Up @@ -78,8 +79,8 @@ public List<OutputSocket> getOutputSockets() {
public void perform() {
final LinesReport inputLines = inputSocket.getValue().get();
final double minLengthSquared = Math.pow(minLengthSocket.getValue().get().doubleValue(), 2);
final double minAngle = angleSocket.getValue().get().get(0).doubleValue();
final double maxAngle = angleSocket.getValue().get().get(1).doubleValue();
final double minAngle = angleSocket.getValue().get().getMin();
final double maxAngle = angleSocket.getValue().get().getMax();

List<LinesReport.Line> lines = inputLines.getLines().stream()
.filter(line -> line.lengthSquared() >= minLengthSquared)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.wpi.grip.core.Operation;
import edu.wpi.grip.core.OperationDescription;
import edu.wpi.grip.core.Range;
import edu.wpi.grip.core.sockets.InputSocket;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.core.sockets.SocketHint;
Expand Down Expand Up @@ -34,8 +35,8 @@ public class FindBlobsOperation implements Operation {
private final SocketHint<Mat> inputHint = SocketHints.Inputs.createMatSocketHint("Input", false);
private final SocketHint<Number> minAreaHint = SocketHints.Inputs
.createNumberSpinnerSocketHint("Min Area", 1);
private final SocketHint<List<Number>> circularityHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Circularity", 0.0, 1.0);
private final SocketHint<Range> circularityHint = SocketHints.Inputs
.createNumberRangeSocketHint("Circularity", 0.0, 1.0);
private final SocketHint<Boolean> colorHint = SocketHints
.createBooleanSocketHint("Dark Blobs", false);

Expand All @@ -46,7 +47,7 @@ public class FindBlobsOperation implements Operation {

private final InputSocket<Mat> inputSocket;
private final InputSocket<Number> minAreaSocket;
private final InputSocket<List<Number>> circularitySocket;
private final InputSocket<Range> circularitySocket;
private final InputSocket<Boolean> colorSocket;

private final OutputSocket<BlobsReport> outputSocket;
Expand Down Expand Up @@ -84,7 +85,7 @@ public List<OutputSocket> getOutputSockets() {
public void perform() {
final Mat input = inputSocket.getValue().get();
final Number minArea = minAreaSocket.getValue().get();
final List<Number> circularity = circularitySocket.getValue().get();
final Range circularity = circularitySocket.getValue().get();
final Boolean darkBlobs = colorSocket.getValue().get();


Expand All @@ -98,8 +99,8 @@ public void perform() {
.blobColor(darkBlobs ? (byte) 0 : (byte) 255)

.filterByCircularity(true)
.minCircularity(circularity.get(0).floatValue())
.maxCircularity(circularity.get(1).floatValue()));
.minCircularity((float) circularity.getMin())
.maxCircularity((float) circularity.getMax()));

// Detect the blobs and store them in the output BlobsReport
final KeyPointVector keyPointVector = new KeyPointVector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import edu.wpi.grip.core.Operation;
import edu.wpi.grip.core.OperationDescription;
import edu.wpi.grip.core.Range;
import edu.wpi.grip.core.sockets.InputSocket;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.core.sockets.SocketHint;
Expand Down Expand Up @@ -37,19 +38,19 @@ public class HSLThresholdOperation extends ThresholdOperation {

private static final Logger logger = Logger.getLogger(HSLThresholdOperation.class.getName());
private final SocketHint<Mat> inputHint = SocketHints.Inputs.createMatSocketHint("Input", false);
private final SocketHint<List<Number>> hueHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Hue", 0.0, 180.0);
private final SocketHint<List<Number>> saturationHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Saturation", 0.0, 255.0);
private final SocketHint<List<Number>> luminanceHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Luminance", 0.0, 255.0);
private final SocketHint<Range> hueHint = SocketHints.Inputs
.createNumberRangeSocketHint("Hue", 0.0, 180.0);
private final SocketHint<Range> saturationHint = SocketHints.Inputs
.createNumberRangeSocketHint("Saturation", 0.0, 255.0);
private final SocketHint<Range> luminanceHint = SocketHints.Inputs
.createNumberRangeSocketHint("Luminance", 0.0, 255.0);

private final SocketHint<Mat> outputHint = SocketHints.Outputs.createMatSocketHint("Output");

private final InputSocket<Mat> inputSocket;
private final InputSocket<List<Number>> hueSocket;
private final InputSocket<List<Number>> saturationSocket;
private final InputSocket<List<Number>> luminanceSocket;
private final InputSocket<Range> hueSocket;
private final InputSocket<Range> saturationSocket;
private final InputSocket<Range> luminanceSocket;

private final OutputSocket<Mat> outputSocket;

Expand Down Expand Up @@ -91,20 +92,20 @@ public void perform() {
}

final Mat output = outputSocket.getValue().get();
final List<Number> channel1 = hueSocket.getValue().get();
final List<Number> channel2 = saturationSocket.getValue().get();
final List<Number> channel3 = luminanceSocket.getValue().get();
final Range channel1 = hueSocket.getValue().get();
final Range channel2 = saturationSocket.getValue().get();
final Range channel3 = luminanceSocket.getValue().get();

// Intentionally 1, 3, 2. This maps to the HLS open cv expects
final Scalar lowScalar = new Scalar(
channel1.get(0).doubleValue(),
channel3.get(0).doubleValue(),
channel2.get(0).doubleValue(), 0);
channel1.getMin(),
channel3.getMin(),
channel2.getMin(), 0);

final Scalar highScalar = new Scalar(
channel1.get(1).doubleValue(),
channel3.get(1).doubleValue(),
channel2.get(1).doubleValue(), 0);
channel1.getMax(),
channel3.getMax(),
channel2.getMax(), 0);

final Mat low = reallocateMatIfInputSizeOrWidthChanged(dataArray, 0, lowScalar, input);
final Mat high = reallocateMatIfInputSizeOrWidthChanged(dataArray, 1, highScalar, input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.wpi.grip.core.Operation;
import edu.wpi.grip.core.OperationDescription;
import edu.wpi.grip.core.Range;
import edu.wpi.grip.core.sockets.InputSocket;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.core.sockets.SocketHint;
Expand Down Expand Up @@ -37,19 +38,19 @@ public class HSVThresholdOperation extends ThresholdOperation {

private static final Logger logger = Logger.getLogger(HSVThresholdOperation.class.getName());
private final SocketHint<Mat> inputHint = SocketHints.Inputs.createMatSocketHint("Input", false);
private final SocketHint<List<Number>> hueHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Hue", 0.0, 180.0);
private final SocketHint<List<Number>> saturationHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Saturation", 0.0, 255.0);
private final SocketHint<List<Number>> valueHint = SocketHints.Inputs
.createNumberListRangeSocketHint("Value", 0.0, 255.0);
private final SocketHint<Range> hueHint = SocketHints.Inputs
.createNumberRangeSocketHint("Hue", 0.0, 180.0);
private final SocketHint<Range> saturationHint = SocketHints.Inputs
.createNumberRangeSocketHint("Saturation", 0.0, 255.0);
private final SocketHint<Range> valueHint = SocketHints.Inputs
.createNumberRangeSocketHint("Value", 0.0, 255.0);

private final SocketHint<Mat> outputHint = SocketHints.Outputs.createMatSocketHint("Output");

private final InputSocket<Mat> inputSocket;
private final InputSocket<List<Number>> hueSocket;
private final InputSocket<List<Number>> saturationSocket;
private final InputSocket<List<Number>> valueSocket;
private final InputSocket<Range> hueSocket;
private final InputSocket<Range> saturationSocket;
private final InputSocket<Range> valueSocket;

private final OutputSocket<Mat> outputSocket;

Expand Down Expand Up @@ -91,18 +92,18 @@ public void perform() {
}

final Mat output = outputSocket.getValue().get();
final List<Number> channel1 = hueSocket.getValue().get();
final List<Number> channel2 = saturationSocket.getValue().get();
final List<Number> channel3 = valueSocket.getValue().get();
final Range channel1 = hueSocket.getValue().get();
final Range channel2 = saturationSocket.getValue().get();
final Range channel3 = valueSocket.getValue().get();

final Scalar lowScalar = new Scalar(
channel1.get(0).doubleValue(),
channel2.get(0).doubleValue(),
channel3.get(0).doubleValue(), 0);
channel1.getMin(),
channel2.getMin(),
channel3.getMin(), 0);
final Scalar highScalar = new Scalar(
channel1.get(1).doubleValue(),
channel2.get(1).doubleValue(),
channel3.get(1).doubleValue(), 0);
channel1.getMax(),
channel2.getMax(),
channel3.getMax(), 0);

final Mat low = reallocateMatIfInputSizeOrWidthChanged(dataArray, 0, lowScalar, input);
final Mat high = reallocateMatIfInputSizeOrWidthChanged(dataArray, 1, highScalar, input);
Expand Down
Loading