Skip to content

Commit

Permalink
feat: initialize RectPacket from proto (#970)
Browse files Browse the repository at this point in the history
* feat: initialize RectPacket from proto

* implement RectPacket.ValidateAsType

* test: add RectPacket tests
  • Loading branch information
homuler authored Jul 29, 2023
1 parent c827fb1 commit d8593f7
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) 2021 homuler
// Copyright (c) 2023 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 Google.Protobuf;

namespace Mediapipe
{
Expand All @@ -18,11 +19,22 @@ public NormalizedRectPacket() : base(true) { }
[UnityEngine.Scripting.Preserve]
public NormalizedRectPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }

public NormalizedRectPacket At(Timestamp timestamp)
public NormalizedRectPacket(NormalizedRect value) : base()
{
return At<NormalizedRectPacket>(timestamp);
var bytes = value.ToByteArray();
UnsafeNativeMethods.mp__MakeNormalizedRectPacket__PKc_i(bytes, bytes.Length, out var ptr).Assert();
this.ptr = ptr;
}

public NormalizedRectPacket(NormalizedRect value, Timestamp timestamp) : base()
{
var bytes = value.ToByteArray();
UnsafeNativeMethods.mp__MakeNormalizedRectPacket_At__PKc_i_Rt(bytes, bytes.Length, timestamp.mpPtr, out var ptr).Assert();
this.ptr = ptr;
}

public NormalizedRectPacket At(Timestamp timestamp) => At<NormalizedRectPacket>(timestamp);

public override NormalizedRect Get()
{
UnsafeNativeMethods.mp_Packet__GetNormalizedRect(mpPtr, out var serializedProto).Assert();
Expand All @@ -34,9 +46,14 @@ public override NormalizedRect Get()
return normalizedRect;
}

public override NormalizedRect Consume()
public override NormalizedRect Consume() => throw new NotSupportedException();

public override void ValidateAsType()
{
throw new NotSupportedException();
UnsafeNativeMethods.mp_Packet__ValidateAsNormalizedRect(mpPtr, out var statusPtr).Assert();

GC.KeepAlive(this);
AssertStatusOk(statusPtr);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) 2021 homuler
// Copyright (c) 2023 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 Google.Protobuf;

namespace Mediapipe
{
Expand All @@ -18,11 +19,22 @@ public RectPacket() : base(true) { }
[UnityEngine.Scripting.Preserve]
public RectPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }

public RectPacket At(Timestamp timestamp)
public RectPacket(Rect value) : base()
{
return At<RectPacket>(timestamp);
var bytes = value.ToByteArray();
UnsafeNativeMethods.mp__MakeRectPacket__PKc_i(bytes, bytes.Length, out var ptr).Assert();
this.ptr = ptr;
}

public RectPacket(Rect value, Timestamp timestamp) : base()
{
var bytes = value.ToByteArray();
UnsafeNativeMethods.mp__MakeRectPacket_At__PKc_i_Rt(bytes, bytes.Length, timestamp.mpPtr, out var ptr).Assert();
this.ptr = ptr;
}

public RectPacket At(Timestamp timestamp) => At<RectPacket>(timestamp);

public override Rect Get()
{
UnsafeNativeMethods.mp_Packet__GetRect(mpPtr, out var serializedProto).Assert();
Expand All @@ -34,9 +46,14 @@ public override Rect Get()
return rect;
}

public override Rect Consume()
public override Rect Consume() => throw new NotSupportedException();

