File: MexInterface.cpp

package info (click to toggle)
freemat 4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 174,736 kB
  • ctags: 67,053
  • sloc: cpp: 351,060; ansic: 255,892; sh: 40,590; makefile: 4,323; perl: 4,058; asm: 3,313; pascal: 2,718; fortran: 1,722; ada: 1,681; ml: 1,360; cs: 879; csh: 795; python: 430; sed: 162; lisp: 160; awk: 5
file content (137 lines) | stat: -rw-r--r-- 5,150 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (c) 2002-2006 Samit Basu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "Array.hpp"
#include "mex.h"
#include "MexInterface.hpp"

static NTuple GetDimensions(const mxArray *dp) {
  NTuple dim;
  for (int i=0;i<dp->number_of_dims;i++)
    dim[i] = dp->dims[i];
  return dim;
}

static int* GetDimensions(const Array& dp, int &numdims) {
  numdims = dp.dimensions().lastNotOne();
  int *dim_vec = (int*) malloc(sizeof(int)*numdims);
  for (int i=0;i<numdims;i++)
    dim_vec[i] = int(dp.dimensions()[i]);
  return dim_vec;
}

template <typename T>
static BasicArray<T> BasicArrayFromMexArray(const mxArray *dp, const void *qp) {
  BasicArray<T> rp(GetDimensions(dp));
  memcpy(rp.data(),qp,size_t(sizeof(T)*rp.length()));
  return rp;
}

template <typename T>
static Array TNumericArrayFromMexArray(const mxArray *dp) {
  if (dp->issparse) 
    throw Exception("mex interface does not support sparse matrices");
  if (dp->iscomplex)
    return Array(BasicArrayFromMexArray<T>(dp,dp->realdata),
		 BasicArrayFromMexArray<T>(dp,dp->imagdata));
  else
    return Array(BasicArrayFromMexArray<T>(dp,dp->realdata));
}

static Array NumericArrayFromMexArray(const mxArray *dp) {
  switch (dp->classID) {
  default:
    throw Exception("Unhandled type for mex array conversion");
  case mxUINT32_CLASS: return TNumericArrayFromMexArray<uint32>(dp);
  case mxINT32_CLASS:  return TNumericArrayFromMexArray<int32>(dp);
  case mxUINT16_CLASS: return TNumericArrayFromMexArray<uint16>(dp);
  case mxINT16_CLASS:  return TNumericArrayFromMexArray<int16>(dp);
  case mxUINT8_CLASS:  return TNumericArrayFromMexArray<uint8>(dp);
  case mxINT8_CLASS:   return TNumericArrayFromMexArray<int8>(dp);
  case mxSINGLE_CLASS: return TNumericArrayFromMexArray<float>(dp);
  case mxDOUBLE_CLASS: return TNumericArrayFromMexArray<double>(dp);
  case mxCHAR_CLASS:   return TNumericArrayFromMexArray<int16>(dp);
  }
}

Array ArrayFromMexArray(mxArray *array_ptr) {
  if (array_ptr->classID == mxCELL_CLASS) {
    NTuple dim = GetDimensions(array_ptr);
    mxArray** dp = (mxArray**) array_ptr->realdata;
    BasicArray<Array> rp(dim);
    for (index_t i=1;i<=rp.length();i++)
      rp[i] = ArrayFromMexArray(dp[int(i-1)]);
    return Array(rp);
  } else 
    return NumericArrayFromMexArray(array_ptr);
}

static mxArray* MexArrayFromCellArray(Array array) {
  // Convert array dimensions into a simple integer array
  int num_dim = array.dimensions().lastNotOne();
  int *dim_vec = (int*) malloc(sizeof(int)*num_dim);
  for (int i=0;i<num_dim;i++)
    dim_vec[i] = int(array.dimensions()[i]);
  mxArray *ret = mxCreateCellArray(num_dim,dim_vec);
  const BasicArray<Array> &rp(array.constReal<Array>());
  mxArray **dp = (mxArray **) ret->realdata;
  for (index_t i=1;i<=array.length();i++)
    dp[int(i-1)] = MexArrayFromArray(rp[i]);
  free(dim_vec);
  return ret;
}

template <typename T>
static mxArray* TMexArrayFromNumericArray(mxClassID classID, Array array) {
  if (array.isSparse()) throw Exception("sparse case not handled");
  mxComplexity flag;
  if (array.allReal())
    flag = mxREAL;
  else
    flag = mxCOMPLEX;
  int numdims;
  int* dims = GetDimensions(array,numdims);
  mxArray* ret = mxCreateNumericArray(numdims,dims,classID,flag);
  memcpy(ret->realdata,array.constReal<T>().constData(),size_t(array.length()*sizeof(T)));
  memcpy(ret->imagdata,array.constImag<T>().constData(),size_t(array.length()*sizeof(T)));
  return ret;
}

mxArray* MexArrayFromNumericArray(Array array) {
  switch (array.dataClass()) {
  default: throw Exception("unhandled case for numeric array");
  case UInt32: return TMexArrayFromNumericArray<uint32>(mxUINT32_CLASS,array);
  case Int32: return TMexArrayFromNumericArray<int32>(mxINT32_CLASS,array);
  case UInt16: return TMexArrayFromNumericArray<uint16>(mxUINT16_CLASS,array);
  case Int16: return TMexArrayFromNumericArray<int16>(mxINT16_CLASS,array);
  case UInt8: return TMexArrayFromNumericArray<uint8>(mxUINT8_CLASS,array);
  case Int8: return TMexArrayFromNumericArray<int8>(mxINT8_CLASS,array);
  case Float: return TMexArrayFromNumericArray<float>(mxSINGLE_CLASS,array);
  case Double: return TMexArrayFromNumericArray<double>(mxDOUBLE_CLASS,array);
  case StringArray: return TMexArrayFromNumericArray<uint16>(mxCHAR_CLASS,array);
  }
}

mxArray* MexArrayFromArray(Array array) {
  if (array.dataClass() == CellArray)
    return MexArrayFromCellArray(array);
  else
    return MexArrayFromNumericArray(array);
}