GetFEM  5.5
bgeot_convex.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 1999-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_convex.h
32  @author Yves Renard <Yves.Renard@insa-lyon.fr>
33  @date December 20, 1999.
34  @brief Convex objects (structure + vertices)
35 */
36 
37 #ifndef BGEOT_CONVEX_H__
38 #define BGEOT_CONVEX_H__
39 
40 #include "bgeot_convex_structure.h"
41 
42 namespace bgeot {
43 
44  /** @defgroup convexes Convexes */
45  /** @addtogroup convexes */
46  /*@{*/
47 
48  /// generic definition of a convex ( bgeot::convex_structure + vertices coordinates )
49  template<class PT, class PT_TAB = std::vector<PT> > class convex {
50  public :
51 
52  typedef PT point_type;
53  typedef PT_TAB point_tab_type;
54  typedef typename PT_TAB::size_type size_type;
55 
56  typedef gmm::tab_ref_index_ref< typename PT_TAB::const_iterator,
57  convex_ind_ct::const_iterator> ref_convex_pt_ct;
58 
59  typedef gmm::tab_ref_index_ref< typename PT_TAB::const_iterator,
61 
62  protected :
63 
65  PT_TAB pts;
66 
67  public :
68 
69  ref_convex_pt_ct points_of_face(short_type i) const {
70  return ref_convex_pt_ct(pts.begin(), cvs->ind_points_of_face(i).begin(),
71  cvs->ind_points_of_face(i).end());
72  }
73 
74  /** Return "direct" points. These are the subset of points than can be
75  used to build a direct vector basis. (rarely used)
76  */
78  return ref_convex_pt_ct(pts.begin(), cvs->ind_dir_points().begin(),
79  cvs->ind_dir_points().end());
80  }
81  /** Direct points for a given face.
82  @param i the face number.
83  */
85  return dref_convex_pt_ct(pts.begin(),
86  cvs->ind_dir_points_of_face(i).begin(),
87  cvs->ind_dir_points_of_face(i).end());
88  }
89  pconvex_structure structure() const { return cvs; }
90  pconvex_structure &structure() { return cvs; }
91  const PT_TAB &points() const { return pts; }
92  PT_TAB &points() { return pts; }
93  short_type nb_points() const { return cvs->nb_points(); }
94 
95  //void translate(const typename PT::vector_type &v);
96  //template <class CONT> void base_of_orthogonal(CONT &tab);
97  convex() { }
98  /** Build a convex object.
99  @param c the convex structure.
100  @param t the points array.
101  */
102  convex(pconvex_structure c, const PT_TAB &t) : cvs(c), pts(t) {}
103  convex(pconvex_structure c) : cvs(c) {}
104  };
105  /*@}*/
106  /*template<class PT, class PT_TAB>
107  void convex<PT, PT_TAB>::translate(const typename PT::vector_type &v) {
108  typename PT_TAB::iterator b = pts.begin(), e = pts.end();
109  for ( ; b != e ; ++b) *b += v;
110  }
111  */
112  /*
113  template<class PT, class PT_TAB> template<class CONT>
114  void convex<PT, PT_TAB>::base_of_orthogonal(CONT &tab)
115  { // programmation a revoir.
116  int N = (points())[0].size();
117  pconvex_structure cv = structure();
118  int n = cv->dim();
119  dal::dynamic_array<typename PT::vector_type> vect_;
120  vsvector<double> A(N), B(N);
121  ref_convex_ind_ct dptf = cv->ind_dir_points_of_face(f);
122  int can_b = 0;
123 
124  for (int i = 0; i < n-1; i++) {
125  vect_[i] = (points())[dptf[i+1]]; vect_[i] -= (points())[dptf[0]];
126 
127  for (j = 0; j < i; j++)
128  A[j] = vect_sp(vect_[i], vect_[j]);
129  for (j = 0; j < i; j++)
130  { B = vect_[j]; B *= A[j]; vect_[i] -= B; }
131  vect_[i] /= vect_norm2(vect_[i]);
132  }
133 
134  for (int i = n; i < N; i++) {
135  vect_[i] = vect_[0];
136  vect_random(vect_[i]);
137  for (j = 0; j < i; j++)
138  A[j] = vect_sp(vect_[i], vect_[j]);
139  for (j = 0; j < i; j++)
140  { B = vect_[j]; B *= A[j]; vect_[i] -= B; }
141 
142  if (vect_norm2(vect_[i]) < 1.0E-4 )
143  i--;
144  else
145  vect_[i] /= vect_norm2(vect_[i]);
146  }
147  for (int i = n; i < N; i++) tab[i-n] = vect_[i];
148  }
149  */
150 
151  template<class PT, class PT_TAB>
152  std::ostream &operator <<(std::ostream &o, const convex<PT, PT_TAB> &cv)
153  {
154  o << *(cv.structure());
155  o << " points : ";
156  for (size_type i = 0; i < cv.nb_points(); ++i) o << cv.points()[i] << " ";
157  o << endl;
158  return o;
159  }
160 
161  /* ********************************************************************** */
162  /* Unstabilized part. */
163  /* ********************************************************************** */
164 
165  template<class PT, class PT_TAB>
166  convex<PT, PT_TAB> simplex(const PT_TAB &t, int nc)
167  { return convex<PT, PT_TAB>(simplex_structure(nc), t); }
168 
169 
170  template<class PT, class PT_TAB1, class PT_TAB2>
171  convex<PT> convex_product(const convex<PT, PT_TAB1> &cv1,
172  const convex<PT, PT_TAB2> &cv2)
173  { // optimisable
174  typename convex<PT>::point_tab_type tab;
175  tab.resize(cv1.nb_points() * cv2.nb_points());
176  size_type i,j,k;
177  for (i = 0, k = 0; i < cv1.nb_points(); ++i)
178  for (j = 0; j < cv2.nb_points(); ++j, ++k)
179  { tab[k] = (cv1.points())[i]; tab[k] += (cv2.points())[j]; }
180  return convex<PT>(
181  convex_product_structure(cv1.structure(), cv2.structure()), tab);
182  }
183 
184  struct special_convex_structure_key_ : virtual public dal::static_stored_object_key {
186  bool compare(const static_stored_object_key &oo) const override {
187  auto &o = dynamic_cast<const special_convex_structure_key_ &>(oo);
188  return p < o.p;
189  }
190  bool equal(const static_stored_object_key &oo) const override {
191  auto &o = dynamic_cast<const special_convex_structure_key_ &>(oo);
192  if (p == o.p) return true;
193 
194  auto pkey = dal::key_of_stored_object(p);
195  auto poo_key = dal::key_of_stored_object(o.p);
196  return *pkey == *poo_key;
197  }
198  special_convex_structure_key_(pconvex_structure pp) : p(pp) {}
199  };
200 
201  template<class PT, class PT_TAB1, class PT_TAB2>
202  convex<PT> convex_direct_product(const convex<PT, PT_TAB1> &cv1,
203  const convex<PT, PT_TAB2> &cv2) {
204  if (cv1.nb_points() == 0 || cv2.nb_points() == 0)
205  throw std::invalid_argument(
206  "convex_direct_product : null convex product");
207 
208  if (!dal::exists_stored_object(cv1.structure())) {
209  dal::pstatic_stored_object_key
210  pcs = std::make_shared<special_convex_structure_key_>(cv1.structure());
211  dal::add_stored_object(pcs, cv1.structure(),
212  dal::AUTODELETE_STATIC_OBJECT);
213  }
214  if (!dal::exists_stored_object(cv2.structure())) {
215  dal::pstatic_stored_object_key
216  pcs = std::make_shared<special_convex_structure_key_>(cv2.structure());
217  dal::add_stored_object(pcs, cv2.structure(),
218  dal::AUTODELETE_STATIC_OBJECT);
219  }
220  convex<PT> r(convex_product_structure(cv1.structure(), cv2.structure()));
221  r.points().resize(r.nb_points());
222  std::fill(r.points().begin(), r.points().end(), PT(r.structure()->dim()));
223  dim_type dim1 = cv1.structure()->dim();
224  typename PT_TAB1::const_iterator it1, it1e = cv1.points().end();
225  typename PT_TAB2::const_iterator it2, it2e = cv2.points().end();
226  typename convex<PT>::point_tab_type::iterator it = r.points().begin();
227  for (it2 = cv2.points().begin(); it2 != it2e; ++it2)
228  for (it1 = cv1.points().begin() ; it1 != it1e; ++it1, ++it)
229  {
230  std::copy((*it1).begin(), (*it1).end(), (*it).begin());
231  std::copy((*it2).begin(), (*it2).end(), (*it).begin()+dim1);
232  }
233  return r;
234  }
235 
236  template<class PT, class PT_TAB>
237  convex<PT> convex_multiply(const convex<PT, PT_TAB> &cv, dim_type n)
238  {
239  if (cv.nb_points() == 0 || n == 0)
240  throw std::invalid_argument(
241  "convex_multiply : null convex product");
242  convex<PT> r(multiply_convex_structure(cv.structure(), n));
243  r.points().resize(r.nb_points());
244  std::fill(r.points().begin(), r.points().end(), PT(r.structure()->dim()));
245  dim_type dim1 = cv.structure()->dim();
246  typename convex<PT>::point_tab_type::iterator it = r.points().begin();
247  typename PT_TAB::const_iterator it1 = cv.points().begin(), it2,
248  it1e = cv.points().end();
249  for (dim_type k = 0; k < n; ++k)
250  for (it2 = it1; it2 != it1e; ++it2) *it++ = *it2;
251  return r;
252  }
253 
254 } /* end of namespace bgeot. */
255 
256 
257 #endif /* BGEOT_CONVEX_H__ */
Definition of convex structures.
generic definition of a convex ( bgeot::convex_structure + vertices coordinates )
Definition: bgeot_convex.h:49
ref_convex_pt_ct dir_points() const
Return "direct" points.
Definition: bgeot_convex.h:77
dref_convex_pt_ct dir_points_of_face(short_type i) const
Direct points for a given face.
Definition: bgeot_convex.h:84
convex(pconvex_structure c, const PT_TAB &t)
Build a convex object.
Definition: bgeot_convex.h:102
indexed array reference (given a container X, and a set of indexes I, this class provides a pseudo-co...
Definition: gmm_ref.h:303
Basic Geometric Tools.
gmm::uint16_type short_type
used as the common short type integer in the library
Definition: bgeot_config.h:72
std::ostream & operator<<(std::ostream &o, const convex_structure &cv)
Print the details of the convex structure cvs to the output stream o.
std::shared_ptr< const convex_structure > pconvex_structure
Pointer on a convex structure description.
pconvex_structure simplex_structure(dim_type nc)
Give a pointer on the structures of a simplex of dimension d.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
pconvex_structure convex_product_structure(pconvex_structure a, pconvex_structure b)
Give a pointer on the structures of a convex which is the direct product of the convexes represented ...
void add_stored_object(pstatic_stored_object_key k, pstatic_stored_object o, permanence perm)
Add an object with two optional dependencies.
bool exists_stored_object(pstatic_stored_object o)
Test if an object is stored.
iterator over a gmm::tab_ref_index_ref<ITER,ITER_INDEX>
Definition: gmm_ref.h:236