Skip to content

Commit

Permalink
SensorList back to non-generic
Browse files Browse the repository at this point in the history
  • Loading branch information
ogoshen committed Jun 12, 2019
1 parent 25b8f34 commit a672297
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 23 deletions.
8 changes: 4 additions & 4 deletions wrappers/csharp/Intel.RealSense/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ internal static T Create<T>(IntPtr ptr, Base.Deleter deleter)
/// create a static snapshot of all connected devices at the time of the call
/// </summary>
/// <returns>The list of sensors</returns>
public ReadOnlyCollection<T> QuerySensors<T>() where T: Sensor
public ReadOnlyCollection<Sensor> QuerySensors()
{
object error;
var ptr = NativeMethods.rs2_query_sensors(Handle, out error);
using (var sl = new SensorList<T>(ptr))
using (var sl = new SensorList(ptr))
{
var a = new T[sl.Count];
var a = new Sensor[sl.Count];
sl.CopyTo(a, 0);
return Array.AsReadOnly(a);
}
Expand All @@ -70,7 +70,7 @@ public ReadOnlyCollection<T> QuerySensors<T>() where T: Sensor
/// Gets a static snapshot of all connected devices at the time of the call
/// </summary>
/// <value>The list of sensors</value>
public ReadOnlyCollection<Sensor> Sensors => QuerySensors<Sensor>();
public ReadOnlyCollection<Sensor> Sensors => QuerySensors();

/// <summary>
/// Send hardware reset request to the device. The actual reset is asynchronous.
Expand Down
4 changes: 2 additions & 2 deletions wrappers/csharp/Intel.RealSense/Frames/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ public double Timestamp

/// <summary>Gets the sensor owning the frame</summary>
/// <value>the pointer to the sensor owning the frame</value>
public IntPtr Sensor
public Sensor Sensor
{
get
{
object error;
return NativeMethods.rs2_get_frame_sensor(Handle, out error);
return Sensor.Create<Sensor>(NativeMethods.rs2_get_frame_sensor(Handle, out error));
}
}

Expand Down
23 changes: 13 additions & 10 deletions wrappers/csharp/Intel.RealSense/Sensors/PoseSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ public byte[] ExportLocalizationMap()

public bool ImportLocalizationMap(byte[] mapBytes)
{
IntPtr nativeBytes = Marshal.AllocHGlobal(mapBytes.Length);
Marshal.Copy(mapBytes, 0, nativeBytes, mapBytes.Length);

object error;
var res = NativeMethods.rs2_import_localization_map(Handle, nativeBytes, (uint)mapBytes.Length, out error);

Marshal.FreeHGlobal(nativeBytes);
nativeBytes = IntPtr.Zero;

return !(res == 0);
IntPtr nativeBytes = IntPtr.Zero;
try
{
nativeBytes = Marshal.AllocHGlobal(mapBytes.Length);
Marshal.Copy(mapBytes, 0, nativeBytes, mapBytes.Length);
object error;
var res = NativeMethods.rs2_import_localization_map(Handle, nativeBytes, (uint)mapBytes.Length, out error);
return !(res == 0);
}
finally
{
Marshal.FreeHGlobal(nativeBytes);
}
}

public bool SetStaticNode(string guid, Math.Vector position, Math.Quaternion rotation)
Expand Down
20 changes: 20 additions & 0 deletions wrappers/csharp/Intel.RealSense/Sensors/Sensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ internal static T Create<T>(IntPtr ptr)
return ObjectPool.Get<T>(ptr);
}

/// <summary>Returns a strongly-typed clone</summary>
/// <typeparam name="T"><see cref="Sensor"/> type or subclass</typeparam>
/// <param name="other"><see cref="Sensor"/> to clone</param>
/// <returns>an instance of <typeparamref name="T"/></returns>
public static T Create<T>(Sensor other)
where T : Frame
{
object error;
return ObjectPool.Get<T>(other.Handle);
}

internal Sensor(IntPtr sensor)
: base(sensor, NativeMethods.rs2_delete_sensor)
{
Expand Down Expand Up @@ -183,6 +194,15 @@ public bool Is(Extension ext)
return NativeMethods.rs2_is_sensor_extendable_to(Handle, ext, out error) != 0;
}

/// <summary>Returns a strongly-typed clone</summary>
/// <typeparam name="T"><see cref="Sensor"/> type or subclass</typeparam>
/// <returns>an instance of <typeparamref name="T"/></returns>
public T As<T>()
where T : Frame
{
return Create<T>(this);
}

/// <summary>
/// Gets the mapping between the units of the depth image and meters
/// </summary>
Expand Down
8 changes: 4 additions & 4 deletions wrappers/csharp/Intel.RealSense/Sensors/SensorList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ namespace Intel.RealSense
/// <summary>
/// List of adjacent devices, sharing the same physical parent composite device
/// </summary>
internal sealed class SensorList<T> : Base.Object, IEnumerable<T>, ICollection where T : Sensor
internal sealed class SensorList : Base.Object, IEnumerable<Sensor>, ICollection
{
internal SensorList(IntPtr ptr)
: base(ptr, NativeMethods.rs2_delete_sensor_list)
{
}

/// <inheritdoc/>
public IEnumerator<T> GetEnumerator()
public IEnumerator<Sensor> GetEnumerator()
{
object error;

int sensorCount = NativeMethods.rs2_get_sensors_count(Handle, out error);
for (int i = 0; i < sensorCount; i++)
{
var ptr = NativeMethods.rs2_create_sensor(Handle, i, out error);
yield return Sensor.Create<T>(ptr);
yield return Sensor.Create<Sensor>(ptr);
}
}

Expand Down Expand Up @@ -80,7 +80,7 @@ public Sensor this[int index]
{
object error;
var ptr = NativeMethods.rs2_create_sensor(Handle, index, out error);
return Sensor.Create<T>(ptr);
return Sensor.Create<Sensor>(ptr);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion wrappers/csharp/cs-tutorial-1-depth/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void Main(string[] args)
Console.WriteLine(" Serial number: {0}", dev.Info[CameraInfo.SerialNumber]);
Console.WriteLine(" Firmware version: {0}", dev.Info[CameraInfo.FirmwareVersion]);

var depthSensor = dev.QuerySensors<Sensor>()[0];
var depthSensor = dev.QuerySensors()[0];

var sp = depthSensor.StreamProfiles
.Where(p => p.Stream == Stream.Depth)
Expand Down
2 changes: 1 addition & 1 deletion wrappers/csharp/cs-tutorial-2-capture/Window.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public CaptureWindow()

Dispatcher.Invoke(new Action(() =>
{
String depth_dev_sn = new Sensor.CameraInfos(depthFrame.Sensor)[CameraInfo.SerialNumber];
String depth_dev_sn = depthFrame.Sensor.Info[CameraInfo.SerialNumber];
txtTimeStamp.Text = depth_dev_sn + " : " + String.Format("{0,-20:0.00}", depthFrame.Timestamp) + "(" + depthFrame.TimestampDomain.ToString() + ")";
}));
}
Expand Down
2 changes: 1 addition & 1 deletion wrappers/csharp/cs-tutorial-3-processing/Window.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ProcessingWindow()
var pp = pipeline.Start(cfg);

// Get the recommended processing blocks for the depth sensor
var sensor = pp.Device.QuerySensors<Sensor>().First(s => s.Is(Extension.DepthSensor));
var sensor = pp.Device.QuerySensors().First(s => s.Is(Extension.DepthSensor));
var blocks = sensor.ProcessingBlocks.ToList();

// Allocate bitmaps for rendring.
Expand Down

0 comments on commit a672297

Please sign in to comment.