G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsTraceCurve.hpp
Go to the documentation of this file.
1 
14 #pragma once
15 
16 #include <iostream>
17 #include <gsCore/gsLinearAlgebra.h>
18 #include <gsCore/gsFunction.h>
19 #include <gsCore/gsFunctionExpr.h>
20 #include <gsNurbs/gsBSplineBasis.h>
21 #include <gsUtils/gsPointGrid.h>
22 
23 
24 namespace gismo
25 {
26 
35 template<class T>
36 void gsTraceLine( std::pair<gsFunction<T>*,gsFunction<T>*> & map,
37  gsMatrix<T> const & x,gsMatrix<T> const & p, gsMatrix<T> & result)
38 {
39  T h;
40  int loops=10;
41  int n_points = 10;
42  T tolerance = 0.0001;
43 
44  gsMatrix<T> line(2,1), px, py;
45  gsMatrix<T> next (2,1), delta;
46  gsMatrix<T> tangent (2,1) ;
47  gsMatrix<T> current (2,1);
48  gsMatrix<T> Jmap (2,2);
49 
50 
51 
52  map.first->eval_into(x, px);
53  map.second->eval_into(x, py);
54 
55 
56 
57  // matrix \line stores the evaluation of the \map at the current point
58  line.row(0)= px;
59  line.row(1)= py;
60  next.row(0)= px;
61  next.row(1)= py;
62 
63  tangent = p.col(0)-line.col(0);
64 
65  h = tangent.norm()/n_points ;
66 
67  tangent/=tangent.norm();
68 
69 
70  // "current" stores the point we start with
71  current = x.col(0);
72 
73  int count=0;
74 
75  for(int i=0; i!=n_points; i++)
76  { next = next + h*tangent;
77  loops=10;
78 
79  do
80  {
81  count++;
82  //gsDebug<< "Point "<<count <<"= "<< current.transpose() <<"\n";
83 
84  Jmap.row(0) = map.first->jacobian(current );
85  Jmap.row(1) = map.second->jacobian(current);
86 
87  if ( math::abs( Jmap.determinant() ) < 0.0001) //0.025 amoeba_hole,austria_hole
88  {
89  gsDebug<< "\n trace line: \n Jacobian vanished at : " << current.transpose() <<"\n"
90  "with Jacobian = "<< math::abs( Jmap.determinant() )<<"\n \n";
91 
92  loops =-1 ;
93  break; // stop Newton iteration
94  }
95 
96  delta = Jmap.inverse()*(next.col(0)-line.col(0));
97  current += delta;
98 
99  map.first->eval_into(current,px);
100  map.second->eval_into(current,py);
101 
102  line.row(0)=px;
103  line.row(1)=py;
104 
105  // gsDebug<<"loops="<<loops<<"\n";
106 
107  }
108  while((--loops > 0) && ( (line-next).norm() > tolerance ) );
109 
110 
111  result = current;
112 
113  // gsDebug<<"points traced : "<< result<< "\n";
114 
115 
116 
117  // example:
118  // gsFunctionExpr<T> g1("x");
119  // gsFunctionExpr<T> g2("y");
120  // std::pair<gsFunction<T>*,gsFunction<T>*> map(g1,g2);
121  // gsMatrix<T> res, p(2,2);
122  // p<< 0,1,
123  // 0,1 ;
124  // gsTraceCurve(map, p, res);
125 
126 
127 
128 }
129 
130 }
131 
132 
133 template<class T>
134 void gsTraceCurve( std::pair<gsFunction<T>*,gsFunction<T>*> & map,
135  gsMatrix<T> const & x, gsBSpline<T> * bs,gsMatrix<T> & result,const int n_points = 50,
136  const T tolerance = 0.0001)
137 {
138 
139  int loops=10;// usually are 100
140 
141 
142  result.resize(2,n_points-1);
143 
144  gsMatrix<T> current (2,1);
145  gsMatrix<T> next (2,1), delta;
146  gsMatrix<T> line(2,1), px, py;
147  gsMatrix<T> Jmap (2,2);
148 
149  // to do: change with bs->parameterRange()
150  gsMatrix<T> pts = gsPointGrid<T>(0,1,n_points);
151 
152  gsMatrix<T> ev = bs->eval(pts);
153 
154  //next = ev.col(0); // ev.col(0)==0 point removed!
155  next= ev.col(1);
156 
157  gsMatrix<T> rr; // rr stores the initial point P0 from which the tracing starts
158  gsTraceLine( map, x, next, rr );
159 
160 
161 
162  current = rr.col(0);
163 
164 
165  result.col(n_points-2)= current;
166 
167 
168  next=ev.col(1);
169 
170  map.first->eval_into(current, px);
171  map.second->eval_into(current, py);
172 
173  // matrix \line stores the evaluation of the \map at the current point
174  line.row(0)= px;
175  line.row(1)= py;
176 
177  int count=0;
178 
179  for(int i=2; i!=n_points; i++)
180  {
181  next = ev.col(i);
182  loops=10;
183  do
184  {
185 
186  count++;
187 
188 
189  // TO DO: transpose the result of gsFunction::grad and add transpose()
190  Jmap.row(0) = map.first->jacobian(current);
191  Jmap.row(1) = map.second->jacobian(current);
192 
193 
194  delta = Jmap.inverse()*(next.col(0)-line.col(0));
195 
196  current += delta;
197 
198  // gsMatrix<T> * grad1 = map.first->deriv() ;
199  // Jmap.row(0) = *grad1;
200  // delete grad1;
201 
202  map.first->eval_into(current,px);
203  map.second->eval_into(current,py);
204 
205  line.row(0)=px;
206  line.row(1)=py;
207 
208  //gsDebug<<"\n loops = "<<loops<<"\n";
209 
210 
211  }
212  while((--loops > 0) && ( (line-next).norm() > tolerance ) );
213 
214 
215  result.col(n_points-i-1) = current;
216 // gsDebug<<"\n result.col("<<i<<") : \n" << result.col(i)<<"\n";
217 
218 
219 
220 
221  // example:
222  // gsFunctionExpr<T> g1("x");
223  // gsFunctionExpr<T> g2("y");
224  // std::pair<gsFunction<T>*,gsFunction<T>*> map(g1,g2);
225  // gsMatrix<T> res, p(2,2);
226  // p<< 0,1,
227  // 0,1 ;
228  // gsTraceCurve(map, p, res);
229 
230 
231 
232 }
233 
234 
235 }
236 
246 template<class T>
248  gsMatrix<T> const & x, gsBSpline<T> * bs, T t0, T t1,
249  gsMatrix<T> & result,const int n_points = 50,
250  const T tolerance = 0.0001)
251 {
252  int loops=100;
253 
254  gsMatrix<T> current (2,1);
255  current=x;
256  gsMatrix<T> next (2,1), delta, rr;
257  gsMatrix<T> line(2,1), px, py;
258  gsMatrix<T> Jmap (2,2);
259  gsMatrix<T> medium_point (1,1);
260  medium_point<< 0.5 ;
261 
262  gsMatrix<T> discretI = gsPointGrid<T>(t0,t1, n_points );
263  gsMatrix<T> ev = bs->eval(discretI);
264  map.first->eval_into(current, px);
265  map.second->eval_into(current, py);
266  line.row(0)= px;
267  line.row(1)= py;
268 
269  result.resize(2,ev.cols()+1);
270  result.col(0)=x;
271  bool cont = true;
272  index_t i;
273  for(i =1; i< ev.cols()&& cont; i++)
274  {
275  next= ev.col(i);
276  loops=100;
277  do
278  {
279  // TO DO: transpose the result of gsFunction::grad and add transpose()
280  Jmap.row(0) = map.first->jacobian(current);
281  Jmap.row(1) = map.second->jacobian(current);
282 
283  //gsDebug<<"jacobian matrix is \n"<< Jmap<<"\n";
284  if ( math::abs( Jmap.determinant() ) < 0.01) //0.025 amoeba_hole,austria_hole
285  {
286  gsDebug<< "Jacobian vanished at : " << current.transpose() <<"\n"
287  "with Jacobian = "<< math::abs( Jmap.determinant() )<<"\n \n";
288 
289  cont = false;
290  break; // stop Newton iteration
291  }
292 
293  delta = Jmap.inverse()*(next.col(0)-line.col(0));
294 
295  current += delta;
296 
297  map.first->eval_into(current,px);
298  map.second->eval_into(current,py);
299 
300  line.row(0)=px;
301  line.row(1)=py;
302 
303 
304 
305  }
306  while((--loops > 0) && ( (line-next).norm() > tolerance ) );
307 
308  result.col(i) = current;
309  }
310 
311  // gsDebug<< "Size expected:"<< result.cols() <<"\n";
312  result.conservativeResize( gsEigen::NoChange, i);
313  //gsDebug<< "Size finally :"<< result.cols() <<"\n";
314 
315 
316 
317 
318 
319 }
320 
321 } // namespace gismo
Provides declaration of FunctionExpr class.
#define gsDebug
Definition: gsDebug.h:61
void gsTraceCurvePart(std::pair< gsFunction< T > *, gsFunction< T > * > &map, gsMatrix< T > const &x, gsBSpline< T > *bs, T t0, T t1, gsMatrix< T > &result, const int n_points=50, const T tolerance=0.0001)
Definition: gsTraceCurve.hpp:247
gsMatrix< T > eval(const gsMatrix< T > &u) const
Evaluate the function,.
Definition: gsFunctionSet.hpp:120
#define index_t
Definition: gsConfig.h:32
A function from a n-dimensional domain to an m-dimensional image.
Definition: gsFunction.h:59
A B-spline function of one argument, with arbitrary target dimension.
Definition: gsBSpline.h:50
Provides declaration of BSplineBasis class.
void gsTraceLine(std::pair< gsFunction< T > *, gsFunction< T > * > &map, gsMatrix< T > const &x, gsMatrix< T > const &p, gsMatrix< T > &result)
Definition: gsTraceCurve.hpp:36
Provides functions to generate structured point data.
This is the main header file that collects wrappers of Eigen for linear algebra.
EIGEN_STRONG_INLINE abs_expr< E > abs(const E &u)
Absolute value.
Definition: gsExpressions.h:4488
Provides declaration of Function abstract interface.