Skip to content

Commit

Permalink
TunableField caching & improvement of field context getting
Browse files Browse the repository at this point in the history
  • Loading branch information
serivesmejia committed Nov 5, 2024
1 parent 992435f commit 186b56c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.github.serivesmejia.eocvsim.EOCVSim
import com.github.serivesmejia.eocvsim.gui.DialogFactory
import com.github.serivesmejia.eocvsim.input.SourceType
import com.github.serivesmejia.eocvsim.pipeline.PipelineSource
import com.github.serivesmejia.eocvsim.tuner.TunableField
import com.github.serivesmejia.eocvsim.util.loggerForThis
import io.github.deltacv.eocvsim.pipeline.StreamableOpenCvPipelineInstantiator
import io.github.deltacv.eocvsim.plugin.EOCVSimPlugin
Expand Down Expand Up @@ -50,10 +51,9 @@ import io.github.deltacv.papervision.util.replaceLast
import io.github.deltacv.papervision.util.toValidIdentifier
import io.javalin.Javalin
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.server.handler.HandlerWrapper
import org.eclipse.jetty.server.handler.StatisticsHandler
import org.opencv.core.Size
import java.io.File
import java.util.WeakHashMap
import java.util.concurrent.Executors
import javax.swing.JLabel
import javax.swing.JOptionPane
Expand All @@ -75,6 +75,8 @@ class PaperVisionEOCVSimPlugin : EOCVSimPlugin() {

var currentPrevizSession: EOCVSimPrevizSession? = null

private val tunableFieldCache = WeakHashMap<VirtualField, TunableField<*>>()

/**
* If the plugin comes from a file, we will just use the file classpath, since it's a single fat jar.
* If the plugin comes from Maven, we will use the classpath of all the transitive dependencies.
Expand Down Expand Up @@ -248,12 +250,35 @@ class PaperVisionEOCVSimPlugin : EOCVSimPlugin() {
}

override fun onEnable() {
fun tunableFieldOf(field: VirtualField): TunableField<*> {
if(tunableFieldCache.containsKey(field)) {
return tunableFieldCache[field]!!
}

val tunableFieldClass = eocvSim.tunerManager.getTunableFieldOf(field)

val tunableField = tunableFieldClass.getConstructor(
Object::class.java,
VirtualField::class.java,
EOCVSim::class.java
).newInstance(
currentPrevizSession!!.latestPipeline,
field,
eocvSim
)

tunableFieldCache[field] = tunableField
return tunableField
}

engine.setMessageHandlerOf<TunerChangeValueMessage> {
eocvSim.onMainUpdate.doOnce {
val field = currentPrevizSession?.latestVirtualReflect?.getLabeledField(message.label)

if (field != null) {
field.set(message.value)
val tunableField = tunableFieldOf(field)

tunableField.setFieldValue(0, message.value)
logger.info("Received tuner change value message for ${message.label} to ${message.value} ")
}

Expand All @@ -266,24 +291,13 @@ class PaperVisionEOCVSimPlugin : EOCVSimPlugin() {
val field = currentPrevizSession?.latestVirtualReflect?.getLabeledField(message.label)

if(field != null) {
val tunableFieldClass = eocvSim.tunerManager.getTunableFieldOf(field)

val tunableField = tunableFieldClass.getConstructor(
Object::class.java,
VirtualField::class.java,
EOCVSim::class.java
).newInstance(
currentPrevizSession!!.latestPipeline,
field,
eocvSim
)
val tunableField = tunableFieldOf(field)

for (i in message.values.indices) {
tunableField.setFieldValue(i, message.values[i])
}

logger.info("Received tuner change values message for ${message.label} to ${message.values}")

}

respond(OkResponse())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.github.serivesmejia.eocvsim.pipeline.PipelineManager
import com.github.serivesmejia.eocvsim.pipeline.PipelineSource
import com.github.serivesmejia.eocvsim.util.loggerForThis
import io.github.deltacv.eocvsim.pipeline.StreamableOpenCvPipeline
import io.github.deltacv.eocvsim.pipeline.StreamableOpenCvPipelineInstantiator
import io.github.deltacv.eocvsim.stream.ImageStreamer
import io.github.deltacv.eocvsim.virtualreflect.VirtualReflectContext
import io.github.deltacv.eocvsim.virtualreflect.jvm.JvmVirtualReflection
Expand Down Expand Up @@ -75,8 +74,13 @@ class EOCVSimPrevizSession(

eocvSim.pipelineManager.forceChangePipeline(eocvSim.pipelineManager.getIndexOf(latestClass!!, PipelineSource.COMPILED_ON_RUNTIME))

latestPipeline = eocvSim.pipelineManager.currentPipeline!!
latestVirtualReflect = JvmVirtualReflection.contextOf(latestPipeline!!)
latestPipeline = eocvSim.pipelineManager.currentPipeline

latestVirtualReflect = if(latestPipeline != null) {
JvmVirtualReflection.contextOf(latestPipeline!!)
} else {
null
}

refreshPrevizPipelineStreamer()

Expand Down

0 comments on commit 186b56c

Please sign in to comment.