Salome HOME
Copyright update 2020
[tools/medcoupling.git] / src / INTERP_KERNEL / MeshUtils.hxx
1 // Copyright (C) 2007-2020  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
20 #ifndef __MESHUTILS_HXX__
21 #define __MESHUTILS_HXX__
22
23 #include "InterpolationUtils.hxx"
24
25 namespace INTERP_KERNEL
26 {
27   /**
28    * Returns the global number of the node of an element.
29    *
30    * @param      node       the node for which the global number is sought (ALWAYS in C mode)
31    * @param      element    an element of the mesh (in numPol policy)
32    * @param      mesh       a mesh
33    * @return    the node's global number so that (its coordinates in the coordinates array are at [SPACEDIM*globalNumber, SPACEDIM*globalNumber + SPACEDIM]
34    */
35   template<class MyMeshType>
36   inline typename MyMeshType::MyConnType getGlobalNumberOfNode(typename MyMeshType::MyConnType node, typename MyMeshType::MyConnType element, const MyMeshType& mesh)
37   {
38     typedef typename MyMeshType::MyConnType ConnType;
39     const NumberingPolicy numPol=MyMeshType::My_numPol;
40     const ConnType elemIdx=OTT<ConnType,numPol>::conn2C(mesh.getConnectivityIndexPtr()[OTT<ConnType,numPol>::ind2C(element)]);
41     if(mesh.getTypeOfElement(element)!=INTERP_KERNEL::NORM_POLYHED)
42       return OTT<ConnType,numPol>::coo2C(mesh.getConnectivityPtr()[elemIdx + node]);
43     else
44       {
45         const ConnType *startNodalConnOfElem=mesh.getConnectivityPtr()+elemIdx;
46         ConnType ptr=0,ret=0;
47         while(startNodalConnOfElem[ret]==-1 || ptr!=node)
48           {
49             ret++;
50             if(startNodalConnOfElem[ret]!=-1)
51               ptr++;
52           }
53         return OTT<ConnType,numPol>::coo2C(startNodalConnOfElem[ret]);
54       }
55   }
56
57   /**
58    * Returns the coordinates of a node of an element
59    *
60    * @param      node       the node for which the coordinates are sought. In C mode.
61    * @param      element    an element of the mesh. In mesh policy.
62    * @param      mesh       a mesh
63    * @return    pointer to an array of 3 doubles containing the coordinates of the node
64    */
65   template<class MyMeshType>
66   inline const double* getCoordsOfNode(typename MyMeshType::MyConnType node, typename MyMeshType::MyConnType element, const MyMeshType& mesh)
67   {
68     typedef typename MyMeshType::MyConnType ConnType;
69     const ConnType connIdx = getGlobalNumberOfNode(node, element, mesh);
70     const double *ret=mesh.getCoordinatesPtr()+MyMeshType::MY_SPACEDIM*connIdx;
71     return ret;
72   }
73
74   /**
75    * Returns the coordinates of a node of an element
76    *
77    * @param      node       the node for which the coordinates are sought. In C mode.
78    * @param      element    an element of the mesh. In mesh policy.
79    * @param      mesh       a mesh
80    * @param      nodeId     globale nodeId in whole mesh point of view in C mode.
81    * @return    pointer to an array of 3 doubles containing the coordinates of the node
82    */
83   template<class MyMeshType>
84   inline const double* getCoordsOfNode2(typename MyMeshType::MyConnType node, typename MyMeshType::MyConnType element, const MyMeshType& mesh, typename MyMeshType::MyConnType& nodeId)
85   {
86     nodeId= getGlobalNumberOfNode(node, element, mesh);
87     return mesh.getCoordinatesPtr()+MyMeshType::MY_SPACEDIM*nodeId;
88   }
89
90   /**
91    * Returns the barycentric coordinates of a point within a triangle or tetrahedron
92    *
93    * @param      point       the point for which the barycentric coordinates are sought
94    * @param      element    an element of the mesh
95    * @param      mesh       a mesh
96    * @param     barycentricCoords an array of 3 doubles containing the coordinates of the node
97    */
98   template<class MyMeshType, int NB_NODES>
99   inline void getBarycentricCoordinates(const double*                   point,
100                                         typename MyMeshType::MyConnType element,
101                                         const MyMeshType&               mesh,
102                                         double*                         barycentricCoords)
103   {
104     std::vector<const double*> nodes( NB_NODES );
105     for ( int node = 0; node < NB_NODES; ++node )
106       {
107         nodes[ node ] = getCoordsOfNode( node, element, mesh );
108       }
109     barycentric_coords( nodes, point, barycentricCoords );
110   }
111     
112 }
113
114 #endif