Commit 24c85af3 authored by Ahmed's avatar Ahmed

add ConvHelper file

parent 2123b848
......@@ -19,6 +19,7 @@ import java.util.List;
public class BluePrintCPP extends BluePrint {
public List<String> additionalIncludeStrings = new ArrayList<>();
public List<String> cvIncludeStrings = new ArrayList<>();
public static boolean usedCV = false;
public BluePrintCPP(String name) {
super(name);
......@@ -43,8 +44,10 @@ public class BluePrintCPP extends BluePrint {
}
public void addCVIncludeString(String includeString) {
if (!hasCVIncludeString(includeString))
if (!hasCVIncludeString(includeString)) {
cvIncludeStrings.add(includeString);
usedCV = true;
}
}
public boolean hasCVIncludeString(String includeString) {
......
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.generator.cpp;
import de.monticore.lang.monticar.generator.FileContent;
/**
* @author Ahmed Diab
*/
public class ConversionHelper {
public static String fileName = "ConvHelper";
public static FileContent getConversionHelperFileContent(boolean generateTests) {
FileContent fileContent = new FileContent();
fileContent.setFileName(fileName + ".h");
String fileContentString = ConversionHelperSource.conversionHelperSourceCode;
fileContent.setFileContent(fileContentString);
return fileContent;
}
public static FileContent getConversionHelperFileContent(){
return getConversionHelperFileContent(false);
}
}
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.lang.monticar.generator.cpp;
/**
* @author Ahmed Diab
*/
public class ConversionHelperSource {
public static String conversionHelperSourceCode = "// convert an OpenCV matrix to Armadillo matrix. NOTE: a copy is made\n" +
"template <typename T>\n" +
"arma::Mat<T> to_arma(const cv::Mat_<T>& src) {\n" +
" arma::Mat<T> dst(reinterpret_cast<double*>(src.data), src.cols, src.rows);\n" +
" //src.copyTo({ src.rows, src.cols, dst.memptr() });\n" +
" return dst;\n" +
"}\n" +
"\n" +
"// convert an Armadillo matrix to OpenCV matrix. NOTE: no copy is made\n" +
"template<typename T>\n" +
"cv::Mat_<T> to_cvmat(const arma::Mat<T>& src) {\n" +
" return cv::Mat_<T>{int(src.n_cols), int(src.n_rows), const_cast<T*>(src.memptr())};\n" +
"}\n" +
"\n" +
"// convert an OpenCV multi-channel matrix to Armadillo cube. A copy is made\n" +
"template <typename T, int NC>\n" +
"Cube<T> to_armaCube(const cv::Mat_<cv::Vec<T, NC>>& src)\n" +
"{\n" +
" std::vector<cv::Mat_<T>> channels;\n" +
" Cube<T> dst(src.cols, src.rows, NC);\n" +
" for (int c = 0; c < NC; ++c)\n" +
" channels.push_back({ src.rows, src.cols, dst.slice(c).memptr() });\n" +
" cv::split(src, channels);\n" +
" return dst;\n" +
"}\n" +
"\n" +
"// convert an Armadillo cube to OpenCV matrix. NOTE: a copy is made\n" +
"template <typename T>\n" +
"cv::Mat to_cvmat(Cube<T>& src) {\n" +
" std::vector<cv::Mat_<T>> channels;\n" +
" for (size_t c = 0; c < src.n_slices; ++c) {\n" +
" auto* data = const_cast<T*>(src.slice(c).memptr());\n" +
" channels.push_back({ int(src.n_cols), int(src.n_rows), data });\n" +
" }\n" +
" cv::Mat dst;\n" +
" cv::merge(channels, dst);\n" +
" return dst;\n" +
"}\n";
}
......@@ -16,6 +16,7 @@ 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.converter.OptimizationSymbolHandler;
import de.monticore.lang.monticar.generator.cpp.converter.TypeConverter;
import de.monticore.lang.monticar.generator.cpp.BluePrintCPP.*;
import de.monticore.lang.monticar.generator.cpp.mathopt.MathOptSolverConfig;
import de.monticore.lang.monticar.generator.cpp.template.AllTemplates;
import de.monticore.lang.monticar.generator.cpp.viewmodel.AutopilotAdapterDataModel;
......@@ -59,6 +60,8 @@ public class GeneratorCPP implements Generator {
private boolean generateCMake = false;
private CMakeConfig cMakeConfig;
//MathOpt
private MathOptSolverConfig mathOptSolverConfig = new MathOptSolverConfig();
private OptimizationSymbolHandler mathOptExecuteMethodGenerator = new OptimizationSymbolHandler();
......@@ -206,7 +209,8 @@ public class GeneratorCPP implements Generator {
fileContents.add(OctaveHelper.getOctaveHelperFileContent());
if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend"))
fileContents.add(ArmadilloHelper.getArmadilloHelperFileContent(isGenerateTests));
if(BluePrintCPP.usedCV)
fileContents.add(ConversionHelper.getConversionHelperFileContent(isGenerateTests));
if (shouldGenerateMainClass()) {
//fileContents.add(getMainClassFileContent(componentInstanceSymbol, fileContents.get(0)));
} else if (shouldGenerateSimulatorInterface()) {
......@@ -217,6 +221,8 @@ public class GeneratorCPP implements Generator {
fileContents.add(OctaveHelper.getOctaveHelperFileContent());
if (MathConverter.curBackend.getBackendName().equals("ArmadilloBackend"))
fileContents.add(ArmadilloHelper.getArmadilloHelperFileContent(isGenerateTests));
if(BluePrintCPP.usedCV)
fileContents.add(ConversionHelper.getConversionHelperFileContent(isGenerateTests));
if(componentInstanceSymbol instanceof EMADynamicComponentInstanceSymbol){
//TODO: add Events Value Helper
......
......@@ -46,11 +46,12 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
MathMatrixNameExpressionSymbol mathMatrixNameExpressionSymbol = (MathMatrixNameExpressionSymbol) mathExpressionSymbol;
mathMatrixNameExpressionSymbol.setNameToAccess("");
BluePrintCPP bluePrintCPP = (BluePrintCPP) bluePrint;
String valueListString = "";
for (MathMatrixAccessSymbol accessSymbol : mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols())
MathFunctionFixer.fixMathFunctions(accessSymbol, (BluePrintCPP) bluePrint);
MathFunctionFixer.fixMathFunctions(accessSymbol, bluePrintCPP);
Method erodeHelperMethod = getErodeHelperMethod(mathMatrixNameExpressionSymbol, (BluePrintCPP) bluePrint);
Method erodeHelperMethod = getErodeHelperMethod(mathMatrixNameExpressionSymbol, bluePrintCPP);
valueListString += ExecuteMethodGenerator.generateExecuteCode(mathExpressionSymbol, new ArrayList<String>());
List<MathMatrixAccessSymbol> newMatrixAccessSymbols = new ArrayList<>();
MathStringExpression stringExpression = new MathStringExpression("erodeHelper" + valueListString,mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().getMathMatrixAccessSymbols());
......@@ -58,7 +59,8 @@ public class ErodeCommand extends ArgumentNoReturnMathCommand{
mathMatrixNameExpressionSymbol.getMathMatrixAccessOperatorSymbol().setMathMatrixAccessSymbols(newMatrixAccessSymbols);
((BluePrintCPP) bluePrint).addCVIncludeString("opencv2/imgproc");
bluePrintCPP.addCVIncludeString("opencv2/imgproc");
bluePrintCPP.addAdditionalIncludeString("ConvHelper");
bluePrint.addMethod(erodeHelperMethod);
}
......
......@@ -3,16 +3,34 @@ package test.math;
component ErodeAndGaussianBlurCommandTest{
port
in Q^{3,n,m} src,
in Q^{n,m,3} src,
in Z erosion_elemIn,
in Q^{3} sizeY,
in Q^{2} two,
out Q^{n,m,3} dst,
out Q^{3,n,m} outMatrix,
out Q^{2,m} out2Matrix;
implementation Math{
contours = findContours(image, mode, method);
//Fall1 no pre-cv and no suc-cv
dst = erode(src, erosion_elem, iterations);
//Fall2
Q^{270,340,3} dst2;
dst = erode(src, erosion_elem, iterations);
//Fall3 a pre-cv and suc-cv
dst = erode(src, erosion_elem, iterations);
dst3 = dilate(dst, dilation_elem, iterations);
//Fall4
Q^{240, 340, 3} src4 = det(src);
dst = erode(src4, erosion_elem, iterations);
//Fall5
dst = erode(src, erosion_elem, iterations);
dst3 = dilate(dst, dilation_elem, iterations);
contours = findContours(dst3, method, mode);
}
}
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