From c8c8a83cb3ef6f9478cc27013053a63cd4f6254e Mon Sep 17 00:00:00 2001 From: Junrou Nishida Date: Tue, 2 Jan 2024 17:30:31 +0900 Subject: [PATCH 1/3] feat: marshal NormalizedLandmarks directly --- .../Runtime/Scripts/Marshal.meta | 8 +++ .../Runtime/Scripts/Marshal/NativeLandmark.cs | 64 +++++++++++++++++++ .../Scripts/Marshal/NativeLandmark.cs.meta | 11 ++++ .../NativeMethods/Tasks/Components.meta | 8 +++ .../Tasks/Components/Containers_Unsafe.cs | 20 ++++++ .../Components/Containers_Unsafe.cs.meta | 11 ++++ .../Tasks/Components/Containers/Landmark.cs | 50 +++++++++++++++ .../Components/Containers/PacketExtension.cs | 20 ++++++ .../Containers/PacketExtension.cs.meta | 11 ++++ mediapipe_api/BUILD | 1 + .../tasks/c/components/containers/BUILD | 39 +++++++++++ .../tasks/c/components/containers/landmark.cc | 27 ++++++++ .../tasks/c/components/containers/landmark.h | 23 +++++++ third_party/mediapipe_visibility.diff | 13 ++++ 14 files changed, 306 insertions(+) create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal.meta create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs.meta create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components.meta create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components/Containers_Unsafe.cs create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components/Containers_Unsafe.cs.meta create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/PacketExtension.cs create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/PacketExtension.cs.meta create mode 100644 mediapipe_api/tasks/c/components/containers/BUILD create mode 100644 mediapipe_api/tasks/c/components/containers/landmark.cc create mode 100644 mediapipe_api/tasks/c/components/containers/landmark.h diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal.meta new file mode 100644 index 000000000..7c92c3e05 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 857399720e195f4628a82d8a221e52ab +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs new file mode 100644 index 000000000..85652d3f6 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs @@ -0,0 +1,64 @@ +// 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 System.Runtime.InteropServices; + +namespace Mediapipe +{ + [StructLayout(LayoutKind.Sequential)] + internal readonly struct NativeNormalizedLandmark + { + public readonly float x; + public readonly float y; + public readonly float z; + + [MarshalAs(UnmanagedType.I1)] + public readonly bool hasVisibility; + public readonly float visibility; + + [MarshalAs(UnmanagedType.I1)] + public readonly bool hasPresence; + public readonly float presence; + + public readonly IntPtr name; + }; + + [StructLayout(LayoutKind.Sequential)] + internal readonly struct NativeNormalizedLandmarks + { + private readonly IntPtr _landmarks; + public readonly uint landmarksCount; + + public ReadOnlySpan AsReadOnlySpan() + { + unsafe + { + return new ReadOnlySpan((NativeNormalizedLandmark*)_landmarks, (int)landmarksCount); + } + } + } + + [StructLayout(LayoutKind.Sequential)] + internal readonly struct NativeNormalizedLandmarksArray + { + private readonly IntPtr _data; + public readonly int size; + + public void Dispose() + { + UnsafeNativeMethods.mp_api_NormalizedLandmarksArray__delete(_data, size); + } + + public ReadOnlySpan AsReadOnlySpan() + { + unsafe + { + return new ReadOnlySpan((NativeNormalizedLandmarks*)_data, size); + } + } + } +} diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs.meta new file mode 100644 index 000000000..cfe3e7396 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9c228e45523ca5c96bec2e0a9370b553 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components.meta new file mode 100644 index 000000000..873df8168 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 599b40522090cba58b83840d9fc81099 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components/Containers_Unsafe.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components/Containers_Unsafe.cs new file mode 100644 index 000000000..7182d3bef --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components/Containers_Unsafe.cs @@ -0,0 +1,20 @@ +// 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 System.Runtime.InteropServices; + +namespace Mediapipe +{ + internal static partial class UnsafeNativeMethods + { + [DllImport(MediaPipeLibrary, ExactSpelling = true)] + public static extern MpReturnCode mp_Packet__GetNormalizedLandmarksVector(IntPtr packet, out NativeNormalizedLandmarksArray value); + + [DllImport(MediaPipeLibrary, ExactSpelling = true)] + public static extern void mp_api_NormalizedLandmarksArray__delete(IntPtr data, int size); + } +} diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components/Containers_Unsafe.cs.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components/Containers_Unsafe.cs.meta new file mode 100644 index 000000000..3eb637bc3 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Tasks/Components/Containers_Unsafe.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d5649c99f78b7832f8fcd92b16bfe672 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs index 6ca8ac2b2..41a04fbaa 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs @@ -6,6 +6,8 @@ using System; using System.Collections.Generic; +using System.Runtime.InteropServices; + // TODO: use System.MathF using Mathf = UnityEngine.Mathf; @@ -105,6 +107,15 @@ public override string ToString() public readonly float? presence; public readonly string name; + internal NormalizedLandmark(NativeNormalizedLandmark nativeLandmark) : this( + nativeLandmark.x, nativeLandmark.y, nativeLandmark.z, + nativeLandmark.hasVisibility ? nativeLandmark.visibility : null, + nativeLandmark.hasPresence ? nativeLandmark.presence : null, + Marshal.PtrToStringAnsi(nativeLandmark.name) + ) + { + } + internal NormalizedLandmark(float x, float y, float z, float? visibility, float? presence) : this(x, y, z, visibility, presence, null) { } @@ -197,6 +208,45 @@ public static NormalizedLandmarks CreateFrom(NormalizedLandmarkList proto) return new NormalizedLandmarks(landmarks); } + internal static void Copy(NativeNormalizedLandmarks source, ref NormalizedLandmarks destination) + { + var landmarks = destination.landmarks ?? new List((int)source.landmarksCount); + landmarks.Clear(); + + foreach (var nativeLandmark in source.AsReadOnlySpan()) + { + landmarks.Add(new NormalizedLandmark(nativeLandmark)); + } + destination = new NormalizedLandmarks(landmarks); + } + public override string ToString() => $"{{ \"landmarks\": {Util.Format(landmarks)} }}"; } + + internal static class NativeNormalizedLandmarksArrayExtension + { + public static void FillWith(this List target, NativeNormalizedLandmarksArray source) + { + if (target.Count > source.size) + { + target.RemoveRange(source.size, target.Count - source.size); + } + + var copyCount = Math.Min(source.size, target.Count); + var i = 0; + foreach (var nativeLandmarks in source.AsReadOnlySpan().Slice(0, target.Count)) + { + var landmarks = target[i]; + NormalizedLandmarks.Copy(nativeLandmarks, ref landmarks); + target[i++] = landmarks; + } + + foreach (var nativeLandmarks in source.AsReadOnlySpan().Slice(copyCount)) + { + var landmarks = default(NormalizedLandmarks); + NormalizedLandmarks.Copy(nativeLandmarks, ref landmarks); + target.Add(landmarks); + } + } + } } diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/PacketExtension.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/PacketExtension.cs new file mode 100644 index 000000000..e416634c5 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/PacketExtension.cs @@ -0,0 +1,20 @@ +// 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.Collections.Generic; + +namespace Mediapipe.Tasks.Components.Containers +{ + public static class PacketExtension + { + public static void GetNormalizedLandmarksList(this Packet packet, List outs) + { + UnsafeNativeMethods.mp_Packet__GetNormalizedLandmarksVector(packet.mpPtr, out var landmarksArray).Assert(); + outs.FillWith(landmarksArray); + landmarksArray.Dispose(); + } + } +} diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/PacketExtension.cs.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/PacketExtension.cs.meta new file mode 100644 index 000000000..bab017d07 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/PacketExtension.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 417f7667a40da771c9295f20dc7102b6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/mediapipe_api/BUILD b/mediapipe_api/BUILD index 2d41780e1..66f8af24d 100644 --- a/mediapipe_api/BUILD +++ b/mediapipe_api/BUILD @@ -164,6 +164,7 @@ cc_library( "//mediapipe_api/framework/formats:matrix_data", "//mediapipe_api/framework/formats:rect", "//mediapipe_api/framework/port:logging", + "//mediapipe_api/tasks/c/components/containers:landmark", "//mediapipe_api/tasks/cc/vision/face_geometry/proto:face_geometry", "//mediapipe_api/tasks/cc/core:task_runner", "//mediapipe_api/util:resource_util", diff --git a/mediapipe_api/tasks/c/components/containers/BUILD b/mediapipe_api/tasks/c/components/containers/BUILD new file mode 100644 index 000000000..de56a1f8d --- /dev/null +++ b/mediapipe_api/tasks/c/components/containers/BUILD @@ -0,0 +1,39 @@ +# 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. + +package( + default_visibility = ["//visibility:public"], +) + +cc_library( + name = "detection_result", + srcs = ["detection_result.cc"], + hdrs = ["detection_result.h"], + deps = [ + "//mediapipe_api:common", + "@com_google_mediapipe//mediapipe/framework:packet", + "@com_google_mediapipe//mediapipe/framework/formats:detection_cc_proto", + "@com_google_mediapipe//mediapipe/tasks/c/components/containers:detection_result", + "@com_google_mediapipe//mediapipe/tasks/c/components/containers:detection_result_converter", + "@com_google_mediapipe//mediapipe/tasks/cc/components/containers:detection_result", + ], + alwayslink = True, +) + +cc_library( + name = "landmark", + srcs = ["landmark.cc"], + hdrs = ["landmark.h"], + deps = [ + "//mediapipe_api:common", + "@com_google_mediapipe//mediapipe/framework:packet", + "@com_google_mediapipe//mediapipe/framework/formats:landmark_cc_proto", + "@com_google_mediapipe//mediapipe/tasks/c/components/containers:landmark", + "@com_google_mediapipe//mediapipe/tasks/c/components/containers:landmark_converter", + "@com_google_mediapipe//mediapipe/tasks/cc/components/containers:landmark", + ], + alwayslink = True, +) diff --git a/mediapipe_api/tasks/c/components/containers/landmark.cc b/mediapipe_api/tasks/c/components/containers/landmark.cc new file mode 100644 index 000000000..2195d1e59 --- /dev/null +++ b/mediapipe_api/tasks/c/components/containers/landmark.cc @@ -0,0 +1,27 @@ +#include "mediapipe_api/tasks/c/components/containers/landmark.h" + +MpReturnCode mp_Packet__GetNormalizedLandmarksVector(mediapipe::Packet* packet, mp_api::StructArray* value_out) { + TRY_ALL + // get std::vector and convert it to NormalizedLandmarks* + auto proto_vec = packet->Get>(); + auto vec_size = proto_vec.size(); + auto data = new NormalizedLandmarks[vec_size]; + + for (auto i = 0; i < vec_size; ++i) { + auto landmarks = mediapipe::tasks::components::containers::ConvertToNormalizedLandmarks(proto_vec[i]); + mediapipe::tasks::c::components::containers::CppConvertToNormalizedLandmarks(landmarks.landmarks, &data[i]); + } + + value_out->data = data; + value_out->size = static_cast(vec_size); + RETURN_CODE(MpReturnCode::Success); + CATCH_ALL +} + +void mp_api_NormalizedLandmarksArray__delete(NormalizedLandmarks* data, int size) { + auto landmarks = data; + for (auto i = 0; i < size; ++i) { + mediapipe::tasks::c::components::containers::CppCloseNormalizedLandmarks(landmarks++); + } + delete[] data; +} diff --git a/mediapipe_api/tasks/c/components/containers/landmark.h b/mediapipe_api/tasks/c/components/containers/landmark.h new file mode 100644 index 000000000..afd2de02d --- /dev/null +++ b/mediapipe_api/tasks/c/components/containers/landmark.h @@ -0,0 +1,23 @@ +// 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. + +#ifndef MEDIAPIPE_API_TASKS_C_COMPONENTS_CONTAINERS_LANDMARK_H_ +#define MEDIAPIPE_API_TASKS_C_COMPONENTS_CONTAINERS_LANDMARK_H_ + +#include "mediapipe/framework/packet.h" +#include "mediapipe/tasks/c/components/containers/landmark.h" +#include "mediapipe/tasks/c/components/containers/landmark_converter.h" +#include "mediapipe/tasks/cc/components/containers/landmark.h" +#include "mediapipe_api/common.h" + +extern "C" { + +MP_CAPI(MpReturnCode) mp_Packet__GetNormalizedLandmarksVector(mediapipe::Packet* packet, mp_api::StructArray* value_out); +MP_CAPI(void) mp_api_NormalizedLandmarksArray__delete(NormalizedLandmarks* data, int size); + +} // extern "C" + +#endif // MEDIAPIPE_API_TASKS_C_COMPONENTS_CONTAINERS_LANDMARK_H_ diff --git a/third_party/mediapipe_visibility.diff b/third_party/mediapipe_visibility.diff index bce8a0a97..ff9c5c062 100644 --- a/third_party/mediapipe_visibility.diff +++ b/third_party/mediapipe_visibility.diff @@ -333,6 +333,19 @@ index 05b25475..61266f80 100644 + srcs = glob(["*.proto"]), + visibility = ["//visibility:public"], +) +diff --git a/mediapipe/tasks/c/components/containers/BUILD b/mediapipe/tasks/c/components/containers/BUILD +index 3c4b557b..b8857b00 100644 +--- a/mediapipe/tasks/c/components/containers/BUILD ++++ b/mediapipe/tasks/c/components/containers/BUILD +@@ -12,7 +12,7 @@ + # See the License for the specific language governing permissions and + # limitations under the License. + +-package(default_visibility = ["//mediapipe/tasks:internal"]) ++package(default_visibility = ["//visibility:public"]) + + licenses(["notice"]) + diff --git a/mediapipe/tasks/cc/components/containers/proto/BUILD b/mediapipe/tasks/cc/components/containers/proto/BUILD index 66255aed..8dab4bbc 100644 --- a/mediapipe/tasks/cc/components/containers/proto/BUILD From cec6a3de55a97dd1ad4090eacf79cc96850a559a Mon Sep 17 00:00:00 2001 From: Junrou Nishida Date: Tue, 2 Jan 2024 17:38:16 +0900 Subject: [PATCH 2/3] add NativeLandmarks --- .../Scripts/Extension/ListExtension.cs | 27 ++++++++++++ .../Scripts/Extension/ListExtension.cs.meta | 11 +++++ .../Runtime/Scripts/Marshal/NativeLandmark.cs | 41 ++++++++++++++++++- .../Tasks/Components/Containers/Landmark.cs | 18 ++------ 4 files changed, 80 insertions(+), 17 deletions(-) create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ListExtension.cs create mode 100644 Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ListExtension.cs.meta diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ListExtension.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ListExtension.cs new file mode 100644 index 000000000..73ea84e76 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ListExtension.cs @@ -0,0 +1,27 @@ +// 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.Collections.Generic; + +namespace Mediapipe +{ + internal static class ListExtension + { + public static void ResizeTo(this List list, int size) + { + if (list.Count > size) + { + list.RemoveRange(size, list.Count - size); + } + + var count = size - list.Count; + for (var i = 0; i < count; i++) + { + list.Add(default); + } + } + } +} diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ListExtension.cs.meta b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ListExtension.cs.meta new file mode 100644 index 000000000..924d3b002 --- /dev/null +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Extension/ListExtension.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c0ef92785d38ebed3a54e928d4edae39 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs index 85652d3f6..b115df726 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeLandmark.cs @@ -9,6 +9,26 @@ namespace Mediapipe { + [StructLayout(LayoutKind.Sequential)] + internal readonly struct NativeLandmark + { + public readonly float x; + public readonly float y; + public readonly float z; + + [MarshalAs(UnmanagedType.I1)] + public readonly bool hasVisibility; + public readonly float visibility; + + [MarshalAs(UnmanagedType.I1)] + public readonly bool hasPresence; + public readonly float presence; + + private readonly IntPtr _name; + + public string name => Marshal.PtrToStringAnsi(_name); + } + [StructLayout(LayoutKind.Sequential)] internal readonly struct NativeNormalizedLandmark { @@ -24,8 +44,25 @@ internal readonly struct NativeNormalizedLandmark public readonly bool hasPresence; public readonly float presence; - public readonly IntPtr name; - }; + private readonly IntPtr _name; + + public string name => Marshal.PtrToStringAnsi(_name); + } + + [StructLayout(LayoutKind.Sequential)] + internal readonly struct NativeLandmarks + { + private readonly IntPtr _landmarks; + public readonly uint landmarksCount; + + public ReadOnlySpan AsReadOnlySpan() + { + unsafe + { + return new ReadOnlySpan((NativeLandmark*)_landmarks, (int)landmarksCount); + } + } + } [StructLayout(LayoutKind.Sequential)] internal readonly struct NativeNormalizedLandmarks diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs index 41a04fbaa..027011143 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs @@ -8,7 +8,6 @@ using System.Collections.Generic; using System.Runtime.InteropServices; - // TODO: use System.MathF using Mathf = UnityEngine.Mathf; @@ -227,25 +226,14 @@ internal static class NativeNormalizedLandmarksArrayExtension { public static void FillWith(this List target, NativeNormalizedLandmarksArray source) { - if (target.Count > source.size) - { - target.RemoveRange(source.size, target.Count - source.size); - } + target.ResizeTo(source.size); - var copyCount = Math.Min(source.size, target.Count); var i = 0; - foreach (var nativeLandmarks in source.AsReadOnlySpan().Slice(0, target.Count)) + foreach (var nativeLandmarks in source.AsReadOnlySpan()) { var landmarks = target[i]; NormalizedLandmarks.Copy(nativeLandmarks, ref landmarks); - target[i++] = landmarks; - } - - foreach (var nativeLandmarks in source.AsReadOnlySpan().Slice(copyCount)) - { - var landmarks = default(NormalizedLandmarks); - NormalizedLandmarks.Copy(nativeLandmarks, ref landmarks); - target.Add(landmarks); + target[i] = landmarks; } } } From 841507461d0960fbd62c56bffbab7ab0a76617b2 Mon Sep 17 00:00:00 2001 From: Junrou Nishida Date: Tue, 2 Jan 2024 18:38:42 +0900 Subject: [PATCH 3/3] fix: compile errors --- .../Scripts/Tasks/Components/Containers/Landmark.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs index 027011143..2cc2e4857 100644 --- a/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs +++ b/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Tasks/Components/Containers/Landmark.cs @@ -108,9 +108,11 @@ public override string ToString() internal NormalizedLandmark(NativeNormalizedLandmark nativeLandmark) : this( nativeLandmark.x, nativeLandmark.y, nativeLandmark.z, - nativeLandmark.hasVisibility ? nativeLandmark.visibility : null, - nativeLandmark.hasPresence ? nativeLandmark.presence : null, - Marshal.PtrToStringAnsi(nativeLandmark.name) +#pragma warning disable IDE0004 // for Unity 2020.3.x + nativeLandmark.hasVisibility ? (float?)nativeLandmark.visibility : null, + nativeLandmark.hasPresence ? (float?)nativeLandmark.presence : null, +#pragma warning restore IDE0004 // for Unity 2020.3.x + nativeLandmark.name ) { } @@ -190,9 +192,9 @@ public static Landmarks CreateFrom(LandmarkList proto) /// public readonly struct NormalizedLandmarks { - public readonly IReadOnlyList landmarks; + public readonly List landmarks; - internal NormalizedLandmarks(IReadOnlyList landmarks) + internal NormalizedLandmarks(List landmarks) { this.landmarks = landmarks; }