1 | /* file: mexutils.c |
---|
2 | ** author: Andrea Vedaldi |
---|
3 | ** description: Utility functions to write MEX files. |
---|
4 | **/ |
---|
5 | |
---|
6 | /* AUTORIGHTS |
---|
7 | Copyright (C) 2006 Andrea Vedaldi |
---|
8 | |
---|
9 | This file is part of VLUtil. |
---|
10 | |
---|
11 | VLUtil is free software; you can redistribute it and/or modify |
---|
12 | it under the terms of the GNU General Public License as published by |
---|
13 | the Free Software Foundation; either version 2, or (at your option) |
---|
14 | any later version. |
---|
15 | |
---|
16 | This program is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | GNU General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software Foundation, |
---|
23 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
24 | */ |
---|
25 | |
---|
26 | #include"mex.h" |
---|
27 | |
---|
28 | #undef M_PI |
---|
29 | #define M_PI 3.14159265358979 |
---|
30 | #define MAX(x,y) (((x)>=(y))?(x):(y)) |
---|
31 | |
---|
32 | /** @biref Is real? |
---|
33 | ** |
---|
34 | ** @return @c true if the array @a A is real (and double). |
---|
35 | **/ |
---|
36 | int |
---|
37 | uIsReal(const mxArray* A) |
---|
38 | { |
---|
39 | return |
---|
40 | mxIsDouble(A) && |
---|
41 | !mxIsComplex(A) ; |
---|
42 | } |
---|
43 | |
---|
44 | /** @biref Is real scalar? |
---|
45 | ** |
---|
46 | ** @return @c true if the array @a A is a real (and double) scalar. |
---|
47 | **/ |
---|
48 | int |
---|
49 | uIsRealScalar(const mxArray* A) |
---|
50 | { |
---|
51 | return |
---|
52 | mxIsDouble(A) && |
---|
53 | !mxIsComplex(A) && |
---|
54 | mxGetNumberOfDimensions(A) == 2 && |
---|
55 | mxGetM(A) == 1 && |
---|
56 | mxGetN(A) == 1 ; |
---|
57 | } |
---|
58 | |
---|
59 | /** @brief Is real matrix? |
---|
60 | ** |
---|
61 | ** The function checks wether the argument @a A is a real (and double) |
---|
62 | ** matrix. In addition, if @a M >= 0, it checks wether the number of |
---|
63 | ** rows is equal to @a M and, if @a N >= 0, if the number of columns |
---|
64 | ** is equal to @a N. |
---|
65 | ** |
---|
66 | ** @param M number of rows. |
---|
67 | ** @param N number of columns. |
---|
68 | ** @return @c true if the array is a real matrix with the specified format. |
---|
69 | **/ |
---|
70 | int |
---|
71 | uIsRealMatrix(const mxArray* A, int M, int N) |
---|
72 | { |
---|
73 | return |
---|
74 | mxIsDouble(A) && |
---|
75 | !mxIsComplex(A) && |
---|
76 | mxGetNumberOfDimensions(A) == 2 && |
---|
77 | ((M>=0)?(mxGetM(A) == M):1) && |
---|
78 | ((N>=0)?(mxGetN(A) == N):1) ; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** @brief Is real array? |
---|
83 | ** |
---|
84 | ** The function checks wether the argument @a A is a real (and double) |
---|
85 | ** array. If @a D >= 0, it also checks wether the arrays a @a D |
---|
86 | ** dimensions. If @a D >=0 and a non null pointer @a dims is |
---|
87 | ** provided, it also check that all dimensions of the array are equal |
---|
88 | ** tao the corresponding non-negative entries of the C-array @a dims. |
---|
89 | ** |
---|
90 | ** @param A array to check. |
---|
91 | ** @param D number of dimensions. |
---|
92 | ** @param dims dimensions. |
---|
93 | ** @return @c true if the array is real and has the specified sizes. |
---|
94 | **/ |
---|
95 | int |
---|
96 | uIsRealArray(const mxArray* A, int D, const int* dims) |
---|
97 | { |
---|
98 | if(!mxIsDouble(A) || mxIsComplex(A)) |
---|
99 | return false ; |
---|
100 | |
---|
101 | if(D >= 0) { |
---|
102 | int d ; |
---|
103 | const int* actual_dims = mxGetDimensions(A) ; |
---|
104 | |
---|
105 | if(mxGetNumberOfDimensions(A) != D) |
---|
106 | return false ; |
---|
107 | |
---|
108 | return true ; |
---|
109 | |
---|
110 | if(dims != NULL) { |
---|
111 | for(d = 0 ; d < D ; ++d) { |
---|
112 | if(dims[d] >= 0 && dims[d] != actual_dims[d]) |
---|
113 | return false ; |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | return true ; |
---|
118 | } |
---|
119 | |
---|
120 | /** @brief Is real vector? |
---|
121 | ** |
---|
122 | ** The function checks wether the argument @a V is a real (and |
---|
123 | ** double) vector. By definiton, a matrix is a vector if one of its |
---|
124 | ** dimension is one. In addition, if @a D >= 0, it checks wether the |
---|
125 | ** dimension of the vecotr is equal to @a D. |
---|
126 | ** |
---|
127 | ** @param D lenght of the vector. |
---|
128 | ** @return @c true if the array is a real vector of the specified dimension. |
---|
129 | **/ |
---|
130 | int |
---|
131 | uIsRealVector(const mxArray* V, int D) |
---|
132 | { |
---|
133 | int M = mxGetM(V) ; |
---|
134 | int N = mxGetN(V) ; |
---|
135 | int is_vector = (N == 1) || (M == 1) ; |
---|
136 | |
---|
137 | return |
---|
138 | mxIsDouble(V) && |
---|
139 | !mxIsComplex(V) && |
---|
140 | mxGetNumberOfDimensions(V) == 2 && |
---|
141 | is_vector && |
---|
142 | ( D < 0 || N == D || M == D) ; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | /** @brief Is a string? |
---|
147 | ** |
---|
148 | ** The function checks wether the array @a S is a string. If |
---|
149 | ** @a L is non-negative, it also check wether the strign has |
---|
150 | ** length @a L. |
---|
151 | ** |
---|
152 | ** @return @a c true if S is a string of the specified length. |
---|
153 | **/ |
---|
154 | int |
---|
155 | uIsString(const mxArray* S, int L) |
---|
156 | { |
---|
157 | int M = mxGetM(S) ; |
---|
158 | int N = mxGetN(S) ; |
---|
159 | |
---|
160 | return |
---|
161 | mxIsChar(S) && |
---|
162 | M == 1 && |
---|
163 | (L < 0 || N == L) ; |
---|
164 | } |
---|