xc
GeomGroup.h
1 // -*-c++-*-
2 //----------------------------------------------------------------------------
3 // xc utils library; general purpose classes and functions.
4 //
5 // Copyright (C) Luis C. Pérez Tato
6 //
7 // XC utils is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // This software is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program.
19 // If not, see <http://www.gnu.org/licenses/>.
20 //----------------------------------------------------------------------------
21 //GeomGroup.h
22 
23 #ifndef GEOMGROUP_H
24 #define GEOMGROUP_H
25 
26 #include "utility/utils/stl/pdeque.h"
27 
28 
30 //
32 template <typename GO>
33 class GeomGroup : public GO
34  {
35  static const GO *downcast(const GeomObj *p)
36  { return dynamic_cast<const GO *>(p); }
37  static GO *downcast(GeomObj *p)
38  { return dynamic_cast<GO *>(p); }
39  protected:
41  pdeque_geom_obj objetos;
42  bool equal_dimension(void) const;
43  private:
44  void copia_objetos(const pdeque_geom_obj &objs);
45  public:
46  typedef typename pdeque_geom_obj::const_iterator const_iterator;
47  typedef typename pdeque_geom_obj::iterator iterator;
48 
49  inline bool empty(void) const
50  { return objetos.empty(); }
51 
52  inline const_iterator begin(void) const
53  { return objetos.begin(); }
54  inline const_iterator end(void) const
55  { return objetos.end(); }
56  inline bool Vacio(void) const
57  { return objetos.empty(); }
58 
59  GeomGroup(void) : GO(), objetos() {}
60  GeomGroup(const GeomGroup &other) : GO(other)
61  { copia_objetos(other.objetos); }
62  virtual GEOM_FT GetMax(unsigned short int i) const;
63  virtual GEOM_FT GetMin(unsigned short int i) const;
64  virtual unsigned short int Dimension(void) const;
65  size_t size(void) const
66  { return objetos.size(); }
67  void push_back(const GO &obj)
68  { objetos.push_back(downcast(obj.getCopy())); }
69  virtual GEOM_FT getLength(void) const;
70  virtual GEOM_FT getArea(void) const;
71  virtual GEOM_FT getVolume(void) const;
72  void Print(std::ostream &os) const;
73  };
74 
75 template <typename GO>
76 void GeomGroup<GO>::copia_objetos(const pdeque_geom_obj &objs)
77  {
78  for(const_iterator i= objs.begin();i!=objs.end();i++)
79  objetos.push_back(downcast((*i)->getCopy()));
80  }
81 
83 template <typename GO>
85  {
86  if(objetos.empty()) return true;
87  const_iterator i(objetos.begin());
88  const unsigned short int d((*i)->Dimension());
89  i++;
90  for(;i!=objetos.end();i++)
91  if(d!=(*i)->Dimension()) return false;
92  return true;
93  }
94 
96 template <typename GO>
97 unsigned short int GeomGroup<GO>::Dimension(void) const
98  {
99  if(objetos.empty()) return 0;
100  const_iterator i(objetos.begin());
101  unsigned short int d((*i)->Dimension());
102  i++;
103  for(;i!=objetos.end();i++)
104  d= std::max(d,(*i)->Dimension());
105  return d;
106  }
107 
108 template <typename GO>
109 GEOM_FT GeomGroup<GO>::GetMax(unsigned short int i) const
110  {
111  if(objetos.empty()) return 0.0;
112  const_iterator j=objetos.begin();
113  GEOM_FT mx= (*j)->GetMax(i);
114  j++;
115  for(;j != objetos.end();j++)
116  mx= std::max((*j)->GetMax(i),mx);
117  return mx;
118  }
119 template <typename GO>
120 GEOM_FT GeomGroup<GO>::GetMin(unsigned short int i) const
121  {
122  if(objetos.empty()) return 0.0;
123  const_iterator j=objetos.begin();
124  GEOM_FT mn= (*j)->GetMin(i);
125  j++;
126  for(;j != objetos.end();j++)
127  mn= std::min((*j)->GetMin(i),mn);
128  return mn;
129  }
130 
132 template <typename GO>
133 GEOM_FT GeomGroup<GO>::getLength(void) const
134  {
135  if(objetos.empty()) return 0.0;
136  const_iterator i(objetos.begin());
137  GEOM_FT retval((*i)->getLength());
138  i++;
139  for(;i!=objetos.end();i++)
140  retval+= (*i)->getLength();
141  return retval;
142  }
143 
145 template <typename GO>
146 GEOM_FT GeomGroup<GO>::getArea(void) const
147  {
148  if(objetos.empty()) return 0.0;
149  const_iterator i(objetos.begin());
150  GEOM_FT retval((*i)->getArea());
151  i++;
152  for(;i!=objetos.end();i++)
153  retval+= (*i)->getArea();
154  return retval;
155  }
156 
158 template <typename GO>
159 GEOM_FT GeomGroup<GO>::getVolume(void) const
160  {
161  if(objetos.empty()) return 0.0;
162  const_iterator i(objetos.begin());
163  GEOM_FT retval((*i)->getVolume());
164  i++;
165  for(;i!=objetos.end();i++)
166  retval+= (*i)->getVolume();
167  return retval;
168  }
169 
170 template <typename GO>
171 void GeomGroup<GO>::Print(std::ostream &os) const
172  {
173  if(Vacio()) return;
174  const_iterator i= begin();
175  os << **i; i++;
176  for(;i!=end();i++)
177  os << std::endl << **i;
178  }
179 
180 #endif
virtual GEOM_FT getArea(void) const
Return the object area.
Definition: GeomGroup.h:146
bool equal_dimension(void) const
Return true if all objects have the same dimension.
Definition: GeomGroup.h:84
virtual GEOM_FT getLength(void) const
Return the length of the object.
Definition: GeomGroup.h:133
Base class for geometric entities groups.
Definition: GeomGroup.h:33
pdeque_geom_obj objetos
Objects of the group.
Definition: GeomGroup.h:41
virtual GEOM_FT getVolume(void) const
Return the volume of the object.
Definition: GeomGroup.h:159
virtual unsigned short int Dimension(void) const
Return the dimension of the object 0, 1, 2 or 3.
Definition: GeomGroup.h:97
Clase base para las entidades geométricas.
Definition: GeomObj.h:40