Salome HOME
ImportFromFile()
[modules/hydro.git] / src / HYDROData / HYDROData_LCM_FaceClassifier.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROData_LCM_FaceClassifier.h"
20
21 #include <HYDROData_LandCoverMap.h>
22 #include <Bnd_Box2d.hxx> 
23 #include <BRepTools.hxx>
24 #include <NCollection_UBTree.hxx>
25 #include <NCollection_UBTreeFiller.hxx>
26 #include <BRepTopAdaptor_FClass2d.hxx>
27 #include <TopoDS.hxx>
28 #include <TopTools_IndexedMapOfShape.hxx>
29
30
31 Standard_Boolean HYDROData_FaceClassifier_BndBoxTreeSelector::Accept (const Standard_Integer& theObj)
32 {
33   if (theObj > myMapF2Class2d.Extent())
34     return Standard_False;
35
36   const TopoDS_Face& f = TopoDS::Face(myMapF2Class2d.FindKey(theObj));
37   if(f.IsNull()) 
38     return Standard_False;
39
40   BRepTopAdaptor_FClass2d* class2d = myMapF2Class2d.FindFromKey(f);
41
42   TopAbs_State aState = class2d->Perform( myP, Standard_False );
43
44   if (aState == TopAbs_IN)
45   {
46     myResFaces.Append(f);
47     myStop = 1; //no more faces includes this point; quit
48     return Standard_True;
49   }
50   else if (aState == TopAbs_ON)
51   {
52     myResFaces.Append(f);
53     return Standard_True;
54   }
55
56   return Standard_False;
57
58 }
59
60 void HYDROData_LCM_FaceClassifier::Classify( const std::vector<gp_XY>& thePoints, 
61                                              std::vector<std::set <QString> >& theTypes,
62                                              std::vector<NCollection_Map<TopoDS_Face> >* theFaces) const
63 {
64   HYDROData_LandCoverMap::Explorer anIt( *myLCM );
65   HYDROData_MapOfFaceToStricklerType aMapF2ST;
66   TopTools_IndexedMapOfShape aFaces; 
67   for( ; anIt.More(); anIt.Next() )
68   {
69     const TopoDS_Face& F = anIt.Face();
70     aMapF2ST.Add(F, anIt.StricklerType());
71     aFaces.Add(F);
72   }
73
74   HYDROData_FaceClassifier_BndBoxTree aTree;
75   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box2d> aTreeFiller (aTree);
76   NCollection_IndexedDataMap<TopoDS_Face, BRepTopAdaptor_FClass2d*> aMapF2Class2d;
77
78   int NbF = aFaces.Extent();
79   std::vector<BRepTopAdaptor_FClass2d*> fclass2dpointers;
80   fclass2dpointers.reserve(NbF);
81
82   for (int i = 1; i <= NbF; i++)
83   {
84     Bnd_Box2d B;
85     const TopoDS_Face& F = TopoDS::Face(aFaces(i));
86     BRepTools::AddUVBounds(F, B);
87     aTreeFiller.Add(i, B);
88     BRepTopAdaptor_FClass2d* aClass2d = new BRepTopAdaptor_FClass2d( F, 0 );
89     aMapF2Class2d.Add(F, aClass2d);
90     fclass2dpointers.push_back(aClass2d);
91   }
92
93   aTreeFiller.Fill();
94
95   size_t pntsize = thePoints.size();
96   theTypes.resize(pntsize);
97   if (theFaces)
98     theFaces->resize(pntsize);
99
100   Standard_Integer aSel = 0;
101   for (size_t i = 0; i < pntsize; i++ )
102   {
103     HYDROData_FaceClassifier_BndBoxTreeSelector aSelector(aMapF2Class2d);
104     const gp_Pnt2d& pnt2d = thePoints[i]; 
105     aSelector.SetCurrentPoint(pnt2d);
106     aSel = aTree.Select(aSelector); 
107     if (aSel > 0)
108     {
109       const NCollection_List<TopoDS_Face>& rf = aSelector.GetResFaces();
110       NCollection_List<TopoDS_Face>::Iterator it(rf);
111       for (;it.More();it.Next())
112       {
113         const TopoDS_Face& f = it.Value();
114         QString aST = aMapF2ST.FindFromKey(f);
115         theTypes[i].insert(aST);
116         if (theFaces)
117           (*theFaces)[i].Add(f);
118       }      
119     }
120   }
121
122   for (size_t i = 0; i < fclass2dpointers.size(); i++)
123     delete fclass2dpointers[i];
124
125 }
126
127