Kinect API update, impl moved to separate package

This commit is contained in:
Kazik Pogoda
2019-08-24 18:34:48 +02:00
parent 40b919edd2
commit 4c6d4cc578
3 changed files with 40 additions and 12 deletions

View File

@@ -17,9 +17,10 @@ interface Kinects<CTX> {
/** /**
* Starts kinect device of a given number. * Starts kinect device of a given number.
* *
* @param num the kinect device index. * @param num the kinect device index (starts with 0). If no value specified,
* @throws KinectException if device of such a number does not exist, * it will default to 0.
* better to count them first. * @throws KinectException if device of such a number does not exist
* (better to count them first), or it was already started.
* @see countDevices * @see countDevices
*/ */
fun startDevice(num: Int = 0): KinectDevice<CTX> fun startDevice(num: Int = 0): KinectDevice<CTX>
@@ -34,15 +35,17 @@ interface Kinects<CTX> {
/** /**
* Represents specific device. * Represents specific device.
* *
* @param <CTX> data needed to make low level kinect support calls. * @param CTX type of data needed to make low level kinect support calls (e.g. freenect contexts).
*/ */
interface KinectDevice<CTX> : Extension { interface KinectDevice<CTX> : Extension {
val depthCamera: KinectDepthCamera val depthCamera: KinectDepthCamera
/** /**
* Executes low level Kinect commands in the kinect thread in the context of this device. * Executes low level Kinect commands in the kinect thread in the context of this device.
*/ */
fun <T> execute(commands: (CTX) -> T): T fun <T> execute(commands: (CTX) -> T): T
} }
interface KinectCamera { interface KinectCamera {
@@ -51,6 +54,19 @@ interface KinectCamera {
val height: Int val height: Int
var mirror: Boolean var mirror: Boolean
val currentFrame: ColorBuffer val currentFrame: ColorBuffer
/**
* Returns the latest frame, but only once. Useful for the scenarios
* where each new frame triggers extra computation. Therefore the same
* expensive operation might happen only once, especially when the refresh
* rate of the target screen is higher than kinect's 30 fps.
* <p>
* Example usage:
* <pre>
* kinect.depthCamera.getLatestFrame()?.let { frame ->
* grayscaleFilter.apply(frame, grayscaleBuffer)
* }
* </pre>
*/
fun getLatestFrame(): ColorBuffer? fun getLatestFrame(): ColorBuffer?
} }

View File

@@ -1,7 +1,10 @@
package org.openrndr.extra.kinect package org.openrndr.extra.kinect.impl
import org.openrndr.Program import org.openrndr.Program
import org.openrndr.draw.* import org.openrndr.draw.*
import org.openrndr.extra.kinect.KinectDepthCamera
import org.openrndr.extra.kinect.KinectDevice
import org.openrndr.extra.kinect.Kinects
import org.openrndr.math.Vector2 import org.openrndr.math.Vector2
import org.openrndr.resourceUrl import org.openrndr.resourceUrl
import java.nio.ByteBuffer import java.nio.ByteBuffer
@@ -9,8 +12,11 @@ import java.util.concurrent.atomic.AtomicReference
import java.util.function.Supplier import java.util.function.Supplier
import kotlin.concurrent.thread import kotlin.concurrent.thread
class DefaultKinects<CTX>(
private val program: Program,
private val manager: KinectsManager<CTX>
) : Kinects<CTX> {
class DefaultKinects<CTX>(private val manager: KinectsManager<CTX>) : Kinects<CTX> {
init { init {
manager.initialize() manager.initialize()
// as we don't have explicit shutdown mechanism in OPENRNDR // as we don't have explicit shutdown mechanism in OPENRNDR
@@ -31,10 +37,12 @@ class DefaultKinects<CTX>(private val manager: KinectsManager<CTX>) : Kinects<CT
} }
override fun startDevice(num: Int): KinectDevice<CTX> { override fun startDevice(num: Int): KinectDevice<CTX> {
return manager.startDevice(num) val device = manager.startDevice(num)
program.extend(device)
return device
} }
override fun execute(commands: (CTX) -> Any): Any { override fun <T> execute(commands: (CTX) -> T): T {
return manager.execute(commands) return manager.execute(commands)
} }
@@ -44,7 +52,7 @@ interface KinectsManager<CTX> {
fun initialize() fun initialize()
fun countDevices(): Int fun countDevices(): Int
fun startDevice(num: Int): KinectDevice<CTX> fun startDevice(num: Int): KinectDevice<CTX>
fun execute(commands: (CTX) -> Any): Any fun <T> execute(commands: (CTX) -> T): T
fun shutdown() fun shutdown()
} }
@@ -53,7 +61,7 @@ interface KinectFeatureEnabler {
} }
interface KinectCommandsExecutor<CTX> { interface KinectCommandsExecutor<CTX> {
fun execute(commands: (CTX) -> Any): Any fun <T> execute(commands: (CTX) -> T): T
} }
class DefaultKinectDevice<CTX>( class DefaultKinectDevice<CTX>(
@@ -64,7 +72,8 @@ class DefaultKinectDevice<CTX>(
override fun beforeDraw(drawer: Drawer, program: Program) { override fun beforeDraw(drawer: Drawer, program: Program) {
depthCamera.update() depthCamera.update()
} }
override fun execute(commands: (CTX) -> Any): Any {
override fun <T> execute(commands: (CTX) -> T): T {
return commandsExecutor.execute(commands) return commandsExecutor.execute(commands)
} }
} }
@@ -128,7 +137,10 @@ class DefaultKinectDepthCamera(
} }
private class KinectRawDataToDepthMapper : private class KinectRawDataToDepthMapper :
Filter(filterShaderFromUrl(resourceUrl("kinect-raw-to-depth.frag", Kinects::class.java))) { Filter(filterShaderFromUrl(
resourceUrl("kinect-raw-to-depth.frag",
DefaultKinects::class.java))
) {
var depthScale: Double by parameters var depthScale: Double by parameters
var mirror: Boolean by parameters var mirror: Boolean by parameters
var resolution: Vector2 by parameters var resolution: Vector2 by parameters