Salome HOME
05572e77077481f8cb98fbf9620ce3860787344f
[tools/medcoupling.git] / src / RENUMBER / RENUMBER_BOOSTRenumbering.cxx
1 // Copyright (C) 2007-2016  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 #include "RENUMBER_BOOSTRenumbering.hxx"
21
22 #include "MEDCouplingMemArray.hxx"
23 #include "MCAuto.hxx"
24
25 #include <boost/config.hpp>
26 #include <boost/graph/adjacency_list.hpp>
27 #include <boost/graph/cuthill_mckee_ordering.hpp>
28 #include <boost/graph/properties.hpp>
29 #include <boost/graph/bandwidth.hpp>
30
31 void BOOSTRenumbering::renumber(const int *graph, const int *index_graph, int nbCell, MEDCoupling::DataArrayInt *&iperm, MEDCoupling::DataArrayInt *&perm)
32 {
33   MEDCoupling::MCAuto<MEDCoupling::DataArrayInt> out0(MEDCoupling::DataArrayInt::New()),out1(MEDCoupling::DataArrayInt::New());
34   out0->alloc(nbCell,1); out1->alloc(nbCell,1);
35   out0->fillWithZero(); out1->fillWithZero();
36   //
37   typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, 
38      boost::property<boost::vertex_color_t, boost::default_color_type,
39        boost::property<boost::vertex_degree_t,int> > > Graph;
40   typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
41   typedef boost::graph_traits<Graph>::vertices_size_type size_type;
42   Graph G(nbCell);
43   for (int i=0;i<nbCell;++i)
44     for (int j=index_graph[i];j<index_graph[i+1];++j)
45       add_edge(i,graph[j],G);
46   boost::property_map<Graph, boost::vertex_index_t>::type
47     index_map = boost::get(boost::vertex_index, G);
48   boost::cuthill_mckee_ordering(G, out0->getPointer(), boost::get(boost::vertex_color, G),
49                            boost::make_degree_map(G));
50   int *out0Ptr(out0->getPointer()),*out1Ptr(out1->getPointer());
51   for(int c=0;c!=nbCell;++c)
52     out1Ptr[index_map[out0Ptr[nbCell-c-1]]]=c;
53   out0->reverse();
54   iperm=out0.retn(); perm=out1.retn();
55 }