Salome HOME
Implementation of P0P1Bary
[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           case Barycentric:
233             intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1Bary>(myMeshT,myMeshS,_dim_caracteristic,
234                                                                                                       InterpolationOptions::getPrecision(),
235                                                                                                       InterpolationOptions::getMaxDistance3DSurfIntersect(),
236                                                                                                       InterpolationOptions::getMedianPlane(),
237                                                                                                       InterpolationOptions::getOrientation(),
238                                                                                                       InterpolationOptions::getPrintLevel());
239             break;
240           case BarycentricGeo2D:
241             intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1Bary>(myMeshT, myMeshS, _dim_caracteristic,
242                                                                                                     InterpolationOptions::getMaxDistance3DSurfIntersect(),
243                                                                                                     InterpolationOptions::getMedianPlane(),
244                                                                                                     InterpolationOptions::getPrecision(),
245                                                                                                     InterpolationOptions::getOrientation());
246             break;
247           default:
248             throw INTERP_KERNEL::Exception("For P0P1 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator, Barycentric, BarycentricGeo2D !");
249           }
250       }
251     else if(meth=="P1P0")
252       {
253         switch (InterpolationOptions::getIntersectionType())
254           {
255           case Triangulation:
256             intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT,myMeshS,_dim_caracteristic,
257                                                                                                   InterpolationOptions::getPrecision(),
258                                                                                                   InterpolationOptions::getMaxDistance3DSurfIntersect(),
259                                                                                                   InterpolationOptions::getMedianPlane(),
260                                                                                                   InterpolationOptions::getOrientation(),
261                                                                                                   InterpolationOptions::getPrintLevel());
262             break;
263           case Convex:
264             intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT,myMeshS,_dim_caracteristic,
265                                                                                            InterpolationOptions::getPrecision(),
266                                                                                            InterpolationOptions::getMaxDistance3DSurfIntersect(),
267                                                                                            InterpolationOptions::getMedianPlane(),
268                                                                                            InterpolationOptions::getDoRotate(),
269                                                                                            InterpolationOptions::getOrientation(),
270                                                                                            InterpolationOptions::getPrintLevel());
271             break;
272           case Geometric2D:
273             intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT, myMeshS, _dim_caracteristic,
274                                                                                                 InterpolationOptions::getMaxDistance3DSurfIntersect(),
275                                                                                                 InterpolationOptions::getMedianPlane(),
276                                                                                                 InterpolationOptions::getPrecision(),
277                                                                                                 InterpolationOptions::getOrientation());
278             break;
279           case PointLocator:
280             intersector=new PlanarIntersectorP1P0PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
281                                                                        InterpolationOptions::getMaxDistance3DSurfIntersect(),
282                                                                        InterpolationOptions::getMedianPlane(),
283                                                                        InterpolationOptions::getPrecision(),
284                                                                        InterpolationOptions::getOrientation());
285             break;
286           case Barycentric:
287              intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0Bary>(myMeshT,myMeshS,_dim_caracteristic,
288                                                                                                        InterpolationOptions::getPrecision(),
289                                                                                                        InterpolationOptions::getMaxDistance3DSurfIntersect(),
290                                                                                                        InterpolationOptions::getMedianPlane(),
291                                                                                                        InterpolationOptions::getOrientation(),
292                                                                                                        InterpolationOptions::getPrintLevel());
293              break;
294           case BarycentricGeo2D:
295             intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0Bary>(myMeshT, myMeshS, _dim_caracteristic,
296                                                                                                     InterpolationOptions::getMaxDistance3DSurfIntersect(),
297                                                                                                     InterpolationOptions::getMedianPlane(),
298                                                                                                     InterpolationOptions::getPrecision(),
299                                                                                                     InterpolationOptions::getOrientation());
300             break;
301           }
302       }
303     else if(meth=="P1P1")
304       {
305         switch (InterpolationOptions::getIntersectionType())
306           {
307           case Triangulation:
308             intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT,myMeshS,_dim_caracteristic,
309                                                                                                   InterpolationOptions::getPrecision(),
310                                                                                                   InterpolationOptions::getMaxDistance3DSurfIntersect(),
311                                                                                                   InterpolationOptions::getMedianPlane(),
312                                                                                                   InterpolationOptions::getOrientation(),
313                                                                                                   InterpolationOptions::getPrintLevel());
314             break;
315           case Convex:
316             intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT,myMeshS,_dim_caracteristic,
317                                                                                            InterpolationOptions::getPrecision(),
318                                                                                            InterpolationOptions::getMaxDistance3DSurfIntersect(),
319                                                                                            InterpolationOptions::getMedianPlane(),
320                                                                                            InterpolationOptions::getDoRotate(),
321                                                                                            InterpolationOptions::getOrientation(),
322                                                                                            InterpolationOptions::getPrintLevel());
323             break;
324           case Geometric2D:
325             intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT, myMeshS, _dim_caracteristic,
326                                                                                                 InterpolationOptions::getMaxDistance3DSurfIntersect(),
327                                                                                                 InterpolationOptions::getMedianPlane(),
328                                                                                                 InterpolationOptions::getPrecision(),
329                                                                                                 InterpolationOptions::getOrientation());
330             break;
331           case PointLocator:
332             intersector=new PlanarIntersectorP1P1PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
333                                                                            InterpolationOptions::getMaxDistance3DSurfIntersect(),
334                                                                            InterpolationOptions::getMedianPlane(),
335                                                                            InterpolationOptions::getPrecision(),
336                                                                            InterpolationOptions::getOrientation());
337             break;
338           default:
339             throw INTERP_KERNEL::Exception("For P1P1 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator !");
340           }
341       }
342     else
343       throw INTERP_KERNEL::Exception("Invalid method specified or intersection type ! Must be in : \"P0P0\" \"P0P1\" \"P1P0\" or \"P1P1\"");
344     /****************************************************************/
345     /* Create a search tree based on the bounding boxes             */
346     /* Instanciate the intersector and initialise the result vector */
347     /****************************************************************/
348  
349     long start_filtering=clock();
350  
351     std::vector<double> bbox;
352     intersector->createBoundingBoxes(myMeshS,bbox); // create the bounding boxes
353     performAdjustmentOfBB(intersector,bbox);
354     const double *bboxPtr=0;
355     if(nbMailleS>0)
356       bboxPtr=&bbox[0];
357     BBTree<SPACEDIM,ConnType> my_tree(bboxPtr, 0, 0,nbMailleS);//creating the search structure 
358
359     long end_filtering=clock();
360
361     result.resize(intersector->getNumberOfRowsOfResMatrix());//on initialise.
362
363     /****************************************************/
364     /* Loop on the target cells - core of the algorithm */
365     /****************************************************/
366     long start_intersection=clock();
367     long nbelem_type=myMeshT.getNumberOfElements();
368     const ConnType *connIndxT=myMeshT.getConnectivityIndexPtr();
369     for(int iT=0; iT<nbelem_type; iT++)
370       {
371         int nb_nodesT=connIndxT[iT+1]-connIndxT[iT];
372         std::vector<int> intersecting_elems;
373         double bb[2*SPACEDIM];
374         intersector->getElemBB(bb,myMeshT,OTT<ConnType,numPol>::indFC(iT),nb_nodesT);
375         my_tree.getIntersectingElems(bb, intersecting_elems);
376         intersector->intersectCells(iT,intersecting_elems,result);
377         counter+=intersecting_elems.size();
378         intersecting_elems.clear();
379       }
380     int ret=intersector->getNumberOfColsOfResMatrix();
381     delete intersector;
382
383     if (InterpolationOptions::getPrintLevel() >=1)
384       {
385         long end_intersection=clock();
386         std::cout << "Filtering time= " << end_filtering-start_filtering << std::endl;
387         std::cout << "Intersection time= " << end_intersection-start_intersection << std::endl;
388         long global_end =clock();    
389         std::cout << "Number of computed intersections = " << counter << std::endl;
390         std::cout << "Global time= " << global_end - global_start << std::endl;
391       }
392     return ret;
393   }
394 }
395
396 #endif