UNIVERS  15.3
UNIVERS base processing software API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
zd_f3d_matrix.hpp
1 /* zd_f3d_matrix.hpp */
2 /* $Id: zd_f3d_matrix.hpp,v 1.7 2006/03/07 06:39:08 rus1 Exp $ */
3 #ifndef __zd_f3d_matrix_hpp
4 #define __zd_f3d_matrix_hpp
5 
6 #include <math.h>
7 #include <string.h>
8 
9 #include "zqb_macros.h"
10 #include "zd_function3d.hpp"
11 
12 
14 typedef enum
15 {
16  Z_F3DM_ALA_C,
17  Z_F3DM_ALA_FORTRAN
19 } ZdF3DMatrixPolicy;
20 
21 
22 #ifndef __zd_f3d_matrix_global_vars
23 
25 extern int g_iZdF3DMatrixCounter;
26 
28 extern char* g_szZdF3DMatrixOwnType;
29 
30 #endif /* __zd_f3d_matrix_global_vars */
31 
32 
44 template <class T>
45 class ZdF3DMatrix : public ZdFunction3D
46 {
47 private:
48 
50  struct {
51  int nCount;
52  double fOffset;
53  double fStep;
54  } m_X, m_Y;
55 
57  bool m_bKnownMM;
58 
60  double m_fMinMax[2];
61 
63  ZdF3DMatrixPolicy m_eStPolicy;
64 
66  const T* m_pZMatrix;
67 
69  T* m_pDetached;
70 
71 public:
72 
84  ZdF3DMatrix (int nX, int nY, double pfXYInfo[4] = NULL,
85  ZdF3DMatrixPolicy ePolicy = Z_F3DM_ALA_C)
86  : ZqbAutoName(&g_iZdF3DMatrixCounter, g_szZdF3DMatrixOwnType),
87  m_pZMatrix(NULL), m_pDetached(NULL),
88  m_bKnownMM(false), m_eStPolicy(ePolicy)
89  {
90  m_X.nCount = nX;
91  m_Y.nCount = nY;
92 
93  if(NULL == pfXYInfo)
94  {
95  m_X.fOffset = 0.0;
96  m_Y.fOffset = 0.0;
97  m_X.fStep = 1.0;
98  m_Y.fStep = 1.0;
99  }
100  else
101  {
102  m_X.fOffset = pfXYInfo[0];
103  m_Y.fOffset = pfXYInfo[1];
104  m_X.fStep = pfXYInfo[2];
105  m_Y.fStep = pfXYInfo[3];
106  }
107 
108  /* Allocate memory and copy the array into it. */
109  m_pDetached = new T[m_X.nCount * m_Y.nCount];
110  m_pZMatrix = m_pDetached;
111 
112  /* Fill the array by zeros (regarding IEEE floating point format) */
113  memset(m_pDetached, 0, sizeof(T) * m_X.nCount * m_Y.nCount);
114  m_fMinMax[0] = m_fMinMax[1] = 0.0;
115  }
116 
117 
134  ZdF3DMatrix (const T* pArray, int nX, int nY, double pfXYInfo[4] = NULL,
135  bool bDetached = true, ZdF3DMatrixPolicy ePolicy = Z_F3DM_ALA_C)
136  : ZqbAutoName(&g_iZdF3DMatrixCounter, g_szZdF3DMatrixOwnType),
137  m_pZMatrix(pArray), m_pDetached(NULL),
138  m_bKnownMM(false), m_eStPolicy(ePolicy)
139  {
140  m_X.nCount = nX;
141  m_Y.nCount = nY;
142 
143  if(NULL == pfXYInfo)
144  {
145  m_X.fOffset = 0.0;
146  m_Y.fOffset = 0.0;
147  m_X.fStep = 1.0;
148  m_Y.fStep = 1.0;
149  }
150  else
151  {
152  m_X.fOffset = pfXYInfo[0];
153  m_Y.fOffset = pfXYInfo[1];
154  m_X.fStep = pfXYInfo[2];
155  m_Y.fStep = pfXYInfo[3];
156  }
157 
158  if(bDetached)
159  {
160  /* Allocate memory and copy the array into it. */
161  m_pDetached = new T[m_X.nCount * m_Y.nCount];
162 
163  /* Use instead of pArray */
164  m_pZMatrix = m_pDetached;
165 
166  /* Copy the array into special internal buffer. */
167  memcpy(m_pDetached, pArray, sizeof(T) * m_X.nCount * m_Y.nCount);
168  }
169  }
170 
172  virtual ~ZdF3DMatrix ()
173  {
174  delete[] m_pDetached;
175  }
176 
177 
178  /*
179  * Basic functionality for matrix contents modification
180  */
181 
183  virtual void setFunc (int iX, int iY, T value)
184  {
185  if(iX < 0 || iX >= countX() || iY < 0 || iY >= countY())
186  return;
187 
188  if(m_eStPolicy == Z_F3DM_ALA_C)
189  m_pDetached[iX + iY * countX()] = value;
190  else /* FORTRAN array index convension */
191  m_pDetached[iY + iX * countY()] = value;
192 
193  /* Fix min and max values */
194  if(m_bKnownMM)
195  {
196  m_fMinMax[0] = ZqbMIN(m_fMinMax[0], (double)value);
197  m_fMinMax[1] = ZqbMAX(m_fMinMax[1], (double)value);
198  }
199  }
200 
201 
202  /*
203  * Basic functionality, inherited from ZdFunction3D
204  */
205 
207  virtual int countX ()
208  {
209  return m_X.nCount;
210  }
211 
213  virtual int countY ()
214  {
215  return m_Y.nCount;
216  }
217 
219  virtual double argX (int iX)
220  {
221  return m_X.fOffset + m_X.fStep * iX;
222  }
223 
225  virtual double argY (int iY)
226  {
227  return m_Y.fOffset + m_Y.fStep * iY;
228  }
229 
232  virtual double func (int iX, int iY)
233  {
234  if(iX < 0 || iX >= countX() || iY < 0 || iY >= countY())
235  return 0.0;
236 
237  if(m_eStPolicy == Z_F3DM_ALA_C)
238  return (double) m_pZMatrix[iX + iY * countX()];
239  /* else FORTRAN array index convension */
240  return (double) m_pZMatrix[iY + iX * countY()];
241  }
242 
243 
244  /*
245  * Extended functionality with default implementation
246  */
247 
251  virtual bool isNullAll ()
252  {
253  return false;
254  }
255 
258  virtual bool isNullFunc (int iX, int iY)
259  {
260  if(iX < 0 || iX >= countX() || iY < 0 || iY >= countY())
261  return true;
262  return false;
263  }
264 
267  virtual double argStepX ()
268  {
269  return m_X.fStep;
270  }
271 
274  virtual double argStepY ()
275  {
276  return m_Y.fStep;
277  }
278 
282  virtual void getArgBoundsX (double b[2])
283  {
284  if(NULL == b)
285  return;
286 
287  if(argStepX() > 0.0)
288  {
289  b[0] = m_X.fOffset;
290  b[1] = m_X.fOffset + (countX() - 1) * argStepX();
291  }
292  else
293  {
294  b[1] = m_X.fOffset;
295  b[0] = m_X.fOffset + (countX() - 1) * argStepX();
296  }
297  }
298 
302  virtual void getArgBoundsY (double b[2])
303  {
304  if(NULL == b)
305  return;
306 
307  if(argStepY() > 0.0)
308  {
309  b[0] = m_Y.fOffset;
310  b[1] = m_Y.fOffset + (countY() - 1) * argStepY();
311  }
312  else
313  {
314  b[1] = m_Y.fOffset;
315  b[0] = m_Y.fOffset + (countY() - 1) * argStepY();
316  }
317  }
318 
323  virtual void getFuncBounds (double b[2])
324  {
325  if(!m_bKnownMM)
326  {
327  /* Find min and max values among all function values */
328  ZdFunction3D::getFuncBounds(m_fMinMax);
329  m_bKnownMM = true;
330  }
331 
332  if(NULL != b)
333  {
334  b[0] = (double) m_fMinMax[0];
335  b[1] = (double) m_fMinMax[1];
336  }
337  }
338 
342  virtual int argIndexX (double x)
343  {
344  double fiX = (x - m_X.fOffset) / argStepX();
345  if(fiX - (int)fiX < 0.5)
346  return (int)fiX;
347  return 1 + (int)fiX;
348  }
349 
353  virtual int argIndexY (double y)
354  {
355  double fiY = (y - m_Y.fOffset) / argStepY();
356  if(fiY - (int)fiY < 0.5)
357  return (int)fiY;
358  return 1 + (int)fiY;
359  }
360 
361 };
362 
363 
364 /*
365  * Common use derivations
366  */
367 
368 typedef class ZdF3DMatrix<int> ZdF3DMatrixInt;
369 typedef class ZdF3DMatrix<float> ZdF3DMatrixFloat;
370 typedef class ZdF3DMatrix<double> ZdF3DMatrixDouble;
371 
372 
373 #endif /* zd_f3d_matrix.hpp */
virtual void getFuncBounds(double b[2])
virtual ~ZdF3DMatrix()
Definition: zd_f3d_matrix.hpp:172
ZdF3DMatrix(int nX, int nY, double pfXYInfo[4]=NULL, ZdF3DMatrixPolicy ePolicy=Z_F3DM_ALA_C)
Definition: zd_f3d_matrix.hpp:84
virtual bool isNullFunc(int iX, int iY)
Definition: zd_f3d_matrix.hpp:258
virtual double argStepY()
Definition: zd_f3d_matrix.hpp:274
int nCount
Definition: zd_f3d_matrix.hpp:51
virtual void getArgBoundsX(double b[2])
Definition: zd_f3d_matrix.hpp:282
virtual int countY()
Definition: zd_f3d_matrix.hpp:213
virtual double func(int iX, int iY)
Definition: zd_f3d_matrix.hpp:232
ZdF3DMatrix(const T *pArray, int nX, int nY, double pfXYInfo[4]=NULL, bool bDetached=true, ZdF3DMatrixPolicy ePolicy=Z_F3DM_ALA_C)
Definition: zd_f3d_matrix.hpp:134
virtual void setFunc(int iX, int iY, T value)
Definition: zd_f3d_matrix.hpp:183
double fOffset
Definition: zd_f3d_matrix.hpp:52
Definition: zqb_autoname.hpp:18
virtual double argStepX()
Definition: zd_f3d_matrix.hpp:267
virtual double argX(int iX)
Definition: zd_f3d_matrix.hpp:219
virtual int countX()
Definition: zd_f3d_matrix.hpp:207
virtual int argIndexX(double x)
Definition: zd_f3d_matrix.hpp:342
Definition: zd_f3d_matrix.hpp:45
double fStep
Definition: zd_f3d_matrix.hpp:53
virtual double argY(int iY)
Definition: zd_f3d_matrix.hpp:225
virtual void getArgBoundsY(double b[2])
Definition: zd_f3d_matrix.hpp:302
virtual bool isNullAll()
Definition: zd_f3d_matrix.hpp:251
virtual int argIndexY(double y)
Definition: zd_f3d_matrix.hpp:353
Definition: zd_function3d.hpp:29
virtual void getFuncBounds(double b[2])
Definition: zd_f3d_matrix.hpp:323