Subversion Repositories seema-scanner

Rev

Rev 28 | Rev 154 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 28 Rev 74
1
#include "mex.h"
1
#include "mex.h"
2
#include "class_handle.hpp"
2
#include "class_handle.hpp"
3
 
3
 
4
// Output printf statements in the Matlab workspace
4
// Output printf statements in the Matlab workspace
5
#define printf mexPrintf
5
#define printf mexPrintf
6
 
6
 
7
// The class that we are interfacing to
7
// The class that we are interfacing to
8
#include "Camera.h"
8
#include "Camera.h"
9
 
9
 
10
using namespace std;
10
using namespace std;
11
 
11
 
12
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
12
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){
13
    
13
    
14
    mexPrintf("nlhs: %d , nrhs: %d\n", nlhs, nrhs);
14
    //mexPrintf("nlhs: %d , nrhs: %d\n", nlhs, nrhs);
15
    
15
    
16
    // Get the command string
16
    // Get the command string
17
    char cmd[64];
17
    char cmd[64];
18
	if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
18
	if (nrhs < 1 || mxGetString(prhs[0], cmd, sizeof(cmd)))
19
		mexErrMsgTxt("First input should be a command string less than 64 characters long.");
19
		mexErrMsgTxt("First input should be a command string less than 64 characters long.");
20
    
20
    
21
    // Static methods
21
    // Static methods
22
    // InterfaceCameraList
22
    // InterfaceCameraList
23
    if (!strcmp("GetInterfaceCameraList", cmd)) {
23
    if (!strcmp("GetInterfaceCameraList", cmd)) {
24
        // Call the method
24
        // Call the method
25
        vector< vector<CameraInfo> > interfaceCameraList;
25
        vector< vector<CameraInfo> > interfaceCameraList;
26
        interfaceCameraList = Camera::GetInterfaceCameraList();
26
        interfaceCameraList = Camera::GetInterfaceCameraList();
27
        unsigned int nCameras = 0;
27
        unsigned int nCameras = 0;
28
        for(unsigned int i=0; i<interfaceCameraList.size(); i++)
28
        for(unsigned int i=0; i<interfaceCameraList.size(); i++)
29
            nCameras += interfaceCameraList[i].size();
29
            nCameras += interfaceCameraList[i].size();
30
        const char* fieldNames[] = {"interfaceNum", "cameraNum", "vendor","model"};
30
        const char* fieldNames[] = {"interfaceNum", "cameraNum", "vendor","model"};
31
        plhs[0] = mxCreateStructMatrix(1, nCameras, 4, fieldNames);
31
        plhs[0] = mxCreateStructMatrix(1, nCameras, 4, fieldNames);
32
        unsigned int iCamera = 0;
32
        unsigned int iCamera = 0;
33
        for (unsigned int i=0; i<interfaceCameraList.size(); i++) {
33
        for (unsigned int i=0; i<interfaceCameraList.size(); i++) {
34
            for (unsigned int j=0; j<interfaceCameraList[i].size(); j++) {
34
            for (unsigned int j=0; j<interfaceCameraList[i].size(); j++) {
35
                mxSetFieldByNumber(plhs[0], iCamera, 0, mxCreateDoubleScalar(i));
35
                mxSetFieldByNumber(plhs[0], iCamera, 0, mxCreateDoubleScalar(i));
36
                mxSetFieldByNumber(plhs[0], iCamera, 1, mxCreateDoubleScalar(j));
36
                mxSetFieldByNumber(plhs[0], iCamera, 1, mxCreateDoubleScalar(j));
37
                mxSetFieldByNumber(plhs[0], iCamera, 2, mxCreateString(interfaceCameraList[i][j].vendor.c_str()));
37
                mxSetFieldByNumber(plhs[0], iCamera, 2, mxCreateString(interfaceCameraList[i][j].vendor.c_str()));
38
                mxSetFieldByNumber(plhs[0], iCamera, 3, mxCreateString(interfaceCameraList[i][j].model.c_str()));
38
                mxSetFieldByNumber(plhs[0], iCamera, 3, mxCreateString(interfaceCameraList[i][j].model.c_str()));
39
                iCamera++;
39
                iCamera++;
40
            }
40
            }
41
        }
41
        }
42
        return;
42
        return;
43
    }
43
    }
44
    
44
    
45
    // New Camera
45
    // New Camera
46
    if (!strcmp("NewCamera", cmd)) {
46
    if (!strcmp("NewCamera", cmd)) {
47
        // Check parameters
47
        // Check parameters
48
        if (nlhs != 1)
48
        if (nlhs != 1)
49
            mexErrMsgTxt("newCamera: One output expected.");
49
            mexErrMsgTxt("newCamera: One output expected.");
50
        if (nrhs < 3)
50
        if (nrhs < 3)
51
            mexErrMsgTxt("newCamera: Expected interface and camera number argument.");
51
            mexErrMsgTxt("newCamera: Expected interface and camera number argument.");
52
        unsigned int interfaceNum = (unsigned int)mxGetScalar(prhs[1]);
52
        unsigned int interfaceNum = (unsigned int)mxGetScalar(prhs[1]);
53
        unsigned int camNum = (unsigned int)mxGetScalar(prhs[2]);
53
        unsigned int camNum = (unsigned int)mxGetScalar(prhs[2]);
54
        // Return a handle to a new C++ instance
54
        // Return a handle to a new C++ instance
55
        plhs[0] = convertPtr2Mat<Camera>(Camera::NewCamera(interfaceNum, camNum, triggerModeSoftware));
55
        plhs[0] = convertPtr2Mat<Camera>(Camera::NewCamera(interfaceNum, camNum, triggerModeSoftware));
56
        return;
56
        return;
57
    }
57
    }
58
    
58
    
59
    // Class methods
59
    // Class methods
60
    // Check if there is a second input, which should be the class instance handle
60
    // Check if there is a second input, which should be the class instance handle
61
    if (nrhs < 2)
61
    if (nrhs < 2)
62
		mexErrMsgTxt("Second input should be a class instance handle.");
62
		mexErrMsgTxt("Second input should be a class instance handle.");
63
    
63
    
64
    // Delete
64
    // Delete
65
    if (!strcmp("delete", cmd)) {
65
    if (!strcmp("delete", cmd)) {
66
        // Destroy the C++ object
66
        // Destroy the C++ object
67
        destroyObject<Camera>(prhs[1]);
67
        destroyObject<Camera>(prhs[1]);
68
        // Warn if other commands were ignored
68
        // Warn if other commands were ignored
69
        if (nlhs != 0 || nrhs != 2)
69
        if (nlhs != 0 || nrhs != 2)
70
            mexWarnMsgTxt("Delete: Unexpected arguments ignored.");
70
            mexWarnMsgTxt("Delete: Unexpected arguments ignored.");
71
        return;
71
        return;
72
    }
72
    }
73
    
73
    
74
    // Get the class instance pointer from the second input
74
    // Get the class instance pointer from the second input
75
    Camera *Camera_instance = convertMat2Ptr<Camera>(prhs[1]);
75
    Camera *Camera_instance = convertMat2Ptr<Camera>(prhs[1]);
76
    
76
    
77
	// Call the various class methods
77
	// Call the various class methods
78
    // startCapture
78
    // startCapture
79
    if (!strcmp("startCapture", cmd)) {
79
    if (!strcmp("startCapture", cmd)) {
80
        // Check parameters
80
        // Check parameters
81
        if (nlhs > 0 || nrhs > 2)
81
        if (nlhs > 0 || nrhs > 2)
82
            mexErrMsgTxt("startCapture: Unexpected arguments.");
82
            mexErrMsgTxt("startCapture: Unexpected arguments.");
83
        // Call the method
83
        // Call the method
84
        Camera_instance->startCapture();
84
        Camera_instance->startCapture();
85
        return;
85
        return;
86
    }
86
    }
87
    
87
    
88
	// Call the various class methods
88
	// Call the various class methods
89
    // stopCapture
89
    // stopCapture
90
    if (!strcmp("stopCapture", cmd)) {
90
    if (!strcmp("stopCapture", cmd)) {
91
        // Check parameters
91
        // Check parameters
92
        if (nlhs > 0 || nrhs > 2)
92
        if (nlhs > 0 || nrhs > 2)
93
            mexErrMsgTxt("stopCapture: Unexpected arguments.");
93
            mexErrMsgTxt("stopCapture: Unexpected arguments.");
94
        // Call the method
94
        // Call the method
95
        Camera_instance->stopCapture();
95
        Camera_instance->stopCapture();
96
        return;
96
        return;
97
    }
97
    }
98
    
98
    
99
	// trigger
99
	// trigger
100
    if (!strcmp("trigger", cmd)) {
100
    if (!strcmp("trigger", cmd)) {
101
        // Check parameters
101
        // Check parameters
102
        if (nlhs > 0 || nrhs > 2)
102
        if (nlhs > 0 || nrhs > 2)
103
            mexErrMsgTxt("trigger: Unexpected arguments.");
103
            mexErrMsgTxt("trigger: Unexpected arguments.");
104
        // Call the method
104
        // Call the method
105
        Camera_instance->trigger();
105
        Camera_instance->trigger();
106
        return;
106
        return;
107
    }
107
    }
108
    
108
    
109
    // Call the various class methods
109
    // Call the various class methods
110
    // getFrame
110
    // getFrame
111
    if (!strcmp("getFrame", cmd)) {
111
    if (!strcmp("getFrame", cmd)) {
112
        // Check parameters
112
        // Check parameters
113
        if (nlhs < 1 || nrhs > 2)
113
        if (nlhs < 1 || nrhs > 2)
114
            mexErrMsgTxt("getFrame: Unexpected arguments.");
114
            mexErrMsgTxt("getFrame: Unexpected arguments.");
115
        // Call the method
115
        // Call the method
116
        CameraFrame frame = Camera_instance->getFrame();
116
        CameraFrame frame = Camera_instance->getFrame();
117
        // receive frame data in permuted dimensional order
117
        // receive frame data in permuted dimensional order
118
        int dims[] = {frame.channels, frame.width, frame.height};
118
        int dims[] = {frame.channels, frame.width, frame.height};
119
        plhs[0] = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
119
        plhs[0] = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
120
        memcpy(mxGetData(plhs[0]), frame.memory, frame.height*frame.width*frame.channels);
120
        memcpy(mxGetData(plhs[0]), frame.memory, frame.height*frame.width*frame.channels);
121
        return;
121
        return;
122
    }
122
    }
123
    
123
    
124
    // Got here, so command not recognized
124
    // Got here, so command not recognized
125
    mexErrMsgTxt("Command not recognized.");
125
    mexErrMsgTxt("Command not recognized.");
126
}
126
}
127
 
127