1 // Created on: 1997-03-04
2 // Created by: Prestataire Xuan PHAM PHU
3 // Copyright (c) 1995-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: eap Mar 25 2002 (occ102,occ227), touch case
20 #include <Precision.hxx>
22 #include <TopAbs_Orientation.hxx>
23 #include <TopAbs_State.hxx>
24 #include <TopTrans_SurfaceTransition.hxx>
26 static Standard_Boolean STATIC_DEFINED = Standard_False;
28 static gp_Dir FUN_nCinsideS(const gp_Dir& tgC, const gp_Dir& ngS)
30 // Give us a curve C on suface S, <parOnC>, a parameter
31 // Purpose : compute normal vector to C, tangent to S at
32 // given point , oriented INSIDE S
33 // <tgC> : geometric tangent at point of <parOnC>
34 // <ngS> : geometric normal at point of <parOnC>
39 #define M_REVERSED(st) (st == TopAbs_REVERSED)
40 #define M_INTERNAL(st) (st == TopAbs_INTERNAL)
41 #define M_UNKNOWN(st) (st == TopAbs_UNKNOWN)
43 static Standard_Integer FUN_OO(const Standard_Integer i)
50 //static Standard_Real FUN_Ang(const gp_Dir& Normref,
51 static Standard_Real FUN_Ang(const gp_Dir& ,
52 const gp_Dir& beafter,
55 const TopAbs_Orientation O)
57 gp_Dir dironF = FUN_nCinsideS(TgC,Norm);
58 if (M_REVERSED(O)) dironF.Reverse();
60 Standard_Real ang = beafter.AngleWithRef(dironF,TgC);
64 static void FUN_getSTA(const Standard_Real Ang, const Standard_Real tola,
65 Standard_Integer& i, Standard_Integer& j)
67 Standard_Real cos = Cos(Ang);
68 Standard_Real sin = Sin(Ang);
69 Standard_Boolean nullcos = Abs(cos) < tola;
70 Standard_Boolean nullsin = Abs(sin) < tola;
72 else i = (cos > 0.) ? 1 : 2;
74 else j = (sin > 0.) ? 1 : 2;
77 /*static void FUN_getSTA(const Standard_Real Ang, const Standard_Real tola,
78 const Standard_Real Curv, const Standard_Real CurvRef,
79 Standard_Integer& i, Standard_Integer& j)
81 // Choosing UV referential (beafter,myNorm).
82 // purpose : computes position boundary face relative to the reference surface
83 // notice : j==0 => j==1 : the boundary face is ABOVE the reference surface
84 // j==2 : the boundary face is UNDER the reference surface
85 // - j==0 : the boundary and the reference objects are tangent-
87 FUN_getSTA(Ang,tola,i,j);
89 Standard_Real diff = Curv - CurvRef;
90 if (Abs(diff) < tola) {STATIC_DEFINED = Standard_False; return;} // nyi FUN_Raise
91 j = (diff < 0.) ? 1 : 2;
95 #define M_Unknown (-100)
97 #define M_Unknown (-100.)
99 #define M_noupdate (0)
100 #define M_updateREF (1)
101 #define M_Ointernal (10)
102 static Standard_Integer FUN_refnearest(const Standard_Real Angref, const TopAbs_Orientation Oriref,
103 const Standard_Real Ang, const TopAbs_Orientation Ori, const Standard_Real tola)
105 Standard_Boolean undef = (Angref == 100.);
106 if (undef) return M_updateREF;
108 Standard_Real cosref = Cos(Angref), cos = Cos(Ang);
109 Standard_Real dcos = Abs(cosref) - Abs(cos);
110 if (Abs(dcos) < tola) {
111 // Analysis for tangent cases : if two boundary faces are same sided
112 // and have tangent normals, if they have opposite orientations
113 // we choose INTERNAL as resulting complex transition (case EXTERNAL
114 // refering to no logical case)
115 if (TopAbs::Complement(Ori) == Oriref) return M_Ointernal;
116 else return (Standard_Integer ) M_Unknown; // nyi FUN_RAISE
118 Standard_Integer updateref = (dcos > 0.)? M_noupdate : M_updateREF;
122 //=======================================================================
123 //function : FUN_refnearest
125 //=======================================================================
127 static Standard_Integer FUN_refnearest(const Standard_Integer i,
128 const Standard_Integer j,
129 const Standard_Real CurvSref,
130 const Standard_Real Angref,
131 const TopAbs_Orientation Oriref,
132 const Standard_Real Curvref,
133 const Standard_Real Ang,
134 const TopAbs_Orientation Ori,
135 const Standard_Real Curv,
136 const Standard_Real tola,
137 Standard_Boolean & TouchFlag) // eap Mar 25 2002
139 Standard_Boolean iisj = (i == j);
140 Standard_Real abscos = Abs(Cos(Ang));
141 Standard_Boolean i0 = (Abs(1. - abscos) < tola);
142 Standard_Boolean j0 = (abscos < tola);
143 Standard_Boolean nullcurv = (Curv == 0.);
144 Standard_Boolean curvpos = (Curv > tola);
145 Standard_Boolean curvneg = (Curv < -tola);
146 Standard_Boolean nullcsref = (CurvSref == 0.);
148 Standard_Boolean undef = (Angref == 100.);
151 if (iisj && curvneg) return M_noupdate;
152 if (!iisj && curvpos) return M_noupdate;
155 if (!nullcsref && (j == 1) && iisj && (curvpos || nullcurv)) return M_updateREF;
156 if (!nullcsref && (j == 1) && !iisj && (curvneg || nullcurv)) return M_updateREF;
158 if (iisj && curvpos) return M_noupdate;
159 if (!iisj && curvneg) return M_noupdate;
164 Standard_Real cosref = Cos(Angref), cos = Cos(Ang);
165 Standard_Real dcos = Abs(cosref) - Abs(cos); Standard_Boolean samecos = Abs(dcos) < tola;
167 // Analysis for tangent cases : if two boundary faces are same sided
168 // and have sma dironF.
170 if (Abs(Curvref - Curv) < 1.e-4) {
171 if (TopAbs::Complement(Ori) == Oriref) return M_Ointernal;
172 else return (Standard_Integer ) M_Unknown; // nyi FUN_RAISE
175 Standard_Boolean noupdate = Standard_False;
176 if (iisj && (Curvref > Curv)) noupdate = Standard_True;
177 if (!iisj && (Curvref < Curv)) noupdate = Standard_True;
178 Standard_Integer updateref = noupdate ? M_noupdate : M_updateREF;
179 if (!j0) return updateref;
181 if (!noupdate && !nullcsref) {
182 // check for (j==1) the face is ABOVE Sref
183 // check for (j==2) the face is BELOW Sref
184 if ((j == 2) && (Abs(Curv) < CurvSref)) updateref = M_noupdate;
185 if ((j == 1) && (Abs(Curv) > CurvSref)) updateref = M_noupdate;
190 Standard_Integer updateref = (dcos > 0.)? M_noupdate : M_updateREF;
191 if (Oriref != Ori) TouchFlag = Standard_True; // eap Mar 25 2002
196 // ============================================================
198 // ============================================================
200 TopTrans_SurfaceTransition::TopTrans_SurfaceTransition()
205 myTouchFlag(Standard_False)
207 STATIC_DEFINED = Standard_False;
210 void TopTrans_SurfaceTransition::Reset(const gp_Dir& Tgt,
212 const gp_Dir& MaxD,const gp_Dir& MinD,
213 const Standard_Real MaxCurv,const Standard_Real MinCurv)
215 STATIC_DEFINED = Standard_True;
217 Standard_Real tola = Precision::Angular();
218 Standard_Boolean curismax = (Abs(MaxD.Dot(myTgt)) < tola);
219 Standard_Boolean curismin = (Abs(MinD.Dot(myTgt)) < tola);
221 if ((Abs(MaxCurv) < tola) && (Abs(MinCurv) < tola)) {
226 if (!curismax && !curismin) {
227 // In the plane normal to <myTgt>, we see the boundary face as
229 // NYIxpu : compute the curvature of the curve if not MaxCurv
232 STATIC_DEFINED = Standard_False;
236 if (curismax) myCurvRef = Abs(MaxCurv);
237 if (curismin) myCurvRef = Abs(MinCurv);
238 if (myCurvRef < tola) myCurvRef = 0.;
240 // ============================================================
241 // recall : <Norm> is oriented OUTSIDE the "geometric matter" described
243 // - if (myCurvRef != 0.) Sref is UNDER axis (sin = 0)
244 // referential (beafter,myNorm,myTgt) -
245 // ============================================================
247 // beafter oriented (before, after) the intersection on the reference surface.
251 for (Standard_Integer i = 1; i <=2; i++)
252 for (Standard_Integer j = 1; j <=2; j++)
255 myTouchFlag = Standard_False; // eap Mar 25 2002
258 void TopTrans_SurfaceTransition::Reset(const gp_Dir& Tgt,
261 STATIC_DEFINED = Standard_True;
263 // beafter oriented (before, after) the intersection on the reference surface.
267 for (Standard_Integer i = 1; i <=2; i++)
268 for (Standard_Integer j = 1; j <=2; j++)
272 myTouchFlag = Standard_False; // eap Mar 25 2002
275 void TopTrans_SurfaceTransition::Compare
276 //(const Standard_Real Tole,
277 (const Standard_Real ,
279 const gp_Dir& MaxD,const gp_Dir& MinD,
280 const Standard_Real MaxCurv,const Standard_Real MinCurv,
281 const TopAbs_Orientation S,
282 const TopAbs_Orientation O)
284 if (!STATIC_DEFINED) return;
286 Standard_Real Curv=0.;
288 Standard_Real tola = Precision::Angular();
289 Standard_Boolean curismax = (Abs(MaxD.Dot(myTgt)) < tola);
290 Standard_Boolean curismin = (Abs(MinD.Dot(myTgt)) < tola);
291 if (!curismax && !curismin) {
292 // In the plane normal to <myTgt>, we see the boundary face as
294 // NYIxpu : compute the curvature of the curve if not MaxCurv
297 STATIC_DEFINED = Standard_False;
300 if (curismax) Curv = Abs(MaxCurv);
301 if (curismin) Curv = Abs(MinCurv);
302 if (myCurvRef < tola) Curv = 0.;
303 gp_Dir dironF = FUN_nCinsideS(myTgt,Norm);
304 Standard_Real prod = (dironF^Norm).Dot(myTgt);
305 if (prod < 0.) Curv = -Curv;
309 Ang = ::FUN_Ang(myNorm,beafter,myTgt,Norm,O);
311 Standard_Integer i,j;
313 // i = 0,1,2 : cos = 0,>0,<0
314 // j = 0,1,2 : sin = 0,>0,<0
315 ::FUN_getSTA(Ang,tola,i,j);
319 Standard_Integer kmax = M_INTERNAL(O) ? 2 : 1;
320 for (Standard_Integer k=1; k <=kmax; k++) {
322 // get the opposite Ang
326 Standard_Boolean i0 = (i == 0), j0 = (j == 0);
327 Standard_Integer nmax = (i0 || j0) ? 2 : 1;
328 for (Standard_Integer n=1; n<=nmax; n++) {
332 // if (curvref == 0.) :
333 // Standard_Boolean iisj = (i == j);
334 // Standard_Boolean Curvpos = (Curv > 0.);
335 // if ((Curv != 0.) && i0) {
336 // if (iisj && !Curvpos) continue;
337 // if (!iisj && Curvpos) continue;
339 // if ((Curv != 0.) && j0) {
340 // if (iisj && Curvpos) continue;
341 // if (!iisj && !Curvpos) continue;
344 Standard_Integer refn = ::FUN_refnearest(i,j,myCurvRef,myAng(i,j),myOri(i,j),myCurv(i,j),
345 Ang,/*O*/S,Curv,tola,myTouchFlag); // eap Mar 25 2002
346 if (refn == M_Unknown) {STATIC_DEFINED = Standard_False; return;}
349 myOri(i,j) = (refn == M_Ointernal) ? TopAbs_INTERNAL : S;
357 void TopTrans_SurfaceTransition::Compare
358 //(const Standard_Real Tole,
359 (const Standard_Real ,
361 const TopAbs_Orientation S,
362 const TopAbs_Orientation O)
364 if (!STATIC_DEFINED) return;
366 // oriented Ang(beafter,dironF),
367 // dironF normal to the curve, oriented INSIDE F, the added oriented support
368 Standard_Real Ang = ::FUN_Ang(myNorm,beafter,myTgt,Norm,O);
369 Standard_Real tola = Precision::Angular(); // nyi in arg
371 // i = 0,1,2 : cos = 0,>0,<0
372 // j = 0,1,2 : sin = 0,>0,<0
373 Standard_Integer i,j; ::FUN_getSTA(Ang,tola,i,j);
375 Standard_Integer kmax = M_INTERNAL(O) ? 2 : 1;
376 for (Standard_Integer k=1; k <=kmax; k++) {
378 // get the opposite Ang
383 Standard_Boolean i0 = (i == 0), j0 = (j == 0);
384 Standard_Integer nmax = (i0 || j0) ? 2 : 1;
385 for (Standard_Integer n=1; n<=nmax; n++) {
389 Standard_Integer refn = ::FUN_refnearest(myAng(i,j),myOri(i,j),
390 Ang,/*O*/S,tola); // eap
391 if (refn == M_Unknown) {STATIC_DEFINED = Standard_False; return;}
395 myOri(i,j) = (refn == M_Ointernal) ? TopAbs_INTERNAL : S;
403 static TopAbs_State FUN_getstate(const TColStd_Array2OfReal& Ang,
404 const TopTrans_Array2OfOrientation& Ori,
405 const Standard_Integer iSTA,
406 const Standard_Integer iINDEX)
408 if (!STATIC_DEFINED) return TopAbs_UNKNOWN;
410 Standard_Real a1 = Ang(iSTA,1), a2 = Ang(iSTA,2);
411 Standard_Boolean undef1 = (a1 == 100.), undef2 = (a2 == 100.);
412 Standard_Boolean undef = undef1 && undef2;
413 if (undef) return TopAbs_UNKNOWN;
415 if (undef1 || undef2) {
416 Standard_Integer jok = undef1 ? 2 : 1;
417 TopAbs_Orientation o = Ori(iSTA,jok);
418 TopAbs_State st = (iINDEX == BEFORE) ? TopTrans_SurfaceTransition::GetBefore(o) :
419 TopTrans_SurfaceTransition::GetAfter(o);
423 TopAbs_Orientation o1 = Ori(iSTA,1), o2 = Ori(iSTA,2);
424 TopAbs_State st1 = (iINDEX == BEFORE) ? TopTrans_SurfaceTransition::GetBefore(o1) :
425 TopTrans_SurfaceTransition::GetAfter(o1);
426 TopAbs_State st2 = (iINDEX == BEFORE) ? TopTrans_SurfaceTransition::GetBefore(o2) :
427 TopTrans_SurfaceTransition::GetAfter(o2);
428 if (st1 != st2) return TopAbs_UNKNOWN; // Incoherent data
433 TopAbs_State TopTrans_SurfaceTransition::StateBefore() const
435 if (!STATIC_DEFINED) return TopAbs_UNKNOWN;
437 // we take the state before of before orientations
438 TopAbs_State before = ::FUN_getstate(myAng,myOri,BEFORE,BEFORE);
439 if (M_UNKNOWN(before)) {
440 // looking back in before for defined states
441 // we take the state before of after orientations
442 before = ::FUN_getstate(myAng,myOri,AFTER,BEFORE);
445 if (before == TopAbs_OUT) before = TopAbs_IN;
446 else if (before == TopAbs_IN) before = TopAbs_OUT;
452 TopAbs_State TopTrans_SurfaceTransition::StateAfter() const
454 if (!STATIC_DEFINED) return TopAbs_UNKNOWN;
456 TopAbs_State after = ::FUN_getstate(myAng,myOri,AFTER,AFTER);
457 if (M_UNKNOWN(after)) {
458 // looking back in before for defined states
459 after = ::FUN_getstate(myAng,myOri,BEFORE,AFTER);
462 if (after == TopAbs_OUT) after = TopAbs_IN;
463 else if (after == TopAbs_IN) after = TopAbs_OUT;
469 TopAbs_State TopTrans_SurfaceTransition::GetBefore
470 (const TopAbs_Orientation Tran)
472 if (!STATIC_DEFINED) return TopAbs_UNKNOWN;
476 case TopAbs_FORWARD :
477 case TopAbs_EXTERNAL :
479 case TopAbs_REVERSED :
480 case TopAbs_INTERNAL :
486 TopAbs_State TopTrans_SurfaceTransition::GetAfter
487 (const TopAbs_Orientation Tran)
489 if (!STATIC_DEFINED) return TopAbs_UNKNOWN;
493 case TopAbs_FORWARD :
494 case TopAbs_INTERNAL :
496 case TopAbs_REVERSED :
497 case TopAbs_EXTERNAL :