FiberVISH 0.2
Fish - The Fiber Bundle API for the Vish Visualization Shell
CurveDerivative.hpp
1#ifndef __FIBER_GRID_TYPES_CURVEDERIVATIVE_HPP
2#define __FIBER_GRID_TYPES_CURVEDERIVATIVE_HPP
3
4#include "gridtypesDllApi.h"
5#include <grid/Grid.hpp>
6#include <eagle/PhysicalSpace.hpp>
7
8#include <grid/EuclideanMetric.hpp>
9
10
11#define USE_DEPRECATED_LINESET_FUNCTIONS
12#include "LineSet.hpp"
13
14//#define VERBOSE
15
16namespace Fiber
17{
18
20{
21 template <class ValueType, class PointType>
22 static const ValueType&compute(const ValueType&Value, index_t, const PointType&)
23 {
24 return Value;
25 }
26};
27
29{
30 template <class ValueType, class PointType>
31 static const ValueType&compute(const ValueType&Value, index_t, const PointType&)
32 {
33 return unit(Value);
34 }
35};
36
37
39{
40 WeakPtr<Grid> theGrid;
42 string fieldName;
43
45 : theGrid( theGridP )
46 , fragV( fragVP )
47 , fieldName(fieldNameP)
48 {}
49};
50
51
67template <class Type, class Metric = EuclideanMetric, class ValueOperator = IdentityOperator>
68struct CurveDerivative : public Metric, public ValueOperator
69{
70 typedef typename Metric::point point;
71 using Metric::distance;
72
73 using ValueOperator::compute;
74
75 RefPtr<MemArray<1, Type> > Quantity;
76 RefPtr<MemArray<1, Type> > DerivedQuantity;
77
80
81 RefPtr<ResultArray_t> result() const
82 {
83 return DerivedQuantity;
84 }
85
86 typedef std::string Parameters;
87
89 {
90#ifdef VERBOSE
91 puts("CurveDerivative::CurveDerivative() enter");fflush(stdout);
92#endif
93 if (!C.theGrid)
94 {
95 puts("CurveDerivative::CurveDerivative() No Grid!");fflush(stdout);
96 return;
97 }
98
99 LineSet LS = C.theGrid;
100
101 if (!LS)
102 {
103 puts("CurveDerivative::CurveDerivative() No LineSet!");fflush(stdout);
104 return;
105 }
106
107
108 if (!LS.CartesianVertices)
109 {
110 puts("CurveDerivative::CurveDerivative() No Cartesian Vertices!");fflush(stdout);
111 return;
112 }
113
114 if (!LS.getCoords( C.fragV ) )
115 {
116 if (!C.fragV )
117 puts("CurveDerivative::CurveDerivative() No Coords for global fragment!");
118 else
119 printf("CurveDerivative::CurveDerivative() No Coords for fragment [%s]!\n", C.fragV->Name().c_str() );
120
121 fflush(stdout);
122 return;
123 }
124
125#ifdef VERBOSE
126 puts("CurveDerivative::CurveDerivative() progressing");fflush(stdout);
127#endif
128
129 string QuantityFieldName = C.fieldName;
130
131#ifdef VERBOSE
132 puts("CurveDerivative::CurveDerivative() progressing 2");fflush(stdout);
133 std::cout << "CurveDerivative::CurveDerivative() doing: " << QuantityFieldName << std::endl;fflush(stdout);
134#endif
135
136 RefPtr<Field> QuantityVertexField = (*LS.CartesianVertices)[ QuantityFieldName ];
138 {
139 puts("CurveDerivative::CurveDerivative() No Quantity Field found!");fflush(stdout);
140 return;
141 }
142 if ( (Quantity = QuantityVertexField->getData( C.fragV ) ) )
143 {
144 DerivedQuantity = new ResultArray_t( LS.getCoords( C.fragV )->nElements(), Crec );
145
146#ifdef VERBOSE
147 puts("CurveDerivative::CurveDerivative() iterating"); fflush(stdout);
148#endif
149 LS.ApplyOnVertexFragment( *this, C.fragV );
150
151#ifdef VERBOSE
152 puts("CurveDerivative::CurveDerivative() post iteration"); fflush(stdout);
153#endif
154
155 }
156 else
157 {
158 puts("CurveDerivative::CurveDerivative() No Data retrieved!");fflush(stdout);
159 }
160 }
161
162 void apply(const CreativeIterator<Eagle::PhysicalSpace::point>&Vertices,
164 {
165#ifdef VERBOSE
166 puts("CurveDerivative::apply()");fflush(stdout);
167#endif
168 MultiArray<1, Type>&Values = *Quantity;
169 MultiArray<1, Type>&ValueDerivative = *DerivedQuantity;
170
171 if (Line.size()<1) // no data
172 {
173#ifdef VERBOSE
174 puts("CurveDerivative::apply() No Data");
175#endif
176 return;
177 }
178 if (Line.size()<2) // one element - derivative is zero
179 {
180#ifdef VERBOSE
181 puts("CurveDerivative::apply() One Element");
182#endif
183 const point&P0 = Vertices[ Line[0] ];
184 const Type&Q = ValueOperator::compute( Values [ Line[0] ], Line[0], P0 );
185
186 ValueDerivative[ Line[0] ] = Q-Q;
187#ifdef VERBOSE
188 std::cout << "First" << ValueDerivative[ Line[0] ] << std::endl;
189#endif
190 return;
191 }
192 // at least two elements, 0,1, here
193
194 // First point: forward difference
195 {
196 const point&P0 = Vertices[ Line[0] ],
197 &P1 = Vertices[ Line[1] ];
198
199/*
200 const Type&Q0 = ValueOperator::compute( Values [ Line[0] ], Line[0], P0 );
201 const Type&Q1 = ValueOperator::compute( Values [ Line[1] ], Line[1], P1 );
202
203 double ds = Metric::distance(Line[0], Line[1], P0, P1);
204
205 ValueDerivative[ Line[0] ] = (Q1 - Q0) / ds;
206*/
207
208 const Type&Q0 = Values [ Line[0] ];
209 const Type&Q1 = Values [ Line[1] ];
210
211 double ds = Eagle::norm(P1 - P0);
212
213 ValueDerivative[ Line[0] ] = (Q1 - Q0) / ds;
214#ifdef VERBOSE
215 std::cout << "Q0= " << Q0 << ", Q1= " << Q1 << std::endl;
216 std::cout << "First " << ValueDerivative[ Line[0] ] << std::endl;
217#endif
218 }
219
220 //
221 // TODO: Reuse already computed values and point coordinates.
222 //
223
224 index_t I = Line.size();
225 for(unsigned i=1; i<I-1; i++)
226 {
227 const point&Pm = Vertices[ Line[i-1] ],
228 &P = Vertices[ Line[i ] ],
229 &Pp = Vertices[ Line[i+1] ];
230
231/* const Type&Qm = ValueOperator::compute( Values [ Line[i-1] ], Line[i-1], Pm ),
232 Qp = ValueOperator::compute( Values [ Line[i+1] ], Line[i+1], Pp );
233
234 double ds_m = Metric::distance(Line[i-1], Line[i ], Pm, P),
235 ds_p = Metric::distance(Line[i ], Line[i+1], P , Pp);
236
237 ValueDerivative[ Line[i] ] = (Qp - Qm) / ( ds_m + ds_p);
238*/
239
240 const Type&Qm = Values [ Line[i-1] ],
241 &Q = Values [ Line[i ] ],
242 &Qp = Values [ Line[i+1] ];
243
244 double ds2_m = 2*Eagle::norm( P - Pm ),
245 ds2_p = 2*Eagle::norm( Pp - P );
246
247 ValueDerivative[ Line[i] ] = (Q - Qm ) / ds2_m
248 +
249 (Qp - Q ) / ds2_p
250 ;
251
252#ifdef VERBOSE
253 std::cout << "i: " << i << " " << ValueDerivative[ Line[i] ] << std::endl;
254#endif
255 }
256
257 // Last point: backward difference
258 if (I>2)
259 {
260 index_t I = Line.size();
261
262 const point&P0 = Vertices[ Line[I-2] ],
263 &P1 = Vertices[ Line[I-1] ];
264
265/* const Type&Q0 = ValueOperator::compute( Values [ Line[I-2] ], Line[I-2], P0 );
266 const Type&Q1 = ValueOperator::compute( Values [ Line[I-1] ], Line[I-1], P1 );
267
268 double ds = Metric::distance(Line[I-2], Line[I-1], P0, P1);
269*/
270
271 const Type&Q0 = Values [ Line[I-2] ];
272 const Type&Q1 = Values [ Line[I-1] ];
273
274 double ds = Eagle::norm(P1-P0);
275
276 ValueDerivative[ Line[I-1] ] = (Q1 - Q0) / ds;
277#ifdef VERBOSE
278 std::cout << "Last: " << ValueDerivative[ Line[I-1] ] << std::endl;
279#endif
280 }
281
282 }
283};
284
285} // namespace Fiber
286
287#endif // __FIBER_GRID_TYPES_CURVEDERIVATIVE_HPP
basic_string< char > string
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
ostream cout
An iterator with an optional DataCreator, which is just a class to intercept creation of data along a...
Definition CreativeIterator.hpp:34
A set of lines stored on a Grid.
Definition LineSet.hpp:55
double P0(double x)
double norm(const PhysicalSpace::bivector &v)
double P1(double x)
Given a fragmented field of curvilinear coordinates, (3D array of coordinates), build a uniform Grid ...
Definition FAQ.dox:2
Definition CurveDerivative.hpp:29
An operator for OnDemandCreators to be used on LineSet's.
Definition CurveDerivative.hpp:69
Definition CurveDerivative.hpp:39
Definition CurveDerivative.hpp:20