Salome HOME
Merge from V6_main_20120808 08Aug12
[tools/medcoupling.git] / src / INTERP_KERNEL / PolyhedronIntersectorP1P1.txx
1 // Copyright (C) 2007-2012  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 #ifndef __PolyhedronIntersectorP1P1_TXX__
20 #define __PolyhedronIntersectorP1P1_TXX__
21
22 #include "PolyhedronIntersectorP1P1.hxx"
23 #include "Intersector3DP1P1.txx"
24 #include "MeshUtils.hxx"
25
26 #include "SplitterTetra.txx"
27
28 namespace INTERP_KERNEL
29 {
30
31   /**
32    * Constructor creating object from target cell global number 
33    * 
34    * @param targetMesh  mesh containing the target elements
35    * @param srcMesh     mesh containing the source elements
36    * @param policy      splitting policy to be used
37    */
38   template<class MyMeshType, class MyMatrix>
39   PolyhedronIntersectorP1P1<MyMeshType,MyMatrix>::PolyhedronIntersectorP1P1(const MyMeshType& targetMesh, const MyMeshType& srcMesh, SplittingPolicy policy):Intersector3DP1P1<MyMeshType,MyMatrix>(targetMesh,srcMesh)
40   {
41     // SPEC:
42     // "Limitation. Concerning P1P1 3D improvement only tetrahedron will be supported.
43     // If another type than tetrahedron is detected an INTERP_KERNEL::Exception should be thrown"
44
45     // Check types of elements here rather than in intersectCells() since a wrong type can be
46     // found late after a long time of calculation.
47
48     const unsigned long numSrcElems = srcMesh.getNumberOfElements();
49     for(unsigned long i = 0 ; i < numSrcElems ; ++i)
50       if ( srcMesh.getTypeOfElement( OTT<ConnType,numPol>::indFC( i )) != NORM_TETRA4 )
51         throw INTERP_KERNEL::Exception("P1P1 3D algorithm works only with tetrahedral meshes");
52
53     const unsigned long numTgtElems = targetMesh.getNumberOfElements();
54     for(unsigned long i = 0 ; i < numTgtElems ; ++i)
55       if ( targetMesh.getTypeOfElement( OTT<ConnType,numPol>::indFC( i )) != NORM_TETRA4 )
56         throw INTERP_KERNEL::Exception("P1P1 3D algorithm works only with tetrahedral meshes");
57   }
58
59   /**
60    * Destructor.
61    */
62   template<class MyMeshType, class MyMatrix>
63   PolyhedronIntersectorP1P1<MyMeshType,MyMatrix>::~PolyhedronIntersectorP1P1()
64   {
65   }
66
67   /**
68    * Calculates the volume of intersection of an element in the source mesh and the target element
69    * represented by the object.
70    * 
71    * @param targetCell in C mode.
72    * @param srcCells in C mode.
73    */
74   template<class MyMeshType, class MyMatrix>
75   void PolyhedronIntersectorP1P1<MyMeshType,MyMatrix>::intersectCells(ConnType targetCell, const std::vector<ConnType>& srcCells, MyMatrix& res)
76   {
77 #ifdef _DEBUG_
78     UnitTetraIntersectionBary b; b.init();
79 #endif
80     // split the targetCell into dual cells
81     std::pair< int, std::vector<double> > subTetraNodes[24]; // a node of sub tetra and its coordinates
82     const double* nodes[4]; int conn[4];
83     for(int node = 0; node < 4 ; ++node)
84       nodes[node]=getCoordsOfNode2(node, OTT<ConnType,numPol>::indFC(targetCell),
85                                    Intersector3D<MyMeshType,MyMatrix>::_target_mesh,conn[node]);
86     SplitterTetra<MyMeshType> tgtTetra(Intersector3D<MyMeshType,MyMatrix>::_src_mesh, nodes, conn);
87     for (int i=0; i<24; i++)
88       {
89         subTetraNodes[i].second.resize(12);
90         tgtTetra.splitMySelfForDual(&subTetraNodes[i].second[0],i,subTetraNodes[i].first);
91       }
92     // intersect each source tetrahedron with each of target dual cells
93     SplitterTetra<MyMeshType>* subTetrasS[24];
94     for(typename std::vector<ConnType>::const_iterator iterCellS=srcCells.begin();iterCellS!=srcCells.end();iterCellS++)
95       {
96         // split a source cell into dual cells
97         for(int node = 0; node < 4 ; ++node)
98           nodes[node]=getCoordsOfNode2(node, OTT<ConnType,numPol>::indFC(*iterCellS),
99                                        Intersector3D<MyMeshType,MyMatrix>::_src_mesh,conn[node]);
100
101         SplitterTetra<MyMeshType> srcTetra(Intersector3D<MyMeshType,MyMatrix>::_target_mesh, nodes, conn);
102         srcTetra.splitIntoDualCells(subTetrasS);
103
104         // intersect each target subTetra with each source one
105         for(int i=0;i<24;i++)
106           {
107             SplitterTetra<MyMeshType> *tmp=subTetrasS[i];
108             ConnType sourceNode=OTT<ConnType,numPol>::indFC(tmp->getId(0));
109             for(int j=0;j<24;j++)
110               {
111                 const double* tetraNodes12 = &subTetraNodes[j].second[0];
112                 const double* tetraNodesT[4]={ tetraNodes12, tetraNodes12+3, tetraNodes12+6, tetraNodes12+9 };
113                 double volume = tmp->intersectTetra( tetraNodesT );
114                 if(volume!=0.)
115                   {
116                     ConnType tgtNode=subTetraNodes[j].first;
117                     typename MyMatrix::value_type& resRow = res[tgtNode];
118                     typename MyMatrix::value_type::const_iterator iterRes=resRow.find( sourceNode );
119                     if(iterRes!=resRow.end())
120                       {
121                         volume += (*iterRes).second;
122                         resRow.erase(sourceNode);
123                       }
124                     resRow.insert(std::make_pair(sourceNode,volume));
125                   }
126               }
127             delete tmp;
128           }
129       }
130   }
131 }
132
133 #endif