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  T *getNearest(const Pos3d &p);
79  Pos3d getCentroid(void) const;
80  const T *getNearest(const Pos3d &p) const;
81  GEOM_FT getDistanceTo(const Pos3d &p) const;
82  bool isCloserThan(const Pos3d &, const GEOM_FT &) const;
83  bool isCloserThan(const Segment3d &, const GEOM_FT &) const;
84  bool isCloserThan(const GeomObj::list_Pos3d &, const GEOM_FT &) const;
85  bool isCloserThan(const Polygon3d &, const GEOM_FT &) const;
86  DqPtrsEntities<T> pickEntitiesInside(const GeomObj3d &, const double &tol= 0.0) const;
87  BND3d Bnd(void) const;
88  };
89 
91 template <class T>
92 T *DqPtrsEntities<T>::searchName(const std::string &nmb)
93  {
94  for(const_iterator i= this->begin();i!=this->end();i++)
95  if((*i)->getName()==nmb) return *i;
96  return nullptr;
97  }
98 
100 template <class T>
101 T *DqPtrsEntities<T>::findTag(const size_t &tag)
102  {
103  for(const_iterator i= this->begin();i!=this->end();i++)
104  if((*i)->getTag()==tag) return *i;
105  return nullptr;
106  }
107 
109 template <class T>
111  {
112  Pos3d retval;
113  if(!this->empty())
114  {
115  const size_t sz= this->size();
116  const_iterator bg= this->begin();
117  if(sz<2)
118  {
119  const Pos3d pos= (*bg)->getCentroid();
120  retval= pos;
121  }
122  else
123  {
124  const_iterator i= bg;
125  const Pos3d pos= (*i)->getCentroid();
126  Vector3d vpos_center_of_mass(pos.VectorPos());
127  i++;
128  for(; i != this->end(); i++)
129  {
130  const Pos3d pos= (*i)->getCentroid();
131  vpos_center_of_mass= vpos_center_of_mass + pos.VectorPos();
132  }
133  vpos_center_of_mass= vpos_center_of_mass * (1.0/sz);
134  retval+= vpos_center_of_mass;
135  }
136  }
137  else
138  std::cerr << Color::red << this->getClassName() << "::" << __FUNCTION__
139  << "; set is empty, so it has no centroid."
140  << Color::def << std::endl;
141  return retval;
142  }
143 
145 template <class T>
147  {
148  T *retval= nullptr;
149  if(!this->empty())
150  {
151  const_iterator i= this->begin();
152  double d2= (*i)->getSquaredDistanceTo(p);
153  retval= *i; i++;
154  double tmp;
155  for(;i!=this->end();i++)
156  {
157  tmp= (*i)->getSquaredDistanceTo(p);
158  if(tmp<d2)
159  {
160  d2= tmp;
161  retval= *i;
162  }
163  }
164  }
165  return retval;
166  }
167 
169 template <class T>
170 const T *DqPtrsEntities<T>::getNearest(const Pos3d &p) const
171  {
172  const T *retval= nullptr;
173  if(!this->empty())
174  {
175  const_iterator i= this->begin();
176  double d2= (*i)->getSquaredDistanceTo(p);
177  retval= *i; i++;
178  double tmp;
179  for(;i!=this->end();i++)
180  {
181  tmp= (*i)->getSquaredDistanceTo(p);
182  if(tmp<d2)
183  {
184  d2= tmp;
185  retval= *i;
186  }
187  }
188  }
189  return retval;
190  }
191 
193 template <class T>
195  {
196  GEOM_FT retval= std::numeric_limits<GEOM_FT>::quiet_NaN();
197  if(!this->empty())
198  {
199  const T *nearest= this->getNearest(p);
200  if(nearest)
201  retval= nearest->getDistanceTo(p);
202  else
203  {
204  std::cerr << Color::red << this->getClassName() << "::" << __FUNCTION__
205  << "; something went wrong, can't compute distance."
206  << Color::def << std::endl;
207  }
208  }
209  else
210  {
211  std::cerr << Color::red << this->getClassName() << "::" << __FUNCTION__
212  << "; this set is empty, so there is no distance."
213  << Color::def << std::endl;
214  }
215  return retval;
216  }
217 
222 template <class T>
223 bool DqPtrsEntities<T>::isCloserThan(const Pos3d &p, const GEOM_FT &d) const
224  {
225  bool retval= false;
226  const GEOM_FT dist= this->getDistanceTo(p);
227  if(!std::isnan(dist))
228  retval= (this->getDistanceTo(p)<=d);
229  return retval;
230  }
231 
236 template <class T>
237 bool DqPtrsEntities<T>::isCloserThan(const Segment3d &s, const GEOM_FT &d) const
238  {
239  const Pos3d &p1= s.getFromPoint();
240  const GEOM_FT d1= this->getDistanceTo(p1);
241  const Pos3d &p2= s.getToPoint();
242  const GEOM_FT d2= this->getDistanceTo(p2);
243  return ((d1<=d) && (d2<=d));
244  }
245 
246 
251 template <class T>
252 bool DqPtrsEntities<T>::isCloserThan(const GeomObj::list_Pos3d &vertices, const GEOM_FT &d) const
253  {
254  bool retval= false;
255  if(!vertices.empty())
256  {
257  GeomObj::list_Pos3d::const_iterator i= vertices.begin();
258  const Pos3d &pi= *i;
259  retval= this->isCloserThan(pi, d);
260  if(retval) // The first one is OK.
261  {
262  i++;
263  for(;i!=vertices.end();i++)
264  {
265  const Pos3d &pj= *i;
266  retval= this->isCloserThan(pj, d);
267  if(!retval)
268  break;
269  }
270  }
271  }
272  return retval;
273  }
274 
279 template <class T>
280 bool DqPtrsEntities<T>::isCloserThan(const Polygon3d &plg, const GEOM_FT &d) const
281  {
282  const GeomObj::list_Pos3d vertices= plg.getVertexList();
283  return this->isCloserThan(vertices, d);
284  }
285 
291 template <class T>
292 DqPtrsEntities<T> DqPtrsEntities<T>::pickEntitiesInside(const GeomObj3d &geomObj, const double &tol) const
293  {
294  DqPtrsEntities<T> retval;
295  for(const_iterator i= this->begin();i!= this->end();i++)
296  {
297  T *t= (*i);
298  assert(t);
299  if(t->In(geomObj,tol))
300  retval.push_back(t);
301  }
302  return retval;
303  }
304 
305 
308 template <class T>
310  {
311  BND3d retval;
312  if(!this->empty())
313  {
314  const_iterator i= this->begin();
315  const T *t= (*i);
316  assert(t);
317  retval= t->Bnd();
318  i++;
319  for(;i!= this->end();i++)
320  {
321  const T *t= (*i);
322  assert(t);
323  retval+= t->Bnd();
324  }
325  }
326  return retval;
327  }
328 
330 template <class T>
332  {
333  for(const_iterator i= other.begin();i!= other.end();i++)
334  {
335  const T *t= (*i);
336  iterator j= find(this->begin(),this->end(),t);
337  if(j!=this->end()) //Found.
338  this->erase(j);
339  }
340  }
341 
343 template <class T>
345  {
346  for(const_iterator i= other.begin();i!= other.end();i++)
347  {
348  const T *t= (*i);
349  iterator j= find(this->begin(),this->end(),t);
350  if(j==this->end()) //Not found
351  this->erase(j);
352  }
353  }
354 
356 // (remove means set the corresponding pointer to null).
358 template <class T>
360  {
361  bool retval= false;
362  if(ePtr)
363  {
364  for(const_iterator i= this->begin();i!= this->end();i++)
365  {
366  T *t= (*i);
367  const bool tmp= t->remove(ePtr);
368  if(tmp)
369  retval= true;
370  }
371  }
372  return retval;
373  }
374 
376 // (remove means set the corresponding pointer to null).
378 template <class T>
380  {
381  bool retval= false;
382  if(nPtr)
383  {
384  for(const_iterator i= this->begin();i!= this->end();i++)
385  {
386  T *t= (*i);
387  const bool tmp= t->remove(nPtr);
388  if(tmp)
389  retval= true;
390  }
391  }
392  return retval;
393  }
394 
396 template <class T>
398  {
399  remove(other);
400  return *this;
401  }
402 
404 template <class T>
406  {
407  intersect(other);
408  return *this;
409  }
410 
411 
413 template <class T>
415  {
416  DqPtrsEntities<T> retval(a);
417  retval+=b;
418  return retval;
419  }
420 
422 template <class T>
424  {
425  DqPtrsEntities<T> retval;
426  for(typename DqPtrsEntities<T>::const_iterator i= a.begin();i!= a.end();i++)
427  {
428  const T *t= (*i);
429  const typename DqPtrsEntities<T>::const_iterator j= find(b.begin(),b.end(),t);
430  if(j==b.end()) //Not found in b.
431  retval.push_back(t);
432  }
433  return retval;
434  }
435 
437 template <class T>
439  {
440  DqPtrsEntities<T> retval;
441  for(typename DqPtrsEntities<T>::const_iterator i= a.begin();i!= a.end();i++)
442  {
443  const T *t= (*i);
444  const typename DqPtrsEntities<T>::const_iterator j= find(b.begin(),b.end(),t);
445  if(j!=b.end()) //Found also in b.
446  retval.push_back(t);
447  }
448  return retval;
449  }
450 
453 template <class T>
455  {
456  public:
457  typedef std::set<T*> shadow_makers;
458  typedef boost::icl::interval_map<double, shadow_makers> shadow_interval_map;
459  typedef typename shadow_interval_map::iterator interval_iterator;
460  typedef typename shadow_interval_map::const_iterator interval_const_iterator;
461  typedef boost::icl::interval<double> shadow_interval;
462  protected:
463  shadow_interval_map x_shadows; // "shadows" of the objects in the x axis.
464  shadow_interval_map y_shadows; // "shadows" of the objects in the y axis.
465  shadow_interval_map z_shadows; // "shadows" of the objects in the z axis.
466  public:
468 
469  void add(T *);
470  void remove(T *);
471 
472  std::set<T *> getNeighbors(const Pos3d &pMin, const Pos3d &pMax) const;
473  };
474 
479 template <class T>
481  {
482  typedef typename DqPtrsEntities<T>::const_iterator const_iterator;
483  for(const_iterator i= entities.begin();i!= entities.end();i++)
484  {
485  T *t= (*i);
486  assert(t);
487  this->add(t);
488  }
489  }
490 
492 template <class T>
494  {
495  shadow_makers sm;
496  sm.insert(t);
497  BND3d tmp= t->Bnd();
498  const Pos3d pMin= tmp.getPMin();
499  const Pos3d pMax= tmp.getPMax();
500  auto shadow_interval_x= shadow_interval::closed(pMin.x(), pMax.x());
501  auto shadow_interval_y= shadow_interval::closed(pMin.y(), pMax.y());
502  auto shadow_interval_z= shadow_interval::closed(pMin.z(), pMax.z());
503  x_shadows.add(std::make_pair(shadow_interval_x, sm));
504  y_shadows.add(std::make_pair(shadow_interval_y, sm));
505  z_shadows.add(std::make_pair(shadow_interval_z, sm));
506  }
507 
509 template <class T>
511  {
512  shadow_makers sm;
513  sm.insert(t);
514  BND3d tmp= t->Bnd();
515  const Pos3d pMin= tmp.getPMin();
516  const Pos3d pMax= tmp.getPMax();
517  const double margin= 1.0; // To be sure that the interval is completely removed.
518  auto shadow_interval_x= shadow_interval::closed(pMin.x()-margin, pMax.x()+margin);
519  auto shadow_interval_y= shadow_interval::closed(pMin.y()-margin, pMax.y()+margin);
520  auto shadow_interval_z= shadow_interval::closed(pMin.z()-margin, pMax.z()+margin);
521  x_shadows-= std::make_pair(shadow_interval_x, sm);
522  y_shadows-= std::make_pair(shadow_interval_y, sm);
523  z_shadows-= std::make_pair(shadow_interval_z, sm);
524  }
525 
527 template <class T>
528 std::set<T *> EntitiesShadows<T>::getNeighbors(const Pos3d &pMin, const Pos3d &pMax) const
529  {
530  std::set<T *> retval;
531  auto x_shadow= shadow_interval::closed(pMin.x(), pMax.x());
532  shadow_interval_map x_neighbors= x_shadows & x_shadow;
533  if(!x_neighbors.empty())
534  {
535  auto y_shadow= shadow_interval::closed(pMin.y(), pMax.y());
536  shadow_interval_map y_neighbors= y_shadows & y_shadow;
537  if(!y_neighbors.empty())
538  {
539  auto z_shadow= shadow_interval::closed(pMin.z(), pMax.z());
540  shadow_interval_map z_neighbors= z_shadows & z_shadow;
541  if(!z_neighbors.empty())
542  {
543  std::set<T *> z_set;
544  for(interval_const_iterator k= z_neighbors.begin();k!=z_neighbors.end();k++)
545  { z_set.insert((*k).second.begin(), (*k).second.end()); }
546  std::set<T *> y_set;
547  for(interval_const_iterator j= y_neighbors.begin();j!=y_neighbors.end();j++)
548  { y_set.insert((*j).second.begin(), (*j).second.end()); }
549  std::set<T *> x_set;
550  for(interval_const_iterator i= x_neighbors.begin();i!=x_neighbors.end();i++)
551  { x_set.insert((*i).second.begin(), (*i).second.end()); }
552  // Intersection of the three sets.
553  std::set<T *> yz_set;
554  set_intersection(y_set.begin(), y_set.end(), z_set.begin(), z_set.end(), std::inserter(yz_set, yz_set.begin()));
555  set_intersection(x_set.begin(), x_set.end(), yz_set.begin(), yz_set.end(), std::inserter(retval, retval.begin()));
556  }
557  }
558  }
559  return retval;
560  }
561 
562 
563 } //end of XC namespace
564 
565 #endif
566 
Shadows of the entities on the coordinate axis used for spatial indexing.
Definition: DqPtrsEntities.h:454
void add(T *)
Add entity to the interval map.
Definition: DqPtrsEntities.h:493
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:41
Base class for position lists.
Definition: PolyPos.h:35
void intersect(const DqPtrsEntities< T > &other)
Removes the objects that belongs also to the given container.
Definition: DqPtrsEntities.h:344
EntitiesShadows(const DqPtrsEntities< T > &)
Constructor.
Definition: DqPtrsEntities.h:480
BND3d Bnd(void) const
Return the entities boundary.
Definition: DqPtrsEntities.h:309
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:405
T * getNearest(const Pos3d &p)
Returns the object closest to the position being passed as parameter.
Definition: DqPtrsEntities.h:146
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:223
DqPtrsEntities< T > & operator-=(const DqPtrsEntities< T > &)
-= (difference) operator.
Definition: DqPtrsEntities.h:397
void remove(const DqPtrsEntities< T > &other)
Removes the objects that belongs also to the given container.
Definition: DqPtrsEntities.h:331
void remove(T *)
Remove entity from the interval map.
Definition: DqPtrsEntities.h:510
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:110
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:92
T * findTag(const size_t &)
Returns a pointer to the object identified by the tag argument.
Definition: DqPtrsEntities.h:101
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:292
Mesh node.
Definition: Node.h:111
GEOM_FT getDistanceTo(const Pos3d &p) const
Return the minimum of the distances to the given point.
Definition: DqPtrsEntities.h:194
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:528
Vector en tres dimensiones.
Definition: Vector3d.h:39
Clase base para los objetos en tres dimensiones.
Definition: GeomObj3d.h:43