1 // Created on: 1996-07-15
2 // Created by: Laurent BUCHARD
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // Modified by skv - Thu Sep 4 11:22:05 2003 OCC578
19 #include <BRep_Tool.hxx>
20 #include <BRepClass3d_Intersector3d.hxx>
21 #include <BRepClass3d_SClassifier.hxx>
22 #include <BRepClass3d_SolidExplorer.hxx>
23 #include <BRepTopAdaptor_FClass2d.hxx>
25 #include <Geom_Surface.hxx>
29 #include <IntCurvesFace_Intersector.hxx>
30 #include <math_BullardGenerator.hxx>
31 #include <Precision.hxx>
32 #include <Standard_DomainError.hxx>
34 #include <TopoDS_Face.hxx>
36 // modified by NIZHNY-MKK Mon Jun 21 15:13:40 2004
38 Standard_Boolean FaceNormal (const TopoDS_Face& aF,
39 const Standard_Real U,
40 const Standard_Real V,
44 Standard_Real GetAddToParam(const gp_Lin& L,const Standard_Real P,const Bnd_Box& B);
48 //=======================================================================
49 //function : BRepClass3d_SClassifier
51 //=======================================================================
52 BRepClass3d_SClassifier::BRepClass3d_SClassifier()
57 //=======================================================================
58 //function : BRepClass3d_SClassifier
60 //=======================================================================
61 BRepClass3d_SClassifier::BRepClass3d_SClassifier(BRepClass3d_SolidExplorer& S,
63 const Standard_Real Tol) {
65 myState=3; //-- in ds solid case without face
73 //=======================================================================
74 //function : PerformInfinitePoint
76 //=======================================================================
77 void BRepClass3d_SClassifier::PerformInfinitePoint(BRepClass3d_SolidExplorer& aSE,
78 const Standard_Real /*Tol*/) {
80 //Take a normal to the first extracted face in its random inner point
81 //and intersect this reversed normal with the faces of the solid.
82 //If the min.par.-intersection point is
83 // a) inner point of a face
84 // b) transition is not TANGENT
85 // (the line does not touch the face but pierces it)
86 //then set <myState> to IN or OUT according to transition
87 //else take the next random point inside the min.par.-intersected face
90 if(aSE.Reject(gp_Pnt(0,0,0))) {
91 myState=3; //-- in ds solid case without face
95 //------------------------------------------------------------
97 Standard_Boolean bFound;
98 Standard_Real aParam, aU = 0., aV = 0.;
102 math_BullardGenerator aRandomGenerator;
112 TopoDS_Face aF = aSE.CurrentFace();
113 TopAbs_State aState = TopAbs_OUT;
114 IntCurveSurface_TransitionOnCurve aTransition = IntCurveSurface_Tangent;
115 TopoDS_Face MinFace = aF;
118 aParam = 0.1 + 0.8 * aRandomGenerator.NextReal(); // random number in range [0.1, 0.9]
119 bFound = aSE.FindAPointInTheFace(aF, aPoint, aU, aV, aParam);
123 if (!FaceNormal(aF, aU, aV, aDN))
125 gp_Lin aLin(aPoint, -aDN);
126 Standard_Real parmin = RealLast();
127 for (aSE.InitShell();aSE.MoreShell();aSE.NextShell()) {
128 if (aSE.RejectShell(aLin) == Standard_False) {
129 for (aSE.InitFace();aSE.MoreFace(); aSE.NextFace()) {
130 if (aSE.RejectFace(aLin) == Standard_False) {
131 TopoDS_Shape aLocalShape = aSE.CurrentFace();
132 TopoDS_Face CurFace = TopoDS::Face(aLocalShape);
133 IntCurvesFace_Intersector& Intersector3d = aSE.Intersector(CurFace);
134 Intersector3d.Perform(aLin,-RealLast(),parmin);
136 if(Intersector3d.IsDone()) {
137 if(Intersector3d.NbPnt()) {
138 Standard_Integer imin = 1;
139 for (Standard_Integer i = 2; i <= Intersector3d.NbPnt(); i++)
140 if (Intersector3d.WParameter(i) < Intersector3d.WParameter(imin))
142 parmin = Intersector3d.WParameter(imin);
143 aState = Intersector3d.State(imin);
144 aTransition = Intersector3d.Transition(imin);
153 } //end of loop on the whole solid
155 if (aState == TopAbs_IN)
157 if (aTransition == IntCurveSurface_Out) {
158 //-- The line is going from inside the solid to outside
160 myState = 3; //-- IN --
163 else if (aTransition == IntCurveSurface_In) {
164 myState = 4; //-- OUT --
170 } //if (aSE.MoreFace())
171 } //if (aSE.MoreShell())
174 //=======================================================================
177 //=======================================================================
178 void BRepClass3d_SClassifier::Perform(BRepClass3d_SolidExplorer& SolidExplorer,
180 const Standard_Real Tol)
184 if(SolidExplorer.Reject(P)) {
185 myState=3; //-- in ds solid case without face
192 if(SolidExplorer.Reject(P) == Standard_False) {
195 //-- We compute the intersection betwwen the line builded in the Solid Explorer
198 //-- --------------------------------------------------------------------------------
199 //-- Calculate intersection with the face closest to the direction of bounding boxes
200 //-- by priority so that to have the smallest possible parmin.
201 //-- optimization to produce as much as possible rejections with other faces.
202 Standard_Integer iFlag;
205 // Modified by skv - Thu Sep 4 11:22:05 2003 OCC578 Begin
206 // If found line passes through a bound of any face, it means that the line
207 // is not found properly and it is necessary to repeat whole procedure.
208 // That's why the main loop while is added.
209 Standard_Boolean isFaultyLine = Standard_True;
210 Standard_Integer anIndFace = 0;
211 Standard_Real parmin = 0.;
213 while (isFaultyLine) {
214 if (anIndFace == 0) {
215 iFlag = SolidExplorer.Segment(P,L,Par);
217 iFlag = SolidExplorer.OtherSegment(P,L,Par);
220 Standard_Integer aCurInd = SolidExplorer.GetFaceSegmentIndex();
222 if (aCurInd > anIndFace) {
229 // Modified by skv - Thu Sep 4 11:22:10 2003 OCC578 End
233 // iFlag==1 i.e face is Infinite
238 //SolidExplorer.Segment(P,L,Par);
240 //process results from uncorrected shells
242 //if(Par > 1.e+100 && L.Direction().IsParallel(gp_Dir(0.,0.,1.),1.e-8)) {
247 //-- BRepClass3d_Intersector3d Intersector3d;
249 // Modified by skv - Thu Sep 4 13:48:27 2003 OCC578 Begin
250 // Check if the point is ON surface but OUT of the face.
251 // Just skip this face because it is bad for classification.
255 isFaultyLine = Standard_False;
256 // Standard_Real parmin = RealLast();
258 // for(SolidExplorer.InitShell();
259 // SolidExplorer.MoreShell();
260 // SolidExplorer.NextShell()) {
263 for(SolidExplorer.InitShell();
264 SolidExplorer.MoreShell() && !isFaultyLine;
265 SolidExplorer.NextShell()) {
266 // Modified by skv - Thu Sep 4 13:48:27 2003 OCC578 End
268 if(SolidExplorer.RejectShell(L) == Standard_False) {
270 // Modified by skv - Thu Sep 4 13:48:27 2003 OCC578 Begin
271 // for(SolidExplorer.InitFace();
272 // SolidExplorer.MoreFace();
273 // SolidExplorer.NextFace()) {
274 for(SolidExplorer.InitFace();
275 SolidExplorer.MoreFace() && !isFaultyLine;
276 SolidExplorer.NextFace()) {
277 // Modified by skv - Thu Sep 4 13:48:27 2003 OCC578 End
279 if(SolidExplorer.RejectFace(L) == Standard_False) {
281 //-- Intersector3d.Perform(L,Par,Tol,SolidExplorer.CurrentFace());
282 TopoDS_Shape aLocalShape = SolidExplorer.CurrentFace();
283 TopoDS_Face f = TopoDS::Face(aLocalShape);
284 // TopoDS_Face f = TopoDS::Face(SolidExplorer.CurrentFace());
285 IntCurvesFace_Intersector& Intersector3d = SolidExplorer.Intersector(f);
287 // MSV Oct 25, 2001: prolong segment, since there are cases when
288 // the intersector does not find intersection points with the original
289 // segment due to rough triangulation of a parametrized surface
290 Standard_Real addW = Max(10*Tol, 0.01*Par);
291 Standard_Real AddW = addW;
293 Bnd_Box aBoxF = Intersector3d.Bounding();
295 // MSV 23.09.2004: the box must be finite in order to
296 // correctly prolong the segment to its bounds
297 if (!aBoxF.IsVoid() && !aBoxF.IsWhole()) {
298 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
299 aBoxF.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
301 Standard_Real boxaddW = GetAddToParam(L,Par,aBoxF);
302 addW = Max(addW,boxaddW);
305 Standard_Real minW = -AddW;//-addW;
306 Standard_Real maxW = Min(Par*10,Par+addW);//Par+addW;
307 //cout << "range [" << minW << "," << maxW << "]" << endl << endl;
308 Intersector3d.Perform(L,minW,maxW);
309 //Intersector3d.Perform(L,-Tol,Par+10.0*Tol);
310 if(Intersector3d.IsDone()) {
312 for (i=1; i <= Intersector3d.NbPnt(); i++) {
313 if(Abs(Intersector3d.WParameter(i)) < Abs(parmin) - Precision::PConfusion()) {
315 parmin = Intersector3d.WParameter(i);
316 // Modified by skv - Thu Sep 4 12:46:32 2003 OCC578 Begin
317 TopAbs_State aState = Intersector3d.State(i);
318 // Modified by skv - Thu Sep 4 12:46:33 2003 OCC578 End
319 if(Abs(parmin)<=Tol) {
323 // Modified by skv - Thu Sep 4 12:46:32 2003 OCC578 Begin
324 // Treatment of case TopAbs_ON separately.
326 else if(aState==TopAbs_IN) {
327 // Modified by skv - Thu Sep 4 12:46:32 2003 OCC578 End
329 //-- The intersection point between the line and a face F
330 // -- of the solid is in the face F
332 IntCurveSurface_TransitionOnCurve tran = Intersector3d.Transition(i);
333 if (tran == IntCurveSurface_Tangent) {
335 cout<<"*Problem ds BRepClass3d_SClassifier.cxx"<<endl;
337 continue; // ignore this point
339 // if parmin is negative we should reverse transition
341 tran = (tran == IntCurveSurface_Out
342 ? IntCurveSurface_In : IntCurveSurface_Out);
343 if(tran == IntCurveSurface_Out) {
344 //-- The line is going from inside the solid to outside
346 myState = 3; //-- IN --
348 else /* if(tran == IntCurveSurface_In) */ {
349 myState = 4; //-- OUT --
353 // Modified by skv - Thu Sep 4 12:48:50 2003 OCC578 Begin
354 // If the state is TopAbs_ON, it is necessary to chose
355 // another line and to repeat the whole procedure.
356 else if(aState==TopAbs_ON) {
357 isFaultyLine = Standard_True;
361 // Modified by skv - Thu Sep 4 12:48:50 2003 OCC578 End
364 //-- No point has been found by the Intersector3d.
365 //-- Or a Point has been found with a greater parameter.
367 } //-- loop by intersection points
368 } //-- Face has not been rejected
373 } //-- Exploration of the faces
374 } //-- Shell has not been rejected
378 } //-- Exploration of the shells
380 // Modified by skv - Thu Sep 4 11:42:03 2003 OCC578 Begin
381 // The end of main loop.
383 // Modified by skv - Thu Sep 4 11:42:03 2003 OCC578 End
386 //#################################################
387 SolidExplorer.DumpSegment(P,L,parmin,State());
388 //#################################################
391 } //-- Solid has not been rejected
398 TopAbs_State BRepClass3d_SClassifier::State() const {
399 if(myState==2) return(TopAbs_ON);
400 if(myState==4) return(TopAbs_OUT); //--
401 else if(myState==3) return(TopAbs_IN); //--
405 TopoDS_Face BRepClass3d_SClassifier::Face() const {
409 Standard_Boolean BRepClass3d_SClassifier::Rejected() const {
414 Standard_Boolean BRepClass3d_SClassifier::IsOnAFace() const {
419 void BRepClass3d_SClassifier::ForceIn() {
423 void BRepClass3d_SClassifier::ForceOut() {
427 Standard_Real GetAddToParam(const gp_Lin& L,
428 const Standard_Real P,
431 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
432 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
433 Standard_Real x[2] = {aXmin,aXmax}, y[2] = {aYmin,aYmax}, z[2] = {aZmin,aZmax};
434 Standard_Integer i = 0, j = 0, k = 0;
435 Standard_Real Par = P;
436 for(i = 0 ; i < 2; i++) {
437 for(j = 0; j < 2; j++) {
438 for(k = 0; k < 2; k++) {
439 Standard_Real X = fabs(x[i]-L.Location().X());
440 Standard_Real Y = fabs(y[j]-L.Location().Y());
441 Standard_Real Z = fabs(z[k]-L.Location().Z());
442 if(X < 1.e+20 && Y < 1.e+20 && Z < 1.e+20) {
443 gp_Pnt aP(x[i],y[j],z[k]);
444 Standard_Real par = ElCLib::Parameter(L,aP);
455 //=======================================================================
456 //function : FaceNormal
458 //=======================================================================
459 Standard_Boolean FaceNormal (const TopoDS_Face& aF,
460 const Standard_Real U,
461 const Standard_Real V,
465 gp_Vec aD1U, aD1V, aN;
466 Handle(Geom_Surface) aS;
468 aS=BRep_Tool::Surface(aF);
469 aS->D1 (U, V, aPnt, aD1U, aD1V);
470 aN=aD1U.Crossed(aD1V);
471 if (aN.Magnitude() <= gp::Resolution())
472 return Standard_False;
475 aDN.SetXYZ(aN.XYZ());
476 if (aF.Orientation() == TopAbs_REVERSED){
479 return Standard_True;