Skip to content

Commit e68f4fa

Browse files
committed
Fix LineParametersNode to allow for runtime-changing values
1 parent 30b369b commit e68f4fa

7 files changed

Lines changed: 77 additions & 13 deletions

File tree

PaperVision/src/main/kotlin/org/deltacv/papervision/attribute/Attribute.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ abstract class Attribute :
9292
private set
9393

9494
private var lastFrameDrawn = -1
95+
private var isProcessingEditorValue = false
96+
private var isProcessingTunerValue = false
9597

9698
val onDelete = PaperEventHandler("OnDelete-${this::class.simpleName}")
9799

@@ -216,8 +218,31 @@ abstract class Attribute :
216218

217219
abstract fun genValue(current: CodeGen.Current): GenValue
218220

219-
open val editorValue: EditorValue? = null
220-
open val tunerValue: TunerValue? = null
221+
open val editorValue: EditorValue?
222+
get() {
223+
if(isProcessingEditorValue) return EditorValue.Null
224+
isProcessingEditorValue = true
225+
try {
226+
return internalEditorValue
227+
} finally {
228+
isProcessingEditorValue = false
229+
}
230+
}
231+
232+
protected open val internalEditorValue: EditorValue? get() = null
233+
234+
open val tunerValue: TunerValue?
235+
get() {
236+
if(isProcessingTunerValue) return TunerValue.NullValue
237+
isProcessingTunerValue = true
238+
try {
239+
return internalTunerValue
240+
} finally {
241+
isProcessingTunerValue = false
242+
}
243+
}
244+
245+
protected open val internalTunerValue: TunerValue? get() = null
221246

