Salome HOME
Copyrights update 2015.
[modules/med.git] / src / MEDCoupling / MEDCouplingNormalizedUnstructuredMesh.txx
1 // Copyright (C) 2007-2015  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 // Author : Anthony Geay (CEA/DEN)
20
21 #ifndef __MEDCOUPLINGNORMALIZEDUNSTRUCTUREDMESH_TXX__
22 #define __MEDCOUPLINGNORMALIZEDUNSTRUCTUREDMESH_TXX__
23
24 #include "MEDCouplingNormalizedUnstructuredMesh.hxx"
25
26 #include "MEDCouplingUMesh.hxx"
27 #include "MEDCoupling1GTUMesh.hxx"
28 #include "MEDCouplingMemArray.hxx"
29
30 #include <limits>
31
32 template<int SPACEDIM,int MESHDIM>
33 MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::MEDCouplingNormalizedUnstructuredMesh(const ParaMEDMEM::MEDCouplingPointSet *mesh):_mesh(mesh)
34 {
35   if(_mesh)
36     _mesh->incrRef();
37   prepare();
38 }
39
40 template<int SPACEDIM,int MESHDIM>
41 void MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getBoundingBox(double *boundingBox) const
42 {
43   for(int i=0;i<SPACEDIM;i++)
44     {
45       boundingBox[i]=std::numeric_limits<double>::max();
46       boundingBox[SPACEDIM+i]=-std::numeric_limits<double>::max();
47     }
48   const ParaMEDMEM::DataArrayDouble *array=_mesh->getCoords();
49   const double *ptr=array->getConstPointer();
50   int nbOfPts=array->getNbOfElems()/SPACEDIM;
51   for(int j=0;j<SPACEDIM;j++)
52     {
53       const double *work=ptr+j;
54       for(int i=0;i<nbOfPts;i++,work+=SPACEDIM)
55         {
56           if(boundingBox[j]>*work)
57             boundingBox[j]=*work;
58           if(boundingBox[j+SPACEDIM]<*work)
59             boundingBox[j+SPACEDIM]=*work;
60         }
61     }
62 }
63
64 template<int SPACEDIM,int MESHDIM>
65 INTERP_KERNEL::NormalizedCellType MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getTypeOfElement(int eltId) const
66 {
67   return _mesh->getTypeOfCell(eltId);
68 }
69
70 template<int SPACEDIM,int MESHDIM>
71 int MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getNumberOfNodesOfElement(int eltId) const
72 {
73   return _mesh->getNumberOfNodesInCell(eltId);
74 }
75
76 template<int SPACEDIM,int MESHDIM>
77 int MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getNumberOfElements() const
78 {
79   return _mesh->getNumberOfCells();
80 }
81
82 template<int SPACEDIM,int MESHDIM>
83 int MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getNumberOfNodes() const
84 {
85   return _mesh->getNumberOfNodes();
86 }
87
88 template<int SPACEDIM,int MESHDIM>
89 const int *MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getConnectivityPtr() const
90 {
91   return _conn_for_interp;
92 }
93
94 template<int SPACEDIM,int MESHDIM>
95 const double *MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getCoordinatesPtr() const
96 {
97   const ParaMEDMEM::DataArrayDouble *array=_mesh->getCoords();
98   return array->getConstPointer();
99 }
100
101 template<int SPACEDIM,int MESHDIM>
102 const int *MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::getConnectivityIndexPtr() const
103 {
104   return _conn_index_for_interp;
105 }
106
107 template<int SPACEDIM,int MESHDIM>
108 void MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::releaseTempArrays()
109 {
110   delete [] _conn_for_interp;
111   delete [] _conn_index_for_interp;
112   _conn_for_interp=0;
113   _conn_index_for_interp=0;
114 }
115
116 template<int SPACEDIM,int MESHDIM>
117 MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::~MEDCouplingNormalizedUnstructuredMesh()
118 {
119   if(_mesh)
120     _mesh->decrRef();
121   releaseTempArrays();
122 }
123
124 template<int SPACEDIM,int MESHDIM>
125 void MEDCouplingNormalizedUnstructuredMesh<SPACEDIM,MESHDIM>::prepare()
126 {
127   const ParaMEDMEM::MEDCouplingUMesh *m1(dynamic_cast<const ParaMEDMEM::MEDCouplingUMesh *>(_mesh));
128   if(m1)
129     {
130       int nbOfCell=m1->getNumberOfCells();
131       int initialConnSize=m1->getNodalConnectivity()->getNbOfElems();
132       _conn_for_interp=new int[initialConnSize-nbOfCell];
133       _conn_index_for_interp=new int[nbOfCell+1];
134       _conn_index_for_interp[0]=0;
135       const int *work_conn=m1->getNodalConnectivity()->getConstPointer()+1;
136       const int *work_conn_index=m1->getNodalConnectivityIndex()->getConstPointer();
137       int *work_conn_for_interp=_conn_for_interp;
138       int *work_conn_index_for_interp=_conn_index_for_interp;
139       for(int i=0;i<nbOfCell;i++)
140         {
141           int nbOfValsToCopy=work_conn_index[1]-work_conn_index[0]-1;
142           work_conn_for_interp=std::copy(work_conn,work_conn+nbOfValsToCopy,work_conn_for_interp);
143           work_conn_index_for_interp[1]=work_conn_index_for_interp[0]+nbOfValsToCopy;
144           work_conn_index++;
145           work_conn+=nbOfValsToCopy+1;
146           work_conn_index_for_interp++;
147         }
148       return ;
149     }
150   const ParaMEDMEM::MEDCoupling1DGTUMesh *m2(dynamic_cast<const ParaMEDMEM::MEDCoupling1DGTUMesh *>(_mesh));
151   if(m2)
152     {
153       int nbOfCell(m2->getNumberOfCells());
154       _conn_index_for_interp=new int[nbOfCell+1];
155       const int *conni(m2->getNodalConnectivityIndex()->begin());
156       std::copy(conni,conni+nbOfCell+1,_conn_index_for_interp);
157       _conn_for_interp=new int[m2->getNodalConnectivity()->getNumberOfTuples()];
158       std::copy(m2->getNodalConnectivity()->begin(),m2->getNodalConnectivity()->end(),_conn_for_interp);
159       return ;
160     }
161   const ParaMEDMEM::MEDCoupling1SGTUMesh *m3(dynamic_cast<const ParaMEDMEM::MEDCoupling1SGTUMesh *>(_mesh));
162   if(m3)
163     {
164       int nbOfCell(m3->getNumberOfCells()),nbNodesPerCell(m3->getNumberOfNodesPerCell());
165       _conn_index_for_interp=new int[nbOfCell+1]; _conn_index_for_interp[0]=0;
166       int *work(_conn_index_for_interp);
167       for(int i=0;i<nbOfCell;i++,work++)
168         work[1]=work[0]+nbNodesPerCell;
169       _conn_for_interp=new int[m3->getNodalConnectivity()->getNumberOfTuples()];
170       std::copy(m3->getNodalConnectivity()->begin(),m3->getNodalConnectivity()->end(),_conn_for_interp);
171       return ;
172     }
173   throw INTERP_KERNEL::Exception("MEDCouplingNormalizedUnstructuredMesh::prepare : Unrecognized unstructured mesh ! Type must be in MEDCouplingUMesh, MEDCoupling1DGTUMesh, MEDCoupling1SGTUMesh !");
174 }
175
176 #endif