Salome HOME
4ad2677b6e1a4e5c7c85c537f368f4c7159d2f05
[modules/med.git] / src / MEDCoupling / MEDCouplingNormalizedUnstructuredMesh.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 __MEDCOUPLINGNORMALIZEDUNSTRUCTUREDMESH_TXX__
20 #define __MEDCOUPLINGNORMALIZEDUNSTRUCTUREDMESH_TXX__
21
22 #include "MEDCouplingNormalizedUnstructuredMesh.hxx"
23
24 #include "MEDCouplingUMesh.hxx"
25 #include "MEDCouplingMemArray.hxx"
26
27 #include <limits>
28
29 template<int SPACEDIM,int MESHDIM>
30 MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::MEDCouplingNormalizedUnstructuredMesh(const ParaMEDMEM::MEDCouplingUMesh *mesh):_mesh(mesh)
31 {
32   if(_mesh)
33     _mesh->incrRef();
34   prepare();
35 }
36
37 template<int SPACEDIM,int MESHDIM>
38 void MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getBoundingBox(double *boundingBox) const
39 {
40   for(int i=0;i<SPACEDIM;i++)
41     {
42       boundingBox[i]=std::numeric_limits<double>::max();
43       boundingBox[SPACEDIM+i]=-std::numeric_limits<double>::max();
44     }
45   const ParaMEDMEM::DataArrayDouble *array=_mesh->getCoords();
46   const double *ptr=array->getConstPointer();
47   int nbOfPts=array->getNbOfElems()/SPACEDIM;
48   for(int j=0;j<SPACEDIM;j++)
49     {
50       const double *work=ptr+j;
51       for(int i=0;i<nbOfPts;i++,work+=SPACEDIM)
52         {
53           if(boundingBox[j]>*work)
54             boundingBox[j]=*work;
55           if(boundingBox[j+SPACEDIM]<*work)
56             boundingBox[j+SPACEDIM]=*work;
57         }
58     }
59 }
60
61 template<int SPACEDIM,int MESHDIM>
62 INTERP_KERNEL::NormalizedCellType MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getTypeOfElement(int eltId) const
63 {
64   return _mesh->getTypeOfCell(eltId);
65 }
66
67 template<int SPACEDIM,int MESHDIM>
68 unsigned char MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getNumberOfNodesOfElement(int eltId) const
69 {
70   return _mesh->getNumberOfNodesInCell(eltId);
71 }
72
73 template<int SPACEDIM,int MESHDIM>
74 unsigned long MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getNumberOfElements() const
75 {
76   return _mesh->getNumberOfCells();
77 }
78
79 template<int SPACEDIM,int MESHDIM>
80 unsigned long MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getNumberOfNodes() const
81 {
82   return _mesh->getNumberOfNodes();
83 }
84
85 template<int SPACEDIM,int MESHDIM>
86 const int *MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getConnectivityPtr() const
87 {
88   return _conn_for_interp;
89 }
90
91 template<int SPACEDIM,int MESHDIM>
92 const double *MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getCoordinatesPtr() const
93 {
94   const ParaMEDMEM::DataArrayDouble *array=_mesh->getCoords();
95   return array->getConstPointer();
96 }
97
98 template<int SPACEDIM,int MESHDIM>
99 const int *MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getConnectivityIndexPtr() const
100 {
101   return _conn_index_for_interp;
102 }
103
104 template<int SPACEDIM,int MESHDIM>
105 void MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::releaseTempArrays()
106 {
107   delete [] _conn_for_interp;
108   delete [] _conn_index_for_interp;
109   _conn_for_interp=0;
110   _conn_index_for_interp=0;
111 }
112
113 template<int SPACEDIM,int MESHDIM>
114 MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::~MEDCouplingNormalizedUnstructuredMesh()
115 {
116   if(_mesh)
117     _mesh->decrRef();
118   releaseTempArrays();
119 }
120
121 template<int SPACEDIM,int MESHDIM>
122 void MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::prepare()
123 {
124   int nbOfCell=_mesh->getNumberOfCells();
125   int initialConnSize=_mesh->getNodalConnectivity()->getNbOfElems();
126   _conn_for_interp=new int[initialConnSize-nbOfCell];
127   _conn_index_for_interp=new int[nbOfCell+1];
128   _conn_index_for_interp[0]=0;
129   const int *work_conn=_mesh->getNodalConnectivity()->getConstPointer()+1;
130   const int *work_conn_index=_mesh->getNodalConnectivityIndex()->getConstPointer();
131   int *work_conn_for_interp=_conn_for_interp;
132   int *work_conn_index_for_interp=_conn_index_for_interp;
133   for(int i=0;i<nbOfCell;i++)
134     {
135       int nbOfValsToCopy=work_conn_index[1]-work_conn_index[0]-1;
136       work_conn_for_interp=std::copy(work_conn,work_conn+nbOfValsToCopy,work_conn_for_interp);
137       work_conn_index_for_interp[1]=work_conn_index_for_interp[0]+nbOfValsToCopy;
138       work_conn_index++;
139       work_conn+=nbOfValsToCopy+1;
140       work_conn_index_for_interp++;
141     }
142 }
143
144 #endif