1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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 "MappedBarycentric2DIntersectorP1P1.hxx"
44 #include "MappedBarycentric2DIntersectorP1P1.txx"
45 #include "VectorUtils.hxx"
51 namespace INTERP_KERNEL
54 * \defgroup interpolationPlanar InterpolationPlanar
56 * \class InterpolationPlanar
57 * \brief Class used to compute the coefficients of the interpolation matrix between
58 * two local meshes in two dimensions. Meshes can contain mixed triangular and quadrangular elements.
60 template<class RealPlanar>
61 InterpolationPlanar<RealPlanar>::InterpolationPlanar():_dim_caracteristic(1)
66 template<class RealPlanar>
67 InterpolationPlanar<RealPlanar>::InterpolationPlanar(const InterpolationOptions& io):Interpolation< InterpolationPlanar<RealPlanar> >(io),_dim_caracteristic(1)
73 * \brief Function used to set the options for the intersection calculation
74 * \details The following options can be modified:
75 * -# Intersection_type: the type of algorithm to be used in the computation of the cell-cell intersections.
76 * - Values: Triangle, Convex.
77 * - Default: Triangle.
78 * -# Precision: Level of precision of the computations is precision times the characteristic size of the mesh.
79 * - Values: positive real number.
81 * -# PrintLevel: Level of verboseness during the computations.
82 * - Values: integer between 0 and 3.
85 template<class RealPlanar>
86 void InterpolationPlanar<RealPlanar>::setOptions(double precision, int printLevel, IntersectionType intersectionType, int orientation)
88 InterpolationOptions::setPrecision(precision);
89 InterpolationOptions::setPrintLevel(printLevel);
90 InterpolationOptions::setIntersectionType(intersectionType);
91 InterpolationOptions::setOrientation(orientation);
95 /** \brief Main function to interpolate triangular or quadrangular meshes.
96 \details The algorithm proceeds in two steps: first a filtering process reduces the number of pairs of elements for which the
97 * calculation must be carried out by eliminating pairs that do not intersect based on their bounding boxes. Then, the
98 * volume of intersection is calculated by an object of type IntersectorPlanar for the remaining pairs, and entered into the
99 * intersection matrix.
101 * The matrix is partially sparse : it is a vector of maps of integer - double pairs.
102 * The length of the vector is equal to the number of target elements - for each target element there is a map, regardless
103 * of whether the element intersects any source elements or not. But in the maps there are only entries for those source elements
104 * which have a non-zero intersection volume with the target element. The vector has indices running from
105 * 0 to (#target elements - 1), meaning that the map for target element i is stored at index i - 1. In the maps, however,
106 * the indexing is more natural : the intersection volume of the target element i with source element j is found at matrix[i-1][j].
109 * @param myMeshS Planar source mesh
110 * @Param myMeshT Planar target mesh
111 * @return vector containing for each element i of the source mesh, a map giving for each element j
112 * of the target mesh which i intersects, the area of the intersection
115 template<class RealPlanar>
116 template<class MyMeshType, class MatrixType>
117 int InterpolationPlanar<RealPlanar>::interpolateMeshes(const MyMeshType& myMeshS, const MyMeshType& myMeshT, MatrixType& result, const std::string& method)
119 static const int SPACEDIM=MyMeshType::MY_SPACEDIM;
120 typedef typename MyMeshType::MyConnType ConnType;
121 static const NumberingPolicy numPol=MyMeshType::My_numPol;
123 long global_start =clock();
125 /***********************************************************/
126 /* Check both meshes are made of triangles and quadrangles */
127 /***********************************************************/
129 long nbMailleS=myMeshS.getNumberOfElements();
130 long nbMailleT=myMeshT.getNumberOfElements();
132 /**************************************************/
133 /* Search the characteristic size of the meshes */
134 /**************************************************/
136 double BoxS[2*SPACEDIM]; myMeshS.getBoundingBox(BoxS);
137 double BoxT[2*SPACEDIM]; myMeshT.getBoundingBox(BoxT);
138 double diagonalS,dimCaracteristicS=std::numeric_limits<double>::max();
141 diagonalS=getDistanceBtw2Pts<SPACEDIM>(BoxS+SPACEDIM,BoxS);
142 dimCaracteristicS=diagonalS/nbMailleS;
144 double diagonalT,dimCaracteristicT=std::numeric_limits<double>::max();
147 diagonalT=getDistanceBtw2Pts<SPACEDIM>(BoxT+SPACEDIM,BoxT);
148 dimCaracteristicT=diagonalT/nbMailleT;
151 _dim_caracteristic=std::min(dimCaracteristicS, dimCaracteristicT);
152 if (InterpolationOptions::getPrintLevel()>=1)
154 std::cout << " - Characteristic size of the source mesh : " << dimCaracteristicS << std::endl;
155 std::cout << " - Characteristic size of the target mesh: " << dimCaracteristicT << std::endl;
156 std::cout << "InterpolationPlanar::computation of the intersections" << std::endl;
159 PlanarIntersector<MyMeshType,MatrixType>* intersector=0;
160 std::string meth = InterpolationOptions::filterInterpolationMethod(method);
163 switch (InterpolationOptions::getIntersectionType())
166 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT,myMeshS,_dim_caracteristic,
167 InterpolationOptions::getPrecision(),
168 InterpolationOptions::getMaxDistance3DSurfIntersect(),
169 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
170 InterpolationOptions::getMedianPlane(),
171 InterpolationOptions::getOrientation(),
172 InterpolationOptions::getPrintLevel());
175 intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT,myMeshS,_dim_caracteristic,
176 InterpolationOptions::getPrecision(),
177 InterpolationOptions::getMaxDistance3DSurfIntersect(),
178 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
179 InterpolationOptions::getMedianPlane(),
180 InterpolationOptions::getDoRotate(),
181 InterpolationOptions::getOrientation(),
182 InterpolationOptions::getPrintLevel());
185 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT, myMeshS, _dim_caracteristic,
186 InterpolationOptions::getMaxDistance3DSurfIntersect(),
187 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
188 InterpolationOptions::getMedianPlane(),
189 InterpolationOptions::getPrecision(),
190 InterpolationOptions::getOrientation());
193 intersector=new PointLocator2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P0>(myMeshT, myMeshS, _dim_caracteristic,
194 InterpolationOptions::getMaxDistance3DSurfIntersect(),
195 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
196 InterpolationOptions::getMedianPlane(),
197 InterpolationOptions::getPrecision(),
198 InterpolationOptions::getOrientation());
201 throw INTERP_KERNEL::Exception("For P0P0 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator !");
204 else if(meth=="P0P1")
206 switch (InterpolationOptions::getIntersectionType())
209 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1>(myMeshT,myMeshS,_dim_caracteristic,
210 InterpolationOptions::getPrecision(),
211 InterpolationOptions::getMaxDistance3DSurfIntersect(),
212 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
213 InterpolationOptions::getMedianPlane(),
214 InterpolationOptions::getOrientation(),
215 InterpolationOptions::getPrintLevel());
218 intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1>(myMeshT,myMeshS,_dim_caracteristic,
219 InterpolationOptions::getPrecision(),
220 InterpolationOptions::getMaxDistance3DSurfIntersect(),
221 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
222 InterpolationOptions::getMedianPlane(),
223 InterpolationOptions::getDoRotate(),
224 InterpolationOptions::getOrientation(),
225 InterpolationOptions::getPrintLevel());
228 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1>(myMeshT, myMeshS, _dim_caracteristic,
229 InterpolationOptions::getMaxDistance3DSurfIntersect(),
230 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
231 InterpolationOptions::getMedianPlane(),
232 InterpolationOptions::getPrecision(),
233 InterpolationOptions::getOrientation());
236 intersector=new PlanarIntersectorP0P1PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
237 InterpolationOptions::getMaxDistance3DSurfIntersect(),
238 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
239 InterpolationOptions::getMedianPlane(),
240 InterpolationOptions::getPrecision(),
241 InterpolationOptions::getOrientation());
244 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1Bary>(myMeshT,myMeshS,_dim_caracteristic,
245 InterpolationOptions::getPrecision(),
246 InterpolationOptions::getMaxDistance3DSurfIntersect(),
247 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
248 InterpolationOptions::getMedianPlane(),
249 InterpolationOptions::getOrientation(),
250 InterpolationOptions::getPrintLevel());
252 case BarycentricGeo2D:
253 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP0P1Bary>(myMeshT, myMeshS, _dim_caracteristic,
254 InterpolationOptions::getMaxDistance3DSurfIntersect(),
255 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
256 InterpolationOptions::getMedianPlane(),
257 InterpolationOptions::getPrecision(),
258 InterpolationOptions::getOrientation());
261 throw INTERP_KERNEL::Exception("For P0P1 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator, Barycentric, BarycentricGeo2D !");
264 else if(meth=="P1P0")
266 switch (InterpolationOptions::getIntersectionType())
269 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT,myMeshS,_dim_caracteristic,
270 InterpolationOptions::getPrecision(),
271 InterpolationOptions::getMaxDistance3DSurfIntersect(),
272 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
273 InterpolationOptions::getMedianPlane(),
274 InterpolationOptions::getOrientation(),
275 InterpolationOptions::getPrintLevel());
278 intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT,myMeshS,_dim_caracteristic,
279 InterpolationOptions::getPrecision(),
280 InterpolationOptions::getMaxDistance3DSurfIntersect(),
281 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
282 InterpolationOptions::getMedianPlane(),
283 InterpolationOptions::getDoRotate(),
284 InterpolationOptions::getOrientation(),
285 InterpolationOptions::getPrintLevel());
288 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0>(myMeshT, myMeshS, _dim_caracteristic,
289 InterpolationOptions::getMaxDistance3DSurfIntersect(),
290 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
291 InterpolationOptions::getMedianPlane(),
292 InterpolationOptions::getPrecision(),
293 InterpolationOptions::getOrientation());
296 intersector=new PlanarIntersectorP1P0PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
297 InterpolationOptions::getMaxDistance3DSurfIntersect(),
298 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
299 InterpolationOptions::getMedianPlane(),
300 InterpolationOptions::getPrecision(),
301 InterpolationOptions::getOrientation());
304 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0Bary>(myMeshT,myMeshS,_dim_caracteristic,
305 InterpolationOptions::getPrecision(),
306 InterpolationOptions::getMaxDistance3DSurfIntersect(),
307 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
308 InterpolationOptions::getMedianPlane(),
309 InterpolationOptions::getOrientation(),
310 InterpolationOptions::getPrintLevel());
312 case BarycentricGeo2D:
313 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P0Bary>(myMeshT, myMeshS, _dim_caracteristic,
314 InterpolationOptions::getMaxDistance3DSurfIntersect(),
315 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
316 InterpolationOptions::getMedianPlane(),
317 InterpolationOptions::getPrecision(),
318 InterpolationOptions::getOrientation());
321 throw INTERP_KERNEL::Exception("For P1P0 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator, BarycentricGeo2D or Barycentric!");
324 else if(meth=="P1P1")
326 switch (InterpolationOptions::getIntersectionType())
329 intersector=new TriangulationIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT,myMeshS,_dim_caracteristic,
330 InterpolationOptions::getPrecision(),
331 InterpolationOptions::getMaxDistance3DSurfIntersect(),
332 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
333 InterpolationOptions::getMedianPlane(),
334 InterpolationOptions::getOrientation(),
335 InterpolationOptions::getPrintLevel());
338 intersector=new ConvexIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT,myMeshS,_dim_caracteristic,
339 InterpolationOptions::getPrecision(),
340 InterpolationOptions::getMaxDistance3DSurfIntersect(),
341 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
342 InterpolationOptions::getMedianPlane(),
343 InterpolationOptions::getDoRotate(),
344 InterpolationOptions::getOrientation(),
345 InterpolationOptions::getPrintLevel());
348 intersector=new Geometric2DIntersector<MyMeshType,MatrixType,PlanarIntersectorP1P1>(myMeshT, myMeshS, _dim_caracteristic,
349 InterpolationOptions::getMaxDistance3DSurfIntersect(),
350 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
351 InterpolationOptions::getMedianPlane(),
352 InterpolationOptions::getPrecision(),
353 InterpolationOptions::getOrientation());
356 intersector=new PlanarIntersectorP1P1PL<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
357 InterpolationOptions::getMaxDistance3DSurfIntersect(),
358 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
359 InterpolationOptions::getMedianPlane(),
360 InterpolationOptions::getPrecision(),
361 InterpolationOptions::getOrientation());
363 case MappedBarycentric:
364 intersector=new MappedBarycentric2DIntersectorP1P1<MyMeshType,MatrixType>(myMeshT, myMeshS, _dim_caracteristic,
365 InterpolationOptions::getMaxDistance3DSurfIntersect(),
366 InterpolationOptions::getMinDotBtwPlane3DSurfIntersect(),
367 InterpolationOptions::getMedianPlane(),
368 InterpolationOptions::getPrecision(),
369 InterpolationOptions::getOrientation());
372 throw INTERP_KERNEL::Exception("For P1P1 planar interpolation possibities are : Triangulation, Convex, Geometric2D, PointLocator, MappedBarycentric !");
376 throw INTERP_KERNEL::Exception("Invalid method specified or intersection type ! Must be in : \"P0P0\" \"P0P1\" \"P1P0\" or \"P1P1\"");
377 /****************************************************************/
378 /* Create a search tree based on the bounding boxes */
379 /* Instantiate the intersector and initialise the result vector */
380 /****************************************************************/
382 long start_filtering=clock();
384 std::vector<double> bbox;
385 intersector->createBoundingBoxes(myMeshS,bbox); // create the bounding boxes
386 performAdjustmentOfBB(intersector,bbox);
387 const double *bboxPtr=0;
390 BBTree<SPACEDIM,ConnType> my_tree(bboxPtr, 0, 0,nbMailleS);//creating the search structure
392 long end_filtering=clock();
394 result.resize(intersector->getNumberOfRowsOfResMatrix());//on initialise.
396 /****************************************************/
397 /* Loop on the target cells - core of the algorithm */
398 /****************************************************/
399 long start_intersection=clock();
400 long nbelem_type=myMeshT.getNumberOfElements();
401 const ConnType *connIndxT=myMeshT.getConnectivityIndexPtr();
402 for(int iT=0; iT<nbelem_type; iT++)
404 int nb_nodesT=connIndxT[iT+1]-connIndxT[iT];
405 std::vector<int> intersecting_elems;
406 double bb[2*SPACEDIM];
407 intersector->getElemBB(bb,myMeshT,OTT<ConnType,numPol>::indFC(iT),nb_nodesT);
408 my_tree.getIntersectingElems(bb, intersecting_elems);
409 intersector->intersectCells(iT,intersecting_elems,result);
410 counter+=intersecting_elems.size();
411 intersecting_elems.clear();
413 int ret=intersector->getNumberOfColsOfResMatrix();
416 if (InterpolationOptions::getPrintLevel() >=1)
418 long end_intersection=clock();
419 std::cout << "Filtering time= " << end_filtering-start_filtering << std::endl;
420 std::cout << "Intersection time= " << end_intersection-start_intersection << std::endl;
421 long global_end =clock();
422 std::cout << "Number of computed intersections = " << counter << std::endl;
423 std::cout << "Global time= " << global_end - global_start << std::endl;