Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
monticore
EmbeddedMontiArc
generators
EMAM2Cpp
Commits
08f9912c
Commit
08f9912c
authored
Jan 12, 2020
by
Ahmed
Browse files
add LargestContour Command
parent
2df1956f
Changes
7
Hide whitespace changes
Inline
Side-by-side
src/main/java/de/monticore/lang/monticar/generator/cpp/LanguageUnitCPP.java
View file @
08f9912c
...
...
@@ -146,7 +146,7 @@ public class LanguageUnitCPP extends LanguageUnit {
if
(!
bluePrint
.
cvIncludeStrings
.
isEmpty
()){
resultString
+=
"using namespace cv;\n"
;
}
}
//TODO add here using namespace std;
//class definition start
resultString
+=
"class "
+
bluePrint
.
getName
();
...
...
src/main/java/de/monticore/lang/monticar/generator/cpp/MathCommandRegisterCPP.java
View file @
08f9912c
...
...
@@ -67,6 +67,7 @@ public class MathCommandRegisterCPP extends MathCommandRegister {
registerMathCommand
(
new
InRangeCommand
());
registerMathCommand
(
new
PutTextCommand
());
registerMathCommand
(
new
ThresholdCommand
());
registerMathCommand
(
new
LargestContourCommand
());
//for fixing some errors
...
...
src/main/java/de/monticore/lang/monticar/generator/cpp/commands/LargestContourCommand.java
0 → 100644
View file @
08f9912c
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
LargestContourCommand
extends
MathCommand
{
public
LargestContourCommand
()
{
setMathCommandName
(
"largestContour"
);
}
@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
largestContourMethod
=
getLargestContourMethod
();
valueListString
+=
ExecuteMethodGenerator
.
generateExecuteCode
(
mathExpressionSymbol
,
new
ArrayList
<
String
>());
List
<
MathMatrixAccessSymbol
>
newMatrixAccessSymbols
=
new
ArrayList
<>();
MathStringExpression
stringExpression
=
new
MathStringExpression
(
"largestContour"
+
valueListString
,
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
getMathMatrixAccessSymbols
());
newMatrixAccessSymbols
.
add
(
new
MathMatrixAccessSymbol
(
stringExpression
));
mathMatrixNameExpressionSymbol
.
getMathMatrixAccessOperatorSymbol
().
setMathMatrixAccessSymbols
(
newMatrixAccessSymbols
);
((
BluePrintCPP
)
bluePrint
).
addCVIncludeString
(
"opencv2/imgproc"
);
bluePrint
.
addMethod
(
largestContourMethod
);
}
private
Method
getLargestContourMethod
(){
Method
method
=
new
Method
(
"largestContour"
,
"vector<Point>"
);
//add parameters
Variable
contours
=
new
Variable
();
method
.
addParameter
(
contours
,
"contours"
,
"double"
,
"vector <vector<Point>>"
,
""
);
//add an instruction to the method
method
.
addInstruction
(
methodBody
());
return
method
;
}
private
Instruction
methodBody
()
{
return
new
Instruction
()
{
@Override
public
String
getTargetLanguageInstruction
()
{
return
"double maxArea = 0;\n"
+
"int maxAreaContourId = -1;\n"
+
" for (int j = 0; j < contours.size(); j++) {\n"
+
" double newArea = contourArea(contours.at(j));\n"
+
" if (newArea > maxArea) {\n"
+
" maxArea = newArea;\n"
+
" maxAreaContourId = j;\n"
+
" }\n"
+
" }\n"
+
" return contours.at(getMaxAreaContourId(contours));\n"
+
"}\n"
;
}
@Override
public
boolean
isConnectInstruction
()
{
return
false
;
}
};
}
}
src/test/java/de/monticore/lang/monticar/generator/cpp/armadillo/ArmadilloFunctionTest.java
View file @
08f9912c
...
...
@@ -274,4 +274,9 @@ public class ArmadilloFunctionTest extends AbstractSymtabTest {
public
void
testThresholdCommand
()
throws
IOException
{
testMathCommand
(
"threshold"
);
}
@Test
public
void
testLargestContourCommand
()
throws
IOException
{
testMathCommand
(
"largestContour"
);
}
}
src/test/resources/results/armadillo/testMath/l0/test_math_LargestContourCommandTest.h
0 → 100644
View file @
08f9912c
#ifndef TEST_MATH_LARGESTCONTOURCOMMANDTEST
#define TEST_MATH_LARGESTCONTOURCOMMANDTEST
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include
"armadillo"
#include
"opencv2/imgproc.hpp"
using
namespace
arma
;
using
namespace
cv
;
class
test_math_largestContourCommandTest
{
public:
double
contours
;
double
lContour
;
void
init
()
{
}
vector
<
Point
>
largestContour
(
vector
<
vector
<
Point
>>
contours
)
{
double
maxArea
=
0
;
int
maxAreaContourId
=
-
1
;
for
(
int
j
=
0
;
j
<
contours
.
size
();
j
++
)
{
double
newArea
=
contourArea
(
contours
.
at
(
j
));
if
(
newArea
>
maxArea
)
{
maxArea
=
newArea
;
maxAreaContourId
=
j
;
}
}
return
contours
.
at
(
getMaxAreaContourId
(
contours
));
}
}
void
execute
()
{
lContour
=
(
largestContour
(
contours
));
}
};
#endif
src/test/resources/results/armadillo/testMath/l0/test_math_findContoursCommandTest.h
View file @
08f9912c
...
...
@@ -13,7 +13,7 @@ public:
mat
image
;
int
mode
;
int
method
;
mat
hierarchy
;
mat
hierarchy
;
//Try without hierarchy
mat
contours
;
void
init
()
{
...
...
src/test/resources/test/math/LargestContourCommandTest.emam
0 → 100644
View file @
08f9912c
/*
(
c
)
https
://
github
.
com
/
MontiCore
/
monticore
*/
package
test
.
math
;
component
LargestContourCommandTest
{
port
in
Q
contours
,
out
Q
lContour
;
implementation
Math
{
lContour
=
largestContour
(
contours
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment