Commit b2aa4ee6 authored by Ahmed's avatar Ahmed

update erode

parent 2500d534
...@@ -8,6 +8,7 @@ import de.monticore.lang.monticar.generator.cpp.BluePrintCPP; ...@@ -8,6 +8,7 @@ import de.monticore.lang.monticar.generator.cpp.BluePrintCPP;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional;
import static de.monticore.lang.monticar.generator.cpp.MathCommandRegisterCPP.removeBrackets; import static de.monticore.lang.monticar.generator.cpp.MathCommandRegisterCPP.removeBrackets;
...@@ -85,12 +86,32 @@ public abstract class MathCommand { ...@@ -85,12 +86,32 @@ public abstract class MathCommand {
String targetName = varType.getTypeNameTargetLanguage(); String targetName = varType.getTypeNameTargetLanguage();
if(targetName.equals("mat")){ if(targetName.equals("mat")){
Variable newVar = var; Variable newVar = var;
newVar.setTypeNameTargetLanguage("arma::mat"); newVar.setTypeNameTargetLanguage("arma::Mat<unsigned char>");
bluePrint.replaceVariable(var, newVar); bluePrint.replaceVariable(var, newVar);
}else if(targetName.equals("cube")){
Variable newVarCube = var;
newVarCube.setTypeNameTargetLanguage("Cube<unsigned char>");
bluePrint.replaceVariable(var, newVarCube);
} }
} }
} }
public void redefineInit(BluePrintCPP bluePrint){
Optional<Method> methodOpt = bluePrint.getMethod("init");
Method initMethod = methodOpt.get();
List<Instruction> instructs = initMethod.getInstructions();
for(Instruction instruct : instructs){
String code = instruct.getTargetLanguageInstruction();
if(code.contains("mat(")){
code =code.replace("mat(", "Mat<unsigned char>(");
} else if(code.contains("cube(")){
code = code.replace("mat(", "Cube<unsigned char>(");
}
((TargetCodeInstruction)instruct).setInstruction(code);
}
}
public boolean isArgumentNoReturnMathCommand() { public boolean isArgumentNoReturnMathCommand() {
return false; return false;
} }
......
...@@ -51,7 +51,8 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{ ...@@ -51,7 +51,8 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
BluePrintCPP bluePrintCPP = (BluePrintCPP) bluePrint; BluePrintCPP bluePrintCPP = (BluePrintCPP) bluePrint;
String valueListString = ""; String valueListString = "";
for (MathMatrixAccessSymbol accessSymbol : mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols()) List<MathMatrixAccessSymbol> mathMatrixAccessSymbols= mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols();
for (MathMatrixAccessSymbol accessSymbol : mathMatrixAccessSymbols)
MathFunctionFixer.fixMathFunctions(accessSymbol, bluePrintCPP); MathFunctionFixer.fixMathFunctions(accessSymbol, bluePrintCPP);
Method erodeHelperMethod = getErodeHelperMethod(mathMatrixNameExpressionSymbol, bluePrintCPP, properties); Method erodeHelperMethod = getErodeHelperMethod(mathMatrixNameExpressionSymbol, bluePrintCPP, properties);
...@@ -66,6 +67,7 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{ ...@@ -66,6 +67,7 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
bluePrintCPP.addCVIncludeString("ConvHelper"); bluePrintCPP.addCVIncludeString("ConvHelper");
bluePrint.addMethod(erodeHelperMethod); bluePrint.addMethod(erodeHelperMethod);
redefineArmaMat(bluePrintCPP); redefineArmaMat(bluePrintCPP);
redefineInit(bluePrintCPP);
} }
...@@ -77,7 +79,9 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{ ...@@ -77,7 +79,9 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
String typeNameOut = ""; String typeNameOut = "";
if(typeName.equals("") || typeName.equals("mat")){ if(typeName.equals("") || typeName.equals("mat")){
typeName = "arma::mat"; typeName = "arma::Mat<unsigned char>";
}else if(typeName.equals("cube")){
typeName = "Cube<unsigned char>";
} }
if(properties.isPreCV()){ if(properties.isPreCV()){
...@@ -91,12 +95,14 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{ ...@@ -91,12 +95,14 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
}else { }else {
typeNameOut = typeName; typeNameOut = typeName;
} }
String typeNameInConst = "const " + typeNameIn +"&";
String typeNameOutRef = typeNameOut + "&";
//add parameters //add parameters
Variable src = new Variable(); Variable src = new Variable();
method.addParameter(src, "src", "CommonMatrixType", typeNameIn, MathConverter.curBackend.getIncludeHeaderName()); method.addParameter(src, "src", "CommonMatrixType", typeNameInConst, MathConverter.curBackend.getIncludeHeaderName());
Variable dst = new Variable(); Variable dst = new Variable();
method.addParameter(dst, "dst", "CommonMatrixType", typeNameOut, MathConverter.curBackend.getIncludeHeaderName()); method.addParameter(dst, "dst", "CommonMatrixType", typeNameOutRef, MathConverter.curBackend.getIncludeHeaderName());
Variable erosion_elem = new Variable(); Variable erosion_elem = new Variable();
method.addParameter(erosion_elem,"erosion_elem", "Integer", "int", ""); method.addParameter(erosion_elem,"erosion_elem", "Integer", "int", "");
Variable iterations = new Variable(); Variable iterations = new Variable();
...@@ -112,37 +118,37 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{ ...@@ -112,37 +118,37 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
@Override @Override
public String getTargetLanguageInstruction() { public String getTargetLanguageInstruction() {
String finalInstruction =" int erosion_type = 0;\n" + String finalInstruction =" int erosion_type = 0;\n" +
" if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; }\n" + " if( erosion_elem == 0 ){ erosion_type = cv::MORPH_RECT; }\n" +
" else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; }\n" + " else if( erosion_elem == 1 ){ erosion_type = cv::MORPH_CROSS; }\n" +
" else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }\n" + " else if( erosion_elem == 2) { erosion_type = cv::MORPH_ELLIPSE; }\n" +
" erosion_size = erosion_elem;\n" + " int erosion_size = erosion_elem;\n" +
" cv::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" + " cv::Size( 2*erosion_size + 1, 2*erosion_size+1 ),\n" +
" Point( -1, -1 ) );\n"; " cv::Point( -1, -1 ) );\n";
if(properties.isPreCV() && properties.isSucCV()){ if(properties.isPreCV() && properties.isSucCV()){
finalInstruction += " cv::erode( src, dst, element, Point(-1,-1), iterations );\n"; finalInstruction += " cv::erode( src, dst, element, cv::Point(-1,-1), iterations );\n";
}else if(properties.isPreCV()){ }else if(properties.isPreCV()){
finalInstruction += " cv::Mat dstCV;\n" + finalInstruction += " cv::Mat dstCV;\n" +
" cv::erode( src, dstCV, element, Point(-1,-1), iterations );\n"; " cv::erode( src, dstCV, element, cv::Point(-1,-1), iterations );\n";
if(typeNameOut == "cube"){ if(typeNameOut == "cube"){
finalInstruction += " dst = ConvHelper::to_armaCube(dstCV);\n"; finalInstruction += " dst = to_armaCube<unsigned char, 3>(dstCV);\n";
} else { } else {
finalInstruction += " dst = ConvHelper::to_arma(dstCV);\n"; finalInstruction += " dst = to_arma<unsigned char>(dstCV);\n";
} }
} else if(properties.isSucCV()){ } else if(properties.isSucCV()){
finalInstruction += " cv::Mat srcCV;\n" + finalInstruction += " cv::Mat srcCV;\n" +
" srcCV = ConvHelper::to_cvmat(src);\n" + " srcCV = to_cvmat<unsigned char>(src);\n" +
" cv::erode( srcCV, dst, element, Point(-1,-1), iterations );\n"; " cv::erode( srcCV, dst, element, cv::Point(-1,-1), iterations );\n";
} else { } else {
finalInstruction += " cv::Mat srcCV;\n" + finalInstruction += " cv::Mat srcCV;\n" +
" cv::Mat dstCV;\n" + " cv::Mat dstCV;\n" +
" srcCV = ConvHelper::to_cvmat(src);\n" + " srcCV = to_cvmat<unsigned char>(src);\n" +
" cv::erode( srcCV, dstCV, element, Point(-1,-1), iterations );\n"; " cv::erode( srcCV, dstCV, element, cv::Point(-1,-1), iterations );\n";
if(typeNameOut == "cube"){ if(typeNameOut == "cube"){
finalInstruction += " dst = ConvHelper::to_armaCube(dstCV);\n"; finalInstruction += " dst = to_armaCube<unsigned char, 3>(dstCV);\n";
} else { } else {
finalInstruction += " dst = ConvHelper::to_arma(dstCV);\n"; finalInstruction += " dst = to_arma<unsigned char>(dstCV);\n";
} }
} }
return finalInstruction; return finalInstruction;
......
...@@ -79,9 +79,9 @@ public class ComponentConverter { ...@@ -79,9 +79,9 @@ public class ComponentConverter {
for (MathExpressionSymbol mathExpressionSymbol : mathExpressionSymbols) { for (MathExpressionSymbol mathExpressionSymbol : mathExpressionSymbols) {
MathExpressionProperties properties = new MathExpressionProperties(); MathExpressionProperties properties = new MathExpressionProperties();
MathConverter.setPropertiesForMathExpression(mathExpressionSymbols, mathExpressionSymbol, bluePrint, properties); MathConverter.setPropertiesForMathExpression(mathExpressionSymbols, mathExpressionSymbol, bluePrint, properties);
if(mathExpressionSymbol.isAssignmentExpression()){ if(mathExpressionSymbol.isAssignmentExpression() && ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol().isMatrixExpression()){
MathExpressionSymbol mathExpressionSymbolTemp = ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol(); MathMatrixExpressionSymbol mathMatrixExpressionSymbol = (MathMatrixExpressionSymbol) ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol();
if(mathExpressionSymbolTemp.isMatrixExpression()) { if(mathMatrixExpressionSymbol.isMatrixNameExpression()) {
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) ((MathAssignmentExpressionSymbol) mathExpressionSymbol).getExpressionSymbol(); MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) ((MathAssignmentExpressionSymbol) mathExpressionSymbol).getExpressionSymbol();
ComponentConverter.tuples.put(mathMatrixNameExpressionSymbol, properties); ComponentConverter.tuples.put(mathMatrixNameExpressionSymbol, properties);
} }
...@@ -93,9 +93,12 @@ public class ComponentConverter { ...@@ -93,9 +93,12 @@ public class ComponentConverter {
} else if(mathExpressionSymbols.size() == 1){ } else if(mathExpressionSymbols.size() == 1){
MathExpressionProperties properties = new MathExpressionProperties(); MathExpressionProperties properties = new MathExpressionProperties();
MathExpressionSymbol mathExpressionSymbol = mathExpressionSymbols.get(0); MathExpressionSymbol mathExpressionSymbol = mathExpressionSymbols.get(0);
if(mathExpressionSymbol.isAssignmentExpression()){ if(mathExpressionSymbol.isAssignmentExpression() && ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol().isMatrixExpression()){
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol(); MathMatrixExpressionSymbol mathMatrixExpressionSymbol = (MathMatrixExpressionSymbol) ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol();
ComponentConverter.tuples.put(mathMatrixNameExpressionSymbol, properties); if(mathMatrixExpressionSymbol.isMatrixNameExpression()) {
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) ((MathAssignmentExpressionSymbol) mathExpressionSymbol).getExpressionSymbol();
ComponentConverter.tuples.put(mathMatrixNameExpressionSymbol, properties);
}
} else{ } else{
ComponentConverter.tuples.put(mathExpressionSymbol, properties); ComponentConverter.tuples.put(mathExpressionSymbol, properties);
} }
...@@ -415,17 +418,20 @@ public class ComponentConverter { ...@@ -415,17 +418,20 @@ public class ComponentConverter {
public static void redefineVariables(List<MathExpressionSymbol> mathExpressionSymbols, BluePrintCPP bluePrintCPP){ public static void redefineVariables(List<MathExpressionSymbol> mathExpressionSymbols, BluePrintCPP bluePrintCPP){
for(MathExpressionSymbol mathExpressionSymbol : mathExpressionSymbols){ for(MathExpressionSymbol mathExpressionSymbol : mathExpressionSymbols){
if(mathExpressionSymbol.isAssignmentExpression() && ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol().isMatrixExpression()) { if(mathExpressionSymbol.isAssignmentExpression() && ((MathAssignmentExpressionSymbol)mathExpressionSymbol).getExpressionSymbol().isMatrixExpression()) {
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) ((MathAssignmentExpressionSymbol) mathExpressionSymbol).getExpressionSymbol(); MathMatrixExpressionSymbol mathMatrixExpressionSymbol = (MathMatrixExpressionSymbol) ((MathAssignmentExpressionSymbol) mathExpressionSymbol).getExpressionSymbol();
String nameOfFirstParameter = mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().get(0).getTextualRepresentation(); if (mathMatrixExpressionSymbol.isMatrixNameExpression()) {
String nameOfOutput = getNameOfOutput(mathExpressionSymbol); MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) ((MathAssignmentExpressionSymbol) mathExpressionSymbol).getExpressionSymbol();
String nameOfFirstParameter = mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols().get(0).getTextualRepresentation();
MathExpressionProperties properties = tuples.get(mathExpressionSymbol); String nameOfOutput = getNameOfOutput(mathExpressionSymbol);
if(properties != null) {
if (properties.isPreCV()) { MathExpressionProperties properties = tuples.get(mathExpressionSymbol);
fixVariableType(nameOfFirstParameter, bluePrintCPP, "CommonMatrixType", "cv::Mat", ""); if (properties != null) {
} if (properties.isPreCV()) {
if (properties.isSucCV()) { fixVariableType(nameOfFirstParameter, bluePrintCPP, "CommonMatrixType", "cv::Mat", "");
fixVariableType(nameOfOutput, bluePrintCPP, "CommonMatrixType", "cv::Mat", ""); }
if (properties.isSucCV()) {
fixVariableType(nameOfOutput, bluePrintCPP, "CommonMatrixType", "cv::Mat", "");
}
} }
} }
} }
......
...@@ -10,30 +10,30 @@ using namespace arma; ...@@ -10,30 +10,30 @@ using namespace arma;
using namespace std; using namespace std;
class test_math_erodeCommandTest{ class test_math_erodeCommandTest{
public: public:
arma::mat src; arma::Mat<unsigned char> src;
int erosion_elem; int erosion_elem;
int iterations; int iterations;
arma::mat dst; arma::Mat<unsigned char> dst;
void init() void init()
{ {
src=mat(n,m); src=Mat<unsigned char>(n,m);
dst=mat(n,m); dst=Mat<unsigned char>(n,m);
} }
void erodeHelper(arma::mat src, arma::mat dst, int erosion_elem, int iterations) void erodeHelper(const arma::Mat<unsigned char>& src, arma::Mat<unsigned char>& dst, int erosion_elem, int iterations)
{ {
int erosion_type = 0; int erosion_type = 0;
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; } if( erosion_elem == 0 ){ erosion_type = cv::MORPH_RECT; }
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; } else if( erosion_elem == 1 ){ erosion_type = cv::MORPH_CROSS; }
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; } else if( erosion_elem == 2) { erosion_type = cv::MORPH_ELLIPSE; }
erosion_size = erosion_elem; int erosion_size = erosion_elem;
cv::Mat element = cv::getStructuringElement( erosion_type, cv::Mat element = cv::getStructuringElement( erosion_type,
Size( 2*erosion_size + 1, 2*erosion_size+1 ), cv::Size( 2*erosion_size + 1, 2*erosion_size+1 ),
Point( -1, -1 ) ); cv::Point( -1, -1 ) );
cv::Mat srcCV; cv::Mat srcCV;
cv::Mat dstCV; cv::Mat dstCV;
srcCV = ConvHelper::to_cvmat(src); srcCV = to_cvmat<unsigned char>(src);
cv::erode( srcCV, dstCV, element, Point(-1,-1), iterations ); cv::erode( srcCV, dstCV, element, cv::Point(-1,-1), iterations );
dst = ConvHelper::to_arma(dstCV); dst = to_arma<unsigned char>(dstCV);
} }
void execute() void execute()
{ {
......
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