222247
fun rebuildPreviz() {
223248
if(!isOnEditor) return

PaperVision/src/main/kotlin/org/deltacv/papervision/attribute/TypedAttribute.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,11 @@ abstract class TypedAttribute<R: GenValue>(
313313
open fun readEditorValue(): EditorValue? = null
314314
open fun readTunerValue(): TunerValue? = null
315315

316-
override val editorValue get() = when {
316+
override val internalEditorValue get() = when {
317317
mode == AttributeMode.INPUT -> availableLinkedAttribute?.editorValue ?: readEditorValue()
318318
else -> readEditorValue()
319319
}
320-
override val tunerValue get() = when {
320+
override val internalTunerValue get() = when {
321321
mode == AttributeMode.INPUT -> readTunerValue()
322322
else -> null
323323
}

PaperVision/src/main/kotlin/org/deltacv/papervision/attribute/vision/structs/ScalarAttribute.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class ScalarAttribute(
8686
values?.getOrElse(3) { GenValue.Double.ZERO } ?: GenValue.Double.ZERO,
8787
)
8888

89-
return readGenValue(current, value)
89+
return readGenValue<GenValue.Scalar>(current, value)
9090
}
9191

9292
}

PaperVision/src/main/kotlin/org/deltacv/papervision/codegen/CodeGenManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class CodeGenManager(val paperVision: PaperVision) {
111111
font = Font.find("calcutta-big")
112112
).enable()
113113

114-
logger.error("-- CodeGen #${codeGen.hashCodeString}FAILED due to ${if(ex is GenException) "gen" else "unknown"} exception --", ex)
114+
logger.error("-- CodeGen #${codeGen.hashCodeString} FAILED due to ${if(ex is GenException) "gen" else "unknown"} exception --", ex)
115115
return null
116116
}
117117

PaperVision/src/main/kotlin/org/deltacv/papervision/node/vision/imageproc/AverageColorNode.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ import org.deltacv.papervision.node.DrawNode
1717
import org.deltacv.papervision.node.NodeCategory
1818
import org.deltacv.papervision.node.PaperNode
1919
import org.deltacv.papervision.node.vision.ColorSpace
20+
import org.deltacv.papervision.serialization.v2.CodecType
21+
import org.deltacv.papervision.serialization.v2.DataDecoder
22+
import org.deltacv.papervision.serialization.v2.DataEncoder
23+
import org.deltacv.papervision.serialization.v2.objOrSkip
2024

2125
@PaperNode(
22-
name = "nod_averagecolor",
26+
name = "nod_avgcolor",
2327
category = NodeCategory.IMAGE_PROC,
24-
description = "des_averagecolor"
28+
description = "des_avgcolor"
2529
)
30+
@CodecType
2631
class AverageColorNode : DrawNode<AverageColorNode.Session>() {
2732

2833
val input = MatAttribute(INPUT, "$[att_input]")
@@ -42,14 +47,15 @@ class AverageColorNode : DrawNode<AverageColorNode.Session>() {
4247
current {
4348
val inputValue = input.genValue(current)
4449

45-
val outputVar = uniqueVariable("${inputValue.value.v}Avg", JvmOpenCv.Scalar.new())
50+
val outputVar = uniqueVariable("${inputValue.value.v}Avg", JvmOpenCv.Scalar.new(0.v, 0.v, 0.v, 0.v))
4651

4752
group {
4853
private(outputVar)
4954
}
5055

5156
current.scope {
52-
outputVar instanceSet inputValue.value.v.callValue("mean", JvmOpenCv.Scalar)
57+
nameComment()
58+
outputVar instanceSet JvmOpenCv.Core.callValue("mean", JvmOpenCv.Scalar, inputValue.value.v)
5359
}
5460

5561
session.output = GenValue.Scalar.Inst(outputVar.resolved())
@@ -98,6 +104,18 @@ class AverageColorNode : DrawNode<AverageColorNode.Session>() {
98104
}
99105
}
100106

107+
override fun encode(encoder: DataEncoder) {
108+
super.encode(encoder)
109+
encoder.obj("input", input)
110+
encoder.obj("output", output)
111+
}
112+
113+
override fun decode(decoder: DataDecoder) {
114+
super.decode(decoder)
115+
decoder.objOrSkip("input", input)
116+
decoder.objOrSkip("output", output)
117+
}
118+
101119
class Session : CodeGenSession {
102120
lateinit var output: GenValue.Scalar.Inst
103121
val elementGenValues = mutableMapOf<Int, GenValue.Double.Runtime>()

PaperVision/src/main/kotlin/org/deltacv/papervision/node/vision/overlay/LineParametersNode.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.deltacv.papervision.codegen.CodeGenSession
2828
import org.deltacv.papervision.codegen.GenValue
2929
import org.deltacv.papervision.codegen.build.language.jvm.JvmOpenCv
3030
import org.deltacv.papervision.codegen.dsl.polyglot
31+
import org.deltacv.papervision.codegen.language.BaseLanguage
3132
import org.deltacv.papervision.codegen.language.interpreted.CPythonLanguage
3233
import org.deltacv.papervision.codegen.language.jvm.JavaLanguage
3334
import org.deltacv.papervision.codegen.resolve.resolved
@@ -62,21 +63,37 @@ class LineParametersNode : DrawNode<LineParametersNode.Session>() {
6263
}
6364

6465
override val generators = polyglot {
65-
generatorFor(JavaLanguage) {
66+
generatorFor<BaseLanguage> {
6667
val session = Session()
6768

6869
current {
6970
val lineColorValue = lineColor.genValue(current)
71+
val lineThicknessValue = lineThickness.genValue(current)
7072

7173
val lineColorVar = uniqueVariable("lineColor", JvmOpenCv.Scalar(lineColorValue, current))
72-
73-
val lineThicknessVar = uniqueVariable("lineThickness", lineThickness.genValue(current).v)
74+
val lineThicknessVar = uniqueVariable("lineThickness", lineThicknessValue.v)
7475

7576
group {
7677
public(lineColorVar, lineColor.label())
7778
public(lineThicknessVar, lineThickness.label())
7879
}
7980

81+
if(lineColorValue is GenValue.Scalar.Inst || lineThicknessValue is GenValue.Int.Runtime) {
82+
current.scope {
83+
nameComment()
84+
85+
// if these are runtime values, we need to set them to the line variables
86+
// to reflect any changes that might have happened since it was first set
87+
// (e.g. through a tuner)
88+
if(lineColorValue is GenValue.Scalar.Inst) {
89+
lineColorVar instanceSet JvmOpenCv.Scalar(lineColorValue, current)
90+
}
91+
if(lineThicknessValue is GenValue.Int.Runtime) {
92+
lineThicknessVar instanceSet lineThickness.genValue(current).v
93+
}
94+
}
95+
}
96+
8097
session.lineParameters = GenValue.LineParameters.Runtime(GenValue.Scalar.Inst(lineColorVar.resolved()), GenValue.Int.Runtime(lineThicknessVar.resolved()))
8198
}
8299

PaperVision/src/main/resources/lang_pv.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ nod_pipelineoutput,Pipeline Output,Salida
2424
nod_binarymask,Binary Mask,Mascara Binaria
2525
nod_colorthresh,Color Threshold,Umbral de Color
2626
nod_cvtcolor,Convert Color,Convertir Color
27+
nod_avgcolor,Average Color,Color Promedio
28+
nod_extractchannel,Extract Channel,Extraer Canal
2729
nod_blur,Blur,Difuminado
2830
nod_bitwiseor,Bitwise OR,OR Bitwise
2931
nod_bitwiseand,Bitwise AND,AND Bitwise
@@ -176,6 +178,8 @@ des_bitwiseand,Performs a bitwise AND operation<br>on two binary images.,Realiza
176178
des_bitwisexor,Performs a bitwise XOR operation<br>on two binary images.,Realiza una operación XOR a nivel<br>de bits en dos imágenes binarias.
177179
des_bitwisenot,Performs a bitwise NOT operation<br>on a binary image.,Realiza una operación NOT a nivel<br>de bits en una imagen binaria.
178180
des_cvtcolor,"Converts a Mat from its current color space<br>to the specified color space. If the mat is already<br>in the specified color space, no conversion is made.","Convierte un Mat de su espacio de color actual<br>al espacio de color especificado. Si el Mat ya está<br>en el espacio de color especificado,<br>no se realiza ninguna conversión."
181+
des_avgcolor,Calculates the average of each color<br>channel of the input image,Calcula el promedio de cada<br>canal de color de la imagen de entrad
182+
des_extractchannel,Extracts a single color channel<br>from the input image,Extrae un solo canal de<br>color de la imagen de entrada
179183
des_erodedilate,Erodes and dilates the areas<br>of a given binary image.,Erosiona y dilata las áreas<br>de una imagen binaria dada.
180184
des_binarymask,"Takes a normal image and performs a mask<br>based on a binary image, discards or includes areas<br>from the normal image based on the binary image.","Toma una imagen normal y aplica una máscara basada<br>en una imagen binaria, descartando o incluyendo áreas<br>de la imagen normal según la imagen binaria."
181185
des_colorthresh,"Performs a threshold in the input image<br>and returns a binary image, discarding<br>the pixels that were outside the range.","Realiza un umbral en la imagen de entrada<br>y devuelve una imagen binaria, descartando<br>los píxeles que estaban fuera del rango."

0 commit comments

Comments
 (0)