Woolz Image Processing  Version 1.8.3
AlgType.h
Go to the documentation of this file.
1 #ifndef ALGTYPE_H
2 #define ALGTYPE_H
3 #if defined(__GNUC__)
4 #ident "University of Edinburgh $Id: 11ae03e5d828d0fdd55b8ea78ff6a8f88ec8693b $"
5 #else
6 static char _AlgType_h[] = "University of Edinburgh $Id: 11ae03e5d828d0fdd55b8ea78ff6a8f88ec8693b $";
7 #endif
8 /*!
9 * \file libAlg/AlgType.h
10 * \author Bill Hill
11 * \date March 1999
12 * \version $Id: 11ae03e5d828d0fdd55b8ea78ff6a8f88ec8693b $
13 * \par
14 * Address:
15 * MRC Human Genetics Unit,
16 * MRC Institute of Genetics and Molecular Medicine,
17 * University of Edinburgh,
18 * Western General Hospital,
19 * Edinburgh, EH4 2XU, UK.
20 * \par
21 * Copyright (C), [2012],
22 * The University Court of the University of Edinburgh,
23 * Old College, Edinburgh, UK.
24 *
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version 2
28 * of the License, or (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be
31 * useful but WITHOUT ANY WARRANTY; without even the implied
32 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
33 * PURPOSE. See the GNU General Public License for more
34 * details.
35 *
36 * You should have received a copy of the GNU General Public
37 * License along with this program; if not, write to the Free
38 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
39 * Boston, MA 02110-1301, USA.
40 * \brief Type definitions for the Woolz numerical algorithm
41 * library.
42 * \ingroup Alg
43 */
44 
45 
46 #ifndef WLZ_EXT_BIND
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 #endif /* WLZ_EXT_BIND */
51 
52 /* Standard min, max, absolute value and nearest integer macros */
53 #define ALG_MAX(X,Y) (((X)>(Y))?(X):(Y))
54 #define ALG_MIN(X,Y) (((X)<(Y))?(X):(Y))
55 #define ALG_CLAMP(V,N,X) (((V)<(N))?(N):((V)>(X))?(X):(V))
56 #define ALG_MAXIDX(X,Y) (((X)>(Y))?(0):(1))
57 #define ALG_MINIDX(X,Y) (((X)<(Y))?(0):(1))
58 #define ALG_ABS(X) (((X)>0)?(X):(-(X)))
59 #define ALG_NINT(X) ((int)(((X)<0)?((X)-(0.5)):((X)+(0.5))))
60 #define ALG_SQR(X) ((X)*(X))
61 #define ALG_MAX3(X,Y,Z) (((X)>(Y))?(((X)>(Z))?(X):(Z)):(((Y)>(Z))?(Y):(Z)))
62 #define ALG_MIN3(X,Y,Z) (((X)<(Y))?(((X)<(Z))?(X):(Z)):(((Y)<(Z))?(Y):(Z)))
63 #define ALG_MAXIDX3(X,Y,Z) \
64  (((X)>(Y))?(((X)>(Z))?(0):(3)):(((Y)>(Z))?(1):(3)))
65 #define ALG_MININD3(X,Y,Z) \
66  (((X)<(Y))?(((X)<(Z))?(0):(3)):(((Y)<(Z))?(1):(3)))
67 
68 /* Determinannts on 2x2 and 3x3 matrices as an ordered 1D array. */
69 #define ALG_DETERMINANT_4(M) \
70  ((((M)[0])*((M)[3]))-(((M)[1])*((M)[2])))
71 #define ALG_DETERMINANT_9(M) \
72  ((((M)[0])*((M)[4])*((M)[8]))-(((M)[0])*((M)[5])*((M)[7]))+ \
73  (((M)[1])*((M)[5])*((M)[6]))-(((M)[1])*((M)[3])*((M)[8]))+ \
74  (((M)[2])*((M)[3])*((M)[7]))-(((M)[2])*((M)[4])*((M)[6])))
75 
76 /* Standard math constants */
77 #define ALG_M_E (2.7182818284590452354)
78 #define ALG_M_LOG2E (1.4426950408889634074)
79 #define ALG_M_LOG10E (0.43429448190325182765)
80 #define ALG_M_LN2 (0.69314718055994530942)
81 #define ALG_M_LN10 (2.30258509299404568402)
82 #define ALG_M_PI (3.14159265358979323846)
83 #define ALG_M_PI_2 (1.57079632679489661923)
84 #define ALG_M_PI_4 (0.78539816339744830961)
85 #define ALG_M_1_PI (0.31830988618379067154)
86 #define ALG_M_2_PI (0.63661977236758134308)
87 #define ALG_M_2_SQRTPI (1.12837916709551257390)
88 #define ALG_M_SQRT2 (1.41421356237309504880)
89 #define ALG_M_SQRT3 (1.73205080756887729353)
90 #define ALG_M_SQRT1_2 (0.70710678118654752440)
91 
92 /* A tollerance value for double precission arithmetic. */
93 #define ALG_DBL_TOLLERANCE (1.0E-9)
94 
95 /* Maximmum orderr of Gauss-Legendre polynomial supported. */
96 #define ALG_GAUSSLEGENDRE_ORDER_MAX (6)
97 
98 /*!
99 * \enum _AlgDistribution
100 * \brief Statistical distributions.
101 * Typedef: ::AlgDistribution.
102 */
103 typedef enum _AlgDistribution
104 {
110 
111 /*!
112 * \enum _AlgMatrixType
113 * \brief Matrix representations.
114 * Typedef: ::AlgMatrixType
115 */
116 typedef enum _AlgMatrixType
117 {
118  ALG_MATRIX_NULL = 0, /*!< A NULL matric with no elements. */
119  ALG_MATRIX_RECT, /*!< Rectangular matrix, with storage
120  for each element. These matrices should
121  be allocated using the libAlc array
122  allocation functions. */
123  ALG_MATRIX_SYM, /*!< Symmetric matrix, with storage
124  for the upper triangle only. These
125  matrices should be allocated using the
126  libAlc symmetric array allocation
127  functions. */
128  ALG_MATRIX_LLR /*!< Sparse matrix stored in linked list
129  row format. */
130 } AlgMatrixType;
131 
132 /*!
133 * \struct _AlgMatrix
134 * \brief A union of all valid matrix types.
135 * Typedef: ::AlgMatrix..
136 */
137 typedef union _AlgMatrix
138 {
143 } AlgMatrix;
144 
145 /*!
146 * \struct _AlgMatrixCore
147 * \brief A core matrix type with members common to all matrix types.
148 * Typedef: ::AlgMatrixCore.
149 */
150 typedef struct _AlgMatrixCore
151 {
152  AlgMatrixType type; /*!< Matrix type. */
153  size_t nR; /*!< Number of rows. */
154  size_t nC; /*!< Number of columns. */
155 } AlgMatrixCore;
156 
157 /*!
158 * \struct _AlgMatrixRect
159 * \brief Rectangular matrix.
160 * Typedef: ::AlgMatrixRect.
161 */
162 typedef struct _AlgMatrixRect
163 {
164  AlgMatrixType type; /*!< From AlgmatrixCore. */
165  size_t nR; /*!< From AlgmatrixCore. */
166  size_t nC; /*!< From AlgmatrixCore. */
167  size_t maxR; /*!< Rows space allocated for. */
168  size_t maxC; /*!< Columns space allocated for. */
169  double **array; /* Array of elements. */
170 } AlgMatrixRect;
171 
172 /*!
173 * \struct _AlgMatrixSym
174 * \brief Symmetric matrix.
175 * Typedef: ::AlgMatrixRect.
176 */
177 typedef struct _AlgMatrixSym
178 {
179  AlgMatrixType type; /*!< From AlgmatrixCore. */
180  size_t nR; /*!< From AlgmatrixCore. */
181  size_t nC; /*!< From AlgmatrixCore. */
182  size_t maxN; /*!< Max rows/columns space allocated for. */
183  double **array;
184 } AlgMatrixSym;
185 
186 /*!
187 * \struct _AlgMatrixLLRE
188 * \brief Entry in the linked list row matrix.
189 * Typedef: ::AlgMatrixLLRE.
190 */
191 typedef struct _AlgMatrixLLRE
192 {
193  size_t col; /*!< Column in matrix. */
194  double val; /*!< Value in the row, column. */
195  struct _AlgMatrixLLRE *nxt; /*!< Next entry either in the value list or
196  the free list. */
197 } AlgMatrixLLRE;
198 
199 /*!
200 * \struct _AlgMatrixLLRE
201 * \brief Linked list row matrix, in which the values are stored
202 * in linked lists, with a linked list for each row of
203 * the matrix. This can be very efficient if the matrix
204 * is very sparse, but is very ineffiecient if the matrix
205 * is dense. The cross over is around 5 percent values
206 * being non-zero. At 1 percent of values being non-zero
207 * the linked list matrix is faster than a rectangular matrix
208 * (eg for matrix multiplication) by about a factor of ten.
209 */
210 typedef struct _AlgMatrixLLR
211 {
212  AlgMatrixType type; /*!< From AlgmatrixCore. */
213  size_t nR; /*!< From AlgmatrixCore. */
214  size_t nC; /*!< From AlgmatrixCore. */
215  size_t numEnt; /*!< Number of (no-zero) entries. */
216  size_t maxEnt; /*!< Maximum number of entries. */
217  double tol; /*!< Lowest absolute non-zero value. */
218  void *blk; /*!< Stack of blocks of triples allocated
219  managed using AlcFreeStack. */
220  AlgMatrixLLRE *freeStk; /*!< Stack of free linked list entries. */
221  AlgMatrixLLRE **tbl; /*!< Table of matrix linked lists with a list
222  for each row of the matrix. */
223 } AlgMatrixLLR;
224 
225 typedef struct _AlgMatrixTriple
226 {
227  size_t row; /*!< Row in matrix. */
228  size_t col; /*!< Column in matrix. */
229  double val; /*!< Value in the row, column. */
231 
232 /*!
233 * \enum _AlgPadType
234 * \brief Types of daat padding.
235 * Typedef: ::AlgPadType.
236 */
237 typedef enum _AlgPadType
238 {
239  ALG_PAD_NONE, /*!< No padding, same as padding with zeros. */
240  ALG_PAD_ZERO, /*!< Pad data with zeros. */
241  ALG_PAD_END, /*!< Pad data with first/last data values. */
242  ALG_PAD_VALUE /*!< Pad data with given value. */
243 } AlgPadType;
244 
245 /*!
246 * \struct _ComplexD
247 * \brief Complex number data type.
248 * Typedef: ::ComplexD.
249 */
250 typedef struct _ComplexD
251 {
252  double re;
253  double im;
254 } ComplexD;
255 
256 
257 /*
258 * \enum _AlgError
259 * \brief Error codes.
260 * Typedef: ::AlgError.
261 */
262 typedef enum _AlgError
263 {
265  ALG_ERR_CONVERGENCE, /*!< Failure to converge. */
266  ALG_ERR_DIVZERO, /*!< Divide by zero. */
267  ALG_ERR_FUNC, /*!< Function parameters invalid. */
268  ALG_ERR_MALLOC, /*!< Memory allocation failure. */
269  ALG_ERR_MATRIX_CONDITION, /*!< Matrix condition number out of range. */
270  ALG_ERR_MATRIX_HOMOGENEOUS, /*!< Homogeneous matrix. */
271  ALG_ERR_MATRIX_SINGULAR, /*!< Singular matrix. */
272  ALG_ERR_MATRIX_TYPE, /*!< Invalid matrix type given. */
273  ALG_ERR_NONGLOBAL, /*!< Finds local solution, but fails to global
274  solution. */
275  ALG_ERR_READ, /*!< Read failure. */
276  ALG_ERR_WRITE, /*!< Write failure. */
277  ALG_ERR_UNIMPLEMENTED, /*!< Unimplemented code. */
278  ALG_ERR_UNKNOWN, /*!< Catch all, all other error causes. */
280 } AlgError;
281 
282 /*
283 * \enum _AlgDbgMask
284 * \brief Debug mask values.
285 * Typedef: ::AlgDbgMask.
286 */
287 typedef enum _AlgDbgMask
288 {
291  ALG_DBG_LVL_2 = (1<<1),
292  ALG_DBG_LVL_3 = (1<<2),
293  ALG_DBG_LVL_FN = (1<<3)
294 } AlgDbgMask;
295 
296 #ifndef CTYPESGEN
297 typedef AlgError (*AlgDbgFn)(char *, ...);
298 
299 extern AlgDbgFn algDbgOutFn;
300 #define ALG_DBG_FN (*algDbgOutFn)
301 #define ALG_DBG(F,M) ((((F)&(algDbgMask))==(F))?ALG_DBG_FN M:ALG_ERR_NONE)
302 #endif
303 
304 #ifndef WLZ_EXT_BIND
305 #ifdef __cplusplus
306 }
307 #endif /* __cplusplus */
308 #endif /* WLZ_EXT_BIND */
309 
310 #endif /* ! ALGTYPE_H */
Definition: AlgType.h:242
double ** array
Definition: AlgType.h:169
Definition: AlgType.h:265
_AlgDbgMask
Definition: AlgType.h:287
struct _AlgMatrixCore * core
Definition: AlgType.h:139
AlgDbgFn algDbgOutFn
Definition: AlgDebug.c:49
size_t maxC
Definition: AlgType.h:168
Definition: AlgType.h:278
size_t maxN
Definition: AlgType.h:182
AlgMatrixType type
Definition: AlgType.h:179
enum _AlgMatrixType AlgMatrixType
size_t col
Definition: AlgType.h:228
Definition: AlgType.h:267
Definition: AlgType.h:123
_AlgDistribution
Statistical distributions. Typedef: AlgDistribution.
Definition: AlgType.h:103
struct _AlgMatrixRect AlgMatrixRect
Definition: AlgType.h:240
Definition: AlgType.h:118
size_t nC
Definition: AlgType.h:181
size_t maxR
Definition: AlgType.h:167
Symmetric matrix. Typedef: AlgMatrixRect.
Definition: AlgType.h:177
AlgMatrixType type
Definition: AlgType.h:212
AlgError(* AlgDbgFn)(char *,...)
Definition: AlgType.h:297
double im
Definition: AlgType.h:253
enum _AlgDistribution AlgDistribution
Definition: AlgType.h:225
Definition: AlgType.h:119
size_t maxEnt
Definition: AlgType.h:216
Definition: AlgType.h:291
size_t nC
Definition: AlgType.h:154
Definition: AlgType.h:290
double ** array
Definition: AlgType.h:183
Definition: AlgType.h:268
size_t row
Definition: AlgType.h:227
struct _AlgMatrixSym * sym
Definition: AlgType.h:141
enum _AlgPadType AlgPadType
Definition: AlgType.h:241
size_t nR
Definition: AlgType.h:180
Definition: AlgType.h:271
Definition: AlgType.h:293
_AlgError
Definition: AlgType.h:262
Definition: AlgType.h:266
void * blk
Definition: AlgType.h:218
Definition: AlgType.h:270
Rectangular matrix. Typedef: AlgMatrixRect.
Definition: AlgType.h:162
Definition: AlgType.h:275
Definition: AlgType.h:128
union _AlgMatrix AlgMatrix
enum _AlgError AlgError
Definition: AlgType.h:279
struct _AlgMatrixTriple AlgMatrixTriple
AlgMatrixLLRE * freeStk
Definition: AlgType.h:220
Definition: AlgType.h:264
AlgMatrixType type
Definition: AlgType.h:164
Definition: AlgType.h:269
struct _AlgMatrixLLR * llr
Definition: AlgType.h:142
Definition: AlgType.h:292
Definition: AlgType.h:272
_AlgPadType
Types of daat padding. Typedef: AlgPadType.
Definition: AlgType.h:237
struct _AlgMatrixLLRE AlgMatrixLLRE
struct _AlgMatrixSym AlgMatrixSym
AlgMatrixType type
Definition: AlgType.h:152
Definition: AlgType.h:108
size_t nR
Definition: AlgType.h:213
Definition: AlgType.h:289
size_t nC
Definition: AlgType.h:214
double re
Definition: AlgType.h:252
double tol
Definition: AlgType.h:217
size_t nC
Definition: AlgType.h:166
Definition: AlgType.h:277
struct _AlgMatrixLLRE * nxt
Definition: AlgType.h:195
A core matrix type with members common to all matrix types. Typedef: AlgMatrixCore.
Definition: AlgType.h:150
struct _AlgMatrixLLR AlgMatrixLLR
Definition: AlgType.h:106
Definition: AlgType.h:276
struct _AlgMatrixCore AlgMatrixCore
AlgMatrixLLRE ** tbl
Definition: AlgType.h:221
size_t nR
Definition: AlgType.h:153
_AlgMatrixType
Matrix representations. Typedef: AlgMatrixType.
Definition: AlgType.h:116
size_t col
Definition: AlgType.h:193
double val
Definition: AlgType.h:229
Entry in the linked list row matrix. Typedef: AlgMatrixLLRE.
Definition: AlgType.h:191
Definition: AlgType.h:273
Definition: AlgType.h:239
Complex number data type. Typedef: ComplexD.
Definition: AlgType.h:250
struct _AlgMatrixRect * rect
Definition: AlgType.h:140
size_t nR
Definition: AlgType.h:165
enum _AlgDbgMask AlgDbgMask
Definition: AlgType.h:107
size_t numEnt
Definition: AlgType.h:215
A union of all valid matrix types. Typedef: AlgMatrix..
Definition: AlgType.h:137
double val
Definition: AlgType.h:194
Definition: AlgType.h:210
struct _ComplexD ComplexD
Definition: AlgType.h:105