-
-
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: implement Packet.CreateColMajorMatrix (#1133)
* refactor: matrix_data -> matrix * feat: port Matrix * feat: implement Packet.CreateColMajorMatrix * refactor: remove stale Matrix-related functions
- Loading branch information
Showing
15 changed files
with
359 additions
and
83 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Formats/Matrix.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,56 @@ | ||
// 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; | ||
|
||
namespace Mediapipe | ||
{ | ||
public readonly struct Matrix | ||
{ | ||
public enum Layout | ||
{ | ||
ColMajor = 0, | ||
RowMajor = 1, | ||
} | ||
|
||
internal readonly float[] data; | ||
public readonly int rows; | ||
public readonly int cols; | ||
internal readonly Layout layout; | ||
|
||
public Matrix(float[] data, int rows, int cols, Layout layout = Layout.ColMajor) | ||
{ | ||
if (rows * cols != data.Length) | ||
{ | ||
throw new ArgumentException($"Matrix size mismatch ({rows}x{cols} != {data.Length})"); | ||
} | ||
|
||
this.data = data; | ||
this.rows = rows; | ||
this.cols = cols; | ||
this.layout = layout; | ||
} | ||
|
||
internal Matrix(NativeMatrix nativeMatrix) : this(nativeMatrix.AsReadOnlySpan().ToArray(), nativeMatrix.rows, nativeMatrix.cols, nativeMatrix.layout == 0 ? Layout.ColMajor : Layout.RowMajor) | ||
{ } | ||
|
||
internal static void Copy(NativeMatrix source, ref Matrix destination) | ||
{ | ||
if (destination.rows != source.rows || destination.cols != source.cols) | ||
{ | ||
throw new ArgumentException($"Matrix size mismatch ({source.rows}x{source.cols} != {destination.rows}x{destination.cols})"); | ||
} | ||
|
||
source.AsReadOnlySpan().CopyTo(destination.data); | ||
var layout = source.layout == 0 ? Layout.ColMajor : Layout.RowMajor; | ||
|
||
destination = new Matrix(destination.data, source.rows, source.cols, layout); | ||
} | ||
|
||
public readonly bool isColMajor => layout == Layout.ColMajor; | ||
public readonly bool isRowMajor => layout == Layout.RowMajor; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Formats/Matrix.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
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
33 changes: 33 additions & 0 deletions
33
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeMatrix.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,33 @@ | ||
// 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 NativeMatrix | ||
{ | ||
private readonly IntPtr _data; | ||
public readonly int rows; | ||
public readonly int cols; | ||
public readonly int layout; | ||
|
||
public void Dispose() | ||
{ | ||
UnsafeNativeMethods.mp_api_Matrix__delete(this); | ||
} | ||
|
||
public ReadOnlySpan<float> AsReadOnlySpan() | ||
{ | ||
unsafe | ||
{ | ||
return new ReadOnlySpan<float>((float*)_data, rows * cols); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Marshal/NativeMatrix.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
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
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,60 @@ | ||
// 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. | ||
|
||
#include "mediapipe_api/framework/formats/matrix.h" | ||
|
||
|
||
MpReturnCode mp__MakeColMajorMatrixPacket__Pf_i_i(float* pcm_data, int rows, int cols, mediapipe::Packet** packet_out) { | ||
TRY | ||
Eigen::Map<Eigen::MatrixXf> m(pcm_data, rows, cols); | ||
|
||
*packet_out = new mediapipe::Packet{mediapipe::MakePacket<mediapipe::Matrix>(m)}; | ||
RETURN_CODE(MpReturnCode::Success); | ||
CATCH_EXCEPTION | ||
} | ||
|
||
MpReturnCode mp__MakeColMajorMatrixPacket_At__Pf_i_i_ll(float* pcm_data, int rows, int cols, int64 timestamp_microsec, mediapipe::Packet** packet_out) { | ||
TRY | ||
Eigen::Map<Eigen::MatrixXf> m(pcm_data, rows, cols); | ||
|
||
*packet_out = new mediapipe::Packet{mediapipe::MakePacket<mediapipe::Matrix>(m).At(mediapipe::Timestamp(timestamp_microsec))}; | ||
RETURN_CODE(MpReturnCode::Success); | ||
CATCH_EXCEPTION | ||
} | ||
|
||
MpReturnCode mp_Packet__GetMpMatrix(mediapipe::Packet* packet, mp_api::Matrix* value_out) { | ||
TRY | ||
auto matrix = packet->Get<mediapipe::Matrix>(); | ||
auto rows = matrix.rows(); | ||
auto cols = matrix.cols(); | ||
auto data = matrix.data(); | ||
auto len = rows * cols; | ||
|
||
value_out->rows = rows; | ||
value_out->cols = cols; | ||
if (matrix.IsRowMajor) { | ||
value_out->layout = mp_api::rowMajor; | ||
} else { | ||
value_out->layout = mp_api::colMajor; | ||
} | ||
value_out->data = new float[len]; | ||
memcpy(value_out->data, data, len * sizeof(float)); | ||
|
||
RETURN_CODE(MpReturnCode::Success); | ||
CATCH_EXCEPTION | ||
} | ||
|
||
MpReturnCode mp_Packet__ValidateAsMatrix(mediapipe::Packet* packet, absl::Status** status_out) { | ||
TRY | ||
*status_out = new absl::Status{packet->ValidateAsType<mediapipe::Matrix>()}; | ||
RETURN_CODE(MpReturnCode::Success); | ||
CATCH_EXCEPTION | ||
} | ||
|
||
|
||
void mp_api_Matrix__delete(mp_api::Matrix matrix) { | ||
delete[] matrix.data; | ||
} |
Oops, something went wrong.