Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_ClsfBox.cxx
1 //  Copyright (C) 2007-2008  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.
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 // File:        GEOMAlgo_ClsfSurf.cxx
23 // Created:     Wed Nov 22 10:41:47 2006
24 // Author:      Peter KURNEV
25 //              <pkv@irinox>
26 //
27 #include <GEOMAlgo_ClsfBox.ixx>
28 #include <GeomAbs_SurfaceType.hxx>
29 #include <GEOMAlgo_SurfaceTools.hxx>
30 #include <TopAbs_ShapeEnum.hxx>
31 #include <TopTools_IndexedMapOfShape.hxx>
32 #include <TopExp.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <TopoDS.hxx>
35 #include <Geom_Surface.hxx>
36 #include <BRep_Tool.hxx>
37 #include <Geom_Plane.hxx>
38 #include <gp_Pnt.hxx>
39 #include <gp_Dir.hxx>
40 #include <gp_Pln.hxx>
41 #include <gp_Ax1.hxx>
42 #include <Geom_Plane.hxx>
43
44 //=======================================================================
45 //function : 
46 //purpose  : 
47 //=======================================================================
48   GEOMAlgo_ClsfBox::GEOMAlgo_ClsfBox()
49 :
50   GEOMAlgo_Clsf()
51 {
52 }
53 //=======================================================================
54 //function : ~
55 //purpose  : 
56 //=======================================================================
57   GEOMAlgo_ClsfBox::~GEOMAlgo_ClsfBox()
58 {
59 }
60 //=======================================================================
61 //function : SetBox
62 //purpose  : 
63 //=======================================================================
64   void GEOMAlgo_ClsfBox::SetBox(const TopoDS_Shape& aBox)
65 {
66   myBox=aBox;
67 }
68 //=======================================================================
69 //function : Box
70 //purpose  : 
71 //=======================================================================
72   const TopoDS_Shape& GEOMAlgo_ClsfBox::Box() const
73 {
74   return myBox;
75 }
76 //=======================================================================
77 //function : CheckData
78 //purpose  : 
79 //=======================================================================
80   void GEOMAlgo_ClsfBox::CheckData()
81
82   Standard_Integer i, aNbF;
83   TopAbs_ShapeEnum aTypeShape;
84   TopAbs_Orientation aOr;
85   GeomAbs_SurfaceType aType;
86   Handle(Geom_Surface) aS;
87   TopTools_IndexedMapOfShape aMF;
88   //
89   myErrorStatus=0;
90   //
91   if(myBox.IsNull()) {
92     myErrorStatus=10; // myBox=NULL
93     return;
94   }
95   //
96   aTypeShape=myBox.ShapeType();
97   if (aTypeShape!=TopAbs_SOLID) {
98     myErrorStatus=11; // unallowed shape type
99     return;
100   }
101   //
102   TopExp::MapShapes(myBox, TopAbs_FACE, aMF);
103   aNbF=aMF.Extent();
104   if (aNbF!=6) {
105     myErrorStatus=12; // wrong number of faces
106     return;
107   }
108   //
109   for (i=1; i<=aNbF; ++i) {
110     const TopoDS_Face& aF=TopoDS::Face(aMF(i));
111     aOr=aF.Orientation();
112     if (!(aOr==TopAbs_FORWARD || aOr==TopAbs_REVERSED)) {
113       myErrorStatus=12; // unallowed orientation of face
114       return;
115     }
116     //
117     aS=BRep_Tool::Surface(aF);
118     myGAS[i-1].Load(aS);
119     aType=myGAS[i-1].GetType();
120     if (!aType==GeomAbs_Plane) { 
121       myErrorStatus=13; // unallowed surface type
122       return;
123     }
124     //
125     if(aOr==TopAbs_REVERSED) {
126       gp_Ax1 aAx1;
127       gp_Pln aPln;
128       gp_Pnt aP;
129       gp_Dir aD;
130       Handle(Geom_Plane) aSR;
131       //
132       aPln=myGAS[i-1].Plane();
133       aAx1=aPln.Axis();
134       aP=aAx1.Location();
135       aD=aAx1.Direction();
136       aD.Reverse();
137       aSR=new Geom_Plane(aP, aD);
138       myGAS[i-1].Load(aSR);
139     }
140   }
141 }
142 //=======================================================================
143 //function : Perform
144 //purpose  : 
145 //=======================================================================
146   void GEOMAlgo_ClsfBox::Perform()
147 {
148   myErrorStatus=0;
149   //
150   const Standard_Integer aNbS=6;
151   Standard_Integer i, aNbON, aNbIN, iNext;
152   TopAbs_State aSt;
153   /*
154   CheckData();
155   if(myErrorStatus) {
156     return;
157   }
158   */
159   iNext=1;
160   aNbON=0; 
161   aNbIN=0;
162   for(i=0; i<aNbS && iNext; i++) {
163     GEOMAlgo_SurfaceTools::GetState(myPnt, myGAS[i], myTolerance, aSt);
164     //
165     switch (aSt) {
166       case TopAbs_OUT:
167         myState=aSt;
168         iNext=0;
169         break;
170       case TopAbs_ON:
171         ++aNbON;
172         break;
173       case TopAbs_IN:
174         ++aNbIN;
175         break;
176       default:
177         myState=TopAbs_UNKNOWN;
178         iNext=0;
179         break; 
180     }
181   }
182   //
183   if (iNext) {
184     myState=TopAbs_UNKNOWN;
185     //
186     if (aNbON && aNbIN) {
187       myState=TopAbs_ON;
188     }
189     else if (aNbIN==aNbS){
190       myState=TopAbs_IN;
191     }
192   }
193 }
194 //=======================================================================
195 //function : CanBeON
196 //purpose  : 
197 //=======================================================================
198   Standard_Boolean GEOMAlgo_ClsfBox::CanBeON(const Handle(Geom_Curve)& aC) const
199 {
200   return GEOMAlgo_Clsf::CanBeON(aC);
201 }
202 //=======================================================================
203 //function : CanBeON
204 //purpose  : 
205 //=======================================================================
206   Standard_Boolean GEOMAlgo_ClsfBox::CanBeON(const Handle(Geom_Surface)& aS1) const
207 {
208   Standard_Boolean bRet;
209   GeomAbs_SurfaceType  aST1;
210   GeomAdaptor_Surface aGAS1;
211   //
212   aGAS1.Load(aS1);
213   aST1=aGAS1.GetType();
214   bRet=(aST1==GeomAbs_Plane);
215   //
216   return bRet;
217 }