Memosa-FVM  0.2
ArrayWriter.h
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifndef _ARRAYWRITER_H_
6 #define _ARRAYWRITER_H_
7 
8 
9 #include "Array.h"
10 #include "Vector.h"
11 
13 {
14 public:
15  ArrayWriter(const bool binary, const int vectorComponent,
16  const int atypeComponent):
17  _binary(binary),
18  _vectorComponent(vectorComponent),
19  _atypeComponent(atypeComponent)
20  {}
21 
22  virtual ~ArrayWriter() {};
23 
24  template<class T>
25  void writeFloats(FILE *fp, const T *data, const int count, const int stride,
26  const Array<bool> *mask=0)
27  {
28  if (mask)
29  {
30  for(int n=0,i=0; n<count; i+=stride,n++)
31  if ((*mask)[n])
32  fprintf(fp,"%12.5e\n",data[i]);
33  }
34  else
35  {
36  for(int n=0,i=0; n<count; i+=stride,n++)
37  fprintf(fp,"%12.5e\n",data[i]);
38  }
39  }
40 
41 protected:
42  const bool _binary;
43  const int _vectorComponent;
44  const int _atypeComponent;
45 };
46 
47 template<class T>
49 {
50 public:
51  typedef T ElementType;
54 
55  ScalarArrayWriter(const bool binary, const int vectorComponent,
56  const int atypeComponent) :
57  ArrayWriter(binary,vectorComponent,atypeComponent)
58  {}
59 
60  void write(FILE *fp, const Array<T>& array,
61  const int iBeg, int count,
62  const Array<bool> *mask=0)
63  {
64  const int length = array.getLength();
65  if (count == -1)
66  count = length;
67  else if (count+iBeg > length)
68  throw CException("invalid count ");
69 
70  ElementType *dataArray = (ElementType*)(array.getData());
71 
72  T_BuiltIn *data = (T_BuiltIn*)(dataArray+iBeg);
73 
74  int stride = 1;
75 
76 #ifdef USING_ATYPE_TANGENT
77  if ((_atypeComponent == 0) || (_atypeComponent == 1))
78  {
79  data += _atypeComponent;
80  stride = 2;
81  }
82  else
83  throw CException("invalid component for Tangent");
84 
85 #endif
86  writeFloats(fp,data,count,stride,mask);
87  }
88 };
89 
90 
91 template<class T, int N>
93 {
94 public:
99 
100  VectorArrayWriter(const bool binary, const int vectorComponent,
101  const int atypeComponent) :
102  ArrayWriter(binary,vectorComponent,atypeComponent)
103  {}
104 
105  virtual void write(FILE *fp, const ArrayType& array,
106  const int iBeg, int count,
107  const Array<bool> *mask=0)
108  {
109  const int length = array.getLength();
110  if (count == -1)
111  count = length;
112  else if (count+iBeg > length)
113  throw CException("invalid count ");
114 
115 
116  ElementType *dataArray = (ElementType*)(array.getData());
117  T_Scalar *dataScalar = (T_Scalar*)(dataArray+iBeg);
118 
119  int stride = 1;
120 
121  if (_vectorComponent == -2)
122  {
123  T_BuiltIn *data = (T_BuiltIn*)(dataScalar);
124 
125  // write all components in coupled order
126 
127 #ifdef USING_ATYPE_TANGENT
128  if ((_atypeComponent == 0) || (_atypeComponent == 1))
129  {
130  data += _atypeComponent;
131  stride = 2;
132  }
133  else
134  throw CException("invalid component for Tangent");
135 #endif
136  if (mask)
137  throw CException("masked write not implemented");
138  writeFloats(fp,data,count*N,stride,0);
139  }
140  else
141  {
142  // write all components in sequential order
143  int ndBeg=0;
144  int ndEnd=N;
145 
146  if (_vectorComponent >= 0 && _vectorComponent <N)
147  {
148  // restrict to just one component
149  ndBeg=_vectorComponent;
150  ndEnd=ndBeg+1;
151  }
152  else if (_vectorComponent != -1)
153  {
154  throw CException("invalid vector component");
155  }
156 
157  stride = N;
158 
159  for(int nd=ndBeg; nd<ndEnd; nd++)
160  {
161  T_BuiltIn *nddata = (T_BuiltIn*) (dataScalar+nd);
162 
163 #ifdef USING_ATYPE_TANGENT
164  if ((_atypeComponent == 0) || (_atypeComponent == 1))
165  {
166  nddata += _atypeComponent;
167  stride = N*2;
168  }
169  else
170  throw CException("invalid component for Tangent");
171 #endif
172  writeFloats(fp,nddata,count,stride,mask);
173  }
174  }
175  }
176 };
177 
178 
179 #endif
Vector< T, N > ElementType
Definition: ArrayWriter.h:95
virtual void write(FILE *fp, const ArrayType &array, const int iBeg, int count, const Array< bool > *mask=0)
Definition: ArrayWriter.h:105
NumTypeTraits< T >::T_BuiltIn T_BuiltIn
Definition: ArrayWriter.h:53
Array< ElementType > ArrayType
Definition: ArrayWriter.h:96
Definition: Vector.h:19
void write(FILE *fp, const Array< T > &array, const int iBeg, int count, const Array< bool > *mask=0)
Definition: ArrayWriter.h:60
void writeFloats(FILE *fp, const T *data, const int count, const int stride, const Array< bool > *mask=0)
Definition: ArrayWriter.h:25
const int _atypeComponent
Definition: ArrayWriter.h:44
NumTypeTraits< T >::T_Scalar T_Scalar
Definition: ArrayWriter.h:98
ArrayWriter(const bool binary, const int vectorComponent, const int atypeComponent)
Definition: ArrayWriter.h:15
virtual void * getData() const
Definition: Array.h:275
Array< ElementType > ArrayType
Definition: ArrayWriter.h:52
ScalarArrayWriter(const bool binary, const int vectorComponent, const int atypeComponent)
Definition: ArrayWriter.h:55
const int _vectorComponent
Definition: ArrayWriter.h:43
const bool _binary
Definition: ArrayWriter.h:42
VectorArrayWriter(const bool binary, const int vectorComponent, const int atypeComponent)
Definition: ArrayWriter.h:100
NumTypeTraits< T >::T_BuiltIn T_BuiltIn
Definition: ArrayWriter.h:97
virtual ~ArrayWriter()
Definition: ArrayWriter.h:22
int getLength() const
Definition: Array.h:87