Salome HOME
Merge branch 'abn/fix_orient' into V7_main
[tools/medcoupling.git] / src / INTERP_KERNEL / PlanarIntersectorP0P1Bary.txx
1 // Copyright (C) 2007-2014  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, 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 (CEA/DEN)
20
21 #ifndef __PlanarIntersectorP0P1Bary_TXX__
22 #define __PlanarIntersectorP0P1Bary_TXX__
23
24 #include "PlanarIntersectorP0P1Bary.hxx"
25 #include "InterpolationUtils.hxx"
26
27 namespace INTERP_KERNEL
28 {
29   template<class MyMeshType, class MyMatrix, class ConcreteP0P1Intersector>
30   PlanarIntersectorP0P1Bary<MyMeshType,MyMatrix,ConcreteP0P1Intersector>::PlanarIntersectorP0P1Bary(const MyMeshType& meshT, const MyMeshType& meshS,
31                                                                                                     double dimCaracteristic, double precision,
32                                                                                                     double md3DSurf, double medianPlane,
33                                                                                                     bool doRotate, int orientation, int printLevel):
34     PlanarIntersector<MyMeshType,MyMatrix>(meshT,meshS,dimCaracteristic,precision,md3DSurf,
35                                            medianPlane,doRotate,orientation,printLevel)
36   {
37     // SPEC:
38     // "Limitation. For the P0P1 barycentric improvement only triangle target cells in 2D and
39     // tetrahedrons in 3D will be supported by interpolators. If a non
40     // triangle/tetrahedron source cell is detected an INTERP_KERNEL::Exception should be thrown."
41
42     // Check types of source elements here rather than in intersectCells() since a wrong type can be
43     // found late after a long time of calculation.
44
45     const unsigned long numTrgElems = meshT.getNumberOfElements();
46     for(unsigned long i = 0 ; i < numTrgElems ; ++i)
47       if ( meshT.getTypeOfElement( OTT<ConnType,numPol>::indFC( i )) != NORM_TRI3 )
48         throw INTERP_KERNEL::Exception("P0P1 barycentric algorithm works only with triangular target meshes");
49   }
50
51   template<class MyMeshType, class MyMatrix, class ConcreteP0P1Intersector>
52   int PlanarIntersectorP0P1Bary<MyMeshType,MyMatrix,ConcreteP0P1Intersector>::getNumberOfRowsOfResMatrix() const
53   {
54     return PlanarIntersector<MyMeshType,MyMatrix>::_meshT.getNumberOfNodes();
55   }
56
57   template<class MyMeshType, class MyMatrix, class ConcreteP0P1Intersector>
58   int PlanarIntersectorP0P1Bary<MyMeshType,MyMatrix,ConcreteP0P1Intersector>::getNumberOfColsOfResMatrix() const
59   {
60     return PlanarIntersector<MyMeshType,MyMatrix>::_meshS.getNumberOfElements();
61   }
62
63   /*!
64    * This method computes a value per each node of each source triangle for target.
65    */
66   template<class MyMeshType, class MyMatrix, class ConcreteP0P1Intersector>
67   void PlanarIntersectorP0P1Bary<MyMeshType,MyMatrix,ConcreteP0P1Intersector>::intersectCells(ConnType icellT, const std::vector<ConnType>& icellsS, MyMatrix& res)
68   {
69     int orientation=1;
70     std::vector<double> trgTriaCoords,trgTriaCoordsTmp;
71     // target cell data
72     PlanarIntersector<MyMeshType,MyMatrix>::getRealTargetCoordinates(OTT<ConnType,numPol>::indFC(icellT),trgTriaCoords);
73     std::vector<double> *tgtCoords(&trgTriaCoords);
74     const ConnType *startOfCellNodeConn=PlanarIntersector<MyMeshType,MyMatrix>::_connectT+OTT<ConnType,numPol>::conn2C(PlanarIntersector<MyMeshType,MyMatrix>::_connIndexT[icellT]);
75     // treat each source cells
76     for(typename std::vector<ConnType>::const_iterator iter=icellsS.begin();iter!=icellsS.end();iter++)
77     {
78       std::vector<double> srcCellCoords,srcCellCoordsTmp,nodeCeffs;
79       int iS=*iter;
80       NormalizedCellType tS=PlanarIntersector<MyMeshType,MyMatrix>::_meshS.getTypeOfElement(OTT<ConnType,numPol>::indFC(iS));
81       bool isSourceQuad=CellModel::GetCellModel(tS).isQuadratic();
82       PlanarIntersector<MyMeshType,MyMatrix>::getRealSourceCoordinates(OTT<ConnType,numPol>::indFC(iS),srcCellCoords);
83       std::vector<double> *srcCoords(&srcCellCoords);
84       int srcNbNodes = srcCellCoords.size()/SPACEDIM;
85       if(SPACEDIM==3)
86         {
87           srcCellCoordsTmp=srcCellCoords;
88           trgTriaCoordsTmp=trgTriaCoords;
89           srcCoords=&srcCellCoordsTmp;
90           tgtCoords=&trgTriaCoordsTmp;
91           orientation=PlanarIntersector<MyMeshType,MyMatrix>::projectionThis(&trgTriaCoordsTmp[0],&srcCellCoordsTmp[0],
92                                                                              3,srcNbNodes);
93         }
94       //double surf=orientation*intersectGeometryWithQuadrangle(quadrangle,targetCellCoordsTmp,isTargetQuad);
95       double surf=orientation*intersectGeoBary(*srcCoords,isSourceQuad,&((*tgtCoords)[0]),nodeCeffs);
96       surf=PlanarIntersector<MyMeshType,MyMatrix>::getValueRegardingOption(surf);
97       if(surf!=0.)
98       {
99         for(int nodeIdT=0;nodeIdT<3;nodeIdT++)
100         {
101           ConnType curNodeT=startOfCellNodeConn[nodeIdT];
102           typename MyMatrix::value_type& resRow=res[curNodeT];
103           typename MyMatrix::value_type::const_iterator iterRes=resRow.find(*iter);
104           if(iterRes!=resRow.end())
105           {
106             nodeCeffs[*iter] += iterRes->second;
107             resRow.erase(*iter);
108           }
109           resRow.insert(std::make_pair(*iter,nodeCeffs[nodeIdT]));
110         }
111       }
112     }
113   }
114 }
115 #endif