using System.Runtime.InteropServices;
namespace RobotNet10.RobotApp.TF3;
///
/// C# P/Invoke wrapper for TF3 (Transform Framework) C API
/// Provides coordinate transform management for robotics applications
///
///
/// Opaque handle to TF3 BufferCore (TF3_BufferCore in C API)
///
public struct TF3_BufferCore
{
public IntPtr Handle;
}
///
/// Transform structure for TF3 (matches TF3_Transform in C API)
///
[StructLayout(LayoutKind.Sequential)]
public struct TF3_Transform
{
// Header
public long timestamp_sec;
public long timestamp_nsec;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string frame_id;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string child_frame_id;
// Translation
public double translation_x;
public double translation_y;
public double translation_z;
// Rotation (quaternion)
public double rotation_x;
public double rotation_y;
public double rotation_z;
public double rotation_w;
}
///
/// TF3 error codes (matches TF3_ErrorCode in C API)
///
public enum TF3_ErrorCode
{
TF3_OK = 0,
TF3_ERROR_LOOKUP = 1,
TF3_ERROR_CONNECTIVITY = 2,
TF3_ERROR_EXTRAPOLATION = 3,
TF3_ERROR_INVALID_ARGUMENT = 4,
TF3_ERROR_TIMEOUT = 5,
TF3_ERROR_UNKNOWN = 99
}
///
/// P/Invoke declarations for libtf3.so
///
public static class TF3NativeInterface
{
private const string LibraryPath = "/usr/local/lib/libtf3.so";
///
/// Create TF3 buffer with cache time
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr tf3_buffer_create(int cacheTimeSec);
///
/// Destroy TF3 buffer
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void tf3_buffer_destroy(IntPtr buffer);
///
/// Set a transform in the buffer
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern bool tf3_set_transform(
IntPtr buffer,
ref TF3_Transform transform,
string authority,
bool isStatic);
///
/// Lookup transform between two frames
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern bool tf3_lookup_transform(
IntPtr buffer,
string targetFrame,
string sourceFrame,
long timeSec,
long timeNsec,
ref TF3_Transform transform,
ref TF3_ErrorCode errorCode);
///
/// Check if transform can be computed
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern bool tf3_can_transform(
IntPtr buffer,
string targetFrame,
string sourceFrame,
long timeSec,
long timeNsec,
IntPtr errorMsg,
int errorMsgLen);
///
/// Clear all transforms
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void tf3_clear(IntPtr buffer);
///
/// Get current time
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern void tf3_get_current_time(ref long sec, ref long nsec);
///
/// Get version string
///
[DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr tf3_get_version();
// [DllImport(LibraryPath, CallingConvention = CallingConvention.Cdecl)]
// public static extern int tf3_get_all_frame_names(
// TF3_BufferCore buffer,
// IntPtr frames,
// int frames_len);
}