Commit 7ba43353 authored by Ahmed's avatar Ahmed

add rectanlge Command

parent 5e000733
......@@ -68,6 +68,7 @@ public class MathCommandRegisterCPP extends MathCommandRegister {
registerMathCommand(new PutTextCommand());
registerMathCommand(new ThresholdCommand());
registerMathCommand(new LargestContourCommand());
registerMathCommand(new RectangleCommand());
//for fixing some errors
......
......@@ -68,7 +68,7 @@ public class ErodeCommand extends ArgumentReturnMathCommand{
//add parameters
Variable src = new Variable();
method.addParameter(src, "src", "CommonMatrixType",MathConverter.curBackend.getMatrixTypeName(), MathConverter.curBackend.getIncludeHeaderName());;
method.addParameter(src, "src", "CommonMatrixType",MathConverter.curBackend.getMatrixTypeName(), MathConverter.curBackend.getIncludeHeaderName());
Variable dst = new Variable();
method.addParameter(dst, "dst", "CommonMatrixType", MathConverter.curBackend.getMatrixTypeName(), MathConverter.curBackend.getIncludeHeaderName());
Variable erosion_elem = new Variable();
......
package de.monticore.lang.monticar.generator.cpp.commands;
import de.monticore.lang.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math._symboltable.matrix.MathMatrixAccessSymbol;
import de.monticore.lang.math._symboltable.matrix.MathMatrixNameExpressionSymbol;
import de.monticore.lang.monticar.generator.*;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
import de.monticore.lang.monticar.generator.cpp.MathFunctionFixer;
import de.monticore.lang.monticar.generator.cpp.converter.ExecuteMethodGenerator;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.symbols.MathStringExpression;
import de.se_rwth.commons.logging.Log;
import java.util.ArrayList;
import java.util.List;
/**
* @author Ahmed Diab
*/
public class RectangleCommand extends MathCommand{
public RectangleCommand() {
setMathCommandName("rectangle");
}
@Override
public void convert(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
String backendName = MathConverter.curBackend.getBackendName();
if (backendName.equals("OctaveBackend")) {
convertUsingOctaveBackend(mathExpressionSymbol, bluePrint);
} else if (backendName.equals("ArmadilloBackend")) {
convertUsingArmadilloBackend(mathExpressionSymbol, bluePrint);
}
}
@Override
public boolean isCVMathCommand(){
return true;
}
public void convertUsingOctaveBackend(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
Log.error("No implementation for Octave Backend");
}
public void convertUsingArmadilloBackend(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) mathExpressionSymbol;
mathMatrixNameExpressionSymbol.setNameToAccess("");
String valueListString = "";
for (MathMatrixAccessSymbol accessSymbol : mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols())
MathFunctionFixer.fixMathFunctions(accessSymbol, (BluePrintCPP) bluePrint);
Method rectangleHelperMethod = getRectangleHelperMethod();
valueListString += ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, new ArrayList<String>());
List<MathMatrixAccessSymbol> newMatrixAccessSymbols = new ArrayList<>();
MathStringExpression stringExpression = new MathStringExpression("rectangleHelper" + valueListString,mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols());
newMatrixAccessSymbols.add(new MathMatrixAccessSymbol(stringExpression));
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().setMathMatrixAccessSymbols(newMatrixAccessSymbols);
((BluePrintCPP) bluePrint).addCVIncludeString("opencv2/imgproc");
bluePrint.addMethod(rectangleHelperMethod);
}
private Method getRectangleHelperMethod(){
Method method = new Method("rectangleHelper", "Mat");
//add parameters
Variable src = new Variable();
method.addParameter(src, "src", "CommonMatrixType","Mat", "");
Variable rect = new Variable();
method.addParameter(rect, "rect", "double", "Rect", "");
Variable color = new Variable();
method.addParameter(color,"color", "colvec", "colvec", "");
Variable thickness = new Variable();
method.addParameter(thickness, "thickness", "Integer","int", "");
Variable lineType = new Variable();
method.addParameter(lineType, "lineType", "Integer","int", "");
//add an instruction to the method
method.addInstruction(methodBody());
return method;
}
private Instruction methodBody() {
return new Instruction() {
@Override
public String getTargetLanguageInstruction() {
return " cv::rectangle(src, rect.tl(), rect.br(), Scalar(color(0), color(1), color(2)), thickness, lineType);\n" +
" return src;\n";
}
@Override
public boolean isConnectInstruction() {
return false;
}
};
}
}
......@@ -279,4 +279,9 @@ public class ArmadilloFunctionTest extends AbstractSymtabTest {
public void testLargestContourCommand() throws IOException{
testMathCommand("largestContour");
}
@Test
public void testRectangleCommand() throws IOException{
testMathCommand("rectangle");
}
}
#ifndef TEST_MATH_RECTANGLECOMMANDTEST
#define TEST_MATH_RECTANGLECOMMANDTEST
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo"
#include "opencv2/imgproc.hpp"
using namespace arma;
using namespace cv;
class test_math_rectangleCommandTest{
public:
cube src;
double rect;
colvec color;
int thickness;
int lineType;
cube outputImg;
void init()
{
src = cube(3, n, m);
color=colvec(3);
outputImg = cube(3, n, m);
}
Mat rectangleHelper(Mat src, Rect rect, colvec color, int thickness, int lineType)
{
cv::rectangle(src, rect.tl(), rect.br(), Scalar(color(0), color(1), color(2)), thickness, lineType);
return src;
}
void execute()
{
outputImg = (rectangleHelper(src, rect, color, thickness, lineType));
}
};
#endif
/* (c) https://github.com/MontiCore/monticore */
package test.math;
component RectangleCommandTest {
port
in Q^{3,n,m} src,
in Q rect,
in Q^{3} color,
in Z thickness,
in Z lineType,
out Q^{3,n,m} outputImg;
implementation Math {
outputImg = rectangle(src, rect, color, thickness, lineType);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment