-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add contour angle to contours report
Angle is from minAreaRect, and is in the range [-90, 0) Rework contours operations to be a bit more functional Make filter contours a bit more optimized
- Loading branch information
1 parent
d4d5d5e
commit eaf2e4d
Showing
6 changed files
with
219 additions
and
55 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
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,44 @@ | ||
package edu.wpi.grip.core.util; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A holder for data that gets lazily initialized. | ||
* | ||
* @param <T> the type of held data | ||
*/ | ||
public class LazyInit<T> { | ||
|
||
private T value = null; | ||
private final Supplier<? extends T> factory; | ||
|
||
/** | ||
* Creates a new lazily initialized data holder. | ||
* | ||
* @param factory the factory to use to create the held value | ||
*/ | ||
public LazyInit(Supplier<? extends T> factory) { | ||
this.factory = Objects.requireNonNull(factory, "factory"); | ||
} | ||
|
||
/** | ||
* Gets the value, initializing it if it has not yet been created. | ||
* | ||
* @return the held value | ||
*/ | ||
public T get() { | ||
if (value == null) { | ||
value = factory.get(); | ||
} | ||
return value; | ||
} | ||
|
||
/** | ||
* Clears the held value. The next call to {@link #get()} will re-instantiate the held value. | ||
*/ | ||
public void clear() { | ||
value = null; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
core/src/main/java/edu/wpi/grip/core/util/PointerStream.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,26 @@ | ||
package edu.wpi.grip.core.util; | ||
|
||
import java.util.stream.LongStream; | ||
import java.util.stream.Stream; | ||
|
||
import static org.bytedeco.javacpp.opencv_core.Mat; | ||
import static org.bytedeco.javacpp.opencv_core.MatVector; | ||
|
||
/** | ||
* Utility class for streaming native vector wrappers like {@code MatVector} | ||
* ({@code std::vector<T>}) with the Java {@link Stream} API. | ||
*/ | ||
public final class PointerStream { | ||
|
||
/** | ||
* Creates a stream of {@code Mat} objects in a {@code MatVector}. | ||
* | ||
* @param vector the vector of {@code Mats} to stream | ||
* | ||
* @return a new stream object for the contents of the vector | ||
*/ | ||
public static Stream<Mat> ofMatVector(MatVector vector) { | ||
return LongStream.range(0, vector.size()) | ||
.mapToObj(vector::get); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
core/src/test/java/edu/wpi/grip/core/util/LazyInitTest.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,45 @@ | ||
package edu.wpi.grip.core.util; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class LazyInitTest { | ||
|
||
@Test | ||
public void testFactoryIsOnlyCalledOnce() { | ||
final String output = "foo"; | ||
final int[] count = {0}; | ||
final Supplier<String> factory = () -> { | ||
count[0]++; | ||
return output; | ||
}; | ||
|
||
LazyInit<String> lazyInit = new LazyInit<>(factory); | ||
lazyInit.get(); | ||
assertEquals(1, count[0]); | ||
|
||
lazyInit.get(); | ||
assertEquals("Calling get() more than once should only call the factory once", 1, count[0]); | ||
} | ||
|
||
@Test | ||
public void testClear() { | ||
final String output = "foo"; | ||
final int[] count = {0}; | ||
final Supplier<String> factory = () -> { | ||
count[0]++; | ||
return output; | ||
}; | ||
LazyInit<String> lazyInit = new LazyInit<>(factory); | ||
lazyInit.get(); | ||
assertEquals(1, count[0]); | ||
|
||
lazyInit.clear(); | ||
lazyInit.get(); | ||
assertEquals(2, count[0]); | ||
} | ||
|
||
} |
Oops, something went wrong.