Salome HOME
Merge branch 'V9_11_BR'
[tools/medcoupling.git] / src / INTERP_KERNEL / Interpolation3D1D.txx
1 // Copyright (C) 2007-2023  CEA, EDF
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, or (at your option) any later version.
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 (EDF R&D)
20
21 #pragma once
22
23 #include "Interpolation3D1D.hxx"
24 #include "Interpolation.txx"
25 #include "MeshElement.txx"
26 #include "PointLocator3DIntersectorP0P0.txx"
27 #include "PointLocator3DIntersectorP0P1.txx"
28 #include "PointLocator3DIntersectorP1P0.txx"
29 #include "PointLocator3DIntersectorP1P1.txx"
30 #include "Log.hxx"
31
32 #include "InterpolationHelper.txx"
33
34 namespace INTERP_KERNEL
35 {
36   /**
37    *  Very similar to Interpolation3D::interpolateMeshes, except for the bounding boxes that can be
38    *  adjusted in a similar fashion as in InterpolationPlanar::performAdjustmentOfBB()
39    **/
40   template<class MyMeshType, class MatrixType>
41   typename MyMeshType::MyConnType Interpolation3D1D::interpolateMeshes(const MyMeshType& srcMesh, const MyMeshType& targetMesh, MatrixType& result, const std::string& method)
42   {
43     if(InterpolationOptions::getIntersectionType() != PointLocator)
44       INTERP_KERNEL::Exception("Invalid 3D/1D-0D intersection type specified : must be PointLocator.");
45
46     typedef typename MyMeshType::MyConnType ConnType;
47     // create MeshElement objects corresponding to each element of the two meshes
48     const ConnType numTargetElems = targetMesh.getNumberOfElements();
49
50     LOG(2, "Target mesh has " << numTargetElems << " elements ");
51
52     std::unique_ptr< Intersector3D<MyMeshType,MatrixType> > intersector;
53     std::string methC = InterpolationOptions::filterInterpolationMethod(method);
54     if(methC=="P0P0")
55       { intersector.reset( new PointLocator3DIntersectorP0P0<MyMeshType,MatrixType>(targetMesh, srcMesh, getPrecision()) );
56       }
57     else if(methC=="P0P1")
58       {  intersector.reset( new PointLocator3DIntersectorP0P1<MyMeshType,MatrixType>(targetMesh, srcMesh, getPrecision()) );
59       }
60     else if(methC=="P1P0")
61       {  intersector.reset( new PointLocator3DIntersectorP1P0<MyMeshType,MatrixType>(targetMesh, srcMesh, getPrecision()) );
62       }
63     else if(methC=="P1P1")
64       {  intersector.reset( new PointLocator3DIntersectorP1P1<MyMeshType,MatrixType>(targetMesh, srcMesh, getPrecision()) );
65       }
66     else
67       throw Exception("Invalid method chosen must be in \"P0P0\", \"P0P1\", \"P1P0\" or \"P1P1\".");
68     // create empty maps for all source elements
69     result.resize(intersector->getNumberOfRowsOfResMatrix());
70
71     BBTreeStandAlone<3,ConnType> tree( BuildBBTreeWithAdjustment(srcMesh,[this](double *bbox, typename MyMeshType::MyConnType sz){ this->adjustBoundingBoxes(bbox,sz); }) );
72
73     // for each target element, get source elements with which to calculate intersection
74     // - calculate intersection by calling intersectCells
75     for(ConnType i = 0; i < numTargetElems; ++i)
76       {
77         MeshElement<ConnType> trgMeshElem(i, targetMesh);
78         
79         const BoundingBox* box = trgMeshElem.getBoundingBox();
80         // get target bbox in right order
81         double targetBox[6];
82         box->fillInXMinXmaxYminYmaxZminZmaxFormat(targetBox);
83
84         std::vector<ConnType> intersectElems;
85
86         tree.getIntersectingElems(targetBox, intersectElems);
87
88         if ( !intersectElems.empty() )
89           intersector->intersectCells(i,intersectElems,result);
90       }
91     return intersector->getNumberOfColsOfResMatrix();
92   }
93 }