Skip to content

Commit

Permalink
updated c# wrapper, samples and unity
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoshen committed Mar 27, 2019
1 parent 3627e02 commit 4b7b6c9
Show file tree
Hide file tree
Showing 67 changed files with 2,544 additions and 698 deletions.
4 changes: 2 additions & 2 deletions wrappers/csharp/Documentation/cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ PipelineProfile selection = pipe.Start();
var selected_device = selection.Device;
var depth_sensor = selected_device.Sensors[0];

if (depth_sensor.Options[Option.EmitterEnabled].Supported)
if (depth_sensor.Options.Supports(Option.EmitterEnabled))
{
depth_sensor.Options[Option.EmitterEnabled].Value = 1f; // Enable emitter
depth_sensor.Options[Option.EmitterEnabled].Value = 0f; // Disable emitter
}
if (depth_sensor.Options[Option.LaserPower].Supported)
if (depth_sensor.Options.Supports(Option.LaserPower))
{
var laserPower = depth_sensor.Options[Option.LaserPower];
laserPower.Value = laserPower.Max; // Set max power
Expand Down
5 changes: 4 additions & 1 deletion wrappers/csharp/Intel.RealSense/Base/DeleterHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Intel.RealSense.Base
{
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;

Expand All @@ -15,6 +16,7 @@ namespace Intel.RealSense.Base
/// Native handle with deleter delegate to release unmanaged resources
/// </summary>
// TODO: CriticalFinalizerObject & CER
//[DebuggerDisplay("{deleter?.Method.Name,nq}", Name = nameof(Deleter))]
internal sealed class DeleterHandle : IDisposable
{
private IntPtr handle;
Expand Down Expand Up @@ -64,8 +66,9 @@ internal void Reset(IntPtr ptr)

internal void Reset(IntPtr ptr, Deleter deleter)
{
this.handle = ptr;
this.deleter = deleter;
Reset(ptr);
//GC.ReRegisterForFinalize(this);
}

~DeleterHandle()
Expand Down
2 changes: 2 additions & 0 deletions wrappers/csharp/Intel.RealSense/Base/Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
namespace Intel.RealSense.Base
{
using System;
using System.Diagnostics;

/// <summary>
/// Base class for disposable objects with native resources
/// </summary>
public abstract class Object : IDisposable
{
// TODO: rename, kept for backwards compatiblity
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal readonly DeleterHandle m_instance;

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions wrappers/csharp/Intel.RealSense/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ include(StreamProfiles/CMakeLists.txt)
include(Sensors/CMakeLists.txt)
include(Types/CMakeLists.txt)
include(Helpers/CMakeLists.txt)
include(Options/CMakeLists.txt)

csharp_set_designer_cs_properties(
.nuget/Intel.RealSense.targets
Expand Down
2 changes: 1 addition & 1 deletion wrappers/csharp/Intel.RealSense/Devices/AdvancedDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static AdvancedDevice FromDevice(Device dev)
/// <summary>
/// Gets or sets a value indicating whether Advanced-Mode is enabled
/// </summary>
/// <value>true when Advanced-Mode is enabled</value>
/// <value><see langword="true"/> when Advanced-Mode is enabled</value>
public bool AdvancedModeEnabled
{
get
Expand Down
74 changes: 12 additions & 62 deletions wrappers/csharp/Intel.RealSense/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Intel.RealSense
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;

/// <summary>
Expand All @@ -15,7 +16,7 @@ public class Device : Base.PooledObject
{
internal override void Initialize()
{
Info = new CameraInfos(this);
Info = new InfoCollection(NativeMethods.rs2_supports_device_info, NativeMethods.rs2_get_device_info, Handle);
}

internal Device(IntPtr ptr)
Expand All @@ -40,87 +41,36 @@ internal static T Create<T>(IntPtr ptr, Base.Deleter deleter)
where T : Device
{
var dev = ObjectPool.Get<T>(ptr);
dev.m_instance.Reset(ptr, deleter);
dev.Reset(ptr, deleter);
return dev;
}

public class CameraInfos : IEnumerable<KeyValuePair<CameraInfo, string>>
{
private readonly Device device;

internal CameraInfos(Device device)
{
this.device = device;
}

/// <summary>
/// Retrieve camera specific information, like versions of various internal components.
/// </summary>
/// <param name="info">Camera info type to retrieve</param>
/// <returns>The requested camera info string, in a format specific to the device model</returns>
public string this[CameraInfo info]
{
get
{
object err;
if (NativeMethods.rs2_supports_device_info(device.Handle, info, out err) > 0)
{
return Marshal.PtrToStringAnsi(NativeMethods.rs2_get_device_info(device.Handle, info, out err));
}

return null;
}
}

private static readonly CameraInfo[] CameraInfoValues = Enum.GetValues(typeof(CameraInfo)) as CameraInfo[];

/// <inheritdoc/>
public IEnumerator<KeyValuePair<CameraInfo, string>> GetEnumerator()
{
object error;
foreach (var key in CameraInfoValues) {
if (NativeMethods.rs2_supports_device_info(device.Handle, key, out error) > 0)
{
var value = Marshal.PtrToStringAnsi(NativeMethods.rs2_get_device_info(device.Handle, key, out error));
yield return new KeyValuePair<CameraInfo, string>(key, value);
}
}
}

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

/// <summary>
/// Gets camera specific information, like versions of various internal components
/// </summary>
public CameraInfos Info { get; private set; }
public InfoCollection Info { get; private set; }

/// <summary>
/// create a static snapshot of all connected devices at the time of the call
/// </summary>
/// <returns>The list of sensors</returns>
public SensorList QuerySensors()
public ReadOnlyCollection<Sensor> QuerySensors()
{
object error;
var ptr = NativeMethods.rs2_query_sensors(Handle, out error);
return new SensorList(ptr);
using (var sl = new SensorList(ptr))
{
var a = new Sensor[sl.Count];
sl.CopyTo(a, 0);
return Array.AsReadOnly(a);
}
}

/// <summary>
/// Gets a static snapshot of all connected devices at the time of the call
/// </summary>
/// <value>The list of sensors</value>
public SensorList Sensors
{
get
{
return QuerySensors();
}
}
public ReadOnlyCollection<Sensor> Sensors => QuerySensors();

/// <summary>
/// Send hardware reset request to the device. The actual reset is asynchronous.
Expand Down
37 changes: 35 additions & 2 deletions wrappers/csharp/Intel.RealSense/Devices/DeviceList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,45 @@ namespace Intel.RealSense
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;

[DebuggerTypeProxy(typeof(DeviceListDebugView))]
[DebuggerDisplay("Count = {Count}")]
public class DeviceList : Base.Object, IEnumerable<Device>
{
internal static readonly Base.Deleter DeviceDeleter = NativeMethods.rs2_delete_device;

internal sealed class DeviceListDebugView
{
private readonly DeviceList dl;

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public Device[] Items
{
get
{
Device[] array = new Device[dl.Count];
for (int i = 0; i < dl.Count; i++)
{
array.SetValue(dl[i], i);
}

return array;
}
}

public DeviceListDebugView(DeviceList optionList)
{
if (optionList == null)
{
throw new ArgumentNullException(nameof(optionList));
}

dl = optionList;
}
}

/// <summary>
/// Initializes a new instance of the <see cref="DeviceList"/> class.
/// </summary>
Expand All @@ -33,7 +66,7 @@ public IEnumerator<Device> GetEnumerator()
for (int i = 0; i < deviceCount; i++)
{
var ptr = NativeMethods.rs2_create_device(Handle, i, out error);
yield return Device.Create<Device>(ptr, DeviceDeleter);
yield return Device.Create<Device>(ptr, NativeMethods.rs2_delete_device);
}
}

Expand Down Expand Up @@ -64,7 +97,7 @@ public Device this[int index]
{
object error;
var ptr = NativeMethods.rs2_create_device(Handle, index, out error);
return Device.Create<Device>(ptr, DeviceDeleter);
return Device.Create<Device>(ptr, NativeMethods.rs2_delete_device);
}
}

Expand Down
32 changes: 27 additions & 5 deletions wrappers/csharp/Intel.RealSense/Frames/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ namespace Intel.RealSense
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;

/// <summary>
/// Base class for multiple frame extensions
/// </summary>
public class Frame : Base.PooledObject
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly Base.Deleter FrameReleaser = NativeMethods.rs2_release_frame;

internal override void Initialize()
{
}

internal Frame(IntPtr ptr)
: base(ptr, NativeMethods.rs2_release_frame)
: base(ptr, FrameReleaser)
{
}

Expand Down Expand Up @@ -57,7 +62,7 @@ public static T Create<T>(Frame other)

/// <summary>Test if the given frame can be extended to the requested extension</summary>
/// <param name="extension">The extension to which the frame should be tested if it is extendable</param>
/// <returns>true iff the frame can be extended to the given extension</returns>
/// <returns><see langword="true"/> iff the frame can be extended to the given extension</returns>
public bool Is(Extension extension)
{
object error;
Expand All @@ -73,7 +78,7 @@ public T As<T>()
return Create<T>(this);
}

/// <summary>Returns a strongly-typed clone, <c>this</c> is disposed</summary>
/// <summary>Returns a strongly-typed clone, <see langword="this"/> is disposed</summary>
/// <typeparam name="T"><see cref="Frame"/> type or subclass</typeparam>
/// <returns>an instance of <typeparamref name="T"/></returns>
public T Cast<T>()
Expand Down Expand Up @@ -111,9 +116,11 @@ public void Keep()

/// <summary>
/// Gets a value indicating whether frame is a composite frame
/// <para>Shorthand for <c>Is(Extension.CompositeFrame)</c></para>
/// <para>Shorthand for <c>Is(<see cref="Extension.CompositeFrame"/>)</c></para>
/// </summary>
/// <value>true if frame is a composite frame and false otherwise</value>
/// <seealso cref="Is(Extension)"/>
/// <value><see langword="true"/> if frame is a composite frame and false otherwise</value>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public bool IsComposite
{
get
Expand Down Expand Up @@ -149,6 +156,7 @@ public T GetProfile<T>()
/// <summary>
/// Gets the stream profile that was used to start the stream of this frame
/// </summary>
/// <see cref="GetProfile{T}"/>
public StreamProfile Profile => GetProfile<StreamProfile>();

/// <summary>Gets the frame number of the frame</summary>
Expand Down Expand Up @@ -213,5 +221,19 @@ public bool SupportsFrameMetaData(FrameMetadataValue frame_metadata)
object error;
return NativeMethods.rs2_supports_frame_metadata(Handle, frame_metadata, out error) != 0;
}

#if DEBUGGER_METADATA
private static readonly FrameMetadataValue[] MetadataValues = Enum.GetValues(typeof(FrameMetadataValue)) as FrameMetadataValue[];
public ICollection<KeyValuePair<FrameMetadataValue, long>> MetaData
{
get
{
return MetadataValues
.Where(m => SupportsFrameMetaData(m))
.Select(m => new KeyValuePair<FrameMetadataValue, long>(m, GetFrameMetadata(m)))
.ToArray();
}
}
#endif
}
}
Loading

0 comments on commit 4b7b6c9

Please sign in to comment.