]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_LCM_FaceClassifier.cxx
Salome HOME
Initial merge of branch 'BR_HYDRO_IMPS_2016' into BR_PORTING_OCCT_7
[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 #include <Geom_Plane.hxx>
30 #include <BRep_Tool.hxx>
31 #include <Geom_Surface.hxx>
32 #include <ElSLib.hxx>
33
34 Standard_Boolean HYDROData_FaceClassifier_BndBoxTreeSelector::Accept (const Standard_Integer& theObj)
35 {
36   if (theObj > myMapF2Class2d.Extent())
37     return Standard_False;
38
39   const TopoDS_Face& f = TopoDS::Face(myMapF2Class2d.FindKey(theObj));
40   if(f.IsNull()) 
41     return Standard_False;
42
43   BRepTopAdaptor_FClass2d* class2d = myMapF2Class2d.FindFromKey(f);
44   
45   TopLoc_Location L;
46   Handle(Geom_Surface) C = BRep_Tool::Surface(f, L);
47   Handle(Geom_Plane) Pl = Handle(Geom_Plane)::DownCast(C->Transformed(L.Transformation()));
48   Standard_Real u, v;
49   ElSLib::Parameters(Pl->Pln(), gp_Pnt(myP.X(), myP.Y(), 0.0), u, v);
50
51   TopAbs_State aState = class2d->Perform( gp_Pnt2d(u, v), Standard_False );
52
53   if (aState == TopAbs_IN)
54   {
55     myResFaces.Append(f);
56     myStop = 1; //no more faces includes this point; quit
57     return Standard_True;
58   }
59   else if (aState == TopAbs_ON)
60   {
61     myResFaces.Append(f);
62     return Standard_True;
63   }
64
65   return Standard_False;
66
67 }
68
69 void HYDROData_LCM_FaceClassifier::Classify( const std::vector<gp_XY>& thePoints, 
70                                              std::vector<std::set <QString> >& theTypes,
71                                              std::vector<NCollection_Map<TopoDS_Face> >* theFaces) const
72 {
73   HYDROData_LandCoverMap::Explorer anIt( *myLCM );
74   HYDROData_MapOfFaceToStricklerType aMapF2ST;
75   TopTools_IndexedMapOfShape aFaces; 
76   for( ; anIt.More(); anIt.Next() )
77   {
78     const TopoDS_Face& F = anIt.Face();
79     aMapF2ST.Add(F, anIt.StricklerType());
80     aFaces.Add(F);
81   }
82
83   HYDROData_FaceClassifier_BndBoxTree aTree;
84   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box2d> aTreeFiller (aTree);
85   NCollection_IndexedDataMap<TopoDS_Face, BRepTopAdaptor_FClass2d*> aMapF2Class2d;
86
87   int NbF = aFaces.Extent();
88   std::vector<BRepTopAdaptor_FClass2d*> fclass2dpointers;
89   fclass2dpointers.reserve(NbF);
90
91   for (int i = 1; i <= NbF; i++)
92   {
93     Bnd_Box2d B;
94     const TopoDS_Face& F = TopoDS::Face(aFaces(i));
95     BRepTools::AddUVBounds(F, B);
96     aTreeFiller.Add(i, B);
97     BRepTopAdaptor_FClass2d* aClass2d = new BRepTopAdaptor_FClass2d( F, 0 );
98     aMapF2Class2d.Add(F, aClass2d);
99     fclass2dpointers.push_back(aClass2d);
100   }
101
102   aTreeFiller.Fill();
103
104   size_t pntsize = thePoints.size();
105   theTypes.resize(pntsize);
106   if (theFaces)
107     theFaces->resize(pntsize);
108
109   Standard_Integer aSel = 0;
110   for (size_t i = 0; i < pntsize; i++ )
111   {
112     HYDROData_FaceClassifier_BndBoxTreeSelector aSelector(aMapF2Class2d);
113     const gp_Pnt2d& pnt2d = thePoints[i]; 
114     aSelector.SetCurrentPoint(pnt2d);
115     aSel = aTree.Select(aSelector); 
116     if (aSel > 0)
117     {
118       const NCollection_List<TopoDS_Face>& rf = aSelector.GetResFaces();
119       NCollection_List<TopoDS_Face>::Iterator it(rf);
120       for (;it.More();it.Next())
121       {
122         const TopoDS_Face& f = it.Value();
123         QString aST = aMapF2ST.FindFromKey(f);
124         theTypes[i].insert(aST);
125         if (theFaces)
126           (*theFaces)[i].Add(f);
127       }      
128     }
129   }
130
131   for (size_t i = 0; i < fclass2dpointers.size(); i++)
132     delete fclass2dpointers[i];
133
134 }
135
136