GetFEM  5.6
bgeot_geotrans_inv.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2000-2026 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /**@file bgeot_geotrans_inv.h
32  @author Yves Renard <Yves.Renard@insa-lyon.fr>
33  @date December 20, 2000.
34  @brief Inversion of geometric transformations.
35 
36  Inversion means: given a set of convexes and a point, find:
37 
38  - a subset of candidate convexes, which are likely to contain the
39  point (using bgeot::kdtree).
40 
41  - on these candidate convexes, invert the geometric
42  transformation, i.e. find the corresponding coordinates on the
43  reference element.
44 
45  Inversion of a geometric transformation is not a trivial task,
46  especially with non-linear geometric transformations. This is the
47  central part of interpolation routines from a getfem::mesh_fem onto
48  another.
49 */
50 
51 #ifndef BGEOT_GEOTRANS_INV_H__
52 #define BGEOT_GEOTRANS_INV_H__
53 
54 #include "bgeot_geometric_trans.h"
55 #include "bgeot_small_vector.h"
56 #include "bgeot_kdtree.h"
57 
58 namespace bgeot {
59 
60  /**
61  does the inversion of the geometric transformation for a given convex
62  */
64  size_type N, P;
65  base_matrix G, pc, K, B, CS;
66  base_matrix Grel; // nodes relative to the first one, for the residual:
67  // independent of the element position
68  pgeometric_trans pgt = nullptr;
69  scalar_type EPS;
70 
71  bool has_linearized_approx = false;
72  base_matrix K_ref_B_transp_lin;
73  base_node P_lin, P_ref_lin;
74 
75  public:
76  const base_matrix &get_G() const { return G; }
77 
78  geotrans_inv_convex(scalar_type e=10e-12)
79  : N(0), P(0), pgt(0), EPS(e) {}
80 
81  template<class TAB> geotrans_inv_convex(const convex<base_node, TAB> &cv,
82  pgeometric_trans pgt_,
83  scalar_type e=10e-12)
84  : N(0), P(0), pgt(0), EPS(e)
85  {
86  init(cv.points(), pgt_);
87  }
88 
89  geotrans_inv_convex(const std::vector<base_node> &nodes,
90  pgeometric_trans pgt_,
91  scalar_type e=10e-12)
92  : N(0), P(0), pgt(0), EPS(e)
93  {
94  init(nodes, pgt_);
95  }
96 
97  template<class TAB> void init(const TAB &nodes, pgeometric_trans pgt_);
98 
99  /**
100  given the node on the real element, returns the node on the
101  reference element (even if it is outside of the ref. convex).
102 
103  If the geometric transformation is not invertible at point x,
104  an exception is thrown.
105 
106  @return true if the x is inside the convex
107 
108  @param x node on the real element
109 
110  @param xref computed node on the reference convex
111 
112  @param IN_EPS a threshold.
113  */
114  bool invert(const base_node& x, base_node& xref,
115  scalar_type IN_EPS=1e-12, bool project_into_element=false);
116 
117  /**
118  given the node on the real element, returns the node
119  on the reference element (even if it is outside of the ref. convex).
120 
121  This version will not throw an exception if the geometric
122  transformation is not invertible at point x.
123 
124  @return true if the x is inside the convex
125 
126  @param x node on the real element
127 
128  @param xref computed node on the reference convex
129 
130  @param converged on output, will be set to true if the
131  geometric transformation could be inverted.
132 
133  @param IN_EPS a threshold.
134  */
135  bool invert(const base_node& x, base_node& xref, bool &converged,
136  scalar_type IN_EPS=1e-12, bool project_into_element=false);
137  private:
138  bool invert_lin(const base_node& x, base_node& xref, scalar_type IN_EPS);
139  bool invert_nonlin(const base_node& x, base_node& xref,
140  scalar_type IN_EPS, bool &converged, bool throw_except,
141  bool project_into_element);
142  bool update_B(); // returns true if successful
143  void update_linearization(); // TODO: return success state
144 
145  friend class geotrans_inv_convex_bfgs;
146  };
147 
148  template<class TAB>
149  void geotrans_inv_convex::init(const TAB &nodes, pgeometric_trans pgt_) {
150  bool geotrans_changed = (pgt != pgt_); if (geotrans_changed) pgt = pgt_;
151  GMM_ASSERT3(!nodes.empty(), "empty points!");
152  if (N != nodes[0].size())
153  { N = nodes[0].size(); geotrans_changed = true; }
154  if (geotrans_changed) {
155  P = pgt->structure()->dim();
156  pc.resize(pgt->nb_points() , P);
157  K.resize(N,P);
158  B.resize(N,P);
159  CS.resize(P,P);
160  G.resize(N, pgt->nb_points());
161  }
162  vectors_to_base_matrix(G, nodes);
163  Grel = G;
164  for (size_type j = 0; j < G.ncols(); ++j)
165  gmm::add(gmm::scaled(mat_col(G, 0), -1.0), mat_col(Grel, j));
166  if (pgt->is_linear()) {
167  if (geotrans_changed) {
168  base_node Dummy(P);
169  pgt->poly_vector_grad(Dummy, pc);
170  }
171  // computation of the pseudo inverse
172  update_B();
173  } else if (pgt->complexity() > 1)
174  update_linearization();
175  }
176 
177 
178  /**
179  handles the geometric inversion for a given (supposedly quite large)
180  set of points
181  */
183  {
184  protected :
185  mutable kdtree tree;
186  scalar_type EPS;
188  public :
189  void clear() { tree.clear(); }
190  /// Add the points contained in c to the list of points.
191  template<class CONT> void add_points(const CONT &c) {
192  tree.reserve(std::distance(c.begin(),c.end()));
193  typename CONT::const_iterator it = c.begin(), ite = c.end();
194  for (; it != ite; ++it) tree.add_point(*it);
195  }
196 
197  /// Number of points.
198  size_type nb_points() const { return tree.nb_points(); }
199  /// Add point p to the list of points.
200  size_type add_point(base_node p) { return tree.add_point(p); }
201  void add_point_with_id(base_node p,size_type id)
202  { tree.add_point_with_id(p,id); }
203 
204  /// Find all the points present in the box between min and max.
206  const base_node &min,
207  const base_node &max) const {
208  tree.points_in_box(ipts, min, max);
209  return ipts.size();
210  }
211 
212  /** Search all the points in the convex cv, which is the transformation
213  * of the convex cref via the geometric transformation pgt.
214  *
215  * IMPORTANT : It is assumed that the whole convex is included in the
216  * minmax box of its nodes times a factor 1.2. If the transformation is
217  * linear, the factor is 1.0.
218  *
219  * @param cv the convex points (as given by getfem_mesh::convex(ic)).
220  *
221  * @param pgt the geometric trans (as given by
222  * getfem_mesh::trans_of_convex(ic)).
223  *
224  * @param pftab container for the coordinates of points in the reference
225  * convex (should be of size nb_points())
226  *
227  * @param itab the indices of points found in the convex.
228  *
229  * @param bruteforce use a brute force search(only for debugging purposes).
230  *
231  * @return the number of points in the convex (i.e. the size of itab,
232  * and pftab)
233  */
234  template<class TAB, class CONT1, class CONT2>
236  pgeometric_trans pgt,
237  CONT1 &pftab, CONT2 &itab,
238  bool bruteforce=false);
239 
240  geotrans_inv(scalar_type EPS_ = 10E-12) : EPS(EPS_) {}
241  };
242 
243 
244 
245  template<class TAB, class CONT1, class CONT2>
247  pgeometric_trans pgt,
248  CONT1 &pftab, CONT2 &itab,
249  bool bruteforce) {
250  base_node min, max; /* bound of the box enclosing the convex */
251  size_type nbpt = 0; /* nb of points in the convex */
252  kdtree_tab_type boxpts;
253  bounding_box(min, max, cv.points(), pgt);
254  for (size_type k=0; k < min.size(); ++k) { min[k] -= EPS; max[k] += EPS; }
255  gic.init(cv.points(),pgt);
256  /* get the points in a box enclosing the convex */
257  if (!bruteforce) points_in_box(boxpts, min, max);
258  else boxpts = tree.points();
259  /* and invert the geotrans, and check if the obtained point is
260  inside the reference convex */
261  for (size_type l = 0; l < boxpts.size(); ++l) {
262  // base_node pt_ref;
263  if (gic.invert(boxpts[l].n, pftab[nbpt], EPS)) {
264  itab[nbpt++] = boxpts[l].i;
265  }
266  }
267  return nbpt;
268  }
269 
270 
271 
272 
273 
274 } /* end of namespace bgeot. */
275 
276 
277 #endif /* BGEOT_GEOMETRIC_TRANSFORMATION_H__ */
Geometric transformations on convexes.
Simple implementation of a KD-tree.
Small (dim < 8) vectors.
generic definition of a convex ( bgeot::convex_structure + vertices coordinates )
Definition: bgeot_convex.h:49
does the inversion of the geometric transformation for a given convex
bool invert(const base_node &x, base_node &xref, scalar_type IN_EPS=1e-12, bool project_into_element=false)
given the node on the real element, returns the node on the reference element (even if it is outside ...
handles the geometric inversion for a given (supposedly quite large) set of points
void add_points(const CONT &c)
Add the points contained in c to the list of points.
size_type points_in_box(kdtree_tab_type &ipts, const base_node &min, const base_node &max) const
Find all the points present in the box between min and max.
size_type nb_points() const
Number of points.
size_type add_point(base_node p)
Add point p to the list of points.
size_type points_in_convex(const convex< base_node, TAB > &cv, pgeometric_trans pgt, CONT1 &pftab, CONT2 &itab, bool bruteforce=false)
Search all the points in the convex cv, which is the transformation of the convex cref via the geomet...
Balanced tree over a set of points.
Definition: bgeot_kdtree.h:101
void add_point_with_id(const base_node &n, size_type i)
insert a new point, with an associated number.
Definition: bgeot_kdtree.h:119
size_type add_point(const base_node &n)
insert a new point
Definition: bgeot_kdtree.h:115
void clear()
reset the tree, remove all points
Definition: bgeot_kdtree.h:112
Basic Geometric Tools.
std::vector< index_node_pair > kdtree_tab_type
store a set of points with associated indexes.
Definition: bgeot_kdtree.h:67
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
std::shared_ptr< const bgeot::geometric_trans > pgeometric_trans
pointer type for a geometric transformation