xc
DqPtrsKDTree.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 //DqPtrsKDTree.h
29 
30 
31 #ifndef DQPTRSKDTREE_H
32 #define DQPTRSKDTREE_H
33 
34 #include "DqPtrs.h"
35 #include <set>
36 
37 class Pos3d;
38 class Vector3d;
39 
40 namespace XC {
41 class TrfGeom;
42 
47 template <class T,class KDTree>
48 class DqPtrsKDTree: public DqPtrs<T>
49  {
50  KDTree kdtree;
51  protected:
52  void create_tree(void);
53  public:
54  typedef typename DqPtrs<T>::const_iterator const_iterator;
55  typedef typename DqPtrs<T>::iterator iterator;
56  typedef typename DqPtrs<T>::reference reference;
57  typedef typename DqPtrs<T>::const_reference const_reference;
58  typedef typename DqPtrs<T>::size_type size_type;
59 
60  DqPtrsKDTree(CommandEntity *owr= nullptr);
61  DqPtrsKDTree(const DqPtrsKDTree &);
62  explicit DqPtrsKDTree(const std::deque<T *> &);
63  explicit DqPtrsKDTree(const std::set<const T *> &);
66  void extend(const DqPtrsKDTree &);
67  //void extend_cond(const DqPtrsKDTree &,const std::string &cond);
68  bool push_back(T *);
69  bool push_front(T *);
70  bool remove(T *);
71  void clearAll(void);
72 
73  T *getNearest(const Pos3d &p);
74  const T *getNearest(const Pos3d &p) const;
75  };
76 
78 template <class T,class KDTree>
80  {
81  kdtree.clear();
82  for(iterator i= this->begin();i!=this->end();i++)
83  {
84  T *tPtr= *i;
85  assert(tPtr);
86  kdtree.insert(*tPtr);
87  }
88  }
89 
91 template <class T,class KDTree>
93  : DqPtrs<T>(owr) {}
94 
96 template <class T,class KDTree>
98  : DqPtrs<T>(other)
99  { create_tree(); }
100 
102 template <class T,class KDTree>
103 DqPtrsKDTree<T,KDTree>::DqPtrsKDTree(const std::deque<T *> &ts)
104  : DqPtrs<T>(ts)
105  { create_tree(); }
106 
108 template <class T,class KDTree>
109 DqPtrsKDTree<T,KDTree>::DqPtrsKDTree(const std::set<const T *> &st)
110  : DqPtrs<T>()
111  {
112  typename std::set<const T *>::const_iterator k;
113  k= st.begin();
114  for(;k!=st.end();k++)
115  push_back(const_cast<T *>(*k));
116  }
117 
119 template <class T,class KDTree>
121  {
122  DqPtrs<T>::operator=(other);
123  kdtree= other.kdtree;
124  return *this;
125  }
126 
129 template <class T,class KDTree>
131  {
132  for( DqPtrsKDTree<T,KDTree>::const_iterator i= other.begin();i!=other.end();i++)
133  push_back(*i);
134  }
136 template <class T,class KDTree>
138  {
139  extend(other);
140  return *this;
141  }
142 
144 template <class T,class KDTree>
146  {
147  bool retval= DqPtrs<T>::push_back(t);
148  if(retval)
149  kdtree.insert(*t);
150  return retval;
151  }
152 
154 template <class T,class KDTree>
156  {
157  bool retval= DqPtrs<T>::push_front(t);
158  if(retval)
159  kdtree.insert(*t);
160  return retval;
161  }
162 
164 template <class T,class KDTree>
166  {
167  bool retval= DqPtrs<T>::remove(t);
168  if(retval)
169  kdtree.erase(*t);
170  return retval;
171  }
172 
174 template <class T,class KDTree>
176  {
178  kdtree.clear();
179  }
181 template <class T,class KDTree>
183  {
184  T *retval= const_cast<T *>(kdtree.getNearest(p));
185  return retval;
186  }
187 
189 template <class T,class KDTree>
191  {
192  DqPtrsKDTree<T,KDTree> *this_no_const= const_cast<DqPtrsKDTree *>(this);
193  return this_no_const->getNearest(p);
194  }
195 
197 template <class T,class KDTree>
199  {
200  DqPtrsKDTree<T,KDTree> retval(a);
201  retval+=b;
202  return retval;
203  }
204 
206 template <class T,class KDTree>
208  {
209  DqPtrsKDTree<T,KDTree> retval;
210  for(typename DqPtrsKDTree<T,KDTree>::const_iterator i= a.begin();i!=a.end();i++)
211  {
212  T *n= (*i);
213  assert(n);
214  if(!b.in(n)) //If not in b.
215  retval.push_back(n);
216  }
217  return retval;
218  }
219 
221 template <class T,class KDTree>
223  {
224  DqPtrsKDTree<T,KDTree> retval;
225  for(typename DqPtrsKDTree<T,KDTree>::const_iterator i= a.begin();i!=a.end();i++)
226  {
227  T *n= (*i);
228  assert(n);
229  if(b.in(n)) //If also in b.
230  retval.push_back(n);
231  }
232  return retval;
233  }
234 
235 } //end of XC namespace
236 #endif
237 
bool remove(T *)
Removes an object from the container.
Definition: DqPtrsKDTree.h:165
bool push_back(T *)
Inserts an object at the end of the container.
Definition: DqPtrsKDTree.h:145
void clearAll(void)
Clears out the list of pointers and erases the properties of the object (if any). ...
Definition: DqPtrsKDTree.h:175
DqPtrsKDTree & operator=(const DqPtrsKDTree &)
Assignment operator.
Definition: DqPtrsKDTree.h:120
FiberSet operator+(const FiberSet &, const FiberSet &)
Return the union of both containers.
Definition: FiberSet.cc:65
DqPtrsKDTree(CommandEntity *owr=nullptr)
Constructor.
Definition: DqPtrsKDTree.h:92
void create_tree(void)
Creates the KD tree.
Definition: DqPtrsKDTree.h:79
Objet that can execute python scripts.
Definition: CommandEntity.h:40
T * getNearest(const Pos3d &p)
Returns the object closest to the point being passed as parameter.
Definition: DqPtrsKDTree.h:182
DqPtrsKDTree & operator+=(const DqPtrsKDTree &)
+= operator.
Definition: DqPtrsKDTree.h:137
Container with a KDTree.
Definition: DqPtrsKDTree.h:48
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
void extend(const DqPtrsKDTree &)
Extend this container with the elements of the container being passed as parameter.
Definition: DqPtrsKDTree.h:130
Pointer to (nodes, elements, points, lines,...) container.
Definition: DqPtrs.h:57
bool in(const T *) const
Returns true if the pointer is in the container.
Definition: DqPtrs.h:219
bool push_front(T *)
Inserts an object at the beginning of the container.
Definition: DqPtrsKDTree.h:155
Vector en tres dimensiones.
Definition: Vector3d.h:39