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