Exemplo n.º 1
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	// Check number of arguments
	if (nrhs != 1)
		mexErrMsgTxt("Unexpected number of input arguments.");
	if (nlhs != 1)
		mexErrMsgTxt("Unexpected number of output arguments.");

	// Check argument types are valid
	if (mxGetClassID(prhs[0]) != mxDOUBLE_CLASS || mxIsComplex(prhs[0]))
		mexErrMsgTxt("Input must be a real double matrix");
    int nparams = mxGetM(prhs[0]);
    if (nparams != 3 && nparams != 6 && nparams != 7)
        mexErrMsgTxt("Input must have 3, 6 or 7 rows");
    
    // Construct the output array
    int ndims = mxGetNumberOfDimensions(prhs[0]);
	if (ndims > 19)
		mexErrMsgTxt("Input has an unsupported number of dimensions");
	size_t out_dims[20];
	out_dims[0] = 3;
	out_dims[1] = 3 + (nparams != 3);
	const mwSize *dims = mxGetDimensions(prhs[0]);
	int nmatrices = 1;
	for (int a = 1; a < ndims; ++a) {
		out_dims[a+1] = dims[a];
		nmatrices *= dims[a];
	}
    plhs[0] = mxCreateUninitNumericArray(ndims+1, out_dims, mxDOUBLE_CLASS, mxREAL);
    double *out = (double *)mxGetData(plhs[0]);
    
    // Get the data pointers and strides
    double *r = (double *)mxGetData(prhs[0]);
    double *t = NULL;
    int t_stride = 0;
    if (nparams > 3) {
        t = r + 3;
        t_stride = nparams;
    }
    double s_ = 0;
    double *s = &s_;
    int s_stride = 0;
    if (nparams == 7) { 
        s = r + 6;
        s_stride = 7;
    }
    int out_stride = 3 * (3 + (nparams != 3));
	    
	// Call the expm function
    int a;
