Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autodiscover operations at startup. Use annotations for descriptions
Browse files Browse the repository at this point in the history
Re-enable CompatibilityTest. Fix some backwards compatbility issues.

Not really a fan of injecting the Injector into the Operations class. Need to see if there's a better solution
SamCarlberg committed Apr 8, 2019

Verified

This commit was signed with the committer’s verified signature.
prabhu prabhu
1 parent 142b1de commit 0868182
Showing 5 changed files with 180 additions and 3 deletions.
49 changes: 49 additions & 0 deletions core/src/main/java/edu/wpi/grip/core/Description.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package edu.wpi.grip.core;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static edu.wpi.grip.core.OperationDescription.Category;
import static edu.wpi.grip.core.OperationDescription.Category.MISCELLANEOUS;

/**
* Annotates an {@link Operation} subclass to describe it. This annotation gets transformed into a
* {@link OperationDescription}. All operation classes with this annotation will be automatically
* discovered and added to the palette at startup.
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Description {

/**
* The name of the operation being described.
*/
String name();

/**
* A brief summary of the operation. In-depth descriptions, usage guides, and examples
* should be on the wiki, not here.
*/
String summary();

/**
* The category the operation belongs to. Defaults to
* {@link OperationDescription.Category#MISCELLANEOUS MISCELLANEOUS}.
*/
Category category() default MISCELLANEOUS;

/**
* All known aliases of the operation. If the name of the operation changes, the previous name
* should be here. Defaults to an empty array.
*/
String[] aliases() default {};

/**
* The name of the icon to use to display the operation. If empty ({@code ""}), no icon will be
* shown. The icon should be located in {@code /edu/wpi/grip/ui/icons/}.
*/
String iconName() default "";

}
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.eventbus.EventBus;
import com.google.common.reflect.ClassPath;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
Original file line number Diff line number Diff line change
@@ -1,4 +1,130 @@
package edu.wpi.grip.core.operations.composite;


import edu.wpi.grip.core.Operation;
import edu.wpi.grip.core.sockets.InputSocket;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.core.sockets.SocketHint;
import edu.wpi.grip.core.sockets.SocketHints;

import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;

import org.bytedeco.javacpp.opencv_core.Mat;

import java.util.List;

import static org.bytedeco.javacpp.opencv_core.CV_8U;
import static org.bytedeco.javacpp.opencv_imgproc.CV_DIST_C;
import static org.bytedeco.javacpp.opencv_imgproc.CV_DIST_L1;
import static org.bytedeco.javacpp.opencv_imgproc.CV_DIST_L2;
import static org.bytedeco.javacpp.opencv_imgproc.distanceTransform;

/**
* GRIP {@link Operation} for {@link org.bytedeco.javacpp.opencv_imgproc#distanceTransform}.
*/
public class DistanceTransformOperation implements Operation {

private final SocketHint<Mat> srcHint = SocketHints.Inputs.createMatSocketHint("Input", false);
private final SocketHint<Type> typeHint = SocketHints.createEnumSocketHint("Type", Type.DIST_L2);
private final SocketHint<MaskSize> maskSizeHint = SocketHints.createEnumSocketHint("Mask size",
MaskSize.ZERO);
private final SocketHint<Mat> outputHint = SocketHints.Inputs.createMatSocketHint("Output", true);
private final InputSocket<Mat> srcSocket;
private final InputSocket<Type> typeSocket;
private final InputSocket<MaskSize> maskSizeSocket;
private final OutputSocket<Mat> outputSocket;

@Inject
@SuppressWarnings("JavadocMethod")
public DistanceTransformOperation(InputSocket.Factory inputSocketFactory, OutputSocket.Factory
outputSocketFactory) {
this.srcSocket = inputSocketFactory.create(srcHint);
this.typeSocket = inputSocketFactory.create(typeHint);
this.maskSizeSocket = inputSocketFactory.create(maskSizeHint);

this.outputSocket = outputSocketFactory.create(outputHint);
}

@Override
public List<InputSocket> getInputSockets() {
return ImmutableList.of(
srcSocket,
typeSocket,
maskSizeSocket
);
}

@Override
public List<OutputSocket> getOutputSockets() {
return ImmutableList.of(
outputSocket
);
}

@Override
public void perform() {
final Mat input = srcSocket.getValue().get();

if (input.type() != CV_8U) {
throw new IllegalArgumentException("Distance transform only works on 8-bit binary images");
}

final Type type = typeSocket.getValue().get();
final MaskSize maskSize = maskSizeSocket.getValue().get();

final Mat output = outputSocket.getValue().get();

distanceTransform(input, output, type.value, maskSize.value);
output.convertTo(output, CV_8U);

outputSocket.setValue(output);
}

private enum Type {

DIST_L1("CV_DIST_L1", CV_DIST_L1),
DIST_L2("CV_DIST_L2", CV_DIST_L2),
DIST_C("CV_DIST_C", CV_DIST_C);

private final String label;
private final int value;

Type(String label, int value) {
this.label = label;
this.value = value;
}

@Override
public String toString() {
return label;
}
}

/**
* Masks are either 0x0, 3x3, or 5x5.
*/
private enum MaskSize {

ZERO("0x0", 0),
THREE("3x3", 3),
FIVE("5x5", 5);

private final String label;
private final int value;

MaskSize(String label, int value) {
this.label = label;
this.value = value;
}

@Override
public String toString() {
return label;
}
}

}


import edu.wpi.grip.annotation.operation.Description;
import edu.wpi.grip.annotation.operation.OperationCategory;
Original file line number Diff line number Diff line change
@@ -136,7 +136,7 @@ public void convert(J javaType, Message message, MessageFactory messageFactory)
}

private abstract static class SimpleConverter<J, M extends Message> extends
JavaToMessageConverter<J, M> {
JavaToMessageConverter<J, M> {
private final BiConsumer<M, J> messageDataAssigner;

private SimpleConverter(String type, BiConsumer<M, J> messageDataAssigner) {
3 changes: 2 additions & 1 deletion core/src/test/java/edu/wpi/grip/core/CoreSanityTest.java
Original file line number Diff line number Diff line change
@@ -28,7 +28,8 @@ public CoreSanityTest() {
ManualPipelineRunner.class,
SubtractionOperation.class,
Main.class,
CoreCommandLineHelper.class
CoreCommandLineHelper.class,
OperationDescription.class
).contains(c));
setDefault(OutputSocket.class, new MockOutputSocket("Mock Out"));
setDefault(InputSocket.class, new MockInputSocket("Mock In"));

0 comments on commit 0868182

Please sign in to comment.