public override void ValidateAsType()
{
throw new NotSupportedException();
UnsafeNativeMethods.mp_Packet__ValidateAsRect(mpPtr, out var statusPtr).Assert();

GC.KeepAlive(this);
AssertStatusOk(statusPtr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,6 @@ public static extern MpReturnCode google_protobuf__SetLogHandler__PF(

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetNormalizedLandmarkListVector(IntPtr packet, out SerializedProtoVector serializedProtoVector);

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

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetNormalizedRectVector(IntPtr packet, out SerializedProtoVector serializedProtoVector);

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

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetRectVector(IntPtr packet, out SerializedProtoVector serializedProtoVector);
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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
{
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeRectPacket__PKc_i(byte[] serializedData, int size, out IntPtr packet_out);

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

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

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetRectVector(IntPtr packet, out SerializedProtoVector serializedProtoVector);

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

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

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

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

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetNormalizedRectVector(IntPtr packet, out SerializedProtoVector serializedProtoVector);

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

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,131 @@
// 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 NUnit.Framework;
using System;

namespace Mediapipe.Tests
{
public class NormalizedRectPacketTest
{
#region Constructor
[Test, SignalAbort]
public void Ctor_ShouldInstantiatePacket_When_CalledWithNoArguments()
{
using (var packet = new NormalizedRectPacket())
{
var exception = Assert.Throws<BadStatusException>(packet.ValidateAsType);
Assert.AreEqual(StatusCode.Internal, exception.statusCode);
_ = Assert.Throws<MediaPipeException>(() => { _ = packet.Get(); });
Assert.AreEqual(Timestamp.Unset(), packet.Timestamp());
}

}

[Test]
public void Ctor_ShouldInstantiatePacket_When_CalledWithRect()
{
var rect = new NormalizedRect() { XCenter = 0, YCenter = 0, Width = 0.1f, Height = 0.2f };
using (var packet = new NormalizedRectPacket(rect))
{
Assert.DoesNotThrow(packet.ValidateAsType);
var value = packet.Get();
Assert.AreEqual(rect.Width, value.Width);
Assert.AreEqual(rect.Height, value.Height);
Assert.AreEqual(Timestamp.Unset(), packet.Timestamp());
}
}

[Test]
public void Ctor_ShouldInstantiatePacket_When_CalledWithValueAndTimestamp()
{
using (var timestamp = new Timestamp(1))
{
var rect = new NormalizedRect() { XCenter = 0, YCenter = 0, Width = 0.1f, Height = 0.2f };
using (var packet = new NormalizedRectPacket(rect, timestamp))
{
Assert.DoesNotThrow(packet.ValidateAsType);
var value = packet.Get();
Assert.AreEqual(rect.Width, value.Width);
Assert.AreEqual(rect.Height, value.Height);
Assert.AreEqual(timestamp, packet.Timestamp());
}
}
}
#endregion

#region #isDisposed
[Test]
public void IsDisposed_ShouldReturnFalse_When_NotDisposedYet()
{
using (var packet = new NormalizedRectPacket())
{
Assert.False(packet.isDisposed);
}
}

[Test]
public void IsDisposed_ShouldReturnTrue_When_AlreadyDisposed()
{
var packet = new NormalizedRectPacket();
packet.Dispose();

Assert.True(packet.isDisposed);
}
#endregion

#region #At
[Test]
public void At_ShouldReturnNewPacketWithTimestamp()
{
using (var timestamp = new Timestamp(1))
{
var rect = new NormalizedRect() { XCenter = 0, YCenter = 0, Width = 0.1f, Height = 0.2f };
var packet = new NormalizedRectPacket(rect).At(timestamp);

var value = packet.Get();
Assert.AreEqual(rect.Width, value.Width);
Assert.AreEqual(rect.Height, value.Height);
Assert.AreEqual(timestamp, packet.Timestamp());

using (var newTimestamp = new Timestamp(2))
{
var newPacket = packet.At(newTimestamp);
Assert.AreEqual(newTimestamp, newPacket.Timestamp());
}

Assert.AreEqual(timestamp, packet.Timestamp());
}
}
#endregion

#region #Consume
[Test]
public void Consume_ShouldThrowNotSupportedException()
{
var rect = new NormalizedRect() { XCenter = 0, YCenter = 0, Width = 0.1f, Height = 0.2f };
using (var packet = new NormalizedRectPacket(rect))
{
#pragma warning disable IDE0058
Assert.Throws<NotSupportedException>(() => { packet.Consume(); });
#pragma warning restore IDE0058
}
}
#endregion

#region #ValidateAsType
[Test]
public void ValidateAsType_ShouldNotThrow_When_ValueIsSet()
{
var rect = new NormalizedRect() { XCenter = 0, YCenter = 0, Width = 0.1f, Height = 0.2f };
using (var packet = new NormalizedRectPacket(rect))
{
Assert.DoesNotThrow(packet.ValidateAsType);
}
}
#endregion
}
}

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

Loading

0 comments on commit d8593f7

Please sign in to comment.