Salome HOME
c271120f0bdd5f1bb210bf8f40118f04942cff9e
[tools/medcoupling.git] / src / INTERP_KERNEL / IntersectorCU3D.txx
1 // Copyright (C) 2009-2015  OPEN CASCADE
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 // File      : IntersectorCU3D.txx
20 // Created   : Thu Dec 17 14:17:49 2009
21 // Author    : Edward AGAPOV (eap)
22
23 #ifndef __IntersectorCU3D_TXX__
24 #define __IntersectorCU3D_TXX__
25
26 #include "IntersectorCU3D.hxx"
27 #include "IntersectorCU.txx"
28 #include "SplitterTetra.txx"
29
30 #define  IntersectorCU3D_TEMPLATE template<class MyCMeshType, class MyUMeshType, class MyMatrix>
31 #define  INTERSECTOR_CU3D IntersectorCU3D<MyCMeshType,MyUMeshType,MyMatrix >
32 #define _INTERSECTOR_CU   IntersectorCU  <MyCMeshType,MyUMeshType,MyMatrix,IntersectorCU3D<MyCMeshType,MyUMeshType,MyMatrix> >
33
34 namespace INTERP_KERNEL
35 {
36   //================================================================================
37   /*!
38    * \brief Unstructured hexahedral mesh derived from cartesian 3D mesh.
39    *        Mesh contains one HEXA8 element
40    */
41   //================================================================================
42
43   class _Cartesian3D2UnstructHexMesh
44   {
45   public:
46     static const int MY_SPACEDIM=3;
47     static const int MY_MESHDIM=3;
48     typedef int MyConnType;
49     static const NumberingPolicy My_numPol=ALL_C_MODE;
50
51     _Cartesian3D2UnstructHexMesh(const double * coords[3]): _coordsC(coords) {}
52     void setHexa(int I, int J, int K) // indices in C mode
53     {
54       double* pCoord = _coordsU;
55       for ( int k = K; k < K+2; ++k )
56         for ( int j = J; j < J+2; ++j )
57           for ( int i = I; i < I+2; ++i )
58             {
59               *pCoord++ = _coordsC[0][i];
60               *pCoord++ = _coordsC[1][j];
61               *pCoord++ = _coordsC[2][k];
62             }
63     }
64     const int *getConnectivityPtr() const
65     {
66       static int conn[] = { 1,0,2,3,5,4,6,7 };
67       return conn;
68     }
69     const int *getConnectivityIndexPtr() const
70     {
71       static int conInd[] = { 0,8 };
72       return conInd;
73     }
74     void getBoundingBox(double *boundingBox) const
75     {
76       boundingBox[BoundingBox::XMIN] = _coordsU[0];
77       boundingBox[BoundingBox::XMAX] = _coordsU[0+1*MY_SPACEDIM];
78       boundingBox[BoundingBox::YMIN] = _coordsU[1];
79       boundingBox[BoundingBox::YMAX] = _coordsU[1+2*MY_SPACEDIM];
80       boundingBox[BoundingBox::ZMIN] = _coordsU[2];
81       boundingBox[BoundingBox::ZMAX] = _coordsU[2+4*MY_SPACEDIM];
82     }
83     NormalizedCellType getTypeOfElement(int eltId) const { return NORM_HEXA8; }
84     unsigned char getNumberOfNodesOfElement(int eltId) const { return 8; }
85     unsigned long getNumberOfElements() const { return 1; }
86     unsigned long getNumberOfNodes()    const { return 8; }
87     const double *getCoordinatesPtr()   const { return _coordsU; }
88     void releaseTempArrays() {}
89   private:
90     const double** _coordsC;
91     double         _coordsU[3*8];
92   };
93
94   //================================================================================
95   /*!
96    * \brief intersector of the unstructured mesh and the cartesian mesh in 3D
97    */
98   //================================================================================
99
100   IntersectorCU3D_TEMPLATE
101   INTERSECTOR_CU3D::IntersectorCU3D(const MyCMeshType& meshS,
102                                     const MyUMeshType& meshT,
103                                     SplittingPolicy    splitting_policy):
104     _INTERSECTOR_CU( meshS, meshT )
105   {
106     if ( MyCMeshType::MY_SPACEDIM != 3 || MyCMeshType::MY_MESHDIM != 3 ||
107          MyUMeshType::MY_SPACEDIM != 3 || MyUMeshType::MY_MESHDIM != 3 )
108       throw Exception("IntersectorCU3D(): Invalid mesh dimension, it must be 3");
109
110     _uHexMesh = new _Cartesian3D2UnstructHexMesh( _INTERSECTOR_CU::_coordsC );
111     _split    = new TSplitter( meshT, *_uHexMesh, splitting_policy );
112   }
113
114   //================================================================================
115   /*!
116    * \brief destructor
117    */
118   //================================================================================
119
120   IntersectorCU3D_TEMPLATE
121   INTERSECTOR_CU3D::~IntersectorCU3D()
122   {
123     delete _uHexMesh; _uHexMesh=0;
124     delete _split; _split=0;
125   }
126
127   //================================================================================
128   /*!
129    * \brief Calculate volume of intersection of an unstructured cell and a cartesian one.
130    * The cartesian cell is given by its [i,j,k] indices
131    */
132   //================================================================================
133
134   IntersectorCU3D_TEMPLATE
135   double INTERSECTOR_CU3D::intersectGeometry(UConnType                     icellT,
136                                              const std::vector<CConnType>& icellS)
137   {
138     // split an unstructured cell into tetra
139     std::vector< TTetra* > tetra;
140     UConnType nb_nodes =
141       _INTERSECTOR_CU::_connIndexU[icellT+1] - _INTERSECTOR_CU::_connIndexU[icellT];
142     _split->releaseArrays();
143     _split->splitTargetCell( icellT, nb_nodes, tetra);
144
145     // intersect a cartesian 3d cell with tetra
146     _uHexMesh->setHexa( _FMIC(icellS[0]),_FMIC(icellS[1]),_FMIC(icellS[2])); // set cell at i,j,k
147     double res = 0;
148     for ( unsigned int t = 0; t < tetra.size(); ++t )
149       {
150         res += tetra[t]->intersectSourceCell( 0 );
151         delete tetra[t];
152       }
153     return res;
154   }
155 }
156 #endif