bug and stability fixes

This commit is contained in:
Kazik Pogoda
2019-08-16 00:54:31 +02:00
parent cd63516c8b
commit 614d3a653b
3 changed files with 338 additions and 178 deletions

View File

@@ -1,64 +1,44 @@
package org.openrndr.extra.kinect
import org.openrndr.Extension
import org.openrndr.Program
import org.openrndr.draw.*
import org.openrndr.math.Vector2
import org.openrndr.resourceUrl
import java.lang.RuntimeException
import java.nio.ByteBuffer
import java.util.concurrent.atomic.AtomicReference
import kotlin.concurrent.thread
interface Kinects {
/**
* Represents all the accessible kinects handled by a specific driver (V1, V2).
*
* @param <CTX> data needed to make low level kinect support calls.
*/
interface Kinects<CTX> {
fun countDevices(): Int
/**
* Starts kinect device of a given number.
*
* @throws KinectException if device of such a number does not exist.
* @throws KinectException if device of such a number does not exist,
* better to count them first.
* @see countDevices
*/
fun startDevice(num: Int = 0): KinectDevice
fun startDevice(num: Int = 0): KinectDevice<CTX>
/**
* Executes low level Kinect commands in the kinect thread.
*/
fun execute(commands: (CTX) -> Any) : Any
}
class DefaultKinects(private val manager: KinectsManager) : Kinects {
init {
manager.init()
// as we don't have explicit shutdown mechanism in OPENRNDR
// we need to install a shutdown hook for now
Runtime.getRuntime().addShutdownHook(
thread(
name = "${manager.javaClass.simpleName}-closer",
start = false,
isDaemon = false
) {
manager.shutdown()
}
)
}
override fun countDevices(): Int {
return manager.countDevices()
}
override fun startDevice(num: Int): KinectDevice {
return manager.startDevice(num)
}
}
/**
* Represents specific device.
*
* @param <CTX> data needed to make low level kinect support calls.
*/
interface KinectDevice<CTX> : Extension {
val depthCamera: KinectDepthCamera
interface KinectsManager {
fun init()
fun countDevices(): Int
fun startDevice(num: Int): KinectDevice
fun shutdown()
}
abstract class KinectDevice : Extension {
override var enabled: Boolean = true
abstract val depthCamera: KinectDepthCamera
override fun beforeDraw(drawer: Drawer, program: Program) {
if (depthCamera.enabled) {
depthCamera.update()
}
}
/**
* Executes low level Kinect commands in the kinect thread in the context of this device.
*/
fun execute(commands: (CTX) -> Any): Any
}
interface KinectCamera {
@@ -68,58 +48,10 @@ interface KinectCamera {
var mirror: Boolean
val currentFrame: ColorBuffer
fun getLatestFrame(): ColorBuffer?
fun update()
}
interface KinectDepthCamera : KinectCamera {
val depthScale: Double
}
abstract class AbstractKinectDepthCamera(
final override val width: Int,
final override val height: Int,
final override val depthScale: Double
) : KinectDepthCamera {
protected val byteBufferRef: AtomicReference<ByteBuffer> = AtomicReference()
private val rawBuffer: ColorBuffer = colorBuffer(
width,
height,
format = ColorFormat.R,
type = ColorType.UINT16 // it would be perfect if we could use isampler in the shader
)
override val currentFrame: ColorBuffer = colorBuffer(
width,
height,
format = ColorFormat.R,
type = ColorType.FLOAT16 // in the future we might want to choose the precision here
)
private val depthMapper = KinectRawDataToDepthMapper()
init {
depthMapper.depthScale = depthScale
depthMapper.mirror = false
depthMapper.resolution = Vector2(width.toDouble(), height.toDouble())
}
private val newFrameRef = AtomicReference<ColorBuffer>()
override fun getLatestFrame(): ColorBuffer? {
return newFrameRef.getAndSet(null)
}
override fun update() {
byteBufferRef.getAndSet(null)?.let { bytes ->
rawBuffer.write(bytes)
depthMapper.apply(rawBuffer, currentFrame)
newFrameRef.set(currentFrame)
}
}
override var mirror: Boolean
get() = depthMapper.mirror
set(value) { depthMapper.mirror = value }
}
class KinectRawDataToDepthMapper :
Filter(filterShaderFromUrl(resourceUrl("kinect-raw-to-depth.frag", Kinects::class.java))) {
var depthScale: Double by parameters
var mirror: Boolean by parameters
var resolution: Vector2 by parameters
/* no special attributes at the moment */
}
class KinectException(msg: String) : RuntimeException(msg)