using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using Iisu;
namespace bbiwarg.Input.InputProviding
{
///
/// signature for the event that the device started
///
/// sender of the event
/// arguments of the event
public delegate void DeviceStartedEventHandler(object sender, EventArgs e);
///
/// signature for the event that a new frame is available
///
/// sender of the event
/// arguments of the event
public delegate void NewFrameEventHandler(object sender, NewFrameEventArgs e);
///
/// Encapsulates the arguments of the event that a new frame is available.
///
public class NewFrameEventArgs : EventArgs
{
///
/// the id of the frame
///
public int FrameID { get; private set; }
///
/// the with of all images in the frame
///
public int Width { get; private set; }
///
/// the height of all images in the frame
///
public int Height { get; private set; }
///
/// pointer to the raw depth data for the frame
///
public IntPtr RawDepthData { get; private set; }
///
/// pointer to the raw confidence data for the frame
///
public IntPtr RawConfidenceData { get; private set; }
///
/// Constructs a NewFrameEventArgs.
///
/// frame id
/// width of all images
/// height of all images
/// pointer to raw depth data
/// pointer to raw confidence data
public NewFrameEventArgs(int frameID, int width, int height, IntPtr rawDepthData, IntPtr rawConfidenceData)
{
FrameID = frameID;
Width = width;
Height = height;
RawDepthData = rawDepthData;
RawConfidenceData = rawConfidenceData;
}
}
///
/// InputProvider provides the raw depth and confidence data through an event.
///
public class InputProvider
{
///
/// iisu handle
///
protected IHandle handle;
///
/// iisu device from which the data is read
///
protected IDevice device;
///
/// paramter handle for the frame rate
///
protected IParameterHandle frameRate;
///
/// parameter handle for the image width
///
protected IParameterHandle width;
///
/// parameter handle for the image height
///
protected IParameterHandle height;
///
/// parameter handle for the horizontal field of view angle
///
protected IParameterHandle hfov;
///
/// parameter handle for the vertical field of view angle
///
protected IParameterHandle vfov;
///
/// data handle for the raw depth data
///
protected IDataHandle depthImage;
///
/// data handle for the raw confidence data
///
protected IDataHandle confidenceImage;
///
/// the width of all images
///
public int ImageWidth { get { return width.Value; } }
///
/// the height of all images
///
public int ImageHeight { get { return height.Value; } }
///
/// the horizontal field of view angle
///
public float HFOV { get { return hfov.Value; } }
///
/// the vertical field of view angle
///
public float VFOV { get { return vfov.Value; } }
///
/// true iff the input source provides data
///
public bool IsActive { get; private set; }
///
/// the id of the current frame
///
public virtual int CurrentFrameID { get { return device.FrameId; } }
///
/// event that the device started
///
public event DeviceStartedEventHandler DeviceStartedEvent;
///
/// event that a new frame is available
///
public event NewFrameEventHandler NewFrameEvent;
///
/// Constructs an InputProvider.
///
public InputProvider()
{
IsActive = false;
}
///
/// Initializes to device and data handles.
///
public void initialize()
{
createDevice();
registerHandles();
}
///
/// Starts the device.
///
public void start()
{
device.Start();
IsActive = true;
if (DeviceStartedEvent != null)
DeviceStartedEvent(this, new EventArgs());
run();
}
///
/// Stops the device.
///
public void stop()
{
IsActive = false;
device.Stop(true);
}
///
/// Creates an iisu device which provides the data.
///
protected void createDevice()
{
handle = Iisu.Iisu.Context.CreateHandle();
IDeviceConfiguration conf = createDeviceConfiguration();
device = handle.InitializeDevice(conf);
}
///
/// Returns an iisu device configuration.
///
/// iisu device configuration
protected virtual IDeviceConfiguration createDeviceConfiguration()
{
IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
conf.IsAsynchronous = false;
return conf;
}
///
/// Registers all parameter and data handles.
///
protected virtual void registerHandles()
{
width = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.Width");
height = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.Height");
hfov = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.HFOV");
vfov = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.VFOV");
frameRate = device.RegisterParameterHandle("SOURCE.FrameRate");
depthImage = device.RegisterDataHandle("SOURCE.CAMERA.DEPTH.Image");
confidenceImage = device.RegisterDataHandle("SOURCE.CAMERA.CONFIDENCE.Image");
}
///
/// Provides the main loop for reading data from the device.
///
protected virtual void run()
{
while (IsActive)
nextFrame();
}
///
/// Gets the next frame from the device.
///
protected virtual void nextFrame()
{
device.UpdateFrame(true);
provideNewFrame();
device.ReleaseFrame();
}
///
/// Triggers the new frame event.
///
protected void provideNewFrame()
{
if (NewFrameEvent != null)
NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, ImageWidth, ImageHeight, depthImage.Value.Raw, confidenceImage.Value.Raw));
}
}
}