xc
EntityMap.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 //EntityMap.h
28 
29 #ifndef MAPENT_H
30 #define MAPENT_H
31 
32 #include "preprocessor/multi_block_topology/ModelComponentContainer.h"
33 #include "EntMdlr.h"
34 #include <map>
35 
36 namespace XC {
37 
39 //
41 template <class Entity>
42 class EntityMap: public ModelComponentContainer<Entity>
43  {
44  protected:
45 
46  public:
47  typedef typename ModelComponentContainer<Entity>::iterator iterator;
48  typedef typename ModelComponentContainer<Entity>::const_iterator const_iterator;
49 
50  EntityMap(MultiBlockTopology *mbt= nullptr);
51 
52  Entity *getNearest(const Pos3d &p);
53  const Entity *getNearest(const Pos3d &p) const;
54  void numera(void);
55  };
56 
58 template <class Entity>
60  : ModelComponentContainer<Entity>(mbt) {}
61 
63 template <class Entity>
64 Entity *EntityMap<Entity>::getNearest(const Pos3d &p)
65  {
66  //Commented code is buggy (inifinite recursion) if
67  //the function is called from Python
68  /* EntityMap<Entity> *this_no_const= const_cast<EntityMap<Entity> *>(this); */
69  /* return const_cast<Entity *>(this_no_const->getNearest(p)); */
70  Entity *retval= nullptr;
71  if(!this->empty())
72  {
73  iterator i= this->begin();
74  double d2= (*i).second->getSquaredDistanceTo(p);
75  retval= (*i).second; i++;
76  double tmp;
77  for(;i!=this->end();i++)
78  {
79  tmp= (*i).second->getSquaredDistanceTo(p);
80  if(tmp<d2)
81  {
82  d2= tmp;
83  retval= (*i).second;
84  }
85  }
86  }
87  return retval;
88  }
89 
91 template <class Entity>
92 const Entity *EntityMap<Entity>::getNearest(const Pos3d &p) const
93  {
94  const Entity *retval= nullptr;
95  if(!this->empty())
96  {
97  const_iterator i= this->begin();
98  double d2= (*i).second->getSquaredDistanceTo(p);
99  retval= (*i).second; i++;
100  double tmp;
101  for(;i!=this->end();i++)
102  {
103  tmp= (*i).second->getSquaredDistanceTo(p);
104  if(tmp<d2)
105  {
106  d2= tmp;
107  retval= (*i).second;
108  }
109  }
110  }
111  return retval;
112  }
113 
115 template <class Entity>
117  {
118  size_t idx= 0;
119  for(iterator i= this->begin();i!=this->end();i++,idx++)
120  {
121  EntMdlr *ptr= (*i).second;
122  ptr->set_index(idx);
123  }
124  }
125 
126 } //end of XC namespace
127 #endif
void numera(void)
Set indices to the objects to allow its use in VTK.
Definition: EntityMap.h:116
Geometric entities container (points, lines, surfaces,...).
Definition: EntityMap.h:42
Model geometry manager.
Definition: MultiBlockTopology.h:68
Multiblock topology object (point, line, face, block,...).
Definition: EntMdlr.h:54
EntityMap(MultiBlockTopology *mbt=nullptr)
Constructor.
Definition: EntityMap.h:59
virtual void set_index(const size_t &i)
Assigns the objects index for its use in VTK arrays(see numera in Set).
Definition: EntMdlr.cc:75
Open source finite element program for structural analysis.
Definition: ContinuaReprComponent.h:34
Container for model entities.
Definition: ModelComponentContainer.h:42
Entity * getNearest(const Pos3d &p)
Returns the object closest to the position being passed as parameter.
Definition: EntityMap.h:64