SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
SLAMflex.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <string>
4 #include <time.h>
5 
6 
7 #import "TooN.h"
8 #import "tracker.h"
9 #import "byte.h"
10 #include "System.h"
11 #include "globals.h"
12 #include <string.h>
13 #include "SendMessage.h"
14 #include "SLAMflex.h"
15 
20 
22 unsigned char *bwImage;
23 
24 
26 void convertToBlackWhite (unsigned char * pixels)
27 {
28  memcpy(bwImage,pixels,640*480*4);
29 
30  unsigned int * pntrBWImage= (unsigned int *)bwImage;
31  unsigned int index=0;
32  unsigned int fourBytes;
33  for (int j=0;j<480; j++)
34  {
35  for (int i=0; i<640; i++)
36  {
37  index=(640)*j+i;
38  fourBytes=pntrBWImage[index];
39  bwImage[index]=((unsigned char)fourBytes>>(2*8)) +((unsigned char)fourBytes>>(1*8))+((unsigned char)fourBytes>>(0*8));
40  }
41  }
42 }
44 void flip_vertically(unsigned char *pixels, const size_t width, const size_t height, const size_t bytes_per_pixel)
45 {
46  const size_t stride = width * bytes_per_pixel;
47  unsigned char *row = (unsigned char*)malloc(stride);
48  unsigned char *low = pixels;
49  unsigned char *high = &pixels[(height - 1) * stride];
50 
51  for (; low < high; low += stride, high -= stride) {
52  memcpy(row, low, stride);
53  memcpy(low, high, stride);
54  memcpy(high, row, stride);
55  }
56  free(row);
57 }
58 
60 void rgba_to_bgra(unsigned char *pixels, int width, int height)
61 {
62  for (int i = 0; i<width*height*4; i+=4)
63  {
64  std::swap(pixels[i], pixels[i+2]);
65 
66  }
67 }
69 extern "C" {
70 
72  void _StartSlam(void* pointerString, void* pointerPose, void* pointerStringLog, void* pointerArrayOfPoints)
73  {
74  std::cout << "=> Start SlamFlex service" << std::endl;
75  LOGV(LOG_TAG, "Info: %s", "=> Start SlamFlex service");
76  SM.SetSendMessagesFunc((sendMessageFunc)pointerString, (sendPoseFunc)pointerPose, (sendArrayPoints)pointerArrayOfPoints, (sendLogFunc)pointerStringLog);
77  ptam.SetSendMessageToUnity(SM);
78  bwImage = (unsigned char *)malloc(ScreenHeight*ScreenWidth*4);
79  memset(bwImage, 0, ScreenHeight*ScreenWidth*4);
80  }
81 
84  {
85  ptam.SendTrackerStartSig();
86  std::cout << "-----Signal--StatusBar------" << std::endl;
87  LOGV(LOG_TAG, "Info: %s", "-----Signal--StatusBar------");
88  }
89 
91  const char* _SetNewFrame (void* pointer, int width, int height)
92  {
93  unsigned char* data = reinterpret_cast<unsigned char*>(pointer);
94  rgba_to_bgra(data, 640, 480);
95  convertToBlackWhite(data);
96 
97  clock_t tFrameStart, tFrameEnd;
98  tFrameStart = clock();
99 
100  ptam.RunOneFrame(bwImage, 1);
101 
102  tFrameEnd = clock();;
103 
104  if(EnableLogging)
105  {
106  std::ostringstream message;
107  message <<"Number Of Corners:: "<<ptam.getVCorners()<<" :: Duration: " << (float)(tFrameEnd - tFrameStart)/CLOCKS_PER_SEC*1000 << std::endl;
108  SM.SendLogToUnity(message.str().c_str());
109  }
110 
111  return (const char *[]){"Started", "InProgres", "Stopped", "Finished"}[ptam.GetCurrentDetectionState()];
112  }
114  void _StopSlam()
115  {
116  std::cout << "=> _Stop SlamFlex" << std::endl;
117  LOGV(LOG_TAG, "Info: %s", "=> _Stop SlamFlex");
118  ptam.SendTrackerKillSig();
119  }
120 
121 }
#define LOGV(LOG_TAG,...)
Definition: globals.h:10
#define LOG_TAG
Definition: globals.h:9
void SendTrackerStartSig()
Definition: System.cpp:48
void RunOneFrame(unsigned char *bwImage, uint hnd)
Definition: System.cpp:63
System ptam
PTAM system, whole plane detection is located here.
Definition: SLAMflex.cpp:17
const int ScreenHeight
Definition: globals.h:49
void SendLogToUnity(const char *st)
Send string to Unity3D
Definition: SendMessage.cpp:34
SendMessage SM
used for communication between library and Unity3D
Definition: SLAMflex.cpp:19
void SetSendMessagesFunc(sendMessageFunc smf, sendPoseFunc spf, sendArrayPoints sar, sendLogFunc sendLog)
Sets callback functions recived from Unity3D
Definition: SendMessage.cpp:22
void _StopSlam()
Stops slam plane detection.
Definition: SLAMflex.cpp:114
void(* sendPoseFunc)(float, float, float, double, double, double)
Definition: SendMessage.h:8
void _StartPlaneDetection()
Initiate plane detection.
Definition: SLAMflex.cpp:83
const int ScreenWidth
Definition: globals.h:48
std::string getVCorners()
Definition: System.cpp:80
unsigned char * bwImage
Black and white image.
Definition: SLAMflex.cpp:22
void(* sendArrayPoints)(int[], int size)
Definition: SendMessage.h:9
void _StartSlam(void *pointerString, void *pointerPose, void *pointerStringLog, void *pointerArrayOfPoints)
Exported functions from libSlamflex library.
Definition: SLAMflex.cpp:72
void SendTrackerKillSig()
Definition: System.cpp:53
void SetSendMessageToUnity(SendMessage sm)
Definition: System.cpp:42
void convertToBlackWhite(unsigned char *pixels)
Convert Unity3D web texture image to black and white image.
Definition: SLAMflex.cpp:26
void(* sendMessageFunc)(const char *)
Definition: SendMessage.h:6
const char * _SetNewFrame(void *pointer, int width, int height)
Function called each frame with pointer to image and width, height of image.
Definition: SLAMflex.cpp:91
const bool EnableLogging
Definition: globals.h:54
DetectionState GetCurrentDetectionState()
Definition: System.cpp:58
void(* sendLogFunc)(const char *)
Definition: SendMessage.h:7
#define cout
Definition: Bundle.cpp:16
void rgba_to_bgra(unsigned char *pixels, int width, int height)
Change image formats from RGBA to BGRA.
Definition: SLAMflex.cpp:60
void flip_vertically(unsigned char *pixels, const size_t width, const size_t height, const size_t bytes_per_pixel)
Heleper function for pixel manipulation.
Definition: SLAMflex.cpp:44