/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
/// 6-dof Plane Tracking's Native API .
public partial class NativePlane
{
/// The native interface.
private NativeInterface m_NativeInterface;
/// The maximum size of the polygon.
private const int m_MaxPolygonSize = 1024;
/// The points.
private float[] m_Points;
/// Handle of the temporary points.
private GCHandle m_TmpPointsHandle;
/// Constructor.
/// The native interface.
public NativePlane(NativeInterface nativeInterface)
{
m_NativeInterface = nativeInterface;
m_Points = new float[m_MaxPolygonSize * 2];
m_TmpPointsHandle = GCHandle.Alloc(m_Points, GCHandleType.Pinned);
}
/// Finalizer.
~NativePlane()
{
m_TmpPointsHandle.Free();
}
/// Gets plane type.
/// Handle of the trackable.
/// The plane type.
public TrackablePlaneType GetPlaneType(UInt64 trackable_handle)
{
TrackablePlaneType plane_type = TrackablePlaneType.INVALID;
NativeApi.NRTrackablePlaneGetType(m_NativeInterface.TrackingHandle, trackable_handle, ref plane_type);
return plane_type;
}
/// Gets center pose.
/// Handle of the trackble.
/// The center pose.
public Pose GetCenterPose(UInt64 trackable_handle)
{
Pose pose = Pose.identity;
NativeMat4f center_native_pos = NativeMat4f.identity;
NativeApi.NRTrackablePlaneGetCenterPose(m_NativeInterface.TrackingHandle, trackable_handle, ref center_native_pos);
ConversionUtility.ApiPoseToUnityPose(center_native_pos, out pose);
return pose;
}
/// Gets extent x coordinate.
/// Handle of the trackable.
/// The extent x coordinate.
public float GetExtentX(UInt64 trackable_handle)
{
float extent_x = 0;
NativeApi.NRTrackablePlaneGetExtentX(m_NativeInterface.TrackingHandle, trackable_handle, ref extent_x);
return extent_x;
}
/// Gets extent z coordinate.
/// Handle of the trackable.
/// The extent z coordinate.
public float GetExtentZ(UInt64 trackable_handle)
{
float extent_z = 0;
NativeApi.NRTrackablePlaneGetExtentZ(m_NativeInterface.TrackingHandle, trackable_handle, ref extent_z);
return extent_z;
}
public void GetBoundaryPolygon(UInt64 trackable_handle, List polygonList)
{
polygonList.Clear();
int polygon_size = 0;
NativeApi.NRTrackablePlaneGetPolygonSize(m_NativeInterface.TrackingHandle, trackable_handle, ref polygon_size);
int size = polygon_size / 2;
NativeApi.NRTrackablePlaneGetPolygon(m_NativeInterface.TrackingHandle, trackable_handle, (m_TmpPointsHandle.AddrOfPinnedObject()));
float[] point_data = new float[size * 2];
Array.Copy(m_Points, point_data, size * 2);
Pose centerPos = GetCenterPose(trackable_handle);
var unityWorldTPlane = Matrix4x4.TRS(centerPos.position, centerPos.rotation, Vector3.one);
var plane_type = GetPlaneType(trackable_handle);
for (int i = 2 * size - 2; i >= 0; i -= 2)
{
Vector3 localpos = new Vector3(point_data[i], 0, -point_data[i + 1]);
polygonList.Add(unityWorldTPlane.MultiplyPoint3x4(localpos));
}
if (plane_type == TrackablePlaneType.VERTICAL)
{
polygonList.Reverse();
}
}
private partial struct NativeApi
{
/// Nr trackable plane get type.
/// Handle of the session.
/// Handle of the trackable.
/// [in,out] Type of the out plane.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRTrackablePlaneGetType(UInt64 session_handle,
UInt64 trackable_handle, ref TrackablePlaneType out_plane_type);
/// Nr trackable plane get center pose.
/// Handle of the session.
/// Handle of the trackable.
/// [in,out] The out center pose.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRTrackablePlaneGetCenterPose(UInt64 session_handle,
UInt64 trackable_handle, ref NativeMat4f out_center_pose);
/// Nr trackable plane get extent x coordinate.
/// Handle of the session.
/// Handle of the trackable.
/// [in,out] The out extent x coordinate.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRTrackablePlaneGetExtentX(UInt64 session_handle,
UInt64 trackable_handle, ref float out_extent_x);
/// Nr trackable plane get extent z coordinate.
/// Handle of the session.
/// Handle of the trackable.
/// [in,out] The out extent z coordinate.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRTrackablePlaneGetExtentZ(UInt64 session_handle,
UInt64 trackable_handle, ref float out_extent_z);
/// Nr trackable plane get polygon size.
/// Handle of the session.
/// Handle of the trackable.
/// [in,out] Size of the out polygon.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRTrackablePlaneGetPolygonSize(UInt64 session_handle,
UInt64 trackable_handle, ref int out_polygon_size);
/// Nr trackable plane get polygon.
/// Handle of the session.
/// Handle of the trackable.
/// The out polygon.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRTrackablePlaneGetPolygon(UInt64 session_handle,
UInt64 trackable_handle, IntPtr out_polygon);
};
}
}