using Iisu; using System; 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); /// /// InputProvider provides the raw depth and confidence data through an event. /// public class InputProvider { /// /// data handle for the raw confidence data /// protected IDataHandle confidenceImage; /// /// data handle for the raw depth data /// protected IDataHandle depthImage; /// /// iisu device from which the data is read /// protected IDevice device; /// /// parameter handle for the horizontal field of view angle /// protected IParameterHandle fieldOfViewHorizontal; /// /// parameter handle for the vertical field of view angle /// protected IParameterHandle fieldOfViewVertical; /// /// paramter handle for the frame rate /// protected IParameterHandle frameRate; /// /// iisu handle /// protected IHandle handle; /// /// parameter handle for the image height /// protected IParameterHandle height; /// /// parameter handle for the image width /// protected IParameterHandle width; /// /// the id of the current frame /// public virtual int CurrentFrameID { get { return device.FrameId; } } /// /// the horizontal field of view angle /// public float FieldOfViewHorizontal { get { return fieldOfViewHorizontal.Value; } } /// /// the vertical field of view angle /// public float FieldOfViewVertical { get { return fieldOfViewVertical.Value; } } /// /// the height of all images /// public int ImageHeight { get { return height.Value; } } /// /// the width of all images /// public int ImageWidth { get { return width.Value; } } /// /// true iff the input source provides data /// public bool IsActive { get; private set; } /// /// 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; } /// /// 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)); } /// /// Registers all parameter and data handles. /// protected virtual void registerHandles() { width = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.Width"); height = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.Height"); fieldOfViewHorizontal = device.RegisterParameterHandle("SOURCE.CAMERA.DEPTH.HFOV"); fieldOfViewVertical = 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(); } } /// /// 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 height of all images in the frame /// public int Height { get; private set; } /// /// pointer to the raw confidence data for the frame /// public IntPtr RawConfidenceData { get; private set; } /// /// pointer to the raw depth data for the frame /// public IntPtr RawDepthData { get; private set; } /// /// the with of all images in the frame /// public int Width { 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; } } }