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