xc
DqPtrsEntities.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // XC program; finite element analysis code
4 // for structural analysis and design.
5 //
6 // Copyright (C) Luis C. Pérez Tato
7 //
8 // This program derives from OpenSees <http://opensees.berkeley.edu>
9 // developed by the «Pacific earthquake engineering research center».
10 //
11 // Except for the restrictions that may arise from the copyright
12 // of the original program (see copyright_opensees.txt)
13 // XC is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // This software is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program.
26 // If not, see <http://www.gnu.org/licenses/>.
27 //----------------------------------------------------------------------------
28 //DqPtrsEntities.h
29 //deque de pointers (se emplear en la clase Set).
30 
31 
32 #ifndef DQPTRSENTITIES_H
33 #define DQPTRSENTITIES_H
34 
35 #include "DqPtrs.h"
36 #include "utility/geom/pos_vec/Pos3d.h"
37 #include "utility/geom/pos_vec/Vector3d.h"
38 #include "utility/geom/d2/Polygon3d.h"
39 #include "utility/geom/d1/Segment3d.h"
40 #include "utility/geom/d3/BND3d.h"
41 #include "boost/icl/interval_map.hpp"
42 #include "utility/utils/misc_utils/colormod.h"
43 
44 class GeomObj3d;
45 class BND3d;
46 
47 namespace XC {
48 
50 template <class T>
51 class DqPtrsEntities: public DqPtrs<T>
52  {
53  public:
54  typedef DqPtrs<T> dq_ptr;
55  typedef typename dq_ptr::const_iterator const_iterator;
56  typedef typename dq_ptr::iterator iterator;
57 
58  public:
59  DqPtrsEntities(CommandEntity *owr= nullptr)
60  : DqPtrs<T>(owr) {}
61  DqPtrsEntities(const DqPtrs<T> &other)
62  : DqPtrs<T>(other) {}
63  explicit DqPtrsEntities(const std::deque<T *> &ts)
64  : DqPtrs<T>(ts) {}
65  explicit DqPtrsEntities(const std::set<const T *> &ts)
66  : DqPtrs<T>(ts) {}
67 
70 
71  void remove(const DqPtrsEntities<T> &other);
72  void intersect(const DqPtrsEntities<T> &other);
73  bool remove(Element *);
74  bool remove(Node *);
75 
76  T *searchName(const std::string &nmb);
77  T *findTag(const size_t &);
78  const T *findTag(const size_t &) const;
79  T *getNearest(const Pos3d &p);
80  Pos3d getCentroid(void) const;
81  const T *getNearest(const Pos3d &p) const;
82  GEOM_FT getDistanceTo(const Pos3d &p) const;
83  bool isCloserThan(const Pos3d &, const GEOM_FT &) const;
84  bool isCloserThan(const Segment3d &, const GEOM_FT &) const;
85  bool isCloserThan(const GeomObj::list_Pos3d &, const GEOM_FT &) const;
86  bool isCloserThan(const Polygon3d &, const GEOM_FT &) const;
87  DqPtrsEntities<T> pickEntitiesInside(const GeomObj3d &, const double &tol= 0.0) const;
88  BND3d Bnd(void) const;
89  };
90 
92 template <class T>
93 T *DqPtrsEntities<T>::searchName(const std::string &nmb)
94  {
95  for(const_iterator i= this->begin();i!=this->end();i++)
96  if((*i)->getName()==nmb) return *i;
97  return nullptr;
98  }
99 
101 template <class T>
102 T *DqPtrsEntities<T>::findTag(const size_t &tag)
103  {
104  for(const_iterator i= this->begin();i!=this->end();i++)
105  if((*i)->getTag()==tag) return *i;
106  return nullptr;
107  }
108 
110 template <class T>
111 const T *DqPtrsEntities<T>::findTag(const size_t &tag) const
112  {
113  for(const_iterator i= this->begin();i!=this->end();i++)
114  if((*i)->getTag()==tag) return *i;
115  return nullptr;
116  }
117 
119 template <class T>
121  {
122  Pos3d retval;
123  if(!this->empty())
124  {
125  const size_t sz= this->size();
126  const_iterator bg= this->begin();
127  if(sz<2)
128  {
129  const Pos3d pos= (*bg)->getCentroid();
130  retval= pos;
131  }
132  else
133  {
134  const_iterator i= bg;
135  const Pos3d pos= (*i)->getCentroid();
136  Vector3d vpos_center_of_mass(pos.VectorPos());
137  i++;
138  for(; i != this->end(); i++)
139  {
140  const Pos3d pos= (*i)->getCentroid();
141  vpos_center_of_mass= vpos_center_of_mass + pos.VectorPos();
142  }
143  vpos_center_of_mass= vpos_center_of_mass * (1.0/sz);
144  retval+= vpos_center_of_mass;
145  }
146  }
147  else
148  std::cerr << Color::red << this->getClassName() << "::" << __FUNCTION__
149  << "; set is empty, so it has no centroid."
150  << Color::def << std::endl;
151  return retval;
152  }
153 
155 template <class T>
157  {
158  T *retval= nullptr;
159  if(!this->empty())
160  {
161  const_iterator i= this->begin();
162  double d2= (*i)->getSquaredDistanceTo(p);
163  retval= *i; i++;
164  double tmp;
165  for(;i!=this->end();i++)
166  {
167  tmp= (*i)->getSquaredDistanceTo(p);
168  if(tmp<d2)
169  {
170  d2= tmp;
171  retval= *i;
172  }
173  }
174  }
175  return retval;
176  }
177 
179 template <class T>
180 const T *DqPtrsEntities<T>::getNearest(const Pos3d &p) const
181  {
182  const T *retval= nullptr;
183  if(!this->empty())
184  {
185  const_iterator i= this->begin();
186  double d2= (*i)->getSquaredDistanceTo(p);
187  retval= *i; i++;
188  double tmp;
189  for(;i!=this->end();i++)
190  {
191  tmp= (*i)->getSquaredDistanceTo(p);
192  if(tmp<d2)
193  {
194  d2= tmp;
195  retval= *i;
196  }
197  }
198  }
199  return retval;
200  }
201 
203 template <class T>
205  {
206  GEOM_FT retval= std::numeric_limits<GEOM_FT>::quiet_NaN();
207  if(!this->empty())
208  {
209  const T *nearest= this->getNearest(p);
210  if(nearest)
211  retval= nearest->getDistanceTo(p);
212  else
213  {
214  std::cerr << Color::red << this->getClassName() << "::" << __FUNCTION__
215  << "; something went wrong, can't compute distance."
216  << Color::def << std::endl;
217  }
218  }
219  else
220  {
221  std::cerr << Color::red << this->getClassName() << "::" << __FUNCTION__
222  << "; this set is empty, so there is no distance."
223  << Color::def << std::endl;
224  }
225  return retval;
226  }
227 
232 template <class T>
233 bool DqPtrsEntities<T>::isCloserThan(const Pos3d &p, const GEOM_FT &d) const
234  {
235  bool retval= false;
236  const GEOM_FT dist= this->getDistanceTo(p);
237  if(!std::isnan(dist))
238  retval= (this->getDistanceTo(p)<=d);
239  return retval;
240  }
241 
246 template <class T>
247 bool DqPtrsEntities<T>::isCloserThan(const Segment3d &s, const GEOM_FT &d) const
248  {
249  const Pos3d &p1= s.getFromPoint();
250  const GEOM_FT d1= this->getDistanceTo(p1);
251  const Pos3d &p2= s.getToPoint();
252  const GEOM_FT d2= this->getDistanceTo(p2);
253  return ((d1<=d) && (d2<=d));
254  }
255 
256 
261 template <class T>
262 bool DqPtrsEntities<T>::isCloserThan(const GeomObj::list_Pos3d &vertices, const GEOM_FT &d) const
263  {
264  bool retval= false;
265  if(!vertices.empty())
266  {
267  GeomObj::list_Pos3d::const_iterator i= vertices.begin();
268  const Pos3d &pi= *i;
269  retval= this->isCloserThan(pi, d);
270  if(retval) // The first one is OK.
271  {
272  i++;
273  for(;i!=vertices.end();i++)
274  {
275  const Pos3d &pj= *i;
276  retval= this->isCloserThan(pj, d);
277  if(!retval)
278  break;
279  }
280  }
281  }
282  return retval;
283  }
284 
289 template <class T>
290 bool DqPtrsEntities<T>::isCloserThan(const Polygon3d &plg, const GEOM_FT &d) const
291  {
292  const GeomObj::list_Pos3d vertices= plg.getVertexList();
293  return this->isCloserThan(vertices, d);
294  }
295 
301 template <class T>
302 DqPtrsEntities<T> DqPtrsEntities<T>::pickEntitiesInside(const GeomObj3d &geomObj, const double &tol) const
303  {
304  DqPtrsEntities<T> retval;
305  for(const_iterator i= this->begin();i!= this->end();i++)
306  {
307  T *t= (*i);
308  assert(t);
309  if(t->In(geomObj,tol))
310  retval.push_back(t);
311  }
312  return retval;
313  }
314 
315 
318 template <class T>
320  {
321  BND3d retval;
322  if(!this->empty())
323  {
324  const_iterator i= this->begin();
325  const T *t= (*i);
326  assert(t);
327  retval= t->Bnd();
328  i++;
329  for(;i!= this->end();i++)
330  {
331  const T *t= (*i);
332  assert(t);
333  retval+= t->Bnd();
334  }
335  }
336  return retval;
337  }
338 
340 template <class T>
342  {
343  for(const_iterator i= other.begin();i!= other.end();i++)
344  {
345  const T *t= (*i);
346  iterator j= find(this->begin(),this->end(),t);
347  if(j!=this->end()) //Found.
348  this->erase(j);
349  }
350  }
351 
353 template <class T>
355  {
356  for(const_iterator i= other.begin();i!= other.end();i++)
357  {
358  const T *t= (*i);
359  iterator j= find(this->begin(),this->end(),t);
360  if(j==this->end()) //Not found
361  this->erase(j);
362  }
363  }
364 
366 // (remove means set the corresponding pointer to null).
368 template <class T>
370  {
371  bool retval= false;
372  if(ePtr)
373  {
374  for(const_iterator i= this->begin();i!= this->end();i++)
375  {
376  T *t= (*i);
377  const bool tmp= t->remove(ePtr);
378  if(tmp)
379  retval= true;
380  }
381  }
382  return retval;
383  }
384 
386 // (remove means set the corresponding pointer to null).
388 template <class T>
390  {
391  bool retval= false;
392  if(nPtr)
393  {
394  for(const_iterator i= this->begin();i!= this->end();i++)
395  {
396  T *t= (*i);
397  const bool tmp= t->remove(nPtr);
398  if(tmp)
399  retval= true;
400  }
401  }
402  return retval;
403  }
404 
406 template <class T>
408  {
409  remove(other);
410  return *this;
411  }
412 
414 template <class T>
416  {
417  intersect(other);
418  return *this;
419  }
420 
421 
423 template <class T>
425  {
426  DqPtrsEntities<T> retval(a);
427  retval+=b;
428  return retval;
429  }
430 
432 template <class T>
434  {
435  DqPtrsEntities<T> retval;
436  for(typename DqPtrsEntities<T>::const_iterator i= a.begin();i!= a.end();i++)
437  {
438  const T *t= (*i);
439  const typename DqPtrsEntities<T>::const_iterator j= find(b.begin(),b.end(),t);
440  if(j==b.end()) //Not found in b.
441  retval.push_back(t);
442  }
443  return retval;
444  }
445 
447 template <class T>
449  {
450  DqPtrsEntities<T> retval;
451  for(typename DqPtrsEntities<T>::const_iterator i= a.begin();i!= a.end();i++)
452  {
453  const T *t= (*i);
454  const typename DqPtrsEntities<T>::const_iterator j= find(b.begin(),b.end(),t);
455  if(j!=b.end()) //Found also in b.
456  retval.push_back(t);
457  }
458  return retval;
459  }
460 
463 template <class T>
465  {
466  public:
467  typedef std::set<T*> shadow_makers;
468  typedef boost::icl::interval_map<double, shadow_makers> shadow_interval_map;
469  typedef typename shadow_interval_map::iterator interval_iterator;
470  typedef typename shadow_interval_map::const_iterator interval_const_iterator;
471  typedef boost::icl::interval<double> shadow_interval;
472  protected:
473  shadow_interval_map x_shadows; // "shadows" of the objects in the x axis.
474  shadow_interval_map y_shadows; // "shadows" of the objects in the y axis.
475  shadow_interval_map z_shadows; // "shadows" of the objects in the z axis.
476  public:
478 
479  void add(T *);
480  void remove(T *);
481 
482  std::set<T *> getNeighbors(const Pos3d &pMin, const Pos3d &pMax) const;
483  };
484 
489 template <class T>
491  {
492  typedef typename DqPtrsEntities<T>::const_iterator const_iterator;
493  for(const_iterator i= entities.begin();i!= entities.end();i++)
494  {
495  T *t= (*i);
496  assert(t);
497  this->add(t);
498  }
499  }
500 
502 template <class T>
504  {
505  shadow_makers sm;
506  sm.insert(t);
507  BND3d tmp= t->Bnd();
508  const Pos3d pMin= tmp.getPMin();
509  const Pos3d pMax= tmp.getPMax();
510  auto shadow_interval_x= shadow_interval::closed(pMin.x(), pMax.x());
511  auto shadow_interval_y= shadow_interval::closed(pMin.y(), pMax.y());
512  auto shadow_interval_z= shadow_interval::closed(pMin.z(), pMax.z());
513  x_shadows.add(std::make_pair(shadow_interval_x, sm));
514  y_shadows.add(std::make_pair(shadow_interval_y, sm));
515  z_shadows.add(std::make_pair(shadow_interval_z, sm));
516  }
517 
519 template <class T>
521  {
522  shadow_makers sm;
523  sm.insert(t);
524  BND3d tmp= t->Bnd();
525  const Pos3d pMin= tmp.getPMin();
526  const Pos3d pMax= tmp.getPMax();
527  const double margin= 1.0; // To be sure that the interval is completely removed.
528  auto shadow_interval_x= shadow_interval::closed(pMin.x()-margin, pMax.x()+margin);
529  auto shadow_interval_y= shadow_interval::closed(pMin.y()-margin, pMax.y()+margin);
530  auto shadow_interval_z= shadow_interval::closed(pMin.z()-margin, pMax.z()+margin);
531  x_shadows-= std::make_pair(shadow_interval_x, sm);
532  y_shadows-= std::make_pair(shadow_interval_y, sm);
533  z_shadows-= std::make_pair(shadow_interval_z, sm);
534  }
535 
537 template <class T>
538 std::set<T *> EntitiesShadows<T>::getNeighbors(const Pos3d &pMin, const Pos3d &pMax) const
539  {
540  std::set<T *> retval;
541  auto x_shadow= shadow_interval::closed(pMin.x(), pMax.x());
542  shadow_interval_map x_neighbors= x_shadows & x_shadow;
543  if(!x_neighbors.empty())
544  {
545  auto y_shadow= shadow_interval::closed(pMin.y(), pMax.y());
546  shadow_interval_map y_neighbors= y_shadows & y_shadow;
547  if(!y_neighbors.empty())
548  {
549  auto z_shadow= shadow_interval::closed(pMin.z(), pMax.z());
550  shadow_interval_map z_neighbors= z_shadows & z_shadow;
551  if(!z_neighbors.empty())
552  {
553  std::set<T *> z_set;
554  for(interval_const_iterator k= z_neighbors.begin();k!=z_neighbors.end();k++)
555  { z_set.insert((*k).second.begin(), (*k).second.end()); }
556  std::set<T *> y_set;
557  for(interval_const_iterator j= y_neighbors.begin();j!=y_neighbors.end();j++)
558  { y_set.insert((*j).second.begin(), (*j).second.end()); }
559  std::set<T *> x_set;
560  for(interval_const_iterator i= x_neighbors.begin();i!=x_neighbors.end();i++)
561  { x_set.insert((*i).second.begin(), (*i).second.end()); }
562  // Intersection of the three sets.
563  std::set<T *> yz_set;
564  set_intersection(y_set.begin(), y_set.end(), z_set.begin(), z_set.end(), std::inserter(yz_set, yz_set.begin()));
565  set_intersection(x_set.begin(), x_set.end(), yz_set.begin(), yz_set.end(), std::inserter(retval, retval.begin()));
566  }
567  }
568  }
569  return retval;
570  }
571 
572 
573 } //end of XC namespace
574 
575 #endif
576 
Shadows of the entities on the coordinate axis used for spatial indexing.
Definition: DqPtrsEntities.h:464
void add(T *)
Add entity to the interval map.
Definition: DqPtrsEntities.h:503
Plane polygon in a 3D space.
Definition: Polygon3d.h:35
GeomObj::list_Pos3d getVertexList(void) const
Return a Python list containing the positions of the polygon vertices.
Definition: Polygon3d.cc:73
Segment en tres dimensiones.
Definition: Segment3d.h:42
void intersect(const DqPtrsEntities< T > &other)
Removes the objects that belongs also to the given container.
Definition: DqPtrsEntities.h:354
EntitiesShadows(const DqPtrsEntities< T > &)
Constructor.
Definition: DqPtrsEntities.h:490
BND3d Bnd(void) const
Return the entities boundary.
Definition: DqPtrsEntities.h:319
Vector3d VectorPos(void) const
Return the position vector of the point.
Definition: Pos3d.cc:93
Base class for the finite elements.
Definition: Element.h:112
FiberSet operator+(const FiberSet &, const FiberSet &)
Return the union of both containers.
Definition: FiberSet.cc:65
DqPtrsEntities< T > & operator*=(const DqPtrsEntities< T > &)
*= (intersection) operator.
Definition: DqPtrsEntities.h:415
T * getNearest(const Pos3d &p)
Returns the object closest to the position being passed as parameter.
Definition: DqPtrsEntities.h:156
Container for preprocessor entities (points, lines, surfaces,...)
Definition: DqPtrsEntities.h:51
virtual std::string getClassName(void) const
Returns demangled class name.
Definition: EntityWithOwner.cc:90
Objet that can execute python scripts.
Definition: CommandEntity.h:40
"boundary" en tres dimensiones.
Definition: BND3d.h:34
bool isCloserThan(const Pos3d &, const GEOM_FT &) const
Return true if the distance to the given point is smaller than the given one.
Definition: DqPtrsEntities.h:233
DqPtrsEntities< T > & operator-=(const DqPtrsEntities< T > &)
-= (difference) operator.
Definition: DqPtrsEntities.h:407
void remove(const DqPtrsEntities< T > &other)
Removes the objects that belongs also to the given container.
Definition: DqPtrsEntities.h:341
void remove(T *)
Remove entity from the interval map.
Definition: DqPtrsEntities.h:520
Posición en tres dimensiones.
Definition: Pos3d.h:44
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:35
FiberSet operator-(const FiberSet &, const FiberSet &)
Return the fibers in a that are not in b.
Definition: FiberSet.cc:73
Pos3d getCentroid(void) const
Returns the centroid of the entities.
Definition: DqPtrsEntities.h:120
BND3d Bnd(void) const
Return the boundary of the object.
Definition: GeomObj3d.cc:82
T * searchName(const std::string &nmb)
Returns a pointer to the object identified by the name.
Definition: DqPtrsEntities.h:93
T * findTag(const size_t &)
Returns a pointer to the object identified by the tag argument.
Definition: DqPtrsEntities.h:102
DqPtrsEntities< T > pickEntitiesInside(const GeomObj3d &, const double &tol=0.0) const
Return a container with the entities that lie inside the geometric object.
Definition: DqPtrsEntities.h:302
Mesh node.
Definition: Node.h:112
GEOM_FT getDistanceTo(const Pos3d &p) const
Return the minimum of the distances to the given point.
Definition: DqPtrsEntities.h:204
Pointer to (nodes, elements, points, lines,...) container.
Definition: DqPtrs.h:57
std::set< T * > getNeighbors(const Pos3d &pMin, const Pos3d &pMax) const
Return the objects whose "shadow" overlaps whith the interval argument.
Definition: DqPtrsEntities.h:538
Vector en tres dimensiones.
Definition: Vector3d.h:39
Clase base para los objetos en tres dimensiones.
Definition: GeomObj3d.h:43