Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET Development #5170

Merged
merged 6 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -194,12 +194,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
8 changes: 4 additions & 4 deletions wrappers/csharp/Intel.RealSense/Frames/Points.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public IntPtr VertexData
}
}

private void Copy<T>(IntPtr src, IntPtr dst)
private void Copy<T>(IntPtr src, IntPtr dst, int size)
{
Debug.Assert(src != IntPtr.Zero);
Debug.Assert(dst != IntPtr.Zero);
NativeMethods.Memcpy(dst, src, Count * Marshal.SizeOf(typeof(T)));
NativeMethods.Memcpy(dst, src, size);
}

/// <summary>
Expand All @@ -66,7 +66,7 @@ public void CopyVertices<T>(T[] vertices)
var handle = GCHandle.Alloc(vertices, GCHandleType.Pinned);
try
{
Copy<T>(VertexData, handle.AddrOfPinnedObject());
Copy<T>(VertexData, handle.AddrOfPinnedObject(), Count * 3 * sizeof(float));
}
finally
{
Expand Down Expand Up @@ -105,7 +105,7 @@ public void CopyTextureCoords<T>(T[] textureArray)
var handle = GCHandle.Alloc(textureArray, GCHandleType.Pinned);
try
{
Copy<T>(TextureData, handle.AddrOfPinnedObject());
Copy<T>(TextureData, handle.AddrOfPinnedObject(), Count * 2 * sizeof(float));
}
finally
{
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be where T : Sensor?

{
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where T : Sensor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks!

{
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