#pragma omp parallel for if (nmatrices > 1000) num_threads(omp_get_num_procs()) default(shared) private(a)
    for (a = 0; a < nmatrices; ++a)
        expm_srt_3d(&out[out_stride*a], &r[nparams*a], &t[t_stride*a], s[s_stride*a]);

	return;
}
Exemplo n.º 2
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	// Check number of arguments
	if (nrhs < 3 || nrhs > 5)
		mexErrMsgTxt("Unexpected number of input arguments.");
	if (nlhs < 1 || nlhs > 2)
		mexErrMsgTxt("Unexpected number of output arguments.");

	// Check argument types are valid
	mxClassID in_class = mxGetClassID(prhs[1]);
	if (mxGetClassID(prhs[2]) != in_class)
		mexErrMsgTxt("X and Y must be arrays of the same type");
	for (int i = 0; i < nrhs; ++i) {
        if (mxIsComplex(prhs[i]))
			mexErrMsgTxt("Inputs cannot be complex.");
	}

	// Get and check array dimensions
	int num_points = static_cast<int>(mxGetNumberOfElements(prhs[1]));
	if (num_points != static_cast<int>(mxGetNumberOfElements(prhs[2])))
		mexErrMsgTxt("X and Y must have the same dimensions");
	int ndims = static_cast<int>(mxGetNumberOfDimensions(prhs[0]));
	std::vector<size_t> out_dims(ndims+2);
    out_dims[0] = 2;
	out_dims[1] = mxGetM(prhs[1]);
	out_dims[2] = mxGetN(prhs[1]);
	const mwSize *dims = mxGetDimensions(prhs[0]);
	out_dims[3] = 1;
	int nchannels = 1;
	for (int i = 2; i < ndims; ++i) {
		out_dims[i+1] = dims[i];
		nchannels *= static_cast<int>(dims[i]);
	}
	
	// Get the out of bounds value (oobv) and set the output class to the same class as the oobv.
	double oobv;
	mxClassID out_class;
	if (nrhs > 4) {
		// Get the value for oobv
		if (mxGetNumberOfElements(prhs[4]) != 1)
			mexErrMsgTxt("oobv must be a scalar.");
		oobv = mxGetScalar(prhs[4]);
		out_class = mxGetClassID(prhs[4]);
	} else {
		// Use the default value for oobv
		oobv = mxGetNaN();
		out_class = in_class;
	}
	
	// Create the output arrays
	plhs[0] = mxCreateUninitNumericArray(ndims, &out_dims[1], out_class, mxREAL);
	void *B = mxGetData(plhs[0]);
    void *G = NULL;
    if (nlhs > 1) {
        plhs[1] = mxCreateUninitNumericArray(ndims+1, &out_dims[0], out_class, mxREAL);
        G = mxGetData(plhs[1]);
    }

	// Get the interpolation method
    char buffer[10] = {'l'};
    int k = 0;
    if (nrhs > 3) {
        // Read in the method string
        if (mxGetString(prhs[3], buffer, sizeof(buffer)))
            mexErrMsgTxt("Unrecognised interpolation method");
        // Remove '*' from the start
        k += (buffer[k] == '*');
        // Remove 'bi' from the start
        k += 2 * ((buffer[k] == 'b') & (buffer[k+1] == 'i'));
    }
    
    // Get pointer to the input image
    const void *A = mxGetData(prhs[0]);
    
	// Call the first wrapper function according to the input image type
    const int width = static_cast<int>(dims[1]);
    const int height = static_cast<int>(dims[0]);
	switch (mxGetClassID(prhs[0])) {
		case mxDOUBLE_CLASS:
			wrapper_func(B, G, (const double *)A, prhs, num_points, width, height, nchannels, oobv, buffer[k], out_class, in_class);
			break;
		case mxSINGLE_CLASS:
			wrapper_func(B, G, (const float *)A, prhs, num_points, width, height, nchannels, oobv, buffer[k], out_class, in_class);
			break;
		case mxINT8_CLASS:
			wrapper_func(B, G, (const int8_t *)A, prhs, num_points, width, height, nchannels, oobv, buffer[k], out_class, in_class);
			break;
		case mxUINT8_CLASS:
			wrapper_func(B, G, (const uint8_t *)A, prhs, num_points, width, height, nchannels, oobv, buffer[k], out_class, in_class);
			break;
		case mxINT16_CLASS:
			wrapper_func(B, G, (const int16_t *)A, prhs, num_points, width, height, nchannels, oobv, buffer[k], out_class, in_class);
			break;
		case mxUINT16_CLASS:
			wrapper_func(B, G, (const uint16_t *)A, prhs, num_points, width, height, nchannels, oobv, buffer[k], out_class, in_class);
			break;
		case mxLOGICAL_CLASS:
			wrapper_func(B, G, (const mxLogical *)A, prhs, num_points, width, height, nchannels, oobv, buffer[k], out_class, in_class);
			break;
		default:
			mexErrMsgTxt("A is of an unsupported type");
			break;
	}
	return;
}
Exemplo n.º 3
0
bool* shared_lib_func(unsigned char* raw, V3DLONG total_bytes, V3DLONG unit_bytes, V3DLONG x, V3DLONG y, V3DLONG z, V3DLONG t,int paran, double* para,std::string fileDir){
	
	std::cerr << "Matlab Part Start." << std::endl;

	// Initialize the MATLAB Compiler Runtime global state
	/*if (!mclInitializeApplication(NULL, 0))
	{
		std::cerr << "Could not initialize the application properly." << mclGetLastErrorMessage()
			<< 'Z' << std::endl;
		//return -1;
	}*/
	// Initialize the Vigenere library
	if (!libvaa_port_testInitialize())
	{
		std::cerr << "Could not initialize the library properly." << mclGetLastErrorMessage()
			<< 'Z' <<std::endl;
		//return -1;
	}
	std::cout << "Intialization Finished" << std::endl;
	mxArray *mx_output = NULL;
	size_t raw_dim[1] = {x*y*z*t};
	size_t dim[1] = { 1 };
	size_t dim_para[1];
	dim_para[0] = paran;

	mxArray *mx_raw, *mx_unit_bytes, *mx_x, *mx_y, *mx_z, *mx_t, *mx_para,*mx_fileDir;
	int pause;
	unsigned char *dynamic_raw = (unsigned char *)mxCalloc(total_bytes, sizeof(UINT8_T));
	for (V3DLONG i = 0; i < total_bytes; i++){
		dynamic_raw[i] = raw[i];
	}
	mx_raw = mxCreateUninitNumericArray(1, raw_dim, get_mxClass(unit_bytes),mxREAL);
	mxSetData(mx_raw, dynamic_raw);
	
	V3DLONG *dynamic_unit_bytes = (V3DLONG *)mxCalloc(1, sizeof(V3DLONG));
	dynamic_unit_bytes[0] = unit_bytes;
	mx_unit_bytes = mxCreateUninitNumericArray(1, dim, get_mxClass(8), mxREAL);
	mxSetData(mx_unit_bytes, dynamic_unit_bytes);

	V3DLONG *dynamic_x = (V3DLONG *)mxCalloc(1, sizeof(V3DLONG));
	dynamic_x[0] = x;
	mx_x = mxCreateUninitNumericArray(1, dim, get_mxClass(8), mxREAL);
	mxSetData(mx_x, dynamic_x);

	V3DLONG *dynamic_y = (V3DLONG *)mxCalloc(1, sizeof(V3DLONG));
	dynamic_y[0] = y;
	mx_y = mxCreateUninitNumericArray(1, dim, get_mxClass(8), mxREAL);
	mxSetData(mx_y, dynamic_y);

	V3DLONG *dynamic_z = (V3DLONG *)mxCalloc(1, sizeof(V3DLONG));
	dynamic_z[0] = z;
	mx_z = mxCreateUninitNumericArray(1, dim, get_mxClass(8), mxREAL);
	mxSetData(mx_z, dynamic_z);

	V3DLONG *dynamic_t = (V3DLONG *)mxCalloc(1, sizeof(V3DLONG));
	dynamic_t[0] = t;
	mx_t = mxCreateUninitNumericArray(1, dim, get_mxClass(8), mxREAL);
	mxSetData(mx_t, dynamic_t);

	double *dynamic_para = (double *)mxCalloc(paran, sizeof(double));
	for (int i = 0; i < paran; i++){
		dynamic_para[i] = para[i];
	}
	mx_para = mxCreateUninitNumericArray(1, dim_para, mxDOUBLE_CLASS, mxREAL);
	mxSetData(mx_para, dynamic_para);

        mx_fileDir = mxCreateString(fileDir.c_str());
	std::cout << "Assignment Finished\n";
	mlfVaa3d_trace3D(1, &mx_output, mx_raw, mx_unit_bytes, mx_x, mx_y, mx_z, mx_t, mx_para,mx_fileDir);
	//y = (double*)mxGetPr(y_ptr);
	//std::cout << "answer:" << *y << '\n';
	// Shut down the library and the application global state.
	bool* output = mxGetLogicals(mx_output);

	
	mxDestroyArray(mx_raw);
	std::cout << "free mx raw\n";
	//mxDestroyArray(mx_output);
	//std::cout << "free mx output";
	mxDestroyArray(mx_unit_bytes);
	std::cout << "free mx unit\n";
	mxDestroyArray(mx_x);
	std::cout << "free mx x\n";
	mxDestroyArray(mx_y);
	std::cout << "free mx y\n";
	mxDestroyArray(mx_z);
	std::cout << "free mx z\n";
	mxDestroyArray(mx_t);
	std::cout << "free mx t\n";
	mxDestroyArray(mx_para);
	std::cout << "free mx para\n";
	libvaa_port_testTerminate();
	//mclTerminateApplication();
	std::cout << "Matlab Part Finished." << std::endl;
	return output;
}