ARWrapper
Provides a C++ wrapper around ARToolKit and exposes a C and JNI API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator
ARController.h
Go to the documentation of this file.
1 /*
2  * ARController.h
3  * ARToolKit5
4  *
5  * A C++ class encapsulating core controller functionality of ARToolKit.
6  *
7  * This file is part of ARToolKit.
8  *
9  * ARToolKit is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * ARToolKit is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with ARToolKit. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * As a special exception, the copyright holders of this library give you
23  * permission to link this library with independent modules to produce an
24  * executable, regardless of the license terms of these independent modules, and to
25  * copy and distribute the resulting executable under terms of your choice,
26  * provided that you also meet, for each linked independent module, the terms and
27  * conditions of the license of that module. An independent module is a module
28  * which is neither derived from nor based on this library. If you modify this
29  * library, you may extend this exception to your version of the library, but you
30  * are not obligated to do so. If you do not wish to do so, delete this exception
31  * statement from your version.
32  *
33  * Copyright 2015 Daqri, LLC.
34  * Copyright 2010-2015 ARToolworks, Inc.
35  *
36  * Author(s): Philip Lamb, Julian Looser.
37  *
38  */
39 
40 
41 #ifndef ARCONTROLLER_H
42 #define ARCONTROLLER_H
43 
44 #include <ARWrapper/Platform.h>
45 
46 #include <AR/ar.h>
47 #include <AR/arMulti.h>
48 #include <AR/video.h>
49 
50 #include <ARWrapper/Image.h>
51 #include <ARWrapper/VideoSource.h>
52 #include <ARWrapper/ARMarker.h>
55 #if HAVE_NFT
56 # include <AR2/tracking.h>
57 # include <KPM/kpm.h>
58 # include <ARWrapper/ARMarkerNFT.h>
59 #endif
60 
61 
62 #include <vector>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <math.h>
66 #include <string.h>
67 #if !defined(_WINRT)
68 # include <pthread.h>
69 #else
70 # define pthread_mutex_t CRITICAL_SECTION
71 # define pthread_mutex_init(pm, a) InitializeCriticalSectionEx(pm, 4000, CRITICAL_SECTION_NO_DEBUG_INFO)
72 # define pthread_mutex_lock(pm) EnterCriticalSection(pm)
73 # define pthread_mutex_unlock(pm) LeaveCriticalSection(pm)
74 # define pthread_mutex_destroy(pm) DeleteCriticalSection(pm)
75 #endif
76 #define PAGES_MAX 64
77 
82 class ARController {
83 
84 private:
85 #pragma mark Private types and instance variables
86  // ------------------------------------------------------------------------------
87  // Private types and instance variables.
88  // ------------------------------------------------------------------------------
89 
90  typedef enum {
91  NOTHING_INITIALISED,
92  BASE_INITIALISED,
93  WAITING_FOR_VIDEO,
94  DETECTION_RUNNING
95  } ARToolKitState;
96 
97  ARToolKitState state;
98  bool stateWaitingMessageLogged;
99 
100  char* versionString;
101 
102  VideoSource *m_videoSource0;
103  VideoSource *m_videoSource1;
104  pthread_mutex_t m_videoSourceLock;
105  bool m_videoSourceIsStereo;
106  int m_videoSourceFrameStamp0;
107  int m_videoSourceFrameStamp1;
108 
109  // Virtual environment parameters.
110  ARdouble m_projectionNearPlane;
111  ARdouble m_projectionFarPlane;
112  ARdouble m_projectionMatrix0[16];
113  ARdouble m_projectionMatrix1[16];
114  bool m_projectionMatrixSet;
115 
116  // ARToolKit configuration. These allow for configuration prior to starting ARToolKit.
117  int threshold;
118  AR_LABELING_THRESH_MODE thresholdMode;
119  int imageProcMode;
120  int labelingMode;
121  ARdouble pattRatio;
122  int patternDetectionMode;
123  AR_MATRIX_CODE_TYPE matrixCodeType;
124  bool debugMode;
125 
126  std::vector<ARMarker *> markers;
127 
128  bool doMarkerDetection;
129  // ARToolKit data.
130  ARHandle *m_arHandle0;
131  ARHandle *m_arHandle1;
132  ARPattHandle *m_arPattHandle;
133  AR3DHandle *m_ar3DHandle;
134  ARdouble m_transL2R[3][4];
135  AR3DStereoHandle *m_ar3DStereoHandle;
136 
137 #if HAVE_NFT
138  bool doNFTMarkerDetection;
139  bool m_nftMultiMode;
140  bool m_kpmRequired;
141  bool m_kpmBusy;
142  // NFT data.
143  THREAD_HANDLE_T *trackingThreadHandle;
144  AR2HandleT *m_ar2Handle;
145  KpmHandle *m_kpmHandle;
146  AR2SurfaceSetT *surfaceSet[PAGES_MAX]; // Weak-reference. Strong reference is now in ARMarkerNFT class.
147 #endif
148 
149  int m_error;
150  void setError(int error);
151 
152 #pragma mark Private methods.
153  // ------------------------------------------------------------------------------
154  // Private methods.
155  // ------------------------------------------------------------------------------
156 
157  void lockVideoSource();
158  void unlockVideoSource();
159 
160  //
161  // Internal marker management.
162  //
163 
169  bool addMarker(ARMarker* marker);
170 
176  bool removeMarker(ARMarker* marker);
177 
178  //
179  // Convenience initialisers.
180  //
181 
182  bool initAR(void);
183 #if HAVE_NFT
184  bool unloadNFTData(void);
185  bool loadNFTData(void);
186  bool initNFT(void);
187 #endif
188 
189 public:
190 #pragma mark Public API
191  // ------------------------------------------------------------------------------
192  // Public API
193  // ------------------------------------------------------------------------------
194 
198  ARController();
199 
203  ~ARController();
204 
206 
207 private:
208  static void logvBuf(va_list args, const char* format, char **bufPtr, int* lenPtr);
209  static void logvWriteBuf(char* buf, int len, const int logLevel);
210 
211 public:
212  static void logv(const int logLevel, const char* format, ...);
213 
219  static void logv(const char* msg, ...);
220 
225  const char* getARToolKitVersion();
226 
227  int getError();
228 
235  bool initialiseBase(const int patternSize = AR_PATT_SIZE1, const int patternCountMax = AR_PATT_NUM_MAX);
236 
242  bool canAddMarker();
243 
244  void setProjectionNearPlane(const ARdouble projectionNearPlane);
245  void setProjectionFarPlane(const ARdouble projectionFarPlane);
246  ARdouble projectionNearPlane(void);
247  ARdouble projectionFarPlane(void);
248 
257  bool startRunning(const char* vconf, const char* cparaName, const char* cparaBuff, const long cparaBuffLen);
258 
274  bool startRunningStereo(const char* vconfL, const char* cparaNameL, const char* cparaBuffL, const long cparaBuffLenL,
275  const char* vconfR, const char* cparaNameR, const char* cparaBuffR, const long cparaBuffLenR,
276  const char* transL2RName, const char* transL2RBuff, const long transL2RBuffLen);
277 
278 #if TARGET_PLATFORM_ANDROID
279  bool videoAcceptImage(JNIEnv* env, jobject obj, const int videoSourceIndex, jbyteArray pinArray, jint width, jint height, jint cameraIndex, jboolean cameraIsFrontFacing);
280 #endif
281 
292  bool videoParameters(const int videoSourceIndex, int *width, int *height, AR_PIXEL_FORMAT *pixelFormat);
293 
298  bool isRunning();
299 
304  bool stopRunning();
305 
311  bool shutdown();
312 
321  bool getProjectionMatrix(const int videoSourceIndex, ARdouble proj[16]);
322 
334  int addMarker(const char* cfg);
335 
341  bool removeMarker(int UID);
342 
347  int removeAllMarkers();
348 
353  unsigned int countMarkers();
354 
360  ARMarker* findMarker(int UID);
361 
362  bool capture();
363 
369  bool updateTexture(const int videoSourceIndex, Color* buffer);
370 
379  bool updateTexture32(const int videoSourceIndex, uint32_t *buffer);
380 
381 #ifndef _WINRT
382 
388  bool updateTextureGL(const int videoSourceIndex, const int textureID);
389 #endif // !_WINRT
390 
401  bool update();
402 
410  void setDebugMode(bool debug);
411 
417  bool getDebugMode() const;
418 
419  void setImageProcMode(int mode);
420 
421  int getImageProcMode() const;
422 
428  void setThreshold(int thresh);
429 
435  int getThreshold() const;
436 
442  void setThresholdMode(int mode);
443 
449  int getThresholdMode() const;
450 
456  void setLabelingMode(int mode);
457 
463  int getLabelingMode() const;
464 
465  void setPatternDetectionMode(int mode);
466 
467  int getPatternDetectionMode() const;
468 
469  void setPattRatio(float ratio);
470 
471  float getPattRatio() const;
472 
473  void setMatrixCodeType(int type);
474 
475  int getMatrixCodeType() const;
476 
477  void setNFTMultiMode(bool on);
478 
479  bool getNFTMultiMode() const;
480 
487  bool updateDebugTexture(const int videoSourceIndex, Color* buffer);
488 
497  bool updateDebugTexture32(const int videoSourceIndex, uint32_t* buffer);
498 
505  bool getPatternImage(int patternID, Color* buffer);
506 
529  bool loadOpticalParams(const char *optical_param_name, const char *optical_param_buff, const long optical_param_buffLen, ARdouble *fovy_p, ARdouble *aspect_p, ARdouble m[16], ARdouble p[16]);
530 
531 };
532 
533 
534 #endif // !ARCONTROLLER_H