Salome HOME
3fd68493704bb21b98c6acd4703f84cb8e6d3dad
[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   Handle(Geom_Plane) Pl = HYDROData_LCM_FaceClassifier::GetPlane(f);
46   Standard_Real u, v;
47   ElSLib::Parameters(Pl->Pln(), gp_Pnt(myP.X(), myP.Y(), 0.0), u, v);
48
49   TopAbs_State aState = class2d->Perform( gp_Pnt2d(u, v), Standard_False );
50
51   if (aState == TopAbs_IN)
52   {
53     myResFaces.Append(f);
54     myStop = 1; //no more faces includes this point; quit
55     return Standard_True;
56   }
57   else if (aState == TopAbs_ON)
58   {
59     myResFaces.Append(f);
60     return Standard_True;
61   }
62
63   return Standard_False;
64
65 }
66
67 Handle(Geom_Plane) HYDROData_LCM_FaceClassifier::GetPlane(const TopoDS_Face& F)
68 {
69   TopLoc_Location L;
70   Handle(Geom_Surface) S = BRep_Tool::Surface(F, L);
71   Handle(Geom_Plane) Pl = Handle(Geom_Plane)::DownCast(S->Transformed(L.Transformation()));
72   return Pl;
73 }
74
75 void HYDROData_LCM_FaceClassifier::Classify( const std::vector<gp_XY>& thePoints, 
76                                              std::vector<std::set <QString> >& theTypes,
77                                              std::vector<NCollection_Map<TopoDS_Face> >* theFaces) const
78 {
79   HYDROData_LandCoverMap::Explorer anIt( *myLCM );
80   HYDROData_MapOfFaceToStricklerType aMapF2ST;
81   TopTools_IndexedMapOfShape aFaces; 
82   for( ; anIt.More(); anIt.Next() )
83   {
84     const TopoDS_Face& F = anIt.Face();
85     aMapF2ST.Add(F, anIt.StricklerType());
86     aFaces.Add(F);
87   }
88
89   HYDROData_FaceClassifier_BndBoxTree aTree;
90   NCollection_UBTreeFiller <Standard_Integer, Bnd_Box2d> aTreeFiller (aTree);
91   NCollection_IndexedDataMap<TopoDS_Face, BRepTopAdaptor_FClass2d*> aMapF2Class2d;
92
93   int NbF = aFaces.Extent();
94   std::vector<BRepTopAdaptor_FClass2d*> fclass2dpointers;
95   fclass2dpointers.reserve(NbF);
96
97   for (int i = 1; i <= NbF; i++)
98   {
99     Bnd_Box2d B;
100     const TopoDS_Face& F = TopoDS::Face(aFaces(i));
101     BRepTools::AddUVBounds(F, B);
102
103     //convert 2d space of planar face to the 3d space of given points 
104     //this is more faster way then getting of bnd3d of faces and project them on plane...
105     Handle(Geom_Plane) Pl = HYDROData_LCM_FaceClassifier::GetPlane(F);
106     gp_Trsf RT;
107     RT.SetTransformation(Pl->Position());
108     RT.Invert();
109     double xmin, ymin, xmax, ymax; 
110     B.Get(xmin, ymin, xmax, ymax);
111     gp_Pnt MinP(xmin, ymin, 0), MaxP(xmax, ymax, 0);
112     MinP.Transform(RT);
113     MaxP.Transform(RT);
114     gp_Pnt2d MinPT(MinP.X(), MinP.Y());
115     gp_Pnt2d MaxPT(MaxP.X(), MaxP.Y());
116     Bnd_Box2d NB;
117     NB.Update(MinPT.X(), MinPT.Y(), MaxPT.X(), MaxPT.Y() );
118
119     aTreeFiller.Add(i, NB);
120     BRepTopAdaptor_FClass2d* aClass2d = new BRepTopAdaptor_FClass2d( F, 0 );
121     aMapF2Class2d.Add(F, aClass2d);
122     fclass2dpointers.push_back(aClass2d);
123   }
124
125   aTreeFiller.Fill();
126
127   size_t pntsize = thePoints.size();
128   theTypes.resize(pntsize);
129   if (theFaces)
130     theFaces->resize(pntsize);
131
132   Standard_Integer aSel = 0;
133   for (size_t i = 0; i < pntsize; i++ )
134   {
135     HYDROData_FaceClassifier_BndBoxTreeSelector aSelector(aMapF2Class2d);
136     const gp_Pnt2d& pnt2d = thePoints[i]; 
137     aSelector.SetCurrentPoint(pnt2d);
138     aSel = aTree.Select(aSelector); 
139     if (aSel > 0)
140     {
141       const NCollection_List<TopoDS_Face>& rf = aSelector.GetResFaces();
142       NCollection_List<TopoDS_Face>::Iterator it(rf);
143       for (;it.More();it.Next())
144       {
145         const TopoDS_Face& f = it.Value();
146         QString aST = aMapF2ST.FindFromKey(f);
147         theTypes[i].insert(aST);
148         if (theFaces)
149           (*theFaces)[i].Add(f);
150       }      
151     }
152   }
153
154   for (size_t i = 0; i < fclass2dpointers.size(); i++)
155     delete fclass2dpointers[i];
156
157 }
158
159