MaxstARSDK  3.5.0
TrackerManager.cs
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 using UnityEngine;
6 using System;
7 using System.Linq;
8 using System.Runtime.InteropServices;
9 using System.Text;
10 
11 namespace maxstAR
12 {
16  public class TrackerManager
17  {
21  public const int TRACKER_TYPE_CODE_SCANNER = 0X01;
22 
26  public const int TRACKER_TYPE_IMAGE = 0X02;
27 
31  public const int TRACKER_TYPE_MARKER = 0X04;
32 
36  public const int TRACKER_TYPE_OBJECT = 0X08;
37 
41  public const int TRACKER_TYPE_SLAM = 0x10;
42 
46  public const int TRACKER_TYPE_INSTANT = 0x20;
47 
48  private static TrackerManager instance = null;
49  private float[] glPoseMatrix = new float[16];
50 
57  public enum TrackingOption
58  {
62  NORMAL_TRACKING = 1,
63 
67  EXTEND_TRACKING = 2,
68 
72  MULTI_TRACKING = 4,
73 
77  JITTER_REDUCTION_ACTIVATION = 8,
78 
82  JITTER_REDUCTION_DEACTIVATION = 16
83  }
84 
89  public static TrackerManager GetInstance()
90  {
91  if (instance == null)
92  {
93  instance = new TrackerManager();
94  }
95  return instance;
96  }
97 
98  //private SurfaceMesh surfaceMesh = null;
99  private GuideInfo guideInfo = null;
100  private TrackingState trackingState = null;
101 
102  private TrackerManager()
103  {
104  trackingState = new TrackingState();
105 
106  // To avoid null pointer error
108  }
109 
112  public void StartTracker(int trackerType)
113  {
114  if (Application.platform == RuntimePlatform.IPhonePlayer)
115  {
116  NativeStaticAPI.TrackerManager_startTracker(trackerType);
117  }
118  else
119  {
120  NativeSharedAPI.TrackerManager_startTracker(trackerType);
121  }
122  }
123 
125  public void StopTracker()
126  {
127  if (Application.platform == RuntimePlatform.IPhonePlayer)
128  {
129  NativeStaticAPI.TrackerManager_stopTracker();
130  }
131  else
132  {
133  NativeSharedAPI.TrackerManager_stopTracker();
134  }
135  }
136 
138  public void DestroyTracker()
139  {
140  if (Application.platform == RuntimePlatform.IPhonePlayer)
141  {
142  NativeStaticAPI.TrackerManager_destroyTracker();
143  }
144  else
145  {
146  NativeSharedAPI.TrackerManager_destroyTracker();
147  }
148  }
149 
153  public void AddTrackerData(string trackingFileName, bool isAndroidAssetFile = false)
154  {
155  if (Application.platform == RuntimePlatform.IPhonePlayer)
156  {
157  NativeStaticAPI.TrackerManager_addTrackerData(trackingFileName, isAndroidAssetFile);
158  }
159  else
160  {
161  NativeSharedAPI.TrackerManager_addTrackerData(trackingFileName, isAndroidAssetFile);
162  }
163  }
164 
168  public void RemoveTrackerData(string trackingFileName = "")
169  {
170  if (Application.platform == RuntimePlatform.IPhonePlayer)
171  {
172  NativeStaticAPI.TrackerManager_removeTrackerData(trackingFileName);
173  }
174  else
175  {
176  NativeSharedAPI.TrackerManager_removeTrackerData(trackingFileName);
177  }
178  }
179 
181  public void LoadTrackerData()
182  {
183  if (Application.platform == RuntimePlatform.IPhonePlayer)
184  {
185  NativeStaticAPI.TrackerManager_loadTrackerData();
186  }
187  else
188  {
189  NativeSharedAPI.TrackerManager_loadTrackerData();
190  }
191  }
192 
194  public void SetTrackingOption(TrackingOption trackingOption)
195  {
196  if (Application.platform == RuntimePlatform.IPhonePlayer)
197  {
198  NativeStaticAPI.TrackerManager_setTrackingOption((int)trackingOption);
199  }
200  else
201  {
202  NativeSharedAPI.TrackerManager_setTrackingOption((int)trackingOption);
203  }
204 
205  if (trackingOption == TrackingOption.MULTI_TRACKING)
206  {
208  }
209  }
210 
214  {
215  if (Application.platform == RuntimePlatform.IPhonePlayer)
216  {
217  return NativeStaticAPI.TrackerManager_isTrackerDataLoadCompleted();
218  }
219  else
220  {
221  return NativeSharedAPI.TrackerManager_isTrackerDataLoadCompleted();
222  }
223  }
224 
230  {
231  ulong trackingStateCPtr = 0;
232 
233  if (Application.platform == RuntimePlatform.IPhonePlayer)
234  {
235  trackingStateCPtr = NativeStaticAPI.TrackerManager_updateTrackingState();
236  }
237  else
238  {
239  trackingStateCPtr = NativeSharedAPI.TrackerManager_updateTrackingState();
240  }
241 
242  trackingState.SetTrackingStateCPtr(trackingStateCPtr);
243  return trackingState;
244  }
245 
251  {
252  return trackingState;
253  }
254 
257  //public string GetCodeScanResult()
258  //{
259  // if (Application.platform == RuntimePlatform.IPhonePlayer)
260  // {
261  // int length = NativeStaticAPI.TrackerManager_getCodeScanResultLength();
262  // if (length > 0)
263  // {
264  // byte[] result = new byte[length];
265  // NativeStaticAPI.TrackerManager_getCodeScanResult(result, length);
266  // return Encoding.Default.GetString(result);
267  // }
268  // else
269  // {
270  // return "";
271  // }
272  // }
273  // else
274  // {
275  // int length = NativeSharedAPI.TrackerManager_getCodeScanResultLength();
276  // if (length > 0)
277  // {
278  // byte[] result = new byte[length];
279  // NativeSharedAPI.TrackerManager_getCodeScanResult(result, length);
280  // return Encoding.Default.GetString(result);
281  // }
282  // else
283  // {
284  // return "";
285  // }
286  // }
287  //}
288 
292  public Vector3 GetWorldPositionFromScreenCoordinate(Vector2 screen)
293  {
294  float[] s = new float[2];
295  float[] w = new float[3];
296 
297  s[0] = screen.x;
298  s[1] = Screen.height - screen.y;
299 
300  if (Application.platform == RuntimePlatform.IPhonePlayer)
301  {
302  NativeStaticAPI.TrackerManager_getWorldPositionFromScreenCoordinate(s, w);
303  }
304  else
305  {
306  NativeSharedAPI.TrackerManager_getWorldPositionFromScreenCoordinate(s, w);
307  }
308 
309  //Vector3 world = new Vector3(w[0], w[1], w[2]);
310  Vector3 world = new Vector3(w[0], w[2], w[1]);
311 
312  switch (Screen.orientation)
313  {
314  case ScreenOrientation.Portrait:
315  world.z *= -1.0f;
316  break;
317 
318  case ScreenOrientation.PortraitUpsideDown:
319  world.x *= -1.0f;
320  break;
321 
322  case ScreenOrientation.Landscape:
323  world.z *= -1.0f;
324  break;
325 
326  case ScreenOrientation.LandscapeRight:
327  world.x *= -1.0f;
328  break;
329  }
330 
331  return world;
332  }
333 
337  //public Vector2 GetScreenCoordinateFromWorldPosition(Vector3 world)
338  //{
339  // switch (Screen.orientation)
340  // {
341  // case ScreenOrientation.Portrait:
342  // world.z *= -1.0f;
343  // break;
344 
345  // case ScreenOrientation.PortraitUpsideDown:
346  // world.x *= -1.0f;
347  // break;
348 
349  // case ScreenOrientation.Landscape:
350  // world.z *= -1.0f;
351  // break;
352 
353  // case ScreenOrientation.LandscapeRight:
354  // world.x *= -1.0f;
355  // break;
356  // }
357 
358  // float[] s = new float[2];
359  // float[] w = new float[3];
360 
361  // w[0] = world.x;
362  // w[1] = world.z;
363  // w[2] = world.y;
364 
365  // if (Application.platform == RuntimePlatform.IPhonePlayer)
366  // {
367  // NativeStaticAPI.getScreenCoordinateFromWorldPosition(w, s);
368  // }
369  // else
370  // {
371  // NativeSharedAPI.getScreenCoordinateFromWorldPosition(w, s);
372  // }
373 
374  // return new Vector2(s[0], Screen.height - s[1]);
375  //}
376 
379  //public int GetKeyframeCount()
380  //{
381  // if (Application.platform == RuntimePlatform.IPhonePlayer)
382  // {
383  // return NativeStaticAPI.TrackerManager_getKeyframeCount();
384  // }
385  // else
386  // {
387  // return NativeSharedAPI.TrackerManager_getKeyframeCount();
388  // }
389  //}
390 
393  //public int GetFeatureCount()
394  //{
395  // if (Application.platform == RuntimePlatform.IPhonePlayer)
396  // {
397  // return NativeStaticAPI.TrackerManager_getFeatureCount();
398  // }
399  // else
400  // {
401  // return NativeSharedAPI.TrackerManager_getFeatureCount();
402  // }
403  //}
404 
405  //public void GetFeatureVertex(float[] vertex)
406  //{
407  // if (Application.platform == RuntimePlatform.IPhonePlayer)
408  // {
409  // NativeStaticAPI.TrackerManager_getFeatureVertex(vertex);
410  // }
411  // else
412  // {
413  // NativeSharedAPI.TrackerManager_getFeatureVertex(vertex);
414  // }
415  //}
416 
417  //public Matrix4x4 GetFeaturePose()
418  //{
419  // if (Application.platform == RuntimePlatform.IPhonePlayer)
420  // {
421  // NativeStaticAPI.TrackerManager_getFeatureTrackingPose(glPoseMatrix);
422  // }
423  // else
424  // {
425  // NativeSharedAPI.TrackerManager_getFeatureTrackingPose(glPoseMatrix);
426  // }
427 
428  // Matrix4x4 poseMatrix = MatrixUtils.ConvertGLMatrixToUnityMatrix4x4(glPoseMatrix);
429 
430  // return poseMatrix;
431  //}
432 
434  public void FindSurface()
435  {
436  if (Application.platform == RuntimePlatform.IPhonePlayer)
437  {
438  NativeStaticAPI.TrackerManager_findSurface();
439  }
440  else
441  {
442  NativeSharedAPI.TrackerManager_findSurface();
443  }
444  }
445 
447  public void QuitFindingSurface()
448  {
449  if (Application.platform == RuntimePlatform.IPhonePlayer)
450  {
451  NativeStaticAPI.TrackerManager_quitFindingSurface();
452  }
453  else
454  {
455  NativeSharedAPI.TrackerManager_quitFindingSurface();
456  }
457  }
458 
463  //public SurfaceMesh GetSurfaceMesh()
464  //{
465  // if (surfaceMesh == null)
466  // {
467  // surfaceMesh = new SurfaceMesh();
468  // }
469 
470  // surfaceMesh.UpdateSurfaceMesh();
471  // return surfaceMesh;
472  //}
473 
479  {
480  if (guideInfo == null)
481  {
482  guideInfo = new GuideInfo();
483  }
484 
485  guideInfo.UpdateGuideInfo();
486  return guideInfo;
487  }
488 
492  public SurfaceThumbnail SaveSurfaceData(string outputFileName)
493  {
494  SurfaceThumbnail surfaceThumbnail = null;
495  ulong SurfaceThumbnail_cPtr = 0;
496  if (Application.platform == RuntimePlatform.IPhonePlayer)
497  {
498  SurfaceThumbnail_cPtr = NativeStaticAPI.TrackerManager_saveSurfaceData(outputFileName);
499  }
500  else
501  {
502  SurfaceThumbnail_cPtr = NativeSharedAPI.TrackerManager_saveSurfaceData(outputFileName);
503  }
504 
505  if (SurfaceThumbnail_cPtr == 0)
506  {
507  return null;
508  }
509  else
510  {
511  surfaceThumbnail = new SurfaceThumbnail(SurfaceThumbnail_cPtr);
512  }
513 
514  return surfaceThumbnail;
515  }
516  }
517 }
WorldCenterMode
The world center mode defines what is the center in game view. If camera is world center then trackab...
void FindSurface()
Start SLAM learning.
void StopTracker()
Stop Tracker.
const int TRACKER_TYPE_OBJECT
Object Tracker(Object data should be created via SLAM tracker)
void RemoveTrackerData(string trackingFileName="")
Delete the Trackable data from the Map List.
Container for individual tracking information
Initialize system environment with app key, screen size and orientation
SurfaceThumbnail SaveSurfaceData(string outputFileName)
Saves SLAM learning results.
Vector3 GetWorldPositionFromScreenCoordinate(Vector2 screen)
Gets the x, y, 0 coordinates on the world coordinate corresponding to x, y in the Screen coordinate...
const int TRACKER_TYPE_SLAM
Visual slam tracker (Can create surface data and save it)
const int TRACKER_TYPE_CODE_SCANNER
Code scanner
void SetTrackingOption(TrackingOption trackingOption)
Change Image Tracke Mode.
const int TRACKER_TYPE_IMAGE
Planar image Tracker
const int TRACKER_TYPE_INSTANT
Instant Tracker
TrackingState GetTrackingState()
Get saved TrackingState value
void QuitFindingSurface()
SLAM stops learning.
Contains surface thumbnail image information of first keyframe
Contains surface's data generated from slam tracking
Definition: GuideInfo.cs:17
void StartTracker(int trackerType)
Start Tracker.
void SetWorldCenterMode(WorldCenterMode worldCenterMode)
Set world center mode
void DestroyTracker()
Destroy Tracker.
TrackingState UpdateTrackingState()
Upate tracking state. This function should be called before GetTrackingResult
void LoadTrackerData()
Load the Trackable data.
bool IsTrackerDataLoadCompleted()
Check that the Trackable data loading is finished.
TrackingOption
Additional tracking option (Image tracker only) 1 : Normal Tracking 2 : Extended Tracking 4 : Multi T...
const int TRACKER_TYPE_MARKER
Marker Tracker
GuideInfo GetGuideInfo()
Get guide information of the found surface on SLAM after the FindSurface method has been called ...
static TrackerManager GetInstance()
Get TrackerManager instance
void AddTrackerData(string trackingFileName, bool isAndroidAssetFile=false)
Add the Trackable data to the Map List.
Control AR Engine (Singletone)