Salome HOME
windows porting
[tools/medcoupling.git] / src / INTERP_KERNEL / InterpolationCurve.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 __INTERPOLATIONCURVE_TXX__
21 #define __INTERPOLATIONCURVE_TXX__
22
23 #include "InterpolationCurve.hxx"
24 #include "InterpolationOptions.hxx"
25 #include "CurveIntersectorP0P0.txx"
26 #include "CurveIntersectorP1P0.txx"
27 #include "CurveIntersectorP0P1.txx"
28 #include "CurveIntersectorP1P1.txx"
29 #include "BBTree.txx"
30
31 #include <time.h>
32
33 namespace INTERP_KERNEL
34 {
35   /**
36    * \defgroup interpolationCurve InterpolationCurve
37    *
38    * \class InterpolationCurve
39    * \brief Class used to compute the coefficients of the interpolation matrix between 
40    * two local meshes in two dimensions.
41    */
42   template<class RealCurve>
43   InterpolationCurve<RealCurve>::InterpolationCurve()
44   {
45   }
46
47   template<class RealCurve>
48   InterpolationCurve<RealCurve>::InterpolationCurve (const InterpolationOptions& io)
49     :Interpolation< InterpolationCurve<RealCurve> >(io)
50   {
51   }
52
53   /** \brief Main function to interpolate 1D meshes.
54       \details  The algorithm proceeds in two steps: first a filtering process reduces the number of pairs of elements for which the
55       * calculation must be carried out by eliminating pairs that do not intersect based on their bounding boxes. Then, the 
56       * volume of intersection is calculated by an object of type IntersectorPlanar for the remaining pairs, and entered into the
57       * intersection matrix. 
58       * 
59       * The matrix is partially sparse : it is a vector of maps of integer - double pairs. 
60       * The length of the vector is equal to the number of target elements - for each target element there is a map, regardless
61       * of whether the element intersects any source elements or not. But in the maps there are only entries for those source elements
62       * which have a non-zero intersection volume with the target element. The vector has indices running from 
63       * 0 to (#target elements - 1), meaning that the map for target element i is stored at index i - 1. In the maps, however,
64       * the indexing is more natural : the intersection volume of the target element i with source element j is found at matrix[i-1][j].
65       * 
66    
67       * @param myMeshS  Planar source mesh
68       * @Param myMeshT  Planar target mesh
69       * @return vector containing for each element i of the source mesh, a map giving for each element j
70       *         of the target mesh which i intersects, the area of the intersection
71       *
72       */
73   template<class RealCurve>
74   template<class MyMeshType, class MatrixType>
75   int InterpolationCurve<RealCurve>::interpolateMeshes (const MyMeshType& myMeshS,
76                                                         const MyMeshType& myMeshT,
77                                                         MatrixType&       result,
78                                                         const char *      method)
79   {
80     static const int SPACEDIM=MyMeshType::MY_SPACEDIM;
81     typedef typename MyMeshType::MyConnType ConnType;
82     static const NumberingPolicy numPol = MyMeshType::My_numPol;
83
84     long global_start = clock();
85     int counter=0;   
86
87     long nbMailleS = myMeshS.getNumberOfElements();
88     long nbMailleT = myMeshT.getNumberOfElements();
89     
90     CurveIntersector<MyMeshType,MatrixType>* intersector=0;
91     std::string meth(method);
92     if(meth=="P0P0")
93       {
94         intersector = new CurveIntersectorP0P0<MyMeshType,MatrixType>
95           (myMeshT, myMeshS,
96            InterpolationOptions::getPrecision(),
97            InterpolationOptions::getBoundingBoxAdjustmentAbs(),
98            InterpolationOptions::getMedianPlane(),
99            InterpolationOptions::getPrintLevel());
100       }
101     else if(meth=="P0P1")
102       {
103         intersector = new CurveIntersectorP0P1<MyMeshType,MatrixType>
104           (myMeshT, myMeshS,
105            InterpolationOptions::getPrecision(),
106            InterpolationOptions::getBoundingBoxAdjustmentAbs(),
107            InterpolationOptions::getMedianPlane(),
108            InterpolationOptions::getPrintLevel());
109       }
110     else if(meth=="P1P0")
111       {
112         intersector = new CurveIntersectorP1P0<MyMeshType,MatrixType>
113           (myMeshT, myMeshS,
114            InterpolationOptions::getPrecision(),
115            InterpolationOptions::getBoundingBoxAdjustmentAbs(),
116            InterpolationOptions::getMedianPlane(),
117            InterpolationOptions::getPrintLevel());
118       }
119     else if(meth=="P1P1")
120       {
121         intersector = new CurveIntersectorP1P1<MyMeshType,MatrixType>
122           (myMeshT, myMeshS,
123            InterpolationOptions::getPrecision(),
124            InterpolationOptions::getBoundingBoxAdjustmentAbs(),
125            InterpolationOptions::getMedianPlane(),
126            InterpolationOptions::getPrintLevel());
127       }
128     else
129       throw INTERP_KERNEL::Exception("Invalid method specified ! Must be in : \"P0P0\" \"P0P1\" \"P1P0\" or \"P1P1\"");
130     /****************************************************************/
131     /* Create a search tree based on the bounding boxes             */
132     /* Instanciate the intersector and initialise the result vector */
133     /****************************************************************/
134  
135     long start_filtering=clock();
136  
137     std::vector<double> bbox;
138     intersector->createBoundingBoxes(myMeshS,bbox); // create the bounding boxes
139     intersector->adjustBoundingBoxes(bbox, InterpolationOptions::getBoundingBoxAdjustmentAbs());
140     BBTree<SPACEDIM,ConnType> my_tree(&bbox[0], 0, 0, nbMailleS);//creating the search structure 
141
142     long end_filtering = clock();
143
144     result.resize(intersector->getNumberOfRowsOfResMatrix());//on initialise.
145
146     /****************************************************/
147     /* Loop on the target cells - core of the algorithm */
148     /****************************************************/
149     long start_intersection = clock();
150     const ConnType *connIndxT = myMeshT.getConnectivityIndexPtr();
151     for(int iT=0; iT<nbMailleT; iT++)
152       {
153         int nb_nodesT = connIndxT[iT+1] - connIndxT[iT];
154         std::vector<int> intersecting_elems;
155         double bb[2*SPACEDIM];
156         intersector->getElemBB(bb,myMeshT,OTT<ConnType,numPol>::indFC(iT),nb_nodesT);
157         my_tree.getIntersectingElems(bb, intersecting_elems);
158         intersector->intersectCells(iT,intersecting_elems,result);
159         counter += intersecting_elems.size();
160       }
161     int ret = intersector->getNumberOfColsOfResMatrix();
162     delete intersector;
163     
164     if (InterpolationOptions::getPrintLevel() >= 1)
165       {
166         long end_intersection=clock();
167         std::cout << "Filtering time= " << end_filtering-start_filtering << std::endl;
168         std::cout << "Intersection time= " << end_intersection-start_intersection << std::endl;
169         long global_end =clock();    
170         std::cout << "Number of computed intersections = " << counter << std::endl;
171         std::cout << "Global time= " << global_end - global_start << std::endl;
172       }
173     return ret;
174   }
175 }
176
177 #endif