Salome HOME
Warning hunting.
[tools/medcoupling.git] / src / INTERP_KERNEL / InterpolationPlanar.txx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (CEA/DEN)
20 #ifndef __INTERPOLATIONPLANAR_TXX__
21 #define __INTERPOLATIONPLANAR_TXX__
22
23 #include "InterpolationPlanar.hxx"
24 #include "Interpolation.txx"
25 #include "InterpolationOptions.hxx"
26 #include "PlanarIntersector.hxx"
27 #include "PlanarIntersector.txx"
28 #include "TriangulationIntersector.hxx"
29 #include "TriangulationIntersector.txx"
30 #include "ConvexIntersector.hxx"
31 #include "ConvexIntersector.txx"
32 #include "Geometric2DIntersector.hxx"
33 #include "Geometric2DIntersector.txx"
34 #include "PointLocator2DIntersector.hxx"
35 #include "PointLocator2DIntersector.txx"
36 #include "PlanarIntersectorP0P1PL.hxx"
37 #include "PlanarIntersectorP0P1PL.txx"
38 #include "PlanarIntersectorP1P0PL.hxx"
39 #include "PlanarIntersectorP1P0PL.txx"
40 #include "PlanarIntersectorP1P1PL.hxx"
41 #include "PlanarIntersectorP1P1PL.txx"
42 #include "VectorUtils.hxx"
43 #include "BBTree.txx"
44
45 #include <limits>
46 #include <time.h>
47
48 namespace INTERP_KERNEL
49 {
50   /**
51    * \defgroup interpolationPlanar InterpolationPlanar
52    *
53    * \class InterpolationPlanar
54    * \brief Class used to compute the coefficients of the interpolation matrix between 
55    * two local meshes in two dimensions. Meshes can contain mixed triangular and quadrangular elements.
56    */
57   template<class RealPlanar>
58   InterpolationPlanar<RealPlanar>::InterpolationPlanar():_dim_caracteristic(1)
59                                                          
60   {
61   }
62
63   template<class RealPlanar>
64   InterpolationPlanar<RealPlanar>::InterpolationPlanar(const InterpolationOptions& io):Interpolation< InterpolationPlanar<RealPlanar> >(io),_dim_caracteristic(1)
65                                                          
66   {
67   }
68
69   /**
70    *  \brief  Function used to set the options for the intersection calculation
71    * \details The following options can be modified:
72    *  -# Intersection_type: the type of algorithm to be used in the computation of the cell-cell intersections.
73    *   - Values: Triangle, Convex.
74    *   - Default: Triangle.
75    *  -# Precision: Level of precision of the computations is precision times the characteristic size of the mesh.
76    *   - Values: positive real number.
77    *   - Default: 1.0E-12.
78    *  -# PrintLevel: Level of verboseness during the computations.
79    *   - Values: interger between 0 and 3.
80    *   - Default: 0.
81    */
82   template<class RealPlanar>
83   void InterpolationPlanar<RealPlanar>::setOptions(double precision, int printLevel, IntersectionType intersectionType, int orientation)
84   {
85     InterpolationOptions::setPrecision(precision);
86     InterpolationOptions::setPrintLevel(printLevel);
87     InterpolationOptions::setIntersectionType(intersectionType);
88     InterpolationOptions::setOrientation(orientation);
89   }
90   
91   
92   /** \brief Main function to interpolate triangular or quadrangular meshes.
93       \details  The algorithm proceeds in two steps: first a filtering process reduces the number of pairs of elements for which the
94       * calculation must be carried out by eliminating pairs that do not intersect based on their bounding boxes. Then, the 
95       * volume of intersection is calculated by an object of type IntersectorPlanar for the remaining pairs, and entered into the
96       * intersection matrix. 
97       * 
98       * The matrix is partially sparse : it is a vector of maps of integer - double pairs. 
99       * The length of the vector is equal to the number of target elements - for each target element there is a map, regardless
100       * of whether the element intersects any source elements or not. But in the maps there are only entries for those source elements
101       * which have a non-zero intersection volume with the target element. The vector has indices running from 
102       * 0 to (#target elements - 1), meaning that the map for target element i is stored at index i - 1. In the maps, however,
103       * the indexing is more natural : the intersection volume of the target element i with source element j is found at matrix[i-1][j].
104       * 
105    
106       * @param myMeshS  Planar source mesh
107       * @Param myMeshT  Planar target mesh
108       * @return            vector containing for each element i of the source mesh, a map giving for each element j
109       *                    of the target mesh which i intersects, the area of the intersection
110       *
111       */
112   template<class RealPlanar>
113   template<class MyMeshType, class MatrixType>
114   int InterpolationPlanar<RealPlanar>::interpolateMeshes(const MyMeshType& myMeshS, const MyMeshType& myMeshT, MatrixType& result, const char *method)
115   {
116     static const int SPACEDIM=MyMeshType::MY_SPACEDIM;
117     typedef typename MyMeshType::MyConnType ConnType;
118     static const NumberingPolicy numPol=MyMeshType::My_numPol;
119
120     long global_start =clock();
121     int counter=0;   
122     /***********************************************************/
123     /* Check both meshes are made of triangles and quadrangles */
124     /***********************************************************/
125
126     long nbMailleS=myMeshS.getNumberOfElements();
127     long nbMailleT=myMeshT.getNumberOfElements();
128     
129     /**************************************************/
130     /* Search the characteristic size of the meshes   */
131     /**************************************************/
132     
133     double BoxS[2*SPACEDIM]; myMeshS.getBoundingBox(BoxS);
134     double BoxT[2*SPACEDIM]; myMeshT.getBoundingBox(BoxT);
135     double diagonalS,dimCaracteristicS=std::numeric_limits<double>::max();
136     if(nbMailleS!=0)
137       {
138         diagonalS=getDistanceBtw2Pts<SPACEDIM>(BoxS+SPACEDIM,BoxS);
139         dimCaracteristicS=diagonalS/nbMailleS;
140       }
141     double diagonalT,dimCaracteristicT=std::numeric_limits<double>::max();
142     if(nbMailleT!=0)
143       {
144         diagonalT=getDistanceBtw2Pts<SPACEDIM>(BoxT+SPACEDIM,BoxT);
145         dimCaracteristicT=diagonalT/nbMailleT;
146       }
147     
148     _dim_caracteristic=std::min(dimCaracteristicS, dimCaracteristicT);
149     if (InterpolationOptions::getPrintLevel()>=1)
150       {
151         std::cout << "  - Characteristic size of the source mesh : " << dimCaracteristicS << std::endl;
152         std::cout << "  - Characteristic size of the target mesh: " << dimCaracteristicT << std::endl;
153         std::cout << "InterpolationPlanar::computation of the intersections" << std::endl;
154       }
155     
156     PlanarIntersector<MyMeshType,MatrixType>* intersector=0;
157     std::string meth = InterpolationOptions::filterInterpolationMethod(method);
158     if(meth=="P0P0")
159       {
160         switch (InterpolationOptions::getIntersectionType())
161           {
162           case Triangulation:
163             intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT,myMeshS,_dim_caracteristic,
164                                                                                                   InterpolationOptions::getPrecision(),
165                                                                                                   InterpolationOptions::getMaxDistance3DSurfIntersect(),
166                                                                                                   InterpolationOptions::getMedianPlane(),
167                                                                                                   InterpolationOptions::getOrientation(),
168                                                                                                   InterpolationOptions::getPrintLevel());
169             break;
170           case Convex:
171             intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT,myMeshS,_dim_caracteristic,
172                                                                                            InterpolationOptions::getPrecision(),
173                                                                                            InterpolationOptions::getMaxDistance3DSurfIntersect(),
174                                                                                            InterpolationOptions::getMedianPlane(),
175                                                                                            InterpolationOptions::getDoRotate(),
176                                                                                            InterpolationOptions::getOrientation(),
177                                                                                            InterpolationOptions::getPrintLevel());
178             break;
179           case Geometric2D:
180             intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT, myMeshS, _dim_caracteristic,
181                                                                                                 InterpolationOptions::getMaxDistance3DSurfIntersect(),
182                                                                                                 InterpolationOptions::getMedianPlane(),
183                                                                                                 InterpolationOptions::getPrecision(),
184                                                                                                 InterpolationOptions::getOrientation());
185             break;
186           case PointLocator:
187             intersector=new PointLocator2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT, myMeshS, _dim_caracteristic,
188                                                                                                    InterpolationOptions::getMaxDistance3DSurfIntersect(),
189                                                                                                    InterpolationOptions::getMedianPlane(),
190                                                                                                    InterpolationOptions::getPrecision(),
191                                                                                                    InterpolationOptions::getOrientation());
192             break;
193           default:
194             throw INTERP_KERNEL::Exception("For P0P0 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator !");
195           }
196       }
197     else if(meth=="P0P1")
198       {
199         switch (InterpolationOptions::getIntersectionType())
200           {
201           case Triangulation:
202             intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1>(myMeshT,myMeshS,_dim_caracteristic,
203                                                                                                   InterpolationOptions::getPrecision(),
204                                                                                                   InterpolationOptions::getMaxDistance3DSurfIntersect(),
205                                                                                                   InterpolationOptions::getMedianPlane(),
206                                                                                                   InterpolationOptions::getOrientation(),
207                                                                                                   InterpolationOptions::getPrintLevel());
208             break;
209           case Convex:
210             intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1>(myMeshT,myMeshS,_dim_caracteristic,
211                                                                                            InterpolationOptions::getPrecision(),
212                                                                                            InterpolationOptions::getMaxDistance3DSurfIntersect(),
213                                                                                            InterpolationOptions::getMedianPlane(),
214                                                                                            InterpolationOptions::getDoRotate(),
215                                                                                            InterpolationOptions::getOrientation(),
216                                                                                            InterpolationOptions::getPrintLevel());
217             break;
218           case Geometric2D:
219             intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1>(myMeshT, myMeshS, _dim_caracteristic,
220                                                                                                 InterpolationOptions::getMaxDistance3DSurfIntersect(),
221                                                                                                 InterpolationOptions::getMedianPlane(),
222                                                                                                 InterpolationOptions::getPrecision(),
223                                                                                                 InterpolationOptions::getOrientation());
224             break;
225           case PointLocator:
226             intersector=new PlanarIntersectorP0P1PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
227                                                                            InterpolationOptions::getMaxDistance3DSurfIntersect(),
228                                                                            InterpolationOptions::getMedianPlane(),
229                                                                            InterpolationOptions::getPrecision(),
230                                                                            InterpolationOptions::getOrientation());
231             break;
232           default:
233             throw INTERP_KERNEL::Exception("For P0P1 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator !");
234           }
235       }
236     else if(meth=="P1P0")
237       {
238         switch (InterpolationOptions::getIntersectionType())
239           {
240           case Triangulation:
241             intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT,myMeshS,_dim_caracteristic,
242                                                                                                   InterpolationOptions::getPrecision(),
243                                                                                                   InterpolationOptions::getMaxDistance3DSurfIntersect(),
244                                                                                                   InterpolationOptions::getMedianPlane(),
245                                                                                                   InterpolationOptions::getOrientation(),
246                                                                                                   InterpolationOptions::getPrintLevel());
247             break;
248           case Convex:
249             intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT,myMeshS,_dim_caracteristic,
250                                                                                            InterpolationOptions::getPrecision(),
251                                                                                            InterpolationOptions::getMaxDistance3DSurfIntersect(),
252                                                                                            InterpolationOptions::getMedianPlane(),
253                                                                                            InterpolationOptions::getDoRotate(),
254                                                                                            InterpolationOptions::getOrientation(),
255                                                                                            InterpolationOptions::getPrintLevel());
256             break;
257           case Geometric2D:
258             intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT, myMeshS, _dim_caracteristic,
259                                                                                                 InterpolationOptions::getMaxDistance3DSurfIntersect(),
260                                                                                                 InterpolationOptions::getMedianPlane(),
261                                                                                                 InterpolationOptions::getPrecision(),
262                                                                                                 InterpolationOptions::getOrientation());
263             break;
264           case PointLocator:
265             intersector=new PlanarIntersectorP1P0PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
266                                                                        InterpolationOptions::getMaxDistance3DSurfIntersect(),
267                                                                        InterpolationOptions::getMedianPlane(),
268                                                                        InterpolationOptions::getPrecision(),
269                                                                        InterpolationOptions::getOrientation());
270             break;
271           case Barycentric:
272              intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0Bary>(myMeshT,myMeshS,_dim_caracteristic,
273                                                                                                        InterpolationOptions::getPrecision(),
274                                                                                                        InterpolationOptions::getMaxDistance3DSurfIntersect(),
275                                                                                                        InterpolationOptions::getMedianPlane(),
276                                                                                                        InterpolationOptions::getOrientation(),
277                                                                                                        InterpolationOptions::getPrintLevel());
278              break;
279           case BarycentricGeo2D:
280             intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0Bary>(myMeshT, myMeshS, _dim_caracteristic,
281                                                                                                     InterpolationOptions::getMaxDistance3DSurfIntersect(),
282                                                                                                     InterpolationOptions::getMedianPlane(),
283                                                                                                     InterpolationOptions::getPrecision(),
284                                                                                                     InterpolationOptions::getOrientation());
285             break;
286           }
287       }
288     else if(meth=="P1P1")
289       {
290         switch (InterpolationOptions::getIntersectionType())
291           {
292           case Triangulation:
293             intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT,myMeshS,_dim_caracteristic,
294                                                                                                   InterpolationOptions::getPrecision(),
295                                                                                                   InterpolationOptions::getMaxDistance3DSurfIntersect(),
296                                                                                                   InterpolationOptions::getMedianPlane(),
297                                                                                                   InterpolationOptions::getOrientation(),
298                                                                                                   InterpolationOptions::getPrintLevel());
299             break;
300           case Convex:
301             intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT,myMeshS,_dim_caracteristic,
302                                                                                            InterpolationOptions::getPrecision(),
303                                                                                            InterpolationOptions::getMaxDistance3DSurfIntersect(),
304                                                                                            InterpolationOptions::getMedianPlane(),
305                                                                                            InterpolationOptions::getDoRotate(),
306                                                                                            InterpolationOptions::getOrientation(),
307                                                                                            InterpolationOptions::getPrintLevel());
308             break;
309           case Geometric2D:
310             intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT, myMeshS, _dim_caracteristic,
311                                                                                                 InterpolationOptions::getMaxDistance3DSurfIntersect(),
312                                                                                                 InterpolationOptions::getMedianPlane(),
313                                                                                                 InterpolationOptions::getPrecision(),
314                                                                                                 InterpolationOptions::getOrientation());
315             break;
316           case PointLocator:
317             intersector=new PlanarIntersectorP1P1PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
318                                                                            InterpolationOptions::getMaxDistance3DSurfIntersect(),
319                                                                            InterpolationOptions::getMedianPlane(),
320                                                                            InterpolationOptions::getPrecision(),
321                                                                            InterpolationOptions::getOrientation());
322             break;
323           default:
324             throw INTERP_KERNEL::Exception("For P1P1 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator !");
325           }
326       }
327     else
328       throw INTERP_KERNEL::Exception("Invalid method specified or intersection type ! Must be in : \"P0P0\" \"P0P1\" \"P1P0\" or \"P1P1\"");
329     /****************************************************************/
330     /* Create a search tree based on the bounding boxes             */
331     /* Instanciate the intersector and initialise the result vector */
332     /****************************************************************/
333  
334     long start_filtering=clock();
335  
336     std::vector<double> bbox;
337     intersector->createBoundingBoxes(myMeshS,bbox); // create the bounding boxes
338     performAdjustmentOfBB(intersector,bbox);
339     const double *bboxPtr=0;
340     if(nbMailleS>0)
341       bboxPtr=&bbox[0];
342     BBTree<SPACEDIM,ConnType> my_tree(bboxPtr, 0, 0,nbMailleS);//creating the search structure 
343
344     long end_filtering=clock();
345
346     result.resize(intersector->getNumberOfRowsOfResMatrix());//on initialise.
347
348     /****************************************************/
349     /* Loop on the target cells - core of the algorithm */
350     /****************************************************/
351     long start_intersection=clock();
352     long nbelem_type=myMeshT.getNumberOfElements();
353     const ConnType *connIndxT=myMeshT.getConnectivityIndexPtr();
354     for(int iT=0; iT<nbelem_type; iT++)
355       {
356         int nb_nodesT=connIndxT[iT+1]-connIndxT[iT];
357         std::vector<int> intersecting_elems;
358         double bb[2*SPACEDIM];
359         intersector->getElemBB(bb,myMeshT,OTT<ConnType,numPol>::indFC(iT),nb_nodesT);
360         my_tree.getIntersectingElems(bb, intersecting_elems);
361         intersector->intersectCells(iT,intersecting_elems,result);
362         counter+=intersecting_elems.size();
363         intersecting_elems.clear();
364       }
365     int ret=intersector->getNumberOfColsOfResMatrix();
366     delete intersector;
367
368     if (InterpolationOptions::getPrintLevel() >=1)
369       {
370         long end_intersection=clock();
371         std::cout << "Filtering time= " << end_filtering-start_filtering << std::endl;
372         std::cout << "Intersection time= " << end_intersection-start_intersection << std::endl;
373         long global_end =clock();    
374         std::cout << "Number of computed intersections = " << counter << std::endl;
375         std::cout << "Global time= " << global_end - global_start << std::endl;
376       }
377     return ret;
378   }
379 }
380
381 #endif