mexopencv  3.4.1
MEX interface for OpenCV library
TickMeter_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
13 // Persistent objects
15 int last_id = 0;
18 
24 {
26  if (arr.isNull())
27  mexErrMsgIdAndTxt("mexopencv:error", "Allocation error");
28  arr.set(0, i);
29  return arr;
30 }
31 }
32 
40 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
41 {
42  // Check the number of arguments
43  nargchk(nrhs>=2 && nlhs<=1);
44 
45  // Argument vector
46  vector<MxArray> rhs(prhs, prhs+nrhs);
47  int id = rhs[0].toInt();
48  string method(rhs[1].toString());
49 
50  // Constructor is called. Create a new object from argument
51  if (method == "new") {
52  nargchk(nrhs==2 && nlhs<=1);
53  obj_[++last_id] = makePtr<TickMeter>();
54  plhs[0] = MxArray(last_id);
55  mexLock();
56  return;
57  }
58  // static method calls
59  else if (method == "getTickCount") {
60  nargchk(nrhs==2 && nlhs<=1);
61  plhs[0] = toMxArray(getTickCount());
62  return;
63  }
64  else if (method == "getTickFrequency") {
65  nargchk(nrhs==2 && nlhs<=1);
66  plhs[0] = MxArray(getTickFrequency());
67  return;
68  }
69  else if (method == "getCPUTickCount") {
70  nargchk(nrhs==2 && nlhs<=1);
71  plhs[0] = toMxArray(getCPUTickCount());
72  return;
73  }
74 
75  // Big operation switch
76  Ptr<TickMeter> obj = obj_[id];
77  if (obj.empty())
78  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
79  if (method == "delete") {
80  nargchk(nrhs==2 && nlhs==0);
81  obj_.erase(id);
82  mexUnlock();
83  }
84  else if (method == "start") {
85  nargchk(nrhs==2 && nlhs==0);
86  obj->start();
87  }
88  else if (method == "stop") {
89  nargchk(nrhs==2 && nlhs==0);
90  obj->stop();
91  }
92  else if (method == "reset") {
93  nargchk(nrhs==2 && nlhs==0);
94  obj->reset();
95  }
96  else if (method == "get") {
97  nargchk(nrhs==3 && nlhs<=1);
98  string prop(rhs[2].toString());
99  if (prop == "TimeTicks")
100  plhs[0] = toMxArray(obj->getTimeTicks());
101  else if (prop == "TimeMicro")
102  plhs[0] = MxArray(obj->getTimeMicro());
103  else if (prop == "TimeMilli")
104  plhs[0] = MxArray(obj->getTimeMilli());
105  else if (prop == "TimeSec")
106  plhs[0] = MxArray(obj->getTimeSec());
107  else if (prop == "Counter")
108  plhs[0] = toMxArray(obj->getCounter());
109  else
110  mexErrMsgIdAndTxt("mexopencv:error",
111  "Unrecognized property %s", prop.c_str());
112  }
113  else
114  mexErrMsgIdAndTxt("mexopencv:error",
115  "Unrecognized operation %s", method.c_str());
116 }
MxArray toMxArray(int64_t i)
MxArray constructor from 64-bit integer.
Definition: TickMeter_.cpp:23
int64 getTimeTicks() const
double getTickFrequency()
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
int last_id
Last object id to allocate.
Definition: TickMeter_.cpp:15
STL namespace.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
Definition: MxArray.hpp:1310
STL class.
double getTimeMicro() const
int64 getCounter() const
map< int, Ptr< TickMeter > > obj_
Object container.
Definition: TickMeter_.cpp:17
int64 getCPUTickCount()
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
#define mxCreateNumericMatrix
Definition: matrix.h:1516
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
STL class.
bool empty() const
Global constant definitions.
double getTimeSec() const
T c_str(T... args)
double getTimeMilli() const
bool isNull() const
Determine whether the array is initialized or not.
Definition: MxArray.hpp:606
Identifies a numeric mxArray whose data is stored as the type specified in the MATLAB Primitive Types...
Definition: matrix.h:305
int64 getTickCount()
Identifies an mxArray with no imaginary components.
Definition: matrix.h:325
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: TickMeter_.cpp:40