1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
21 #ifndef __INTERPOLATIONPLANAR_TXX__
22 #define __INTERPOLATIONPLANAR_TXX__
24 #include "InterpolationPlanar.hxx"
25 #include "Interpolation.txx"
26 #include "InterpolationOptions.hxx"
27 #include "PlanarIntersector.hxx"
28 #include "PlanarIntersector.txx"
29 #include "TriangulationIntersector.hxx"
30 #include "TriangulationIntersector.txx"
31 #include "ConvexIntersector.hxx"
32 #include "ConvexIntersector.txx"
33 #include "Geometric2DIntersector.hxx"
34 #include "Geometric2DIntersector.txx"
35 #include "PointLocator2DIntersector.hxx"
36 #include "PointLocator2DIntersector.txx"
37 #include "PlanarIntersectorP0P1PL.hxx"
38 #include "PlanarIntersectorP0P1PL.txx"
39 #include "PlanarIntersectorP1P0PL.hxx"
40 #include "PlanarIntersectorP1P0PL.txx"
41 #include "PlanarIntersectorP1P1PL.hxx"
42 #include "PlanarIntersectorP1P1PL.txx"
43 #include "VectorUtils.hxx"
49 namespace INTERP_KERNEL
52 * \defgroup interpolationPlanar InterpolationPlanar
54 * \class InterpolationPlanar
55 * \brief Class used to compute the coefficients of the interpolation matrix between
56 * two local meshes in two dimensions. Meshes can contain mixed triangular and quadrangular elements.
58 template<class RealPlanar>
59 InterpolationPlanar<RealPlanar>::InterpolationPlanar():_dim_caracteristic(1)
64 template<class RealPlanar>
65 InterpolationPlanar<RealPlanar>::InterpolationPlanar(const InterpolationOptions& io):Interpolation< InterpolationPlanar<RealPlanar> >(io),_dim_caracteristic(1)
71 * \brief Function used to set the options for the intersection calculation
72 * \details The following options can be modified:
73 * -# Intersection_type: the type of algorithm to be used in the computation of the cell-cell intersections.
74 * - Values: Triangle, Convex.
75 * - Default: Triangle.
76 * -# Precision: Level of precision of the computations is precision times the characteristic size of the mesh.
77 * - Values: positive real number.
79 * -# PrintLevel: Level of verboseness during the computations.
80 * - Values: interger between 0 and 3.
83 template<class RealPlanar>
84 void InterpolationPlanar<RealPlanar>::setOptions(double precision, int printLevel, IntersectionType intersectionType, int orientation)
86 InterpolationOptions::setPrecision(precision);
87 InterpolationOptions::setPrintLevel(printLevel);
88 InterpolationOptions::setIntersectionType(intersectionType);
89 InterpolationOptions::setOrientation(orientation);
93 /** \brief Main function to interpolate triangular or quadrangular meshes.
94 \details The algorithm proceeds in two steps: first a filtering process reduces the number of pairs of elements for which the
95 * calculation must be carried out by eliminating pairs that do not intersect based on their bounding boxes. Then, the
96 * volume of intersection is calculated by an object of type IntersectorPlanar for the remaining pairs, and entered into the
97 * intersection matrix.
99 * The matrix is partially sparse : it is a vector of maps of integer - double pairs.
100 * The length of the vector is equal to the number of target elements - for each target element there is a map, regardless
101 * of whether the element intersects any source elements or not. But in the maps there are only entries for those source elements
102 * which have a non-zero intersection volume with the target element. The vector has indices running from
103 * 0 to (#target elements - 1), meaning that the map for target element i is stored at index i - 1. In the maps, however,
104 * the indexing is more natural : the intersection volume of the target element i with source element j is found at matrix[i-1][j].
107 * @param myMeshS Planar source mesh
108 * @Param myMeshT Planar target mesh
109 * @return vector containing for each element i of the source mesh, a map giving for each element j
110 * of the target mesh which i intersects, the area of the intersection
113 template<class RealPlanar>
114 template<class MyMeshType, class MatrixType>
115 int InterpolationPlanar<RealPlanar>::interpolateMeshes(const MyMeshType& myMeshS, const MyMeshType& myMeshT, MatrixType& result, const std::string& method)
117 static const int SPACEDIM=MyMeshType::MY_SPACEDIM;
118 typedef typename MyMeshType::MyConnType ConnType;
119 static const NumberingPolicy numPol=MyMeshType::My_numPol;
121 long global_start =clock();
123 /***********************************************************/
124 /* Check both meshes are made of triangles and quadrangles */
125 /***********************************************************/
127 long nbMailleS=myMeshS.getNumberOfElements();
128 long nbMailleT=myMeshT.getNumberOfElements();
130 /**************************************************/
131 /* Search the characteristic size of the meshes */
132 /**************************************************/
134 double BoxS[2*SPACEDIM]; myMeshS.getBoundingBox(BoxS);
135 double BoxT[2*SPACEDIM]; myMeshT.getBoundingBox(BoxT);
136 double diagonalS,dimCaracteristicS=std::numeric_limits<double>::max();
139 diagonalS=getDistanceBtw2Pts<SPACEDIM>(BoxS+SPACEDIM,BoxS);
140 dimCaracteristicS=diagonalS/nbMailleS;
142 double diagonalT,dimCaracteristicT=std::numeric_limits<double>::max();
145 diagonalT=getDistanceBtw2Pts<SPACEDIM>(BoxT+SPACEDIM,BoxT);
146 dimCaracteristicT=diagonalT/nbMailleT;
149 _dim_caracteristic=std::min(dimCaracteristicS, dimCaracteristicT);
150 if (InterpolationOptions::getPrintLevel()>=1)
152 std::cout << " - Characteristic size of the source mesh : " << dimCaracteristicS << std::endl;
153 std::cout << " - Characteristic size of the target mesh: " << dimCaracteristicT << std::endl;
154 std::cout << "InterpolationPlanar::computation of the intersections" << std::endl;
157 PlanarIntersector<MyMeshType,MatrixType>* intersector=0;
158 std::string meth = InterpolationOptions::filterInterpolationMethod(method);
161 switch (InterpolationOptions::getIntersectionType())
164 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT,myMeshS,_dim_caracteristic,
165 InterpolationOptions::getPrecision(),
166 InterpolationOptions::getMaxDistance3DSurfIntersect(),
167 InterpolationOptions::getMedianPlane(),
168 InterpolationOptions::getOrientation(),
169 InterpolationOptions::getPrintLevel());
172 intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT,myMeshS,_dim_caracteristic,
173 InterpolationOptions::getPrecision(),
174 InterpolationOptions::getMaxDistance3DSurfIntersect(),
175 InterpolationOptions::getMedianPlane(),
176 InterpolationOptions::getDoRotate(),
177 InterpolationOptions::getOrientation(),
178 InterpolationOptions::getPrintLevel());
181 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT, myMeshS, _dim_caracteristic,
182 InterpolationOptions::getMaxDistance3DSurfIntersect(),
183 InterpolationOptions::getMedianPlane(),
184 InterpolationOptions::getPrecision(),
185 InterpolationOptions::getOrientation());
188 intersector=new PointLocator2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT, myMeshS, _dim_caracteristic,
189 InterpolationOptions::getMaxDistance3DSurfIntersect(),
190 InterpolationOptions::getMedianPlane(),
191 InterpolationOptions::getPrecision(),
192 InterpolationOptions::getOrientation());
195 throw INTERP_KERNEL::Exception("For P0P0 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator !");
198 else if(meth=="P0P1")
200 switch (InterpolationOptions::getIntersectionType())
203 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1>(myMeshT,myMeshS,_dim_caracteristic,
204 InterpolationOptions::getPrecision(),
205 InterpolationOptions::getMaxDistance3DSurfIntersect(),
206 InterpolationOptions::getMedianPlane(),
207 InterpolationOptions::getOrientation(),
208 InterpolationOptions::getPrintLevel());
211 intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1>(myMeshT,myMeshS,_dim_caracteristic,
212 InterpolationOptions::getPrecision(),
213 InterpolationOptions::getMaxDistance3DSurfIntersect(),
214 InterpolationOptions::getMedianPlane(),
215 InterpolationOptions::getDoRotate(),
216 InterpolationOptions::getOrientation(),
217 InterpolationOptions::getPrintLevel());
220 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1>(myMeshT, myMeshS, _dim_caracteristic,
221 InterpolationOptions::getMaxDistance3DSurfIntersect(),
222 InterpolationOptions::getMedianPlane(),
223 InterpolationOptions::getPrecision(),
224 InterpolationOptions::getOrientation());
227 intersector=new PlanarIntersectorP0P1PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
228 InterpolationOptions::getMaxDistance3DSurfIntersect(),
229 InterpolationOptions::getMedianPlane(),
230 InterpolationOptions::getPrecision(),
231 InterpolationOptions::getOrientation());
234 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1Bary>(myMeshT,myMeshS,_dim_caracteristic,
235 InterpolationOptions::getPrecision(),
236 InterpolationOptions::getMaxDistance3DSurfIntersect(),
237 InterpolationOptions::getMedianPlane(),
238 InterpolationOptions::getOrientation(),
239 InterpolationOptions::getPrintLevel());
241 case BarycentricGeo2D:
242 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1Bary>(myMeshT, myMeshS, _dim_caracteristic,
243 InterpolationOptions::getMaxDistance3DSurfIntersect(),
244 InterpolationOptions::getMedianPlane(),
245 InterpolationOptions::getPrecision(),
246 InterpolationOptions::getOrientation());
249 throw INTERP_KERNEL::Exception("For P0P1 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator, Barycentric, BarycentricGeo2D !");
252 else if(meth=="P1P0")
254 switch (InterpolationOptions::getIntersectionType())
257 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT,myMeshS,_dim_caracteristic,
258 InterpolationOptions::getPrecision(),
259 InterpolationOptions::getMaxDistance3DSurfIntersect(),
260 InterpolationOptions::getMedianPlane(),
261 InterpolationOptions::getOrientation(),
262 InterpolationOptions::getPrintLevel());
265 intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT,myMeshS,_dim_caracteristic,
266 InterpolationOptions::getPrecision(),
267 InterpolationOptions::getMaxDistance3DSurfIntersect(),
268 InterpolationOptions::getMedianPlane(),
269 InterpolationOptions::getDoRotate(),
270 InterpolationOptions::getOrientation(),
271 InterpolationOptions::getPrintLevel());
274 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT, myMeshS, _dim_caracteristic,
275 InterpolationOptions::getMaxDistance3DSurfIntersect(),
276 InterpolationOptions::getMedianPlane(),
277 InterpolationOptions::getPrecision(),
278 InterpolationOptions::getOrientation());
281 intersector=new PlanarIntersectorP1P0PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
282 InterpolationOptions::getMaxDistance3DSurfIntersect(),
283 InterpolationOptions::getMedianPlane(),
284 InterpolationOptions::getPrecision(),
285 InterpolationOptions::getOrientation());
288 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0Bary>(myMeshT,myMeshS,_dim_caracteristic,
289 InterpolationOptions::getPrecision(),
290 InterpolationOptions::getMaxDistance3DSurfIntersect(),
291 InterpolationOptions::getMedianPlane(),
292 InterpolationOptions::getOrientation(),
293 InterpolationOptions::getPrintLevel());
295 case BarycentricGeo2D:
296 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0Bary>(myMeshT, myMeshS, _dim_caracteristic,
297 InterpolationOptions::getMaxDistance3DSurfIntersect(),
298 InterpolationOptions::getMedianPlane(),
299 InterpolationOptions::getPrecision(),
300 InterpolationOptions::getOrientation());
304 else if(meth=="P1P1")
306 switch (InterpolationOptions::getIntersectionType())
309 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT,myMeshS,_dim_caracteristic,
310 InterpolationOptions::getPrecision(),
311 InterpolationOptions::getMaxDistance3DSurfIntersect(),
312 InterpolationOptions::getMedianPlane(),
313 InterpolationOptions::getOrientation(),
314 InterpolationOptions::getPrintLevel());
317 intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT,myMeshS,_dim_caracteristic,
318 InterpolationOptions::getPrecision(),
319 InterpolationOptions::getMaxDistance3DSurfIntersect(),
320 InterpolationOptions::getMedianPlane(),
321 InterpolationOptions::getDoRotate(),
322 InterpolationOptions::getOrientation(),
323 InterpolationOptions::getPrintLevel());
326 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT, myMeshS, _dim_caracteristic,
327 InterpolationOptions::getMaxDistance3DSurfIntersect(),
328 InterpolationOptions::getMedianPlane(),
329 InterpolationOptions::getPrecision(),
330 InterpolationOptions::getOrientation());
333 intersector=new PlanarIntersectorP1P1PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
334 InterpolationOptions::getMaxDistance3DSurfIntersect(),
335 InterpolationOptions::getMedianPlane(),
336 InterpolationOptions::getPrecision(),
337 InterpolationOptions::getOrientation());
340 throw INTERP_KERNEL::Exception("For P1P1 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator !");
344 throw INTERP_KERNEL::Exception("Invalid method specified or intersection type ! Must be in : \"P0P0\" \"P0P1\" \"P1P0\" or \"P1P1\"");
345 /****************************************************************/
346 /* Create a search tree based on the bounding boxes */
347 /* Instanciate the intersector and initialise the result vector */
348 /****************************************************************/
350 long start_filtering=clock();
352 std::vector<double> bbox;
353 intersector->createBoundingBoxes(myMeshS,bbox); // create the bounding boxes
354 performAdjustmentOfBB(intersector,bbox);
355 const double *bboxPtr=0;
358 BBTree<SPACEDIM,ConnType> my_tree(bboxPtr, 0, 0,nbMailleS);//creating the search structure
360 long end_filtering=clock();
362 result.resize(intersector->getNumberOfRowsOfResMatrix());//on initialise.
364 /****************************************************/
365 /* Loop on the target cells - core of the algorithm */
366 /****************************************************/
367 long start_intersection=clock();
368 long nbelem_type=myMeshT.getNumberOfElements();
369 const ConnType *connIndxT=myMeshT.getConnectivityIndexPtr();
370 for(int iT=0; iT<nbelem_type; iT++)
372 int nb_nodesT=connIndxT[iT+1]-connIndxT[iT];
373 std::vector<int> intersecting_elems;
374 double bb[2*SPACEDIM];
375 intersector->getElemBB(bb,myMeshT,OTT<ConnType,numPol>::indFC(iT),nb_nodesT);
376 my_tree.getIntersectingElems(bb, intersecting_elems);
377 intersector->intersectCells(iT,intersecting_elems,result);
378 counter+=intersecting_elems.size();
379 intersecting_elems.clear();
381 int ret=intersector->getNumberOfColsOfResMatrix();
384 if (InterpolationOptions::getPrintLevel() >=1)
386 long end_intersection=clock();
387 std::cout << "Filtering time= " << end_filtering-start_filtering << std::endl;
388 std::cout << "Intersection time= " << end_intersection-start_intersection << std::endl;
389 long global_end =clock();
390 std::cout << "Number of computed intersections = " << counter << std::endl;
391 std::cout << "Global time= " << global_end - global_start << std::endl;