Salome HOME
Copyrights update 2015.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_ClsfBox.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        GEOMAlgo_ClsfBox.cxx
24 // Created:     Wed Nov 22 10:41:47 2006
25 // Author:      Peter KURNEV
26 //              <pkv@irinox>
27 //
28 #include <GEOMAlgo_ClsfBox.hxx>
29
30 #include <gp_Pnt.hxx>
31 #include <gp_Dir.hxx>
32 #include <gp_Pln.hxx>
33 #include <gp_Ax3.hxx>
34
35 #include <GeomAbs_SurfaceType.hxx>
36 #include <Geom_Surface.hxx>
37 #include <Geom_Plane.hxx>
38 #include <TopAbs_ShapeEnum.hxx>
39
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS.hxx>
42
43 #include <BRep_Tool.hxx>
44
45 #include <TopExp.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47
48 #include <GEOMAlgo_SurfaceTools.hxx>
49
50 IMPLEMENT_STANDARD_HANDLE(GEOMAlgo_ClsfBox, GEOMAlgo_Clsf)
51 IMPLEMENT_STANDARD_RTTIEXT(GEOMAlgo_ClsfBox, GEOMAlgo_Clsf)
52
53 //=======================================================================
54 //function :
55 //purpose  :
56 //=======================================================================
57   GEOMAlgo_ClsfBox::GEOMAlgo_ClsfBox()
58 :
59   GEOMAlgo_Clsf()
60 {
61 }
62 //=======================================================================
63 //function : ~
64 //purpose  :
65 //=======================================================================
66   GEOMAlgo_ClsfBox::~GEOMAlgo_ClsfBox()
67 {
68 }
69 //=======================================================================
70 //function : SetBox
71 //purpose  :
72 //=======================================================================
73   void GEOMAlgo_ClsfBox::SetBox(const TopoDS_Shape& aBox)
74 {
75   myBox=aBox;
76 }
77 //=======================================================================
78 //function : Box
79 //purpose  :
80 //=======================================================================
81   const TopoDS_Shape& GEOMAlgo_ClsfBox::Box() const
82 {
83   return myBox;
84 }
85 //=======================================================================
86 //function : CheckData
87 //purpose  :
88 //=======================================================================
89   void GEOMAlgo_ClsfBox::CheckData()
90 {
91   Standard_Integer i, aNbF;
92   TopAbs_ShapeEnum aTypeShape;
93   TopAbs_Orientation aOr;
94   GeomAbs_SurfaceType aType;
95   Handle(Geom_Surface) aS;
96   TopTools_IndexedMapOfShape aMF;
97   //
98   myErrorStatus=0;
99   //
100   if(myBox.IsNull()) {
101     myErrorStatus=10; // myBox=NULL
102     return;
103   }
104   //
105   aTypeShape=myBox.ShapeType();
106   if (aTypeShape!=TopAbs_SOLID) {
107     myErrorStatus=11; // unallowed shape type
108     return;
109   }
110   //
111   TopExp::MapShapes(myBox, TopAbs_FACE, aMF);
112   aNbF=aMF.Extent();
113   if (aNbF!=6) {
114     myErrorStatus=12; // wrong number of faces
115     return;
116   }
117   //
118   for (i=1; i<=aNbF; ++i) {
119     const TopoDS_Face& aF=TopoDS::Face(aMF(i));
120     aOr=aF.Orientation();
121     if (!(aOr==TopAbs_FORWARD || aOr==TopAbs_REVERSED)) {
122       myErrorStatus=12; // unallowed orientation of face
123       return;
124     }
125     //
126     aS=BRep_Tool::Surface(aF);
127     myGAS[i-1].Load(aS);
128     aType=myGAS[i-1].GetType();
129     if (!aType==GeomAbs_Plane) {
130       myErrorStatus=13; // unallowed surface type
131       return;
132     }
133     //
134     if(aOr==TopAbs_REVERSED) {
135       gp_Ax3 aPos;
136       gp_Pln aPln;
137       gp_Pnt aP;
138       gp_Dir aD;
139       Handle(Geom_Plane) aSR;
140       //
141       aPln=myGAS[i-1].Plane();
142       aPos=aPln.Position();
143       aP=aPos.Location();
144       aD=aPos.Direction();
145
146       // Is aPos is not direct, aD is already a reversed direction.
147       if (aPos.Direct()) {
148         aD.Reverse();
149       }
150
151       aSR=new Geom_Plane(aP, aD);
152       myGAS[i-1].Load(aSR);
153     }
154   }
155 }
156 //=======================================================================
157 //function : Perform
158 //purpose  :
159 //=======================================================================
160   void GEOMAlgo_ClsfBox::Perform()
161 {
162   myErrorStatus=0;
163   //
164   const Standard_Integer aNbS=6;
165   Standard_Integer i, aNbON, aNbIN, iNext;
166   TopAbs_State aSt;
167   /*
168   CheckData();
169   if(myErrorStatus) {
170     return;
171   }
172   */
173   iNext=1;
174   aNbON=0;
175   aNbIN=0;
176   for(i=0; i<aNbS && iNext; i++) {
177     GEOMAlgo_SurfaceTools::GetState(myPnt, myGAS[i], myTolerance, aSt);
178     //
179     switch (aSt) {
180       case TopAbs_OUT:
181         myState=aSt;
182         iNext=0;
183         break;
184       case TopAbs_ON:
185         ++aNbON;
186         break;
187       case TopAbs_IN:
188         ++aNbIN;
189         break;
190       default:
191         myState=TopAbs_UNKNOWN;
192         iNext=0;
193         break;
194     }
195   }
196   //
197   if (iNext) {
198     myState=TopAbs_UNKNOWN;
199     //
200     if (aNbON && aNbIN) {
201       myState=TopAbs_ON;
202     }
203     else if (aNbIN==aNbS){
204       myState=TopAbs_IN;
205     }
206   }
207 }
208 //=======================================================================
209 //function : CanBeON
210 //purpose  :
211 //=======================================================================
212   Standard_Boolean GEOMAlgo_ClsfBox::CanBeON(const Handle(Geom_Curve)& aC) const
213 {
214   return GEOMAlgo_Clsf::CanBeON(aC);
215 }
216 //=======================================================================
217 //function : CanBeON
218 //purpose  :
219 //=======================================================================
220   Standard_Boolean GEOMAlgo_ClsfBox::CanBeON(const Handle(Geom_Surface)& aS1) const
221 {
222   Standard_Boolean bRet;
223   GeomAbs_SurfaceType  aST1;
224   GeomAdaptor_Surface aGAS1;
225   //
226   aGAS1.Load(aS1);
227   aST1=aGAS1.GetType();
228   bRet=(aST1==GeomAbs_Plane);
229   //
230   return bRet;
231 }