Commit 8e0ff35c authored by Ahmed's avatar Ahmed

extends CvtColorCommand

parent 6af6f4e7
......@@ -3,10 +3,11 @@ 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.BluePrint;
import de.monticore.lang.monticar.generator.MathCommand;
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.MathFunctionFixer;
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.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.symbols.MathStringExpression;
......@@ -46,19 +47,101 @@ public class CvtColorCommand 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 cvtColorHelperMethod = getCvtColorHelperMethod(mathMatrixNameExpressionSymbol, bluePrintCPP, properties);
valueListString += ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, new ArrayList<String>());
List<MathMatrixAccessSymbol> newMatrixAccessSymbols = new ArrayList<>();
MathStringExpression stringExpression = new MathStringExpression("cv::cvtColor" + valueListString,mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols());
MathStringExpression stringExpression = new MathStringExpression("cvtColorHelper" + valueListString,mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols());
newMatrixAccessSymbols.add(new MathMatrixAccessSymbol(stringExpression));
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().setMathMatrixAccessSymbols(newMatrixAccessSymbols);
bluePrintCPP.addCVIncludeString("opencv2/imgproc");
bluePrintCPP.addCVIncludeString("ConvHelper");
bluePrint.addMethod(cvtColorHelperMethod);
}
private Method getCvtColorHelperMethod(MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol, BluePrintCPP bluePrintCPP, MathExpressionProperties properties){
Method method = new Method("cvtColorHelper", "void");
String typeName = getTypeOfFirstInput(mathMatrixNameExpressionSymbol, bluePrintCPP);
String typeNameIn = "";
String typeNameOut = "";
if(typeName.equals("") || typeName.equals("mat")){
typeName = "arma::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", typeNameIn, MathConverter.curBackend.getIncludeHeaderName());
Variable dst = new Variable();
method.addParameter(dst, "dst", "CommonMatrixType", typeNameOut, MathConverter.curBackend.getIncludeHeaderName());
Variable coloerConversion = new Variable();
method.addParameter(coloerConversion,"colorConversion", "Integer", "int", "");
//add an instruction to the method
method.addInstruction(methodBody(properties, typeNameIn, typeNameOut));
return method;
}
private Instruction methodBody(MathExpressionProperties properties, String typeNameIn, String typeNameOut) {
return new Instruction() {
@Override
public String getTargetLanguageInstruction() {
String finalInstruction ="";
if(properties.isPreCV() && properties.isSucCV()){
finalInstruction += " cv::cvtColor(src, dst, colorConversion);\n";
}else if(properties.isPreCV()){
finalInstruction += " cv::Mat dstCV;\n" +
" cv::cvtColor(src, dstCV, colorConversion);\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::cvtColor(srcCV, dst, colorConversion);\n";
} else {
finalInstruction += " cv::Mat srcCV;\n" +
" cv::Mat dstCV;\n" +
" srcCV = ConvHelper::to_cvmat(src);\n" +
" cv::cvtColor(srcCV, dstCV, colorConversion);\n";
if(typeNameOut == "cube"){
finalInstruction += " dst = ConvHelper::to_armaCube(dstCV);\n";
} else {
finalInstruction += " dst = ConvHelper::to_arma(dstCV);\n";
}
}
return finalInstruction;
}
@Override
public boolean isConnectInstruction() {
return false;
}
};
}
}
......@@ -5,7 +5,9 @@ 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.MathFunctionFixer;
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.converter.MathConverter;
import de.monticore.lang.monticar.generator.cpp.symbols.MathStringExpression;
......@@ -45,13 +47,14 @@ public class DilateCommand 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 dilateHelperMethod = getDilateHelperMethod(mathMatrixNameExpressionSymbol, bluePrintCPP);
Method dilateHelperMethod = getDilateHelperMethod(mathMatrixNameExpressionSymbol, bluePrintCPP, properties);
valueListString += ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, new ArrayList<String>());
List<MathMatrixAccessSymbol> newMatrixAccessSymbols = new ArrayList<>();
MathStringExpression stringExpression = new MathStringExpression("dilateHelper" + valueListString,mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols());
......@@ -64,43 +67,84 @@ public class DilateCommand extends ArgumentNoReturnMathCommand{
}
private Method getDilateHelperMethod(MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol,BluePrintCPP bluePrintCPP){
private Method getDilateHelperMethod(MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol,BluePrintCPP bluePrintCPP, MathExpressionProperties properties){
Method method = new Method("dilateHelper", "void");
String typeName = getTypeOfFirstInput(mathMatrixNameExpressionSymbol, bluePrintCPP);
if(typeName.equals("")){
typeName = "mat";
String typeNameIn = "";
String typeNameOut = "";
if(typeName.equals("") || typeName.equals("mat")){
typeName = "arma::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", "CommonMatrix", typeName, MathConverter.curBackend.getIncludeHeaderName());;
method.addParameter(src, "src", "CommonMatrix", 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,"dilation_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 dilation_type = 0;\n" +
String finalInstruction = " int dilation_type = 0;\n" +
" if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; }\n" +
" else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; }\n" +
" else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }\n" +
" dilation_size = dilation_elem;\n" +
" mat element = cv::getStructuringElement( dilation_type,\n" +
" cv::mat element = cv::getStructuringElement( dilation_type,\n" +
" Size( 2*dilation_size + 1, 2*dilation_size+1 ),\n" +
" Point( -1, -1 ) );\n" +
" cv::dilate( src, dst, element, Point(-1,-1), iterations );\n";
" Point( -1, -1 ) );\n";
if(properties.isPreCV() && properties.isSucCV()){
finalInstruction += " cv::dilate( src, dst, element, Point(-1,-1), iterations );\n";
}else if(properties.isPreCV()){
finalInstruction += " cv::Mat dstCV;\n" +
" cv::dilate( 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::dilate( 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::dilate( 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
......
......@@ -76,7 +76,7 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
String typeNameOut = "";
if(typeName.equals("") || typeName.equals("mat")){
typeName = "mat";
typeName = "arma::mat";
}
if(properties.isPreCV()){
......@@ -123,7 +123,7 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
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";
" cv::erode( src, dstCV, element, Point(-1,-1), iterations );\n";
if(typeNameOut == "cube"){
finalInstruction += " dst = ConvHelper::to_armaCube(dstCV);\n";
} else {
......@@ -131,13 +131,13 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
}
} 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";
" 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";
" 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 {
......
......@@ -20,7 +20,7 @@ dst=mat(n,m);
}
void execute()
{
cv::cvtColor(src, dst, colorConversion);
cv::cvtColor(src, dst, colorConversion);
}
};
......
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