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