xc
DqPtrsKDTree.h
1 //----------------------------------------------------------------------------
2 // XC program; finite element analysis code
3 // for structural analysis and design.
4 //
5 // Copyright (C) Luis Claudio Pérez Tato
6 //
7 // This program derives from OpenSees <http://opensees.berkeley.edu>
8 // developed by the «Pacific earthquake engineering research center».
9 //
10 // Except for the restrictions that may arise from the copyright
11 // of the original program (see copyright_opensees.txt)
12 // XC is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This software is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program.
25 // If not, see <http://www.gnu.org/licenses/>.
26 //----------------------------------------------------------------------------
27 //DqPtrsKDTree.h
28 
29 
30 #ifndef DQPTRSKDTREE_H
31 #define DQPTRSKDTREE_H
32 
33 #include "DqPtrs.h"
34 #include <set>
35 
36 class Pos3d;
37 class Vector3d;
38 
39 namespace XC {
40 class TrfGeom;
41 
46 template <class T,class KDTree>
47 class DqPtrsKDTree: public DqPtrs<T>
48  {
49  KDTree kdtree;
50  protected:
51  void create_tree(void);
52  public:
53  typedef typename DqPtrs<T>::const_iterator const_iterator;
54  typedef typename DqPtrs<T>::iterator iterator;
55  typedef typename DqPtrs<T>::reference reference;
56  typedef typename DqPtrs<T>::const_reference const_reference;
57  typedef typename DqPtrs<T>::size_type size_type;
58 
59  DqPtrsKDTree(CommandEntity *owr= nullptr);
60  DqPtrsKDTree(const DqPtrsKDTree &);
61  explicit DqPtrsKDTree(const std::deque<T *> &);
62  explicit DqPtrsKDTree(const std::set<const T *> &);
65  void extend(const DqPtrsKDTree &);
66  //void extend_cond(const DqPtrsKDTree &,const std::string &cond);
67  bool push_back(T *);
68  bool push_front(T *);
69  void clearAll(void);
70 
71  T *getNearest(const Pos3d &p);
72  const T *getNearest(const Pos3d &p) const;
73  };
74 
76 template <class T,class KDTree>
78  {
79  kdtree.clear();
80  for(iterator i= this->begin();i!=this->end();i++)
81  {
82  T *tPtr= *i;
83  assert(tPtr);
84  kdtree.insert(*tPtr);
85  }
86  }
87 
89 template <class T,class KDTree>
91  : DqPtrs<T>(owr) {}
92 
94 template <class T,class KDTree>
96  : DqPtrs<T>(other)
97  { create_tree(); }
98 
100 template <class T,class KDTree>
101 DqPtrsKDTree<T,KDTree>::DqPtrsKDTree(const std::deque<T *> &ts)
102  : DqPtrs<T>(ts)
103  { create_tree(); }
104 
106 template <class T,class KDTree>
107 DqPtrsKDTree<T,KDTree>::DqPtrsKDTree(const std::set<const T *> &st)
108  : DqPtrs<T>()
109  {
110  typename std::set<const T *>::const_iterator k;
111  k= st.begin();
112  for(;k!=st.end();k++)
113  push_back(const_cast<T *>(*k));
114  }
115 
117 template <class T,class KDTree>
119  {
120  DqPtrs<T>::operator=(other);
121  kdtree= other.kdtree;
122  return *this;
123  }
124 
127 template <class T,class KDTree>
129  {
130  for(register DqPtrsKDTree<T,KDTree>::const_iterator i= other.begin();i!=other.end();i++)
131  push_back(*i);
132  }
134 template <class T,class KDTree>
136  {
137  extend(other);
138  return *this;
139  }
140 
142 template <class T,class KDTree>
144  {
145  bool retval= DqPtrs<T>::push_back(t);
146  if(retval)
147  kdtree.insert(*t);
148  return retval;
149  }
150 
152 template <class T,class KDTree>
154  {
155  bool retval= DqPtrs<T>::push_front(t);
156  if(retval)
157  kdtree.insert(*t);
158  return retval;
159 }
160 
162 template <class T,class KDTree>
164  {
166  kdtree.clear();
167  }
169 template <class T,class KDTree>
171  {
172  T *retval= const_cast<T *>(kdtree.getNearest(p));
173  return retval;
174  }
175 
177 template <class T,class KDTree>
178 const T *DqPtrsKDTree<T,KDTree>::getNearest(const Pos3d &p) const
179  {
180  DqPtrsKDTree<T,KDTree> *this_no_const= const_cast<DqPtrsKDTree *>(this);
181  return this_no_const->getNearest(p);
182  }
183 
185 template <class T,class KDTree>
187  {
188  DqPtrsKDTree<T,KDTree> retval(a);
189  retval+=b;
190  return retval;
191  }
192 
194 template <class T,class KDTree>
196  {
197  DqPtrsKDTree<T,KDTree> retval;
198  for(typename DqPtrsKDTree<T,KDTree>::const_iterator i= a.begin();i!=a.end();i++)
199  {
200  T *n= (*i);
201  assert(n);
202  if(!b.in(n)) //If not in b.
203  retval.push_back(n);
204  }
205  return retval;
206  }
207 
209 template <class T,class KDTree>
211  {
212  DqPtrsKDTree<T,KDTree> retval;
213  for(typename DqPtrsKDTree<T,KDTree>::const_iterator i= a.begin();i!=a.end();i++)
214  {
215  T *n= (*i);
216  assert(n);
217  if(b.in(n)) //If also in b.
218  retval.push_back(n);
219  }
220  return retval;
221  }
222 
223 } //end of XC namespace
224 #endif
225 
FiberSet operator*(const FiberSet &, const FiberSet &)
Return the fibers in a that are also in b.
Definition: FiberSet.cc:87
bool push_back(T *)
Inserts an object at the end of the container.
Definition: DqPtrsKDTree.h:143
void clearAll(void)
Clears out the list of pointers and erases the properties of the object (if any). ...
Definition: DqPtrsKDTree.h:163
DqPtrsKDTree & operator=(const DqPtrsKDTree &)
Assignment operator.
Definition: DqPtrsKDTree.h:118
FiberSet operator+(const FiberSet &, const FiberSet &)
Return the union of both containers.
Definition: FiberSet.cc:65
DqPtrsKDTree(CommandEntity *owr=nullptr)
Constructor.
Definition: DqPtrsKDTree.h:90
void create_tree(void)
Creates the KD tree.
Definition: DqPtrsKDTree.h:77
T * getNearest(const Pos3d &p)
Returns the object closest to the point being passed as parameter.
Definition: DqPtrsKDTree.h:170
DqPtrsKDTree & operator+=(const DqPtrsKDTree &)
+= operator.
Definition: DqPtrsKDTree.h:135
Container with a KDTree.
Definition: DqPtrsKDTree.h:47
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:34
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:128
Pointer to (nodes, elements, points, lines,...) container.
Definition: DqPtrs.h:56
bool in(const T *) const
Returns true if the pointer is in the container.
Definition: DqPtrs.h:188
bool push_front(T *)
Inserts an object at the begining of the container.
Definition: DqPtrsKDTree.h:153