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