xc
PosArray.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 //PosArray.h
22 //Matrix of points
23 
24 #ifndef POSARRAY_H
25 #define POSARRAY_H
26 
27 #include "utility/matrices/TMatrix.h"
28 #include "../cgal_types.h"
29 #include "boost/lexical_cast.hpp"
30 #include "utility/utils/misc_utils/matem.h"
31 
32 
34 //
36 template <class POS>
37 class PosArray: public TMatrix<POS,std::vector<POS> >
38  {
39  public:
41  typedef typename POS::vector vector;
42  protected:
43  PosArray(const PosArray<POS> &mp, size_t &f1, size_t &c1, size_t &f2, size_t &c2)
44  : m_pos(mp,f1,c1,f2,c2) {}
45  public:
46  PosArray(const size_t &f=1,const size_t &c=1,const POS &p= POS()): m_pos(f,c,p) {}
47  PosArray(const POS &p1,const POS &p2,const size_t &num,const GEOM_FT &ratio);
48  PosArray(const POS &p1,const POS &p2,const size_t &ndiv);
49  PosArray(const POS &p1,const POS &p2,const std::vector<GEOM_FT> &longs);
50  PosArray(const POS &p0,const POS &p1,const POS &p2,const size_t &ndiv1,const size_t &ndiv2);
51  PosArray(const PosArray &l1_points,const PosArray &l2_points,const PosArray &l3_points,const PosArray &l4_points);
52  inline PosArray<POS> getBox(size_t f1, size_t c1, size_t f2, size_t c2) const
53  { return PosArray(*this,f1,c1,f2,c2); }
54  inline PosArray<POS> getRow(size_t iRow) const
55  { return getBox(iRow,1,iRow,this->n_columns); }
56  inline PosArray<POS> getColumn(size_t col) const
57  { return getBox(1,col,this->n_rows,col); }
58  };
59 
63 template <class POS>
64 PosArray<POS>::PosArray(const POS &p1,const POS &p2,const size_t &num,const GEOM_FT &ratio)
65  : m_pos(num,1)
66  {
67  POS p(p1);
68  const vector v_ratio= ratio * (p2-p1);
69  for(size_t i=1;i<=num;i++)
70  {
71  p= p + v_ratio;
72  (*this)(i,1)= p;
73  }
74  }
75 
81 template <class POS>
82 PosArray<POS>::PosArray(const POS &p1,const POS &p2,const size_t &ndiv)
83  : m_pos(ndiv+1,1)
84  {
85  POS p(p1);
86  if(ndiv==0)
87  (*this)(1,1)= p;
88  else
89  {
90  const GEOM_FT ratio= double_to_FT(1.0/boost::lexical_cast<double>(ndiv));
91  const vector v_ratio= ratio * (p2-p1);
92  for(size_t i=1;i<=ndiv+1;i++)
93  {
94  (*this)(i,1)= p;
95  p= p + v_ratio;
96  }
97  }
98  }
99 
108 template <class POS>
109 PosArray<POS>::PosArray(const POS &p1,const POS &p2,const std::vector<GEOM_FT> &longs)
110  : m_pos(longs.size()+1,1)
111  {
112  const size_t sz= longs.size();
113  const size_t num= sz+1;
114  if(num<1)
115  {
116  std::cerr << "PosArray::" << __FUNCTION__
117  << "; list of lengths is empty." << std::endl;
118  return;
119  }
120  else
121  {
122  (*this)(1,1)= p1;
123  (*this)(num,1)= p2;
124  if(num>1)
125  {
126  //Normalize the list.
127  GEOM_FT denom(0);
128  for(std::vector<GEOM_FT>::const_iterator i=longs.begin();i!=longs.end();i++)
129  denom+= *i;
130  POS p(p1);
131  const vector v= p2-p1;
132  for(size_t i=0;i<sz;i++)
133  {
134  p= p + (longs[i]/denom)*v;
135  (*this)(i+2,1)= p;
136  }
137  }
138  }
139  }
140 
142 template <class POS>
143 PosArray<POS>::PosArray(const POS &p0,const POS &p1,const POS &p2,const size_t &ndiv1,const size_t &ndiv2)
144  : m_pos(ndiv1+1,ndiv2+1)
145  {
146  const GEOM_FT dn1= double_to_FT(1.0/boost::lexical_cast<double>(ndiv1));
147  const GEOM_FT dn2= double_to_FT(1.0/boost::lexical_cast<double>(ndiv2));
148  const vector v01= (p1-p0)*dn1;
149  const vector v02= (p2-p0)*dn2;
150  const size_t n_rows= this->getNumberOfRows();
151  const size_t n_columns= this->getNumberOfColumns();
152  for(size_t i=1;i<=n_rows;i++)
153  {
154  const vector vi= boost::lexical_cast<int>(i-1)*v02;
155  for(size_t j=1;j<=n_columns;j++)
156  (*this)(i,j)= p0+vi+boost::lexical_cast<int>(j-1)*v01;
157  }
158  }
159 
160 template <class POS>
161 PosArray<POS>::PosArray(const PosArray &l1_points,const PosArray &l2_points,const PosArray &l3_points,const PosArray &l4_points)
162  : m_pos(l1_points.getNumberOfRows(),l2_points.getNumberOfRows())
163 //The arguments are the points (X) in the following order (see rows).
164 // l3_points
165 // --->
166 // X---X---X---X
167 // ^ | | ^
168 // l4_points | X X | l2_points
169 // | |
170 // X---X---X---X
171 // --->
172 // l1_points
173 //
174 // and return those points:
175 //
176 // X---X---X---X
177 // ^ | | | |
178 // | X---X---X---X
179 // | | | |
180 // X---X---X---X
181 // --->
182 //
183 //The rows are quasi-parallel to the lines l2 and l4
184 //The columns are quasi-parallel to 11 y l3
185  {
186  const size_t row_number= l1_points.getNumberOfRows();
187  const size_t num_cols= l2_points.getNumberOfRows();
188  PosArray row_points= l4_points;
189  for(size_t i=1;i<=row_number;i++)
190  {
191  if(i>1)
192  {
193  if(i==row_number)
194  row_points= l2_points;
195  else
196  row_points= PosArray(l1_points(i,1),l3_points(i,1),num_cols-1);
197  }
198  for(size_t j=1;j<=num_cols;j++)
199  (*this)(i,j)= row_points(j,1);
200  }
201  }
202 
203 template <class POS,class SEG>
204 POS get_centro(const PosArray<POS> &m,const SEG &sg)
205  {
206  POS retval;
207  const size_t n_rows= m.getNumberOfRows();
208  const size_t n_columns= m.getNumberOfColumns();
209  const size_t iRow= n_rows/2;
210  const size_t iColumn= n_columns/2;
211  if(impar(n_rows) && impar(n_columns))
212  retval= m(iRow+1,iColumn+1);
213  else
214  if(par(n_rows) && par(n_columns))
215  {
216  SEG s(m(iRow,iColumn),m(iRow+1,iColumn+1));
217  retval= s.getCenterOfMass();
218  }
219  else
220  if(impar(n_rows))
221  {
222  SEG s(m(iRow,iColumn),m(iRow,iColumn+1));
223  retval= s.getCenterOfMass();
224  }
225  else
226  {
227  SEG s(m(iRow,iColumn),m(iRow+1,iColumn));
228  retval= s.getCenterOfMass();
229  }
230  return retval;
231  }
232 
234 // The arguments are the points (X):
235 //
236 // q3 q4
237 // X-----------X
238 // | |
239 // | |
240 // | |
241 // X-----------X
242 // q1 q2
243 //
244 // and the number of divisions in directions q1->q2 (ndiv1) and q1->q3 (ndiv2)
245 // and returns the points:
246 //
247 // X---X---X---X
248 // ^ | | | |
249 // | X---X---X---X
250 // | | | |
251 // X---X---X---X
252 // --->
253 //
254 template <class POS>
255 PosArray<POS> Quadrilateral(const POS &q1,const POS &q2,const POS &q3,const POS &q4,const size_t &ndiv1,const size_t &ndiv2)
256  {
257  const PosArray<POS> l1(q1,q2,ndiv1);
258  const PosArray<POS> l2(q2,q4,ndiv2);
259  const PosArray<POS> l3(q3,q4,ndiv1);
260  const PosArray<POS> l4(q1,q3,ndiv2);
261  return PosArray<POS>(l1,l2,l3,l4);
262  }
263 
267 template <class POS>
268 PosArray<POS> generacion_frontal(const PosArray<POS> &l1_points,const PosArray<POS> &l2_points)
269  {
270  const size_t nptos1= l1_points.getNumberOfRows();
271  const size_t nptos2= l2_points.getNumberOfRows();
272  const size_t ntot= nptos1*nptos2;
273  PosArray<POS> retval(nptos1,nptos2);
274 
275  //Points of the first row.
276  for(size_t j=1;j<=nptos1;j++)
277  retval(1,j)= l1_points(j);
278  //Points of the first column.
279  for(size_t i=2;i<=nptos1;i++)
280  retval(i,1)= l2_points(i);
281 
282  for(size_t i=2;i<=nptos1;i++)
283  {
284  const typename PosArray<POS>::vector v= retval(i,1)-retval(i-1,1);
285  for(size_t j=2;j<=nptos1;j++)
286  retval(i,j)= retval(i-1,j)+v;
287  }
288  return retval;
289  }
290 
291 #endif
Definition: TMatrix.h:37
Base class for position matrices used to represent grids of points.
Definition: PosArray.h:37
Base class for quadrilateral domains.
Definition: ShapeFunction.h:181