-
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): implement FloatArrayPacket
- Loading branch information
Showing
7 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
Assets/MediaPipe/SDK/Scripts/Framework/Packet/FloatArrayPacket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
|
||
namespace Mediapipe { | ||
public class FloatArrayPacket : Packet<float[]> { | ||
int _Length = -1; | ||
|
||
public int Length { | ||
get { return _Length; } | ||
set { | ||
if (_Length >= 0) { | ||
throw new InvalidOperationException("Length is already set and cannot be changed"); | ||
} | ||
|
||
_Length = value; | ||
} | ||
} | ||
|
||
public FloatArrayPacket() : base() {} | ||
|
||
public FloatArrayPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) {} | ||
|
||
public FloatArrayPacket(float[] value) : base() { | ||
UnsafeNativeMethods.mp__MakeFloatArrayPacket__Pf_i(value, value.Length, out var ptr).Assert(); | ||
this.ptr = ptr; | ||
Length = value.Length; | ||
} | ||
|
||
public FloatArrayPacket(float[] value, Timestamp timestamp) : base() { | ||
UnsafeNativeMethods.mp__MakeFloatArrayPacket_At__Pf_i_Rtimestamp(value, value.Length, timestamp.mpPtr, out var ptr).Assert(); | ||
GC.KeepAlive(timestamp); | ||
this.ptr = ptr; | ||
Length = value.Length; | ||
} | ||
|
||
public override float[] Get() { | ||
if (Length < 0) { | ||
throw new InvalidOperationException("The array's length is unknown, set Length first"); | ||
} | ||
|
||
var result = new float[Length]; | ||
|
||
unsafe { | ||
float* src = (float*)GetArrayPtr(); | ||
|
||
for (var i = 0; i < result.Length; i++) { | ||
result[i] = *src++; | ||
} | ||
} | ||
|
||
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(); | ||
} | ||
|
||
public override Status ValidateAsType() { | ||
UnsafeNativeMethods.mp_Packet__ValidateAsFloatArray(mpPtr, out var statusPtr).Assert(); | ||
|
||
GC.KeepAlive(this); | ||
return new Status(statusPtr); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/MediaPipe/SDK/Scripts/Framework/Packet/FloatArrayPacket.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
Assets/MediaPipe/SDK/Tests/EditMode/Framework/Packet/FloatArrayPacketTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Mediapipe; | ||
using NUnit.Framework; | ||
using System; | ||
|
||
namespace Tests { | ||
public class FloatArrayPacketTest { | ||
#region Constructor | ||
[Test, SignalAbort] | ||
public void Ctor_ShouldInstantiatePacket_When_CalledWithNoArguments() { | ||
var packet = new FloatArrayPacket(); | ||
packet.Length = 0; | ||
|
||
Assert.AreEqual(packet.ValidateAsType().code, Status.StatusCode.Internal); | ||
Assert.Throws<MediaPipeException>(() => { packet.Get(); }); | ||
Assert.AreEqual(packet.Timestamp(), Timestamp.Unset()); | ||
} | ||
|
||
[Test] | ||
public void Ctor_ShouldInstantiatePacket_When_CalledWithEmptyArray() { | ||
float[] array = {}; | ||
var packet = new FloatArrayPacket(array); | ||
|
||
Assert.True(packet.ValidateAsType().ok); | ||
Assert.AreEqual(packet.Get(), array); | ||
Assert.AreEqual(packet.Timestamp(), Timestamp.Unset()); | ||
} | ||
|
||
[Test] | ||
public void Ctor_ShouldInstantiatePacket_When_CalledWithArray() { | ||
float[] array = { 0.01f }; | ||
var packet = new FloatArrayPacket(array); | ||
|
||
Assert.True(packet.ValidateAsType().ok); | ||
Assert.AreEqual(packet.Get(), array); | ||
Assert.AreEqual(packet.Timestamp(), Timestamp.Unset()); | ||
} | ||
|
||
[Test] | ||
public void Ctor_ShouldInstantiatePacket_When_CalledWithValueAndTimestamp() { | ||
var timestamp = new Timestamp(1); | ||
float[] array = { 0.01f, 0.02f }; | ||
var packet = new FloatArrayPacket(array, timestamp); | ||
|
||
Assert.True(packet.ValidateAsType().ok); | ||
Assert.AreEqual(packet.Get(), array); | ||
Assert.AreEqual(packet.Timestamp(), timestamp); | ||
} | ||
#endregion | ||
|
||
#region #isDisposed | ||
[Test] | ||
public void isDisposed_ShouldReturnFalse_When_NotDisposedYet() { | ||
var packet = new FloatArrayPacket(); | ||
|
||
Assert.False(packet.isDisposed); | ||
} | ||
|
||
[Test] | ||
public void isDisposed_ShouldReturnTrue_When_AlreadyDisposed() { | ||
var packet = new FloatArrayPacket(); | ||
packet.Dispose(); | ||
|
||
Assert.True(packet.isDisposed); | ||
} | ||
#endregion | ||
|
||
#region #Consume | ||
[Test] | ||
public void Consume_ShouldThrowNotSupportedException() { | ||
var packet = new FloatArrayPacket(); | ||
|
||
Assert.Throws<NotSupportedException>(() => { packet.Consume(); }); | ||
} | ||
#endregion | ||
|
||
#region #DebugTypeName | ||
[Test] | ||
public void DebugTypeName_ShouldReturnFloat_When_ValueIsSet() { | ||
float[] array = { 0.01f }; | ||
var packet = new FloatArrayPacket(array); | ||
|
||
Assert.AreEqual(packet.DebugTypeName(), "float []"); | ||
} | ||
#endregion | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/MediaPipe/SDK/Tests/EditMode/Framework/Packet/FloatArrayPacketTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters