/****************************************************************************
* 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 System.Threading.Tasks;
/// A native configration.
public partial class NativeConfigration
{
/// The native interface.
private NativeInterface m_NativeInterface;
/// Dictionary of trackable image databases.
private Dictionary m_TrackableImageDatabaseDict;
/// Handle of the configuration.
private UInt64 m_ConfigHandle = 0;
/// Handle of the database.
private UInt64 m_DatabaseHandle = 0;
/// The last session configuration.
private NRSessionConfig m_LastSessionConfig;
/// The native trackable image.
private NativeTrackableImage m_NativeTrackableImage;
/// True if is update configuration lock, false if not.
private bool m_IsUpdateConfigLock = false;
/// Constructor.
/// The native interface.
public NativeConfigration(NativeInterface nativeInterface)
{
m_NativeInterface = nativeInterface;
m_LastSessionConfig = NRSessionConfig.CreateInstance(typeof(NRSessionConfig)) as NRSessionConfig;
m_NativeTrackableImage = m_NativeInterface.NativeTrackableImage;
m_TrackableImageDatabaseDict = new Dictionary();
}
/// Updates the configuration described by config.
/// The configuration.
/// True if it succeeds, false if it fails.
public async Task UpdateConfig(NRSessionConfig config)
{
if (m_IsUpdateConfigLock)
{
return false;
}
m_IsUpdateConfigLock = true;
if (m_ConfigHandle == 0)
{
m_ConfigHandle = this.Create();
}
if (m_ConfigHandle == 0 || m_LastSessionConfig.Equals(config))
{
NRDebugger.Info("[NativeConfigration] Faild to Update NRSessionConfig!!!");
m_IsUpdateConfigLock = false;
return false;
}
await UpdatePlaneFindMode(config);
await UpdateImageTrackingConfig(config);
m_LastSessionConfig.CopyFrom(config);
m_IsUpdateConfigLock = false;
return true;
}
/// Updates the plane find mode described by config.
/// The configuration.
/// An asynchronous result.
private Task UpdatePlaneFindMode(NRSessionConfig config)
{
return Task.Run(() =>
{
var currentmode = this.GetPlaneFindMode(m_ConfigHandle);
if (currentmode != config.PlaneFindingMode)
{
SetPlaneFindMode(m_ConfigHandle, config.PlaneFindingMode);
}
});
}
/// Updates the image tracking configuration described by config.
/// The configuration.
/// An asynchronous result.
private Task UpdateImageTrackingConfig(NRSessionConfig config)
{
return Task.Run(() =>
{
switch (config.ImageTrackingMode)
{
case TrackableImageFindingMode.DISABLE:
var result = SetTrackableImageDataBase(m_ConfigHandle, 0);
if (result)
{
m_TrackableImageDatabaseDict.Clear();
}
NRDebugger.Info("[NativeConfigration] Disable trackable image result : " + result);
break;
case TrackableImageFindingMode.ENABLE:
if (config.TrackingImageDatabase == null)
{
return;
}
if (!m_TrackableImageDatabaseDict.TryGetValue(config.TrackingImageDatabase.GUID, out m_DatabaseHandle))
{
DeployData(config.TrackingImageDatabase);
m_DatabaseHandle = m_NativeTrackableImage.CreateDataBase();
m_TrackableImageDatabaseDict.Add(config.TrackingImageDatabase.GUID, m_DatabaseHandle);
}
result = m_NativeTrackableImage.LoadDataBase(m_DatabaseHandle, config.TrackingImageDatabase.TrackingImageDataPath);
NRDebugger.Info("[NativeConfigration] LoadDataBase path:{0} result:{1} ", config.TrackingImageDatabase.TrackingImageDataPath, result);
result = SetTrackableImageDataBase(m_ConfigHandle, m_DatabaseHandle);
NRDebugger.Info("[NativeConfigration] SetTrackableImageDataBase result : " + result);
break;
default:
break;
}
});
}
/// Deploy data.
/// The database.
private void DeployData(NRTrackingImageDatabase database)
{
string deploy_path = database.TrackingImageDataOutPutPath;
NRDebugger.Info("[TrackingImageDatabase] DeployData to path :" + deploy_path);
ZipUtility.UnzipFile(database.RawData, deploy_path, NativeConstants.ZipKey);
}
/// Creates a new UInt64.
/// An UInt64.
private UInt64 Create()
{
UInt64 config_handle = 0;
var result = NativeApi.NRConfigCreate(m_NativeInterface.TrackingHandle, ref config_handle);
NativeErrorListener.Check(result, this, "Create");
return config_handle;
}
/// Gets plane find mode.
/// The Configuration handle to destroy.
/// The plane find mode.
public TrackablePlaneFindingMode GetPlaneFindMode(UInt64 config_handle)
{
TrackablePlaneFindingMode mode = TrackablePlaneFindingMode.DISABLE;
var result = NativeApi.NRConfigGetTrackablePlaneFindingMode(m_NativeInterface.TrackingHandle, config_handle, ref mode);
NativeErrorListener.Check(result, this, "GetPlaneFindMode");
return mode;
}
/// Sets plane find mode.
/// The Configuration handle to destroy.
/// The mode.
/// True if it succeeds, false if it fails.
public bool SetPlaneFindMode(UInt64 config_handle, TrackablePlaneFindingMode mode)
{
int mode_value;
switch (mode)
{
case TrackablePlaneFindingMode.DISABLE:
case TrackablePlaneFindingMode.HORIZONTAL:
case TrackablePlaneFindingMode.VERTICLE:
mode_value = (int)mode;
break;
case TrackablePlaneFindingMode.BOTH:
mode_value = ((int)TrackablePlaneFindingMode.HORIZONTAL) | ((int)TrackablePlaneFindingMode.VERTICLE);
break;
default:
mode_value = (int)TrackablePlaneFindingMode.DISABLE;
break;
}
var result = NativeApi.NRConfigSetTrackablePlaneFindingMode(m_NativeInterface.TrackingHandle, config_handle, mode_value);
NativeErrorListener.Check(result, this, "SetPlaneFindMode");
return result == NativeResult.Success;
}
/// Gets trackable image data base.
/// The Configuration handle to destroy.
/// The trackable image data base.
public UInt64 GetTrackableImageDataBase(UInt64 config_handle)
{
UInt64 database_handle = 0;
var result = NativeApi.NRConfigGetTrackableImageDatabase(m_NativeInterface.TrackingHandle, config_handle, ref database_handle);
NativeErrorListener.Check(result, this, "GetTrackableImageDataBase");
return database_handle;
}
/// Sets trackable image data base.
/// The Configuration handle to destroy.
/// Handle of the database.
/// True if it succeeds, false if it fails.
public bool SetTrackableImageDataBase(UInt64 config_handle, UInt64 database_handle)
{
var result = NativeApi.NRConfigSetTrackableImageDatabase(m_NativeInterface.TrackingHandle, config_handle, database_handle);
NativeErrorListener.Check(result, this, "SetTrackableImageDataBase");
return result == NativeResult.Success;
}
/// Destroys the given config_handle.
/// The Configuration handle to destroy.
/// True if it succeeds, false if it fails.
public bool Destroy(UInt64 config_handle)
{
var result = NativeApi.NRConfigDestroy(m_NativeInterface.TrackingHandle, config_handle);
NativeErrorListener.Check(result, this, "Destroy");
return result == NativeResult.Success;
}
/// A native api.
private struct NativeApi
{
/// Nr configuration create.
/// Handle of the session.
/// [in,out] Handle of the out configuration.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRConfigCreate(UInt64 session_handle, ref UInt64 out_config_handle);
/// Nr configuration destroy.
/// Handle of the session.
/// Handle of the configuration.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRConfigDestroy(UInt64 session_handle, UInt64 config_handle);
/// Nr configuration get trackable plane finding mode.
/// Handle of the session.
/// Handle of the configuration.
/// [in,out] The out trackable plane finding mode.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRConfigGetTrackablePlaneFindingMode(UInt64 session_handle,
UInt64 config_handle, ref TrackablePlaneFindingMode out_trackable_plane_finding_mode);
/// Nr configuration set trackable plane finding mode.
/// Handle of the session.
/// Handle of the configuration.
/// The trackable plane finding mode.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRConfigSetTrackablePlaneFindingMode(UInt64 session_handle,
UInt64 config_handle, int trackable_plane_finding_mode);
/// Nr configuration get trackable image database.
/// Handle of the session.
/// Handle of the configuration.
/// [in,out] Handle of the out trackable
/// image database.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRConfigGetTrackableImageDatabase(UInt64 session_handle,
UInt64 config_handle, ref UInt64 out_trackable_image_database_handle);
/// Nr configuration set trackable image database.
/// Handle of the session.
/// Handle of the configuration.
/// Handle of the trackable image database.
/// A NativeResult.
[DllImport(NativeConstants.NRNativeLibrary)]
public static extern NativeResult NRConfigSetTrackableImageDatabase(UInt64 session_handle,
UInt64 config_handle, UInt64 trackable_image_database_handle);
};
}
}