Skip to content

Commit

Permalink
Add support for non-runtime lists to all nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
serivesmejia committed Nov 29, 2024
1 parent adcc91f commit dfa3e96
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ open class ListAttribute(
repeat(fixedLength!!) {
createElement()
}
} else if (previousLength != null || previousLength != 0) {
} else if (previousLength != null) {
val delta = (fixedLength ?: 0) - (previousLength ?: 0)

if (delta < 0) {
Expand Down Expand Up @@ -134,6 +134,20 @@ open class ListAttribute(
previousLength = fixedLength
}

override fun delete() {
super.delete()
for(attribute in listAttributes) {
attribute.delete()
}
}

override fun restore() {
super.restore()
for(attribute in listAttributes) {
attribute.restore()
}
}

override fun draw() {
super.draw()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class CodeGen(
fun flags() = flags.toTypedArray()

val context = CodeGenContext(this)

operator fun <T> invoke(block: CodeGenContext.() -> T) = block(context)

data class Current(val codeGen: CodeGen, val scope: Scope, val isForPreviz: Boolean) {
Expand All @@ -84,11 +85,8 @@ class CodeGen(
}

class CodeGenOptions {

var genAtTheEnd = false

inline operator fun invoke(block: CodeGenOptions.() -> Unit) = block()

}

interface CodeGenSession
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class BoundingRectsNode : DrawNode<BoundingRectsNode.Session>() {
val input = inputContours.value(current)

val listName = if (input is GenValue.GList.RuntimeListOf<*>) {
"${input.value}Rects"
"${input.value.value}Rects"
} else "rects"

val rectsList = uniqueVariable(listName, JavaTypes.ArrayList(JvmOpenCvTypes.Rect).new())
Expand Down Expand Up @@ -105,7 +105,7 @@ class BoundingRectsNode : DrawNode<BoundingRectsNode.Session>() {
val input = inputContours.value(current)

val listName = if (input is GenValue.GList.RuntimeListOf<*>) {
"${input.value}_rects"
"${input.value.value}_rects"
} else "rects"

val rectsList = uniqueVariable(listName, CPythonLanguage.NoType.newArray(0.v))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ import io.github.deltacv.papervision.attribute.vision.structs.RotatedRectAttribu
import io.github.deltacv.papervision.codegen.CodeGen
import io.github.deltacv.papervision.codegen.CodeGenSession
import io.github.deltacv.papervision.codegen.GenValue
import io.github.deltacv.papervision.codegen.build.Value
import io.github.deltacv.papervision.codegen.build.type.CPythonOpenCvTypes.cv2
import io.github.deltacv.papervision.codegen.build.type.JavaTypes
import io.github.deltacv.papervision.codegen.build.type.JvmOpenCvTypes
import io.github.deltacv.papervision.codegen.build.type.JvmOpenCvTypes.Imgproc
import io.github.deltacv.papervision.codegen.dsl.ScopeContext
import io.github.deltacv.papervision.codegen.dsl.generatorsBuilder
import io.github.deltacv.papervision.codegen.language.interpreted.CPythonLanguage
import io.github.deltacv.papervision.codegen.language.jvm.JavaLanguage
import io.github.deltacv.papervision.node.Category
import io.github.deltacv.papervision.node.DrawNode
Expand Down Expand Up @@ -57,12 +61,12 @@ class BoundingRotatedRectsNode : DrawNode<BoundingRotatedRectsNode.Session>() {
current {
val input = contours.value(current)

if(input !is GenValue.GList.RuntimeListOf<*>) {
raise("") // TODO: Handle non-runtime lists
}
val name = if(input is GenValue.GList.RuntimeListOf<*>) {
input.value.value
} else null

val points2f = uniqueVariable("${input.value.value}2f", JvmOpenCvTypes.MatOfPoint2f.new())
val rectsList = uniqueVariable("${input.value.value}RotRects", JavaTypes.ArrayList(JvmOpenCvTypes.RotatedRect).new())
val points2f = uniqueVariable("${name ?: "points"}2f", JvmOpenCvTypes.MatOfPoint2f.new())
val rectsList = uniqueVariable("${name?.run { this + "R"} ?: "r"}otRects", JavaTypes.ArrayList(JvmOpenCvTypes.RotatedRect).new())

group {
private(points2f)
Expand All @@ -72,14 +76,68 @@ class BoundingRotatedRectsNode : DrawNode<BoundingRotatedRectsNode.Session>() {
current.scope {
rectsList("clear")

foreach(variable(JvmOpenCvTypes.MatOfPoint, "points"), input.value) {
fun ScopeContext.withPoints(points: Value) {
points2f("release")
it("convertTo", points2f, cvTypeValue("CV_32F"))
points("convertTo", points2f, cvTypeValue("CV_32F"))

separate()

rectsList("add", Imgproc.callValue("minAreaRect", JvmOpenCvTypes.RotatedRect, points2f))
}

if(input is GenValue.GList.RuntimeListOf<*>) {
foreach(variable(JvmOpenCvTypes.MatOfPoint, "points"), input.value) {
withPoints(it)
}
} else {
for(element in (input as GenValue.GList.ListOf<*>).elements) {
if(element is GenValue.GPoints.RuntimePoints) {
withPoints(element.value)
} else {
raise("Invalid input type for contours")
}
}
}
}

session.rects = GenValue.GList.RuntimeListOf(rectsList, GenValue.GRect.Rotated.RuntimeRotatedRect::class)
}

session
}

generatorFor(CPythonLanguage) {
val session = Session()

current {
val input = contours.value(current)

val name = if(input is GenValue.GList.RuntimeListOf<*>) {
input.value.value + "_r"
} else null

val rectsList = uniqueVariable("${name ?: "r"}ot_rects", CPythonLanguage.NoType.newArray())

current.scope {
local(rectsList)

fun ScopeContext.withPoints(points: Value) {
rectsList("append", cv2.callValue("minAreaRect", CPythonLanguage.NoType, points))
}

if(input is GenValue.GList.RuntimeListOf<*>) {
foreach(variable(CPythonLanguage.NoType, "points"), input.value) {
withPoints(it)
}
} else {
for(element in (input as GenValue.GList.ListOf<*>).elements) {
if(element is GenValue.GPoints.RuntimePoints) {
withPoints(element.value)
} else {
raise("Invalid input type for contours")
}
}
}
}

session.rects = GenValue.GList.RuntimeListOf(rectsList, GenValue.GRect.Rotated.RuntimeRotatedRect::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ class FilterBiggestContourNode : DrawNode<FilterBiggestContourNode.Session>() {

val contoursList = input.value(current)

if(contoursList !is GenValue.GList.RuntimeListOf<*>) {
raise("") // TODO: Handle non-runtime lists
}

val biggestContour = uniqueVariable("biggestContour", JvmOpenCvTypes.MatOfPoint.nullVal) // TODO: huh???

group {
Expand All @@ -70,14 +66,37 @@ class FilterBiggestContourNode : DrawNode<FilterBiggestContourNode.Session>() {
current.scope {
biggestContour instanceSet biggestContour.nullVal

foreach(variable(JvmOpenCvTypes.MatOfPoint, "contour"), contoursList.value) { contour ->
val contourArea = Imgproc.callValue("contourArea", JvmOpenCvTypes.MatOfPoint, contour)
val biggestContourArea = Imgproc.callValue("contourArea", JvmOpenCvTypes.MatOfPoint, biggestContour)
if(contoursList is GenValue.GList.RuntimeListOf<*>) {
foreach(variable(JvmOpenCvTypes.MatOfPoint, "contour"), contoursList.value) { contour ->
val contourArea = Imgproc.callValue("contourArea", JvmOpenCvTypes.MatOfPoint, contour)
val biggestContourArea = Imgproc.callValue("contourArea", JvmOpenCvTypes.MatOfPoint, biggestContour)

ifCondition(
biggestContour equalsTo biggestContour.nullVal or (contourArea greaterThan biggestContourArea)
) {
biggestContour instanceSet contour
ifCondition(
biggestContour equalsTo biggestContour.nullVal or (contourArea greaterThan biggestContourArea)
) {
biggestContour instanceSet contour
}
}
} else {
for(element in (contoursList as GenValue.GList.ListOf<*>).elements) {
separate()

val contour = if(element is GenValue.GPoints.RuntimePoints) {
element.value
} else {
raise("Invalid element in contours list")
}

ifCondition(contour notEqualsTo language.nullValue) {
val contourArea = Imgproc.callValue("contourArea", JvmOpenCvTypes.MatOfPoint, contour)
val biggestContourArea = Imgproc.callValue("contourArea", JvmOpenCvTypes.MatOfPoint, biggestContour)

ifCondition(
biggestContour equalsTo biggestContour.nullVal or (contourArea greaterThan biggestContourArea)
) {
biggestContour instanceSet contour
}
}
}
}
}
Expand All @@ -92,19 +111,35 @@ class FilterBiggestContourNode : DrawNode<FilterBiggestContourNode.Session>() {
current {
val session = Session()

val inputValue = input.value(current)

val contoursList = input.value(current)
current.scope {
val contoursList = if(inputValue is GenValue.GList.RuntimeListOf<*>) {
inputValue.value
} else {
val list = uniqueVariable("contours_list", CPythonLanguage.NoType.newArray())
local(list)

for(element in (inputValue as GenValue.GList.ListOf<*>).elements) {
if(element is GenValue.GPoints.RuntimePoints) {
ifCondition(element.value notEqualsTo language.nullValue) {
list("append", element.value)
}
} else {
raise("Invalid element in contours list")
}
}

if(contoursList !is GenValue.GList.RuntimeListOf<*>) {
raise("") // TODO: Handle non-runtime lists
}
separate()

list
}

current.scope {
val biggestContour = uniqueVariable(
"biggest_contour",
"max".callValue(
CPythonLanguage.NoType,
contoursList.value,
contoursList,
CPythonLanguage.namedArgument("key", cv2.contourArea)
)
)
Expand Down
Loading

0 comments on commit dfa3e96

Please sign in to comment.