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