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

feat: add FloatVectorPacket and MatrixPacket #767

Merged
merged 7 commits into from
Oct 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,23 @@ public override float[] Get()
}

var result = new float[length];
UnsafeNativeMethods.mp_Packet__GetFloatArray_i(mpPtr, length, out var arrayPtr).Assert();
GC.KeepAlive(this);

unsafe
{
var src = (float*)GetArrayPtr();
var src = (float*)arrayPtr;

for (var i = 0; i < result.Length; i++)
{
result[i] = *src++;
}
}

UnsafeNativeMethods.delete_array__Pf(arrayPtr);
return result;
}

public IntPtr GetArrayPtr()
{
UnsafeNativeMethods.mp_Packet__GetFloatArray(mpPtr, out var value).Assert();
GC.KeepAlive(this);
return value;
}

public override StatusOr<float[]> Consume()
{
throw new NotSupportedException();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace Mediapipe
{
[StructLayout(LayoutKind.Sequential)]
internal readonly struct FloatVector
{
private readonly IntPtr _data;
private readonly int _size;

public void Dispose()
{
UnsafeNativeMethods.delete_array__Pf(_data);
}

public List<float> Copy()
{
var data = new List<float>(_size);

unsafe
{
var floatPtr = (float*)_data;

for (var i = 0; i < _size; i++)
{
data.Add(*floatPtr++);
}
}
return data;
}
}

public class FloatVectorPacket : Packet<List<float>>
{
/// <summary>
/// Creates an empty <see cref="FloatVectorPacket" /> instance.
/// </summary>
///
public FloatVectorPacket() : base(true) { }

[UnityEngine.Scripting.Preserve]
public FloatVectorPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }


public FloatVectorPacket(float[] value) : base()
{
UnsafeNativeMethods.mp__MakeFloatVectorPacket__Pf_i(value, value.Length, out var ptr).Assert();
this.ptr = ptr;
}

public FloatVectorPacket(float[] value, Timestamp timestamp) : base()
{
UnsafeNativeMethods.mp__MakeFloatVectorPacket_At__Pf_i_Rt(value, value.Length, timestamp.mpPtr, out var ptr).Assert();
GC.KeepAlive(timestamp);
this.ptr = ptr;
}

public FloatVectorPacket(List<float> value) : base()
{
UnsafeNativeMethods.mp__MakeFloatVectorPacket__Pf_i(value.ToArray(), value.Count, out var ptr).Assert();
this.ptr = ptr;
}

public FloatVectorPacket(List<float> value, Timestamp timestamp) : base()
{
UnsafeNativeMethods.mp__MakeFloatVectorPacket_At__Pf_i_Rt(value.ToArray(), value.Count, timestamp.mpPtr, out var ptr).Assert();
GC.KeepAlive(timestamp);
this.ptr = ptr;
}

public FloatVectorPacket At(Timestamp timestamp)
{
return At<FloatVectorPacket>(timestamp);
}

public override List<float> Get()
{
UnsafeNativeMethods.mp_Packet__GetFloatVector(mpPtr, out var floatVector).Assert();
GC.KeepAlive(this);

var result = floatVector.Copy();
floatVector.Dispose();
return result;
}

public override StatusOr<List<float>> Consume()
{
throw new NotSupportedException();
}

public override Status ValidateAsType()
{
UnsafeNativeMethods.mp_Packet__ValidateAsFloatVector(mpPtr, out var statusPtr).Assert();

GC.KeepAlive(this);
return new Status(statusPtr);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

using Google.Protobuf;
using System;

namespace Mediapipe
{
public class MatrixPacket : Packet<MatrixData>
{
/// <summary>
/// Creates an empty <see cref="MatrixPacket" /> instance.
/// </summary>
public MatrixPacket() : base(true) { }

[UnityEngine.Scripting.Preserve]
public MatrixPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }

public MatrixPacket(MatrixData matrixData) : base()
{
var value = matrixData.ToByteArray();
UnsafeNativeMethods.mp__MakeMatrixPacket__PKc_i(value, value.Length, out var ptr).Assert();
this.ptr = ptr;
}

public MatrixPacket(MatrixData matrixData, Timestamp timestamp) : base()
{
var value = matrixData.ToByteArray();
UnsafeNativeMethods.mp__MakeMatrixPacket_At__PKc_i_Rt(value, value.Length, timestamp.mpPtr, out var ptr).Assert();
GC.KeepAlive(timestamp);
this.ptr = ptr;
}

public MatrixPacket At(Timestamp timestamp)
{
return At<MatrixPacket>(timestamp);
}

public override MatrixData Get()
{
UnsafeNativeMethods.mp_Packet__GetMatrix(mpPtr, out var serializedMatrixData).Assert();
GC.KeepAlive(this);

var matrixData = serializedMatrixData.Deserialize(MatrixData.Parser);
serializedMatrixData.Dispose();

return matrixData;
}

public override StatusOr<MatrixData> Consume()
{
throw new NotSupportedException();
}

public override Status ValidateAsType()
{
UnsafeNativeMethods.mp_Packet__ValidateAsMatrix(mpPtr, out var statusPtr).Assert();

GC.KeepAlive(this);
return new Status(statusPtr);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ internal static partial class UnsafeNativeMethods
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern void delete_array__PKc(IntPtr str);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern void delete_array__Pf(IntPtr str);

#region String
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern void std_string__delete(IntPtr str);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

using System;
using System.Runtime.InteropServices;

namespace Mediapipe
{
internal static partial class UnsafeNativeMethods
{
#region Packet
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeMatrixPacket__PKc_i(byte[] serializedMatrixData, int size, out IntPtr packet_out);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeMatrixPacket_At__PKc_i_Rt(byte[] serializedMatrixData, int size, IntPtr timestamp, out IntPtr packet_out);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__ValidateAsMatrix(IntPtr packet, out IntPtr status);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetMatrix(IntPtr packet, out SerializedProto serializedProto);

#endregion
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,26 @@ internal static partial class UnsafeNativeMethods
public static extern MpReturnCode mp__MakeFloatArrayPacket_At__Pf_i_Rt(float[] value, int size, IntPtr timestamp, out IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetFloatArray(IntPtr packet, out IntPtr value);
public static extern MpReturnCode mp_Packet__GetFloatArray_i(IntPtr packet, int size, out IntPtr value);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__ValidateAsFloatArray(IntPtr packet, out IntPtr status);
#endregion

#region FloatVector
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeFloatVectorPacket__Pf_i(float[] value, int size, out IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeFloatVectorPacket_At__Pf_i_Rt(float[] value, int size, IntPtr timestamp, out IntPtr packet);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetFloatVector(IntPtr packet, out FloatVector value);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__ValidateAsFloatVector(IntPtr packet, out IntPtr status);
#endregion

#region String
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeStringPacket__PKc(string value, out IntPtr packet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ public void Consume_ShouldThrowNotSupportedException()
}
#endregion

#region #DebugTypeName
#region #ValidateAsType
[Test]
public void DebugTypeName_ShouldReturnBool_When_ValueIsSet()
public void ValidateAsType_ShouldReturnOk_When_ValueIsSet()
{
using (var packet = new BoolPacket(true))
{
Assert.AreEqual("bool", packet.DebugTypeName());
Assert.True(packet.ValidateAsType().Ok());
}
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,14 @@ public void Consume_ShouldThrowNotSupportedException()
}
#endregion

#region #DebugTypeName
#region #ValidateAsType
[Test]
public void DebugTypeName_ShouldReturnFloat_When_ValueIsSet()
public void ValidateAsType_ShouldReturnOk_When_ValueIsSet()
{
float[] array = { 0.01f };
using (var packet = new FloatArrayPacket(array))
{
#if UNITY_EDITOR_WIN
Assert.AreEqual("float [0]", packet.DebugTypeName());
#else
Assert.AreEqual("float []", packet.DebugTypeName());
#endif
Assert.True(packet.ValidateAsType().Ok());
}
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public void Consume_ShouldThrowNotSupportedException()
}
#endregion

#region #DebugTypeName
#region #ValidateAsType
[Test]
public void DebugTypeName_ShouldReturnFloat_When_ValueIsSet()
public void ValidateAsType_ShouldReturnOk_When_ValueIsSet()
{
using (var packet = new FloatPacket(0.01f))
{
Assert.AreEqual("float", packet.DebugTypeName());
Assert.True(packet.ValidateAsType().Ok());
}
}
#endregion
Expand Down
Loading