/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal.Experimental
{
using System;
using System.Runtime.InteropServices;
using UnityEngine;
/// Session Native API.
public class NativeMapping
{
/// Handle of the database.
private UInt64 m_DatabaseHandle;
/// The native interface.
private static NativeInterface m_NativeInterface;
/// Constructor.
/// The native interface.
public NativeMapping(NativeInterface nativeInterface)
{
m_NativeInterface = nativeInterface;
}
/// Creates data base.
/// True if it succeeds, false if it fails.
public bool CreateDataBase()
{
var result = NativeApi.NRWorldMapDatabaseCreate(m_NativeInterface.TrackingHandle, ref m_DatabaseHandle);
NativeErrorListener.Check(result, this, "CreateDataBase");
return result == NativeResult.Success;
}
/// Destroys the data base.
/// True if it succeeds, false if it fails.
public bool DestroyDataBase()
{
var result = NativeApi.NRWorldMapDatabaseDestroy(m_NativeInterface.TrackingHandle, m_DatabaseHandle);
NativeErrorListener.Check(result, this, "DestroyDataBase");
return result == NativeResult.Success;
}
/// Loads a map.
/// Full pathname of the file.
/// True if it succeeds, false if it fails.
public bool LoadMap(string path)
{
var result = NativeApi.NRWorldMapDatabaseLoadFile(m_NativeInterface.TrackingHandle, m_DatabaseHandle, path);
NativeErrorListener.Check(result, this, "LoadMap");
return result == NativeResult.Success;
}
/// Saves a map.
/// Full pathname of the file.
/// True if it succeeds, false if it fails.
public bool SaveMap(string path)
{
var result = NativeApi.NRWorldMapDatabaseSaveFile(m_NativeInterface.TrackingHandle, m_DatabaseHandle, path);
NativeErrorListener.Check(result, this, "SaveMap");
return result == NativeResult.Success;
}
/// Reset Map
/// True if it succeeds, false if it fails.
public bool Reset()
{
var result = NativeApi.NRMappingReset(m_NativeInterface.TrackingHandle);
NativeErrorListener.Check(result, this, "Reset");
return result == NativeResult.Success;
}
/// Adds an anchor.
/// The pose.
/// An UInt64.
public UInt64 AddAnchor(Pose pose)
{
UInt64 anchorHandle = 0;
NativeMat4f nativePose;
ConversionUtility.UnityPoseToApiPose(pose, out nativePose);
var result = NativeApi.NRTrackingAcquireNewAnchor(m_NativeInterface.TrackingHandle, ref nativePose, ref anchorHandle);
NativeErrorListener.Check(result, this, "AddAnchor");
return anchorHandle;
}
/// Creates anchor list.
/// The new anchor list.
public UInt64 CreateAnchorList()
{
UInt64 anchorlisthandle = 0;
var result = NativeApi.NRAnchorListCreate(m_NativeInterface.TrackingHandle, ref anchorlisthandle);
NativeErrorListener.Check(result, this, "CreateAnchorList");
return anchorlisthandle;
}
/// Updates the anchor described by anchorlisthandle.
/// The anchorlisthandle.
/// True if it succeeds, false if it fails.
public bool UpdateAnchor(UInt64 anchorlisthandle)
{
var result = NativeApi.NRTrackingUpdateAnchors(m_NativeInterface.TrackingHandle, anchorlisthandle);
NativeErrorListener.Check(result, this, "UpdateAnchor");
return result == NativeResult.Success;
}
/// Destroys the anchor list described by anchorlisthandle.
/// The anchorlisthandle.
/// True if it succeeds, false if it fails.
public bool DestroyAnchorList(UInt64 anchorlisthandle)
{
//NRDebugger.Info("Start to destroy anchor list...");
var result = NativeApi.NRAnchorListDestroy(m_NativeInterface.TrackingHandle, anchorlisthandle);
NativeErrorListener.Check(result, this, "DestroyAnchorList");
return result == NativeResult.Success;
}
/// Gets anchor list size.
/// Handle of the anchor list.
/// The anchor list size.
public int GetAnchorListSize(UInt64 anchor_list_handle)
{
int size = 0;
var result = NativeApi.NRAnchorListGetSize(m_NativeInterface.TrackingHandle, anchor_list_handle, ref size);
NativeErrorListener.Check(result, this, "GetAnchorListSize");
return size;
}
/// Acquires the item.
/// Handle of the anchor list.
/// Zero-based index of the.
/// An UInt64.
public UInt64 AcquireItem(UInt64 anchor_list_handle, int index)
{
UInt64 anchorHandle = 0;
var result = NativeApi.NRAnchorListAcquireItem(m_NativeInterface.TrackingHandle, anchor_list_handle, index, ref anchorHandle);
NativeErrorListener.Check(result, this, "AcquireItem");
return anchorHandle;
}
/// Gets tracking state.
/// Handle of the anchor.
/// The tracking state.
public TrackingState GetTrackingState(UInt64 anchor_handle)
{
TrackingState trackingState = TrackingState.Stopped;
var result = NativeApi.NRAnchorGetTrackingState(m_NativeInterface.TrackingHandle, anchor_handle, ref trackingState);
NativeErrorListener.Check(result, this, "GetTrackingState");
return trackingState;
}
/// Gets anchor native identifier.
/// Handle of the anchor.
/// The anchor native identifier.
public static int GetAnchorNativeID(UInt64 anchor_handle)
{
int anchorID = -1;
NativeApi.NRAnchorGetID(m_NativeInterface.TrackingHandle, anchor_handle, ref anchorID);
return anchorID;
}
/// Gets anchor pose.
/// Handle of the anchor.
/// The anchor pose.
public Pose GetAnchorPose(UInt64 anchor_handle)
{
NativeMat4f nativePose = NativeMat4f.identity;
NativeApi.NRAnchorGetPose(m_NativeInterface.TrackingHandle, anchor_handle, ref nativePose);
Pose unitypose;
ConversionUtility.ApiPoseToUnityPose(nativePose, out unitypose);
return unitypose;
}
/// Destroys the anchor described by anchor_handle.
/// Handle of the anchor.
/// True if it succeeds, false if it fails.
public bool DestroyAnchor(UInt64 anchor_handle)
{
var result = NativeApi.NRAnchorDestroy(m_NativeInterface.TrackingHandle, anchor_handle);
NativeErrorListener.Check(result, this, "DestroyAnchor");
return result == NativeResult.Success;
}
/// A native api.
private struct NativeApi
{
/// Nr world map database create.
/// Handle of the tracking.
/// [in,out] Handle of the out world map database.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRWorldMapDatabaseCreate(UInt64 tracking_handle,
ref UInt64 out_world_map_database_handle);
/// Nr world map database destroy.
/// Handle of the tracking.
/// Handle of the world map database.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRWorldMapDatabaseDestroy(UInt64 tracking_handle,
UInt64 world_map_database_handle);
/// Nr world map database load file.
/// Handle of the tracking.
/// Handle of the world map database.
/// Full pathname of the world map database file.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRWorldMapDatabaseLoadFile(UInt64 tracking_handle,
UInt64 world_map_database_handle, string world_map_database_file_path);
/// Nr world map database save file.
/// Handle of the tracking.
/// Handle of the world map database.
/// Full pathname of the world map database file.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRWorldMapDatabaseSaveFile(UInt64 tracking_handle,
UInt64 world_map_database_handle, string world_map_database_file_path);
/// Reset Map
/// Handle of the tracking.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRMappingReset(UInt64 tracking_handle);
/// NRTracking.
/// Handle of the tracking.
/// [in,out] The pose.
/// [in,out] Handle of the out anchor.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRTrackingAcquireNewAnchor(
UInt64 tracking_handle, ref NativeMat4f pose, ref UInt64 out_anchor_handle);
/// Nr tracking update anchors.
/// Handle of the tracking.
/// Handle of the out anchor list.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRTrackingUpdateAnchors(
UInt64 tracking_handle, UInt64 out_anchor_list_handle);
/// NRAnchorList.
/// Handle of the tracking.
/// [in,out] Handle of the out anchor list.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRAnchorListCreate(
UInt64 tracking_handle, ref UInt64 out_anchor_list_handle);
/// Nr anchor list destroy.
/// Handle of the tracking.
/// Handle of the anchor list.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRAnchorListDestroy(
UInt64 tracking_handle, UInt64 anchor_list_handle);
/// Nr anchor list get size.
/// Handle of the tracking.
/// Handle of the anchor list.
/// [in,out] Size of the out list.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRAnchorListGetSize(UInt64 tracking_handle,
UInt64 anchor_list_handle, ref int out_list_size);
/// Nr anchor list acquire item.
/// Handle of the tracking.
/// Handle of the anchor list.
/// Zero-based index of the.
/// [in,out] The out anchor.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRAnchorListAcquireItem(UInt64 tracking_handle,
UInt64 anchor_list_handle, int index, ref UInt64 out_anchor);
/// NRAnchor.
/// Handle of the tracking.
/// Handle of the anchor.
/// [in,out] State of the out tracking.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRAnchorGetTrackingState(UInt64 tracking_handle,
UInt64 anchor_handle, ref TrackingState out_tracking_state);
/// Nr anchor get identifier.
/// Handle of the tracking.
/// Handle of the anchor.
/// [in,out] Identifier for the out anchor.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRAnchorGetID(UInt64 tracking_handle,
UInt64 anchor_handle, ref int out_anchor_id);
/// Nr anchor get pose.
/// Handle of the tracking.
/// Handle of the anchor.
/// [in,out] The out pose.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRAnchorGetPose(UInt64 tracking_handle,
UInt64 anchor_handle, ref NativeMat4f out_pose);
/// Nr anchor destroy.
/// Handle of the tracking.
/// Handle of the anchor.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRAnchorDestroy(UInt64 tracking_handle,
UInt64 anchor_handle);
}
}
}