Commit 6af6f4e7 authored by Ahmed's avatar Ahmed

extends Erode Command

parent f470d180
......@@ -5,6 +5,8 @@ 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.MathExpressionProperties;
import de.monticore.lang.monticar.generator.cpp.converter.ComponentConverter;
import de.monticore.lang.monticar.generator.cpp.converter.ExecuteMethodGenerator;
import de.monticore.lang.monticar.generator.cpp.MathFunctionFixer;
import de.monticore.lang.monticar.generator.cpp.converter.MathConverter;
......@@ -45,13 +47,14 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
public void convertUsingArmadilloBackend(MathExpressionSymbol mathExpressionSymbol, BluePrint bluePrint) {
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) mathExpressionSymbol;
mathMatrixNameExpressionSymbol.setNameToAccess("");
MathExpressionProperties properties = ComponentConverter.tuples.get(mathExpressionSymbol);
BluePrintCPP bluePrintCPP = (BluePrintCPP) bluePrint;
String valueListString = "";
for (MathMatrixAccessSymbol accessSymbol : mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols())
MathFunctionFixer.fixMathFunctions(accessSymbol, bluePrintCPP);
Method erodeHelperMethod = getErodeHelperMethod(mathMatrixNameExpressionSymbol, bluePrintCPP);
Method erodeHelperMethod = getErodeHelperMethod(mathMatrixNameExpressionSymbol, bluePrintCPP, properties);
valueListString += ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, new ArrayList<String>());
List<MathMatrixAccessSymbol> newMatrixAccessSymbols = new ArrayList<>();
MathStringExpression stringExpression = new MathStringExpression("erodeHelper" + valueListString,mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols());
......@@ -65,42 +68,83 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
}
private Method getErodeHelperMethod(MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol, BluePrintCPP bluePrintCPP){
private Method getErodeHelperMethod(MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol, BluePrintCPP bluePrintCPP, MathExpressionProperties properties){
Method method = new Method("erodeHelper", "void");
String typeName = getTypeOfFirstInput(mathMatrixNameExpressionSymbol, bluePrintCPP);
if(typeName.equals("")){
String typeNameIn = "";
String typeNameOut = "";
if(typeName.equals("") || typeName.equals("mat")){
typeName = "mat";
}
if(properties.isPreCV()){
typeNameIn = "cv::Mat";
} else {
typeNameIn = typeName;
}
if(properties.isSucCV()){
typeNameOut = "cv::Mat";
}else {
typeNameOut = typeName;
}
//add parameters
Variable src = new Variable();
method.addParameter(src, "src", "CommonMatrixType", typeName, MathConverter.curBackend.getIncludeHeaderName());
method.addParameter(src, "src", "CommonMatrixType", typeNameIn, MathConverter.curBackend.getIncludeHeaderName());
Variable dst = new Variable();
method.addParameter(dst, "dst", "CommonMatrixType", typeName, MathConverter.curBackend.getIncludeHeaderName());
method.addParameter(dst, "dst", "CommonMatrixType", typeNameOut, MathConverter.curBackend.getIncludeHeaderName());
Variable erosion_elem = new Variable();
method.addParameter(erosion_elem,"erosion_elem", "Integer", "int", "");
Variable iterations = new Variable();
method.addParameter(iterations, "iterations", "Integer","int", "");
//add an instruction to the method
method.addInstruction(methodBody());
method.addInstruction(methodBody(properties, typeNameIn, typeNameOut));
return method;
}
private Instruction methodBody() {
private Instruction methodBody(MathExpressionProperties properties, String typeNameIn, String typeNameOut) {
return new Instruction() {
@Override
public String getTargetLanguageInstruction() {
return " int erosion_type = 0;\n" +
String finalInstruction =" int erosion_type = 0;\n" +
" if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }\n" +
" else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }\n" +
" else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }\n" +
" erosion_size = erosion_elem;\n" +
" mat element = cv::getStructuringElement( erosion_type,\n" +
" cv::Mat element = cv::getStructuringElement( erosion_type,\n" +
" Size( 2*erosion_size + 1, 2*erosion_size+1 ),\n" +
" Point( -1, -1 ) );\n" +
" cv::erode( src, dst, element, Point(-1,-1), iterations );\n";
" Point( -1, -1 ) );\n";
if(properties.isPreCV() && properties.isSucCV()){
finalInstruction += " cv::erode( src, dst, element, Point(-1,-1), iterations );\n";
}else if(properties.isPreCV()){
finalInstruction += " cv::Mat dstCV;\n" +
" cv::erode( src, dstCV, element, Point(-1,-1), iterations );\n";
if(typeNameOut == "cube"){
finalInstruction += " dst = ConvHelper::to_armaCube(dstCV);\n";
} else {
finalInstruction += " dst = ConvHelper::to_arma(dstCV);\n";
}
} else if(properties.isSucCV()){
finalInstruction += " cv::Mat srcCV;\n" +
" srcCV = ConvHelper::to_cvmat(src);\n" +
" cv::erode( srcCV, dst, element, Point(-1,-1), iterations );\n";
} else {
finalInstruction += " cv::Mat srcCV;\n" +
" cv::Mat dstCV;\n" +
" srcCV = ConvHelper::to_cvmat(src);\n" +
" cv::erode( srcCV, dstCV, element, Point(-1,-1), iterations );\n";
if(typeNameOut == "cube"){
finalInstruction += " dst = ConvHelper::to_armaCube(dstCV);\n";
} else {
finalInstruction += " dst = ConvHelper::to_arma(dstCV);\n";
}
}
return finalInstruction;
}
@Override
......
......@@ -75,8 +75,8 @@ public class GaussianBlurCommand extends ArgumentNoReturnMathCommand{
String typeNameIn = "";
String typeNameOut = "";
if(typeName.equals("")){
typeName = "mat";
if(typeName.equals("") || typeName.equals("mat")){
typeName = "arma::mat";
}
if(properties.isPreCV()){
typeNameIn = "cv::Mat";
......@@ -92,7 +92,6 @@ public class GaussianBlurCommand extends ArgumentNoReturnMathCommand{
//add parameters
Variable src = new Variable();
method.addParameter(src, "src", "CommonMatrix",typeNameIn, MathConverter.curBackend.getIncludeHeaderName());;
Variable dst = new Variable();
method.addParameter(dst, "dst", "CommonMatrixType", typeNameOut, MathConverter.curBackend.getIncludeHeaderName());
......@@ -117,10 +116,10 @@ public class GaussianBlurCommand extends ArgumentNoReturnMathCommand{
public String getTargetLanguageInstruction() {
String finalInstruction = "";
if(properties.isPreCV() && properties.isSucCV()){
finalInstruction = " gaussianBlur(src, dst, Size(sizeX, sizeY), sigmaX, sigmaY);\n";
finalInstruction = " cv::gaussianBlur(src, dst, Size(sizeX, sizeY), sigmaX, sigmaY);\n";
}else if(properties.isPreCV()){
finalInstruction = " cv::Mat dstCV;\n" +
" gaussianBlur(src, dstCV, Size(sizeX, sizeY), sigmaX, sigmaY);\n";
" cv::gaussianBlur(src, dstCV, Size(sizeX, sizeY), sigmaX, sigmaY);\n";
if(typeNameOut == "cube"){
finalInstruction += " dst = ConvHelper::to_armaCube(dstCV);\n";
} else {
......@@ -129,12 +128,12 @@ public class GaussianBlurCommand extends ArgumentNoReturnMathCommand{
} else if(properties.isSucCV()){
finalInstruction = " cv::Mat srcCV;\n" +
" srcCV = ConvHelper::to_cvmat(src);\n" +
" gaussianBlur(srcCV, dst, Size(sizeX, sizeY), sigmaX, sigmaY);\n";
" cv::gaussianBlur(srcCV, dst, Size(sizeX, sizeY), sigmaX, sigmaY);\n";
} else {
finalInstruction = " cv::Mat srcCV;\n" +
" cv::Mat dstCV;\n" +
" srcCV = ConvHelper::to_cvmat(src);\n" +
" gaussianBlur(srcCV, dstCV, Size(sizeX, sizeY), sigmaX, sigmaY);\n";
" cv::gaussianBlur(srcCV, dstCV, Size(sizeX, sizeY), sigmaX, sigmaY);\n";
if(typeNameOut == "cube"){
finalInstruction += " dst = ConvHelper::to_armaCube(dstCV);\n";
} else {
......
......@@ -79,6 +79,21 @@ public class ComponentConverter {
for (MathExpressionSymbol mathExpressionSymbol : mathExpressionSymbols) {
MathExpressionProperties properties = new MathExpressionProperties();
MathConverter.setPropertiesForMathExpression(mathExpressionSymbols, mathExpressionSymbol, bluePrint, properties);
if(mathExpressionSymbol.isAssignmentExpression()){
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol();
ComponentConverter.tuples.put(mathMatrixNameExpressionSymbol, properties);
} else{
ComponentConverter.tuples.put(mathExpressionSymbol, properties);
}
}
} else if(mathExpressionSymbols.size() == 1){
MathExpressionProperties properties = new MathExpressionProperties();
MathExpressionSymbol mathExpressionSymbol = mathExpressionSymbols.get(0);
if(mathExpressionSymbol.isAssignmentExpression()){
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol();
ComponentConverter.tuples.put(mathMatrixNameExpressionSymbol, properties);
} else{
ComponentConverter.tuples.put(mathExpressionSymbol, properties);
}
}
......
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