1 // Created on: 1998-10-06
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16 
17 #include <Geom_Curve.hxx>
18 #include <Geom_TrimmedCurve.hxx>
19 #include <Geom_Surface.hxx>
20 #include <GeomAdaptor_Surface.hxx>
21 #include <Bnd_Box2d.hxx>
22 #include <Bnd_Box.hxx>
23 #include <BRepBndLib.hxx>
24 #include <BndLib_Add2dCurve.hxx>
25 #include <BRepAdaptor_Surface.hxx>
26 #include <gp_Pnt2d.hxx>
27 #include <gp_Pnt.hxx>
28 #include <gp_Dir.hxx>
29 #include <gp_Dir2d.hxx>
30 #include <gp_Vec.hxx>
31 #include <gp_Pln.hxx>
32 #include <gp_Cylinder.hxx>
33 #include <gp_Cone.hxx>
34 #include <gp_Sphere.hxx>
35 #include <gp_Torus.hxx>
36 #include <TopExp.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <TopoDS.hxx>
39 #include <TopoDS_Wire.hxx>
40 #include <BRep_Tool.hxx>
41 #include <BRep_Builder.hxx>
42 #include <ElCLib.hxx>
43 #include <Precision.hxx>
44 #include <TColStd_Array1OfReal.hxx>
45 #include <TopOpeBRepTool_CurveTool.hxx>
46 #include <TopOpeBRepTool_ShapeTool.hxx>
47 #include <TopOpeBRepTool_2d.hxx>
48 
49 #include <TopOpeBRepTool_GEOMETRY.hxx>
50 #include <TopOpeBRepTool_PROJECT.hxx>
51 #include <TopOpeBRepTool_TOPOLOGY.hxx>
52 #include <TopOpeBRepTool_TOOL.hxx>
53 
54 //---------------------------------------------
55 #define  M_FORWARD(ori) (ori == TopAbs_FORWARD)
56 #define M_REVERSED(ori) (ori == TopAbs_REVERSED)
57 #define M_INTERNAL(ori) (ori == TopAbs_INTERNAL)
58 #define M_EXTERNAL(ori) (ori == TopAbs_EXTERNAL)
59 // ----------------------------------------------------------------------
60 
61 // ----------------------------------------------------------------------
FUN_tool_tolUV(const TopoDS_Face & F,Standard_Real & tolu,Standard_Real & tolv)62 Standard_EXPORT void FUN_tool_tolUV(const TopoDS_Face& F,Standard_Real& tolu,Standard_Real& tolv)
63 {
64   Standard_Real tolF = BRep_Tool::Tolerance(TopoDS::Face(F));
65   BRepAdaptor_Surface BS(TopoDS::Face(F));
66   tolu = BS.UResolution(tolF);
67   tolv = BS.VResolution(tolF);
68 }
69 
70 // ----------------------------------------------------------------------
FUN_tool_direct(const TopoDS_Face & F,Standard_Boolean & direct)71 Standard_EXPORT Standard_Boolean FUN_tool_direct(const TopoDS_Face& F,Standard_Boolean& direct)
72 {
73   BRepAdaptor_Surface BS(TopoDS::Face(F));
74   GeomAbs_SurfaceType ST = BS.GetType();
75   Standard_Boolean plane = (ST == GeomAbs_Plane);
76   Standard_Boolean cyl   = (ST == GeomAbs_Cylinder);
77   Standard_Boolean cone  = (ST == GeomAbs_Cone);
78   Standard_Boolean sphe  = (ST == GeomAbs_Sphere);
79   Standard_Boolean torus  = (ST == GeomAbs_Torus);
80   if (plane) {const gp_Pln& plpl = BS.Plane(); direct = plpl.Direct();}
81   if (cyl)   {const gp_Cylinder& cycy = BS.Cylinder(); direct = cycy.Direct();}
82   if (cone)  {const gp_Cone& coco = BS.Cone(); direct = coco.Direct();}
83   if (sphe)  {const gp_Sphere& spsp = BS.Sphere(); direct = spsp.Direct();}
84   if (torus) {const gp_Torus& toto = BS.Torus(); direct = toto.Direct();}
85   Standard_Boolean ok = plane || cyl || cone || sphe || torus;
86   return ok;
87 }
88 
89 /*// ----------------------------------------------------------------------
90 Standard_EXPORT Standard_Boolean FUN_tool_IsUViso(const TopoDS_Shape& E,const TopoDS_Shape& F,
91 				     Standard_Boolean& isoU,Standard_Boolean& isoV,
92 				     gp_Dir2d& d2d,gp_Pnt2d& o2d)
93 {
94   isoU = isoV = Standard_False; Standard_Real f,l,tol;
95   Handle(Geom2d_Curve) PC =
96     FC2D_CurveOnSurface(TopoDS::Edge(E),TopoDS::Face(F),f,l,tol);
97   if (PC.IsNull()) return Standard_False;
98   Standard_Boolean iso = FUN_tool_IsUViso(PC,isoU,isoV,d2d,o2d);
99   return iso;
100 }*/
101 
102 // ----------------------------------------------------------------------
FUN_tool_geombounds(const TopoDS_Face & F,Standard_Real & u1,Standard_Real & u2,Standard_Real & v1,Standard_Real & v2)103 Standard_EXPORT Standard_Boolean FUN_tool_geombounds(const TopoDS_Face& F,
104 					Standard_Real& u1,Standard_Real& u2,Standard_Real& v1,Standard_Real& v2)
105 {
106   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F);
107   if (S.IsNull()) return Standard_False;
108   S->Bounds(u1,u2,v1,v2);
109   return Standard_True;
110 }
111 
112 // ----------------------------------------------------------------------
FUN_tool_bounds(const TopoDS_Shape & Sh,Standard_Real & u1,Standard_Real & u2,Standard_Real & v1,Standard_Real &)113 Standard_EXPORT Standard_Boolean FUN_tool_bounds(const TopoDS_Shape& Sh,
114 				                 Standard_Real& u1,
115 				                 Standard_Real& u2,
116 				                 Standard_Real& v1,
117 //				                 Standard_Real& v2)
118 				                 Standard_Real& )
119 {
120   Bnd_Box2d B2d;
121   const TopoDS_Face& F = TopoDS::Face(Sh);
122   TopExp_Explorer ex(F,TopAbs_WIRE);
123   for (; ex.More(); ex.Next()){
124     const TopoDS_Wire W = TopoDS::Wire(ex.Current());
125     Bnd_Box2d newB2d; FUN_tool_mkBnd2d(W,F,newB2d);
126     B2d.Add(newB2d);
127   }
128   B2d.Get(u1,v1,u2,v1);
129   return Standard_True;
130 }
131 
132 // ----------------------------------------------------------------------
FUN_tool_isobounds(const TopoDS_Shape & Sh,Standard_Real & u1,Standard_Real & u2,Standard_Real & v1,Standard_Real & v2)133 Standard_EXPORT Standard_Boolean FUN_tool_isobounds(const TopoDS_Shape& Sh,
134 				       Standard_Real& u1,Standard_Real& u2,Standard_Real& v1,Standard_Real& v2)
135 {
136   const TopoDS_Face& F = TopoDS::Face(Sh);
137   u1 = v1 = 1.e7;  u2 = v2 = -1.e7;
138 
139   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F);
140   if (S.IsNull()) return Standard_False;
141 
142   Standard_Boolean uclosed,vclosed; Standard_Real uperiod,vperiod;
143 
144 //  Standard_Boolean uvclosed =
145 
146   FUN_tool_closedS(F,uclosed,uperiod,vclosed,vperiod);
147 
148 //  Standard_Real uf,ul,vf,vl; S->Bounds(uf,ul,vf,vl);
149 
150 //  if (!uvclosed) {
151 //    u1 = uf; v1 = vf; u2 = ul; v2 = vl;
152 //    return Standard_True;
153 //  }
154   TopExp_Explorer ex(F,TopAbs_EDGE);
155   for (; ex.More(); ex.Next()){
156     const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
157     Standard_Real f,l,tol; Handle(Geom2d_Curve) PC = FC2D_CurveOnSurface(E,F,f,l,tol);
158     if (PC.IsNull()) return Standard_False;
159 
160     Standard_Boolean isou,isov; gp_Pnt2d o2d; gp_Dir2d d2d;
161     Standard_Boolean isouv = TopOpeBRepTool_TOOL::UVISO(PC,isou,isov,d2d,o2d);
162 
163     if (isouv) {
164       gp_Pnt2d p2df = PC->Value(f); gp_Pnt2d p2dl = PC->Value(l);
165       u1 = Min(p2df.X(),u1); u2 = Max(p2df.X(),u2); v1 = Min(p2df.Y(),v1); v2 = Max(p2df.Y(),v2);
166       u1 = Min(p2dl.X(),u1); u2 = Max(p2dl.X(),u2); v1 = Min(p2dl.Y(),v1); v2 = Max(p2dl.Y(),v2);
167     }
168     if (!isouv) return Standard_False;
169     // ====================
170   }
171   return Standard_True;
172 }
173 
174 // ----------------------------------------------------------------------
FUN_tool_outbounds(const TopoDS_Shape & Sh,Standard_Real & u1,Standard_Real & u2,Standard_Real & v1,Standard_Real & v2,Standard_Boolean & outbounds)175 Standard_EXPORT Standard_Boolean FUN_tool_outbounds(const TopoDS_Shape& Sh,
176 				       Standard_Real& u1,Standard_Real& u2,Standard_Real& v1,Standard_Real& v2,Standard_Boolean& outbounds)
177 {
178   Handle(Geom_Surface) S = TopOpeBRepTool_ShapeTool::BASISSURFACE(TopoDS::Face(Sh));
179   if (S.IsNull()) return Standard_False;
180   Standard_Real uf,ul,vf,vl; S->Bounds(uf,ul,vf,vl);
181 
182   outbounds = Standard_False;
183   Standard_Boolean ok = FUN_tool_bounds(Sh,u1,u2,v1,v2);
184   if (!ok) return Standard_False;
185 
186   const TopoDS_Face& F = TopoDS::Face(Sh);
187   Standard_Boolean uclosed,vclosed; Standard_Real uperiod,vperiod;
188   FUN_tool_closedS(F,uclosed,uperiod,vclosed,vperiod);
189   Standard_Real tolp = 1.e-6;
190 
191   if (uclosed) {
192     Standard_Real dd = u2-u1;
193     if (dd > uperiod + tolp) {u1 = uf; v1 = vf; u2 = ul; v2 = vl; outbounds=Standard_True;}
194   }
195   if (vclosed) {
196     Standard_Real dd = v2-v1;
197     if (dd > vperiod + tolp) {u1 = uf; v1 = vf; u2 = ul; v2 = vl; outbounds=Standard_True;}
198   }
199   return Standard_True;
200 }
201 
202 // ----------------------------------------------------------------------
FUN_tool_PinC(const gp_Pnt & P,const BRepAdaptor_Curve & BAC,const Standard_Real pmin,const Standard_Real pmax,const Standard_Real tol)203 Standard_EXPORT Standard_Boolean FUN_tool_PinC(const gp_Pnt& P,
204 				  const BRepAdaptor_Curve& BAC,
205 				  const Standard_Real pmin,const Standard_Real pmax,
206 				  const Standard_Real tol)
207 {
208   // returns true if <P> is on <C> under a given tolerance <tol>
209   Standard_Boolean PinC = Standard_False;
210   Extrema_ExtPC ponc(P,BAC,pmin,pmax);
211   Standard_Boolean ok = ponc.IsDone();
212   if ( ok ) {
213     if ( ponc.NbExt() ) {
214       Standard_Integer i = FUN_tool_getindex(ponc);
215       Standard_Real d2 = ponc.SquareDistance(i);
216       PinC = (d2 <= tol * tol);
217     }
218   }
219   return PinC;
220 }
221 
222 // ----------------------------------------------------------------------
FUN_tool_PinC(const gp_Pnt & P,const BRepAdaptor_Curve & BAC,const Standard_Real tol)223 Standard_EXPORT Standard_Boolean FUN_tool_PinC(const gp_Pnt& P,
224 				  const BRepAdaptor_Curve& BAC,
225 				  const Standard_Real tol)
226 {
227   // returns true if <P> is on <C> under a given tolerance <tol>
228   Standard_Boolean PinC = Standard_False;
229   Standard_Real pmin = BAC.FirstParameter();
230   Standard_Real pmax = BAC.LastParameter();
231   PinC = FUN_tool_PinC(P,BAC,pmin,pmax,tol);
232   return PinC;
233 }
234 
235 // ----------------------------------------------------------------------
FUN_tool_value(const Standard_Real par,const TopoDS_Edge & E,gp_Pnt & P)236 Standard_EXPORT Standard_Boolean FUN_tool_value(const Standard_Real par,const TopoDS_Edge& E,gp_Pnt& P)
237 {
238   BRepAdaptor_Curve BAC(E);
239   Standard_Real f = BAC.FirstParameter();
240   Standard_Real l = BAC.LastParameter();
241   Standard_Boolean ok = (f <= par) && (par <= l);
242   if (!ok) return Standard_False;
243   P = BAC.Value(par);
244   return Standard_True;
245 }
246 
247 // ----------------------------------------------------------------------
FUN_tool_value(const gp_Pnt2d & UV,const TopoDS_Face & F,gp_Pnt & P)248 Standard_EXPORT Standard_Boolean FUN_tool_value(const gp_Pnt2d& UV,const TopoDS_Face& F,gp_Pnt& P)
249 {
250   BRepAdaptor_Surface BS(F);
251   P = BS.Value(UV.X(),UV.Y());
252   return Standard_True;
253 }
254 
255 // ----------------------------------------------------------------------
FUN_tool_staPinE(const gp_Pnt & P,const TopoDS_Edge & E,const Standard_Real tol)256 Standard_EXPORT TopAbs_State FUN_tool_staPinE(const gp_Pnt& P,const TopoDS_Edge& E,const Standard_Real tol)
257 {
258   // project point on curve,
259   // if projection fails,returns UNKNOWN.
260   // finds a point <pnear> on edge <E> / d(<pnear>,<P>) < tol
261   //   => returns IN
262   // else,returns OUT.
263   TopAbs_State sta = TopAbs_UNKNOWN;
264   BRepAdaptor_Curve BAC(E);
265   Extrema_ExtPC ProjonBAC(P,BAC);
266   if (ProjonBAC.IsDone() && ProjonBAC.NbExt()>0) {
267     Standard_Integer i = FUN_tool_getindex(ProjonBAC);
268     gp_Pnt Pnear = ProjonBAC.Point(i).Value();
269     Standard_Real d = Pnear.Distance(P);
270     sta = (d < tol)? TopAbs_IN: TopAbs_OUT;
271   }
272   return sta;
273 }
274 // ----------------------------------------------------------------------
FUN_tool_staPinE(const gp_Pnt & P,const TopoDS_Edge & E)275 Standard_EXPORT TopAbs_State FUN_tool_staPinE(const gp_Pnt& P,const TopoDS_Edge& E)
276 {
277 //  Standard_Real tol = Precision::Confusion()*10.;
278   Standard_Real tol3d = BRep_Tool::Tolerance(E)*1.e2;//KKKKK a revoir xpu(CTS21118,f14ou,GI13)
279   TopAbs_State sta = FUN_tool_staPinE(P,E,tol3d);
280   return sta;
281 }
282 
283 // ----------------------------------------------------------------------
284 //  subshape's orientation :
285 //    - vertex orientation in edge
286 //    - edge's orientation in face.Oriented(FORWARD)
287 // ----------------------------------------------------------------------
288 
289 #define FIRST   (1)
290 #define LAST    (2)
291 #define CLOSING (3)
292 
293 // ----------------------------------------------------------------------
FUN_tool_orientVinE(const TopoDS_Vertex & v,const TopoDS_Edge & e)294 Standard_EXPORT Standard_Integer FUN_tool_orientVinE(const TopoDS_Vertex& v,const TopoDS_Edge& e)
295 {
296   Standard_Integer result = 0;
297   TopoDS_Vertex vf,vl; TopExp::Vertices(e,vf,vl);
298   Standard_Boolean visf = v.IsSame(vf);
299   Standard_Boolean visl = v.IsSame(vl);
300   if (visf) result = FIRST;
301   if (visl) result = LAST;
302   if (visf && visl) result = CLOSING;
303   return result;
304 }
305 
306 // ----------------------------------------------------------------------
FUN_tool_orientEinF(const TopoDS_Edge & E,const TopoDS_Face & F,TopAbs_Orientation & oriEinF)307 Standard_EXPORT Standard_Boolean FUN_tool_orientEinF(const TopoDS_Edge& E,const TopoDS_Face& F,TopAbs_Orientation& oriEinF)
308 {
309   oriEinF = TopAbs_FORWARD;
310   TopExp_Explorer e(F,TopAbs_EDGE);
311   for (;e.More();e.Next()) {
312     const TopoDS_Shape& EF = e.Current();
313     if (EF.IsSame(E)) {
314       oriEinF=EF.Orientation();
315       break;
316     }
317   }
318   Standard_Boolean ok=e.More();
319   return ok;
320 }
321 
322 // ----------------------------------------------------------------------
FUN_tool_orientEinFFORWARD(const TopoDS_Edge & E,const TopoDS_Face & F,TopAbs_Orientation & oriEinF)323 Standard_EXPORT Standard_Boolean FUN_tool_orientEinFFORWARD(const TopoDS_Edge& E,const TopoDS_Face& F,TopAbs_Orientation& oriEinF)
324 {
325   // <oriEinF> : dummy for closing edge <E> of <F>.
326   // returns false if <E> is not bound of <F>
327   // else,<oriEinF> = orientation of the edge in <F> oriented FORWARD.
328   TopoDS_Shape aLocalShape = F.Oriented(TopAbs_FORWARD);
329   TopoDS_Face FF = TopoDS::Face(aLocalShape);
330 //  TopoDS_Face FF = TopoDS::Face(F.Oriented(TopAbs_FORWARD));
331   oriEinF = TopAbs_EXTERNAL;
332   TopAbs_Orientation reso; Standard_Boolean ok = ::FUN_tool_orientEinF(E,FF,reso);
333   if (ok) oriEinF = reso;
334   return ok;
335 }
336 
337 // ----------------------------------------------------------------------
FUN_tool_EboundF(const TopoDS_Edge & E,const TopoDS_Face & F)338 Standard_EXPORT Standard_Boolean FUN_tool_EboundF(const TopoDS_Edge& E,const TopoDS_Face& F)
339 {
340   TopAbs_Orientation ori; Standard_Boolean ok = FUN_tool_orientEinFFORWARD(E,F,ori);
341   if (!ok) return Standard_False;
342   Standard_Boolean closingE = BRep_Tool::IsClosed(E,F);
343   if (closingE) return Standard_True;
344   Standard_Boolean notbound = (ori == TopAbs_INTERNAL) || (ori == TopAbs_EXTERNAL);
345   return notbound;
346 }
347 
348 // ----------------------------------------------------------------------
FUN_tool_nggeomF(const gp_Pnt2d & p2d,const TopoDS_Face & F)349 Standard_EXPORT gp_Vec FUN_tool_nggeomF(const gp_Pnt2d& p2d,const TopoDS_Face& F)
350 {
351   Handle(Geom_Surface) S = BRep_Tool::Surface(F);
352   gp_Vec ngF(FUN_tool_ngS(p2d,S));
353   return ngF;
354 }
355 
356 // ----------------------------------------------------------------------
FUN_tool_nggeomF(const Standard_Real & paronE,const TopoDS_Edge & E,const TopoDS_Face & F,gp_Vec & nggeomF,const Standard_Real tol)357 Standard_EXPORT Standard_Boolean FUN_tool_nggeomF(const Standard_Real& paronE,const TopoDS_Edge& E,const TopoDS_Face& F,
358 				     gp_Vec& nggeomF,const Standard_Real tol)
359 {
360   // <p2d> :
361   Standard_Real f,l; gp_Pnt2d p2d;
362   Standard_Boolean project = Standard_True;
363   TopAbs_Orientation oef;
364   Standard_Boolean edonfa = FUN_tool_orientEinFFORWARD(E,F,oef);
365   if (edonfa) {
366     Standard_Real ttol;
367     Handle(Geom2d_Curve) c2d = FC2D_CurveOnSurface(E,F,f,l,ttol);
368     project = c2d.IsNull();
369     if (!project)
370       p2d = c2d->Value(paronE);
371   }
372   if (project) {
373     BRepAdaptor_Curve BC(E);
374     gp_Pnt p3d = BC.Value(paronE);
375     Standard_Real d;  Standard_Boolean ok = FUN_tool_projPonF(p3d,F,p2d,d);
376 
377     //modified by NIZHNY-MZV  Wed Dec  1 13:55:08 1999
378     //if !ok try to compute new pcurve
379     if(!ok) {
380       Standard_Real ttol;
381       Handle(Geom2d_Curve) c2d = FC2D_CurveOnSurface(E,F,f,l,ttol);
382       ok = !c2d.IsNull();
383       if (ok)
384 	p2d = c2d->Value(paronE);
385     }
386     if (!ok) return Standard_False;
387     //modified by NIZHNY-MZV  Wed Dec  1 13:56:14 1999
388     //xpu010698
389     gp_Pnt p3duv; FUN_tool_value(p2d,F,p3duv);
390     Standard_Real dd = p3duv.Distance(p3d);
391     if (dd > tol) return Standard_False;
392     //xpu010698
393   }
394   nggeomF = FUN_tool_nggeomF(p2d,F);
395   return Standard_True;
396 }
397 
398 // ----------------------------------------------------------------------
FUN_tool_nggeomF(const Standard_Real & paronE,const TopoDS_Edge & E,const TopoDS_Face & F,gp_Vec & nggeomF)399 Standard_EXPORT Standard_Boolean FUN_tool_nggeomF(const Standard_Real& paronE,const TopoDS_Edge& E,const TopoDS_Face& F,
400 				     gp_Vec& nggeomF)
401 {
402   Standard_Real tol3d = BRep_Tool::Tolerance(F);
403   tol3d *= 1.e2;
404   Standard_Boolean ok = FUN_tool_nggeomF(paronE,E,F,nggeomF,tol3d);
405   return ok;
406 }
407 
408 // ----------------------------------------------------------------------
FUN_tool_EtgF(const Standard_Real & paronE,const TopoDS_Edge & E,const gp_Pnt2d & p2d,const TopoDS_Face & F,const Standard_Real tola)409 Standard_EXPORT Standard_Boolean FUN_tool_EtgF(const Standard_Real& paronE,const TopoDS_Edge& E,
410 				  const gp_Pnt2d& p2d,const TopoDS_Face& F,
411 				  const Standard_Real tola)
412 {
413   gp_Vec tgE; Standard_Boolean ok = TopOpeBRepTool_TOOL::TggeomE(paronE,E,tgE);
414   if (!ok) return Standard_False;//NYIRAISE
415 
416   gp_Vec ngF = FUN_tool_nggeomF(p2d,F);
417   Standard_Real prod = tgE.Dot(ngF);
418   Standard_Boolean tgt = Abs(prod) < tola;
419   return tgt;
420 }
421 
422 // ----------------------------------------------------------------------
FUN_tool_EtgOOE(const Standard_Real & paronE,const TopoDS_Edge & E,const Standard_Real & paronOOE,const TopoDS_Edge & OOE,const Standard_Real tola)423 Standard_EXPORT Standard_Boolean FUN_tool_EtgOOE(const Standard_Real& paronE,const TopoDS_Edge& E,
424 				    const Standard_Real& paronOOE,const TopoDS_Edge& OOE,
425 				    const Standard_Real  tola)
426 {
427   gp_Vec tgOOE; Standard_Boolean ok = TopOpeBRepTool_TOOL::TggeomE(paronOOE,OOE,tgOOE);
428   if (!ok) return Standard_False;//NYIRAISE
429   gp_Vec tgE;       ok = TopOpeBRepTool_TOOL::TggeomE(paronE,E,tgE);
430   if (!ok) return Standard_False;//NYIRAISE
431   Standard_Real prod = tgOOE.Dot(tgE);
432   Standard_Boolean tg = (Abs(1- Abs(prod)) < tola);
433   return tg;
434 }
435 
436 // ----------------------------------------------------------------------
FUN_nearestISO(const TopoDS_Face & F,const Standard_Real xpar,const Standard_Boolean isoU,Standard_Real & xinf,Standard_Real & xsup)437 Standard_EXPORT Standard_Boolean FUN_nearestISO(const TopoDS_Face& F,const Standard_Real xpar,const Standard_Boolean isoU,Standard_Real& xinf,Standard_Real& xsup)
438 {
439   // IMPORTANT : xinf=xf,xsup=xl are INITIALIZED with first and last x
440   //             parameters of F (x = u,v )
441   // purpose : finding greater xinf : xinf <= xpar
442   //                   smaller xsup :        xpar <=xsup
443   Standard_Real tol2d = 1.e-6;
444   Standard_Real df = xpar-xinf; Standard_Boolean onf = (Abs(df)<tol2d);
445   Standard_Real dl = xpar-xsup; Standard_Boolean onl = (Abs(dl)<tol2d);
446   if (onf || onl) return Standard_True;
447 
448   Standard_Boolean isoV = !isoU;
449   TopExp_Explorer ex(F,TopAbs_EDGE);
450   for (; ex.More(); ex.Next()){
451     const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
452     Standard_Boolean isou,isov; gp_Pnt2d o2d; gp_Dir2d d2d;
453     Standard_Boolean isouv = TopOpeBRepTool_TOOL::UVISO(E,F,isou,isov,d2d,o2d);
454     if (!isouv) return Standard_False;
455 
456     Standard_Boolean compare = (isoU && isou) || (isoV && isov);
457     if (!compare) return Standard_False;
458     Standard_Real xvalue = isou ? o2d.X() : o2d.Y();
459 
460     if (xvalue < xpar)
461       if (xinf < xvalue) xinf = xvalue;
462     if (xpar < xvalue)
463       if (xvalue < xsup) xsup = xvalue;
464   }
465   return Standard_True;
466 }
467 
468 // ----------------------------------------------------------------------
FUN_tool_EitangenttoFe(const gp_Dir & ngFe,const TopoDS_Edge & Ei,const Standard_Real parOnEi)469 Standard_EXPORT Standard_Boolean FUN_tool_EitangenttoFe(const gp_Dir& ngFe,const TopoDS_Edge& Ei,
470 					   const Standard_Real parOnEi)
471 {
472   // returns true if <Ei> is tangent to Fe at point
473   // p3d of param <parOnEi>,<ngFe> is normal to Fe at p3d.
474   gp_Vec tgEi; Standard_Boolean ok = TopOpeBRepTool_TOOL::TggeomE(parOnEi,Ei,tgEi);
475   if (!ok) return Standard_False;//NYIRAISE
476 
477   Standard_Real prod = ngFe.Dot(tgEi);
478   Standard_Real tol = Precision::Parametric(Precision::Confusion());
479   Standard_Boolean tangent = (Abs(prod) <= tol);
480   return tangent;
481 }
482 
483 // ----------------------------------------------------------------------
FUN_tool_typ(const TopoDS_Edge & E)484 Standard_EXPORT GeomAbs_CurveType FUN_tool_typ(const TopoDS_Edge& E)
485 {
486   BRepAdaptor_Curve BC(E);
487   GeomAbs_CurveType typ = BC.GetType();
488   return typ;
489 }
490 
491 // ----------------------------------------------------------------------
FUN_tool_line(const TopoDS_Edge & E)492 Standard_EXPORT Standard_Boolean FUN_tool_line(const TopoDS_Edge& E)
493 {
494   BRepAdaptor_Curve BC(E);
495   Standard_Boolean line = (BC.GetType() == GeomAbs_Line);
496   return line;
497 }
498 
499 // ----------------------------------------------------------------------
FUN_tool_plane(const TopoDS_Shape & F)500 Standard_EXPORT Standard_Boolean FUN_tool_plane(const TopoDS_Shape& F)
501 {
502   Handle(Geom_Surface) S = TopOpeBRepTool_ShapeTool::BASISSURFACE(TopoDS::Face(F));
503   GeomAdaptor_Surface GS(S);
504   return (GS.GetType() == GeomAbs_Plane);
505 }
506 
507 // ----------------------------------------------------------------------
FUN_tool_cylinder(const TopoDS_Shape & F)508 Standard_EXPORT Standard_Boolean FUN_tool_cylinder(const TopoDS_Shape& F)
509 {
510   Handle(Geom_Surface) S = TopOpeBRepTool_ShapeTool::BASISSURFACE(TopoDS::Face(F));
511   GeomAdaptor_Surface GS(S);
512   return (GS.GetType() == GeomAbs_Cylinder);
513 }
514 
515 // ----------------------------------------------------------------------
FUN_tool_closedS(const TopoDS_Shape & F,Standard_Boolean & uclosed,Standard_Real & uperiod,Standard_Boolean & vclosed,Standard_Real & vperiod)516 Standard_EXPORT Standard_Boolean FUN_tool_closedS(const TopoDS_Shape& F,
517 				     Standard_Boolean& uclosed,Standard_Real& uperiod,
518 				     Standard_Boolean& vclosed,Standard_Real& vperiod)
519 {
520 //  const Handle(Geom_Surface)& S = BRep_Tool::Surface(TopoDS::Face(F));
521   Handle(Geom_Surface) S =TopOpeBRepTool_ShapeTool::BASISSURFACE(TopoDS::Face(F));
522   if (S.IsNull()) return Standard_False;
523   Standard_Boolean closed = FUN_tool_closed(S,uclosed,uperiod,vclosed,vperiod);
524   return closed;
525 }
526 
527 // ----------------------------------------------------------------------
FUN_tool_closedS(const TopoDS_Shape & F)528 Standard_EXPORT Standard_Boolean FUN_tool_closedS(const TopoDS_Shape& F)
529 {
530   Standard_Boolean uclosed=Standard_False,vclosed=Standard_False; Standard_Real uperiod=0.,vperiod=0.;
531   Standard_Boolean closed = FUN_tool_closedS(F,uclosed,uperiod,vclosed,vperiod);
532   return closed;
533 }
534 
535 // ----------------------------------------------------------------------
FUN_tool_closedS(const TopoDS_Shape & F,Standard_Boolean & inU,Standard_Real & xmin,Standard_Real & xper)536 Standard_EXPORT Standard_Boolean FUN_tool_closedS(const TopoDS_Shape& F,
537 				     Standard_Boolean& inU, Standard_Real& xmin, Standard_Real& xper)
538 {
539   Standard_Boolean uclosed,vclosed; Standard_Real uperiod,vperiod;
540   Handle(Geom_Surface) S =TopOpeBRepTool_ShapeTool::BASISSURFACE(TopoDS::Face(F));
541   if (S.IsNull()) return Standard_False;
542   Standard_Boolean closed = FUN_tool_closed(S,uclosed,uperiod,vclosed,vperiod);
543   if (!closed) return Standard_False;
544   Standard_Real u1,u2,v1,v2; S->Bounds(u1,u2,v1,v2);
545 
546   inU = uclosed;
547   xper = inU ? uperiod : vperiod;
548   xmin = inU ? u1 : v1;
549   return Standard_False ;
550 }
551 // ----------------------------------------------------------------------
FUN_tool_mkBnd2d(const TopoDS_Shape & W,const TopoDS_Shape & FF,Bnd_Box2d & B2d)552 Standard_EXPORT void FUN_tool_mkBnd2d(const TopoDS_Shape& W,const TopoDS_Shape& FF,Bnd_Box2d& B2d)
553 {
554   // greater <B> with <W>'s UV representation on <F>
555   Standard_Real tol = 1.e-8;
556   Bnd_Box2d newB2d;
557   TopExp_Explorer ex;
558   for (ex.Init(W,TopAbs_EDGE); ex.More(); ex.Next()) {
559 //  for (TopExp_Explorer ex(W,TopAbs_EDGE); ex.More(); ex.Next()) {
560     const TopoDS_Edge& E = TopoDS::Edge(ex.Current());
561     const TopoDS_Face& F = TopoDS::Face(FF);
562     Standard_Real f,l,tolpc; Handle(Geom2d_Curve) pc;
563     Standard_Boolean haspc = FC2D_HasCurveOnSurface(E,F);
564     if (!haspc) {
565       Standard_Real tolE = BRep_Tool::Tolerance(E);
566       pc = FC2D_CurveOnSurface(E,F,f,l,tolpc);
567       Standard_Real newtol = Max(tolE,tolpc);
568       BRep_Builder BB; BB.UpdateEdge(E,pc,F,newtol);
569     }
570     BRepAdaptor_Curve2d BC2d(E,F);
571     BndLib_Add2dCurve::Add(BC2d,tol,newB2d);
572   } //ex(W,EDGE)
573 
574   FUN_tool_UpdateBnd2d(B2d,newB2d);
575 }
576 
577 // ----------------------------------------------------------------------
578 //  closing topologies :
579 // ----------------------------------------------------------------------
580 
581 // ----------------------------------------------------------------------
FUN_tool_IsClosingE(const TopoDS_Edge & E,const TopoDS_Shape & S,const TopoDS_Face & F)582 Standard_EXPORT Standard_Boolean FUN_tool_IsClosingE(const TopoDS_Edge& E,const TopoDS_Shape& S,const TopoDS_Face& F)
583 {
584   Standard_Integer nbocc = 0;
585   TopExp_Explorer exp;
586   for (exp.Init(S,TopAbs_EDGE);exp.More();exp.Next())
587 //  for (TopExp_Explorer exp(S,TopAbs_EDGE);exp.More();exp.Next())
588     if (exp.Current().IsSame(E)) nbocc++;
589   if (nbocc != 2) return Standard_False;
590   return BRep_Tool::IsClosed(E,F);
591 }
592 
593 // ----------------------------------------------------------------------
FUN_tool_ClosingE(const TopoDS_Edge & E,const TopoDS_Wire & W,const TopoDS_Face & F)594 Standard_EXPORT Standard_Boolean FUN_tool_ClosingE(const TopoDS_Edge& E,const TopoDS_Wire& W,const TopoDS_Face& F)
595 {
596   Standard_Boolean clo = FUN_tool_IsClosingE(E,W,F);
597   return clo;
598 }
599 
600 /*// ----------------------------------------------------------------------
601 Standard_EXPORT Standard_Boolean FUN_tool_ClosedE(const TopoDS_Edge& E,TopoDS_Shape& vclosing)
602 {
603   // returns true if <E> has a closing vertex <vclosing>
604 //  return E.IsClosed();
605   Standard_Boolean isdgE = BRep_Tool::Degenerated(E);
606   if (isdgE) return Standard_False;
607 
608   TopoDS_Shape vv; vclosing.Nullify();
609   TopExp_Explorer ex(E,TopAbs_VERTEX);
610   for (; ex.More(); ex.Next()) {
611     const TopoDS_Shape& v = ex.Current();
612     if (M_INTERNAL(v.Orientation())) continue;
613     if (vv.IsNull()) vv = v;
614     else if (v.IsSame(vv))
615       {vclosing = vv; return Standard_True;}
616   }
617   return Standard_False;
618 }*/
619 
620 // ----------------------------------------------------------------------
621 //  shared topologies :
622 // ----------------------------------------------------------------------
623 
624 // ----------------------------------------------------------------------
FUN_tool_inS(const TopoDS_Shape & subshape,const TopoDS_Shape & shape)625 Standard_EXPORT Standard_Boolean FUN_tool_inS(const TopoDS_Shape& subshape,const TopoDS_Shape& shape)
626 {
627   TopAbs_ShapeEnum sstyp = subshape.ShapeType();
628   TopTools_IndexedMapOfShape M; TopExp::MapShapes(shape,sstyp,M);
629   Standard_Boolean isbound = M.Contains(subshape);
630   return isbound;
631 }
632 
633 // ----------------------------------------------------------------------
FUN_tool_Eshared(const TopoDS_Shape & v,const TopoDS_Shape & F1,const TopoDS_Shape & F2,TopoDS_Shape & Eshared)634 Standard_EXPORT Standard_Boolean FUN_tool_Eshared(const TopoDS_Shape& v,const TopoDS_Shape& F1,const TopoDS_Shape& F2,
635 				     TopoDS_Shape& Eshared)
636 //purpose : finds out <Eshared> shared by <F1> and <F2>,
637 //          with <V> bound of <Eshared>
638 {
639   TopTools_ListOfShape e1s;
640   TopExp_Explorer ex(F1,TopAbs_EDGE);
641   for (; ex.More(); ex.Next()){
642     const TopoDS_Shape& e1 = ex.Current();
643     Standard_Boolean e1ok = Standard_False;
644     TopExp_Explorer exv(e1,TopAbs_VERTEX);
645     for (; exv.More(); exv.Next())
646       if (exv.Current().IsSame(v)) {e1ok = Standard_True; break;}
647     if (e1ok) e1s.Append(e1);
648   }
649   ex.Init(F2,TopAbs_EDGE);
650   for (; ex.More(); ex.Next()){
651     const TopoDS_Shape& e2 = ex.Current();
652     TopTools_ListIteratorOfListOfShape it1(e1s);
653     for (; it1.More(); it1.Next())
654       if (it1.Value().IsSame(e2)) {Eshared = e2; return Standard_True;}
655   }
656   return Standard_False;
657 }
658 
659 // ----------------------------------------------------------------------
FUN_tool_parVonE(const TopoDS_Vertex & v,const TopoDS_Edge & E,Standard_Real & par)660 Standard_EXPORT Standard_Boolean FUN_tool_parVonE(const TopoDS_Vertex& v,const TopoDS_Edge& E,Standard_Real& par)
661 {
662   Standard_Real tol = Precision::Confusion();
663   Standard_Boolean isVofE = Standard_False;
664   TopExp_Explorer ex;
665   for (ex.Init(E,TopAbs_VERTEX); ex.More(); ex.Next()) {
666 //  for (TopExp_Explorer ex(E,TopAbs_VERTEX); ex.More(); ex.Next()) {
667     isVofE = ex.Current().IsSame(v);
668     if (isVofE) {
669       par = BRep_Tool::Parameter(TopoDS::Vertex(ex.Current()),E);
670       break;
671     }
672   }
673   if (!isVofE) {
674     gp_Pnt pt = BRep_Tool::Pnt(v);
675     // <v> can share same domain with a vertex of <E>
676     for (ex.Init(E,TopAbs_VERTEX); ex.More(); ex.Next()) {
677       const TopoDS_Vertex& vex = TopoDS::Vertex(ex.Current());
678       gp_Pnt ptex = BRep_Tool::Pnt(vex);
679       if (ptex.IsEqual(pt,tol)) {
680 	par = BRep_Tool::Parameter(vex,E);
681 	return Standard_True;
682       }
683     }
684 
685 //    Standard_Real f,l;
686     BRepAdaptor_Curve BAC(E);
687     Extrema_ExtPC pro(pt,BAC);
688     Standard_Boolean done = pro.IsDone() && (pro.NbExt() >0);
689     if (!done) return Standard_False;
690     Standard_Integer i = FUN_tool_getindex(pro);
691     par = pro.Point(i).Parameter();
692   }
693   return Standard_True;
694 }
695 
696 // ----------------------------------------------------------------------
FUN_tool_parE(const TopoDS_Edge & E0,const Standard_Real & par0,const TopoDS_Edge & E,Standard_Real & par,const Standard_Real tol)697 Standard_EXPORT Standard_Boolean FUN_tool_parE(const TopoDS_Edge& E0,const Standard_Real& par0,
698 				  const TopoDS_Edge& E,Standard_Real& par,const Standard_Real tol)
699 {
700   gp_Pnt P; Standard_Boolean ok = FUN_tool_value(par0,E0,P);
701   if (!ok) return Standard_False;
702 
703   Standard_Real dist; ok = FUN_tool_projPonE(P,E,par,dist);
704   if (!ok) return Standard_False;
705 
706   ok = (dist < tol);
707   return ok;
708 }
709 // ----------------------------------------------------------------------
FUN_tool_parE(const TopoDS_Edge & E0,const Standard_Real & par0,const TopoDS_Edge & E,Standard_Real & par)710 Standard_EXPORT Standard_Boolean FUN_tool_parE(const TopoDS_Edge& E0,const Standard_Real& par0,
711 				  const TopoDS_Edge& E,Standard_Real& par)
712 // ? <par> :  P -> <par0> on <E0>,<par> on <E>
713 // prequesitory : point(par0 ,E0) is IN 1d(E)
714 {
715   Standard_Real tol3d = BRep_Tool::Tolerance(E)*1.e2;//KKKKK a revoir xpu(CTS21118,f14ou,GI13)
716   Standard_Boolean ok = FUN_tool_parE(E0,par0,E,par,tol3d);
717   return ok;
718 }
719 
720 // ----------------------------------------------------------------------
FUN_tool_parF(const TopoDS_Edge & E,const Standard_Real & par,const TopoDS_Face & F,gp_Pnt2d & UV,const Standard_Real tol3d)721 Standard_EXPORT Standard_Boolean FUN_tool_parF(const TopoDS_Edge& E,const Standard_Real& par,
722 				  const TopoDS_Face& F,gp_Pnt2d& UV, const Standard_Real tol3d)
723 // ? <UV> : P -> <par> on <E>,<UV> on <F>
724 {
725   gp_Pnt P; Standard_Boolean ok = FUN_tool_value(par,E,P);
726   if (!ok) return Standard_False;
727 
728   Standard_Real dist; ok = FUN_tool_projPonF(P,F,UV,dist);
729   if (!ok) return Standard_False;
730 
731   ok = (dist < tol3d);
732   return ok;
733 }
FUN_tool_parF(const TopoDS_Edge & E,const Standard_Real & par,const TopoDS_Face & F,gp_Pnt2d & UV)734 Standard_EXPORT Standard_Boolean FUN_tool_parF(const TopoDS_Edge& E,const Standard_Real& par,
735 				  const TopoDS_Face& F,gp_Pnt2d& UV)
736 {
737   Standard_Real tol3d = BRep_Tool::Tolerance(F)*1.e2; //KK xpu
738   Standard_Boolean ok = FUN_tool_parF(E,par,F,UV,tol3d);
739   return ok;
740 }
741 
742 // ----------------------------------------------------------------------
FUN_tool_paronEF(const TopoDS_Edge & E,const Standard_Real & par,const TopoDS_Face & F,gp_Pnt2d & UV,const Standard_Real tol3d)743 Standard_EXPORT Standard_Boolean FUN_tool_paronEF(const TopoDS_Edge& E,const Standard_Real& par,
744 				     const TopoDS_Face& F,gp_Pnt2d& UV, const Standard_Real tol3d)
745 // <E> is on <F> ? <UV> : P -> <par> on <E>,<UV> on <F>
746 {
747   Standard_Real f,l,tol; Handle(Geom2d_Curve) PC = FC2D_CurveOnSurface(E,F,f,l,tol);
748   if (PC.IsNull()) {
749     Standard_Boolean ok = FUN_tool_parF(E,par,F,UV,tol3d);
750     return ok;
751   }
752   Standard_Boolean ok = (f <= par) && (par <= l);
753   if (!ok) return Standard_False;
754   UV = PC->Value(par);
755   return Standard_True;
756 }
FUN_tool_paronEF(const TopoDS_Edge & E,const Standard_Real & par,const TopoDS_Face & F,gp_Pnt2d & UV)757 Standard_EXPORT Standard_Boolean FUN_tool_paronEF(const TopoDS_Edge& E,const Standard_Real& par,
758 				     const TopoDS_Face& F,gp_Pnt2d& UV)
759 {
760   Standard_Real tol3d = BRep_Tool::Tolerance(F)*1.e2; // KKxpu
761   Standard_Boolean ok = FUN_tool_paronEF(E,par,F,UV,tol3d);
762   return ok;
763 }
764 
765 // ----------------------------------------------------------------------
FUN_tool_dirC(const Standard_Real par,const BRepAdaptor_Curve & BAC)766 Standard_EXPORT gp_Dir FUN_tool_dirC(const Standard_Real par,const BRepAdaptor_Curve& BAC)
767 {
768   gp_Pnt p; gp_Vec tgE; BAC.D1(par,p,tgE);
769   gp_Dir dirC(tgE);
770   return dirC;
771 }
772 
773 // ----------------------------------------------------------------------
FUN_tool_tggeomE(const Standard_Real paronE,const TopoDS_Edge & E)774 Standard_EXPORT gp_Vec FUN_tool_tggeomE(const Standard_Real paronE,const TopoDS_Edge& E)
775 {
776   Standard_Boolean isdgE = BRep_Tool::Degenerated(E);
777   if (isdgE) return gp_Vec(0,0,0);
778   gp_Vec dirE(FUN_tool_dirC(paronE,E));
779   return dirE;
780 }
781 
782 // ----------------------------------------------------------------------
FUN_tool_line(const BRepAdaptor_Curve & BAC)783 Standard_EXPORT Standard_Boolean FUN_tool_line(const BRepAdaptor_Curve& BAC)
784 {
785   Standard_Boolean line = (BAC.GetType() == GeomAbs_Line);
786   return line;
787 }
788 
789 // ----------------------------------------------------------------------
FUN_tool_quad(const TopoDS_Edge & E)790 Standard_EXPORT Standard_Boolean FUN_tool_quad(const TopoDS_Edge& E)
791 {
792   BRepAdaptor_Curve BC(E);
793   GeomAbs_CurveType CT = BC.GetType();
794   Standard_Boolean quad = FUN_quadCT(CT);
795   return quad;
796 }
797 
798 // ----------------------------------------------------------------------
FUN_tool_quad(const BRepAdaptor_Curve & BAC)799 Standard_EXPORT Standard_Boolean FUN_tool_quad(const BRepAdaptor_Curve& BAC)
800 {
801   GeomAbs_CurveType CT = BAC.GetType();
802   Standard_Boolean isquad = Standard_False;
803   if (CT == GeomAbs_Line) isquad = Standard_True;
804   if (CT == GeomAbs_Circle) isquad = Standard_True;
805   if (CT == GeomAbs_Ellipse) isquad = Standard_True;
806   if (CT == GeomAbs_Hyperbola) isquad = Standard_True;
807   if (CT == GeomAbs_Parabola) isquad = Standard_True;
808   return isquad;
809 }
810 
811 // ----------------------------------------------------------------------
FUN_tool_quad(const TopoDS_Face & F)812 Standard_EXPORT Standard_Boolean FUN_tool_quad(const TopoDS_Face& F)
813 {
814   Handle(Geom_Surface) S = TopOpeBRepTool_ShapeTool::BASISSURFACE(F);
815   Standard_Boolean quad = FUN_tool_quad(S);
816   return quad;
817 }
818 
819 // ----------------------------------------------------------------------
FUN_tool_findPinBAC(const BRepAdaptor_Curve & BAC,gp_Pnt & P,Standard_Real & par)820 Standard_EXPORT Standard_Boolean FUN_tool_findPinBAC(const BRepAdaptor_Curve& BAC,gp_Pnt& P,Standard_Real& par)
821 {
822   FUN_tool_findparinBAC(BAC,par);
823   BAC.D0(par,P);
824   return Standard_True;
825 }
826 
827 // ----------------------------------------------------------------------
FUN_tool_findparinBAC(const BRepAdaptor_Curve & BAC,Standard_Real & par)828 Standard_EXPORT Standard_Boolean FUN_tool_findparinBAC(const BRepAdaptor_Curve& BAC,Standard_Real& par)
829 {
830   Standard_Real fE = BAC.FirstParameter(),lE = BAC.LastParameter();
831   Standard_Real t = 0.34567237; par = (1-t)*fE + t*lE;
832   return Standard_True;
833 }
834 
835 // ----------------------------------------------------------------------
FUN_tool_findparinE(const TopoDS_Shape & E,Standard_Real & par)836 Standard_EXPORT Standard_Boolean FUN_tool_findparinE(const TopoDS_Shape& E,Standard_Real& par)
837 {
838   BRepAdaptor_Curve BAC(TopoDS::Edge(E));
839   Standard_Boolean r = FUN_tool_findparinBAC(BAC,par);
840   return r;
841 }
842 
843 // ----------------------------------------------------------------------
FUN_tool_findPinE(const TopoDS_Shape & E,gp_Pnt & P,Standard_Real & par)844 Standard_EXPORT Standard_Boolean FUN_tool_findPinE(const TopoDS_Shape& E,gp_Pnt& P,Standard_Real& par)
845 {
846   BRepAdaptor_Curve BAC(TopoDS::Edge(E));
847   Standard_Boolean r = FUN_tool_findPinBAC(BAC,P,par);
848   return r;
849 }
850 
851 // ----------------------------------------------------------------------
852 //  tolerances :
853 // ----------------------------------------------------------------------
854 
855 // ----------------------------------------------------------------------
FUN_tool_maxtol(const TopoDS_Shape & S,const TopAbs_ShapeEnum & typ,Standard_Real & maxtol)856 Standard_EXPORT Standard_Boolean FUN_tool_maxtol(const TopoDS_Shape& S,const TopAbs_ShapeEnum& typ,Standard_Real& maxtol)
857 // purpose : returns maxtol of <S>'s shapes of type <typ>
858 {
859   Standard_Boolean face   = (typ == TopAbs_FACE);
860   Standard_Boolean edge   = (typ == TopAbs_EDGE);
861   Standard_Boolean vertex = (typ == TopAbs_VERTEX);
862   TopExp_Explorer ex(S,typ);
863   Standard_Boolean hasshatyp = ex.More();
864   for (; ex.More(); ex.Next()){
865     const TopoDS_Shape& ss = ex.Current();
866     Standard_Real tolss = 0.;
867     if (face)   tolss =  BRep_Tool::Tolerance(TopoDS::Face(ss));
868     if (edge)   tolss =  BRep_Tool::Tolerance(TopoDS::Edge(ss));
869     if (vertex) tolss =  BRep_Tool::Tolerance(TopoDS::Vertex(ss));
870     if (tolss > maxtol) maxtol = tolss;
871   }
872   return hasshatyp;
873 }
874 
875 // ----------------------------------------------------------------------
FUN_tool_maxtol(const TopoDS_Shape & S)876 Standard_EXPORT Standard_Real FUN_tool_maxtol(const TopoDS_Shape& S)
877 // purpose : returns maxtol between <S>'s shapes.
878 {
879   Standard_Real maxtol = 0.;
880   FUN_tool_maxtol(S,TopAbs_FACE,maxtol);
881   FUN_tool_maxtol(S,TopAbs_EDGE,maxtol);
882   FUN_tool_maxtol(S,TopAbs_VERTEX,maxtol);
883   return maxtol;
884 }
885 
886 // ----------------------------------------------------------------------
FUN_tool_nbshapes(const TopoDS_Shape & S,const TopAbs_ShapeEnum & typ)887 Standard_EXPORT Standard_Integer FUN_tool_nbshapes(const TopoDS_Shape& S,const TopAbs_ShapeEnum& typ)
888 {
889   TopExp_Explorer ex(S,typ);
890   Standard_Integer i = 0;
891   for (; ex.More(); ex.Next()) i++;
892   return i;
893 }
894 
895 // ----------------------------------------------------------------------
FUN_tool_shapes(const TopoDS_Shape & S,const TopAbs_ShapeEnum & typ,TopTools_ListOfShape & ltyp)896 Standard_EXPORT void FUN_tool_shapes(const TopoDS_Shape& S,const TopAbs_ShapeEnum& typ,
897 				     TopTools_ListOfShape& ltyp)
898 {
899   TopExp_Explorer ex(S,typ);
900   for (; ex.More(); ex.Next()) ltyp.Append(ex.Current());
901 }
902 
903 // ----------------------------------------------------------------------
FUN_tool_comparebndkole(const TopoDS_Shape & sh1,const TopoDS_Shape & sh2)904 Standard_EXPORT Standard_Integer FUN_tool_comparebndkole(const TopoDS_Shape& sh1,const TopoDS_Shape& sh2)
905 // purpose: comparing bounding boxes of <sh1> and <sh2>,
906 //          returns k =1,2 if shi is contained in shk
907 //          else returns 0
908 {
909   Bnd_Box bnd1; BRepBndLib::Add(sh1,bnd1); bnd1.SetGap(0.);
910   Bnd_Box bnd2; BRepBndLib::Add(sh2,bnd2); bnd2.SetGap(0.);
911 
912   if(bnd1.IsOut(bnd2)) return 0;
913   TColStd_Array1OfReal xyz1(1,6),xyz2(1,6);
914   bnd1.Get(xyz1(1),xyz1(2),xyz1(3),xyz1(4),xyz1(5),xyz1(6));
915   bnd2.Get(xyz2(1),xyz2(2),xyz2(3),xyz2(4),xyz2(5),xyz2(6));
916   Standard_Real tol = Precision::Confusion();
917 
918   Standard_Integer neq,n2sup; neq=n2sup=0;
919 //  for (Standard_Integer i = 1; i<=3; i++) {
920   Standard_Integer i ;
921   for ( i = 1; i<=3; i++) {
922     Standard_Real d = xyz2(i)-xyz1(i);
923     Standard_Boolean eq = (Abs(d) < tol);
924     if (eq) {neq++; continue;}
925     if (d<0.) n2sup++;
926   }
927   for (i = 4; i<=6; i++) {
928     Standard_Real d = xyz2(i)-xyz1(i);
929     Standard_Boolean eq = (Abs(d) < tol);
930     if (eq) {neq++; continue;}
931     if (d>0.) n2sup++;
932   }
933   if (n2sup + neq != 6) return 0;
934   if (neq == 6) return 0;
935 
936   Standard_Integer ires = (n2sup > 0)? 2: 1;
937   return ires;
938 }
939 
940 // ----------------------------------------------------------------------
FUN_tool_SameOri(const TopoDS_Edge & E1,const TopoDS_Edge & E2)941 Standard_EXPORT Standard_Boolean FUN_tool_SameOri(const TopoDS_Edge& E1,const TopoDS_Edge& E2)
942 //prequesitory : 1- <E1> and <E2> share same domain,
943 //               2- C3d<E1> contains C3d<E2>
944 {
945   Standard_Real f,l; FUN_tool_bounds(E2,f,l);
946   Standard_Real x = 0.345;
947   Standard_Real mid = x*f + (1-x)*l;
948   gp_Pnt Pmid; FUN_tool_value(mid,E2,Pmid);
949   gp_Vec tmp; Standard_Boolean ok = TopOpeBRepTool_TOOL::TggeomE(mid,E2,tmp);
950   if (!ok) return Standard_False;
951   gp_Dir tgE2(tmp);
952   TopAbs_Orientation oriE2 = E2.Orientation();
953   if (M_REVERSED(oriE2)) tgE2.Reverse();
954 
955   Standard_Real pE1,dist; ok = FUN_tool_projPonE(Pmid,E1,pE1,dist);
956   Standard_Real tol1 = BRep_Tool::Tolerance(E1);
957   Standard_Real tol2 = BRep_Tool::Tolerance(E2);
958   Standard_Real tol  = Max(tol1,tol2)* 10.;
959   if (dist > tol) return Standard_False;
960   if (!ok) return Standard_False;
961   ok = TopOpeBRepTool_TOOL::TggeomE(pE1,E1,tmp);
962   if (!ok) return Standard_False;
963   gp_Dir tgE1(tmp);
964   TopAbs_Orientation oriE1 = E1.Orientation();
965   if (M_REVERSED(oriE1)) tgE1.Reverse();
966 
967   Standard_Boolean sameori = (tgE2.Dot(tgE1) > 0.);
968   return sameori;
969 }
970 
971 // ----------------------------------------------------------------------
FUN_tool_haspc(const TopoDS_Edge & E,const TopoDS_Face & F)972 Standard_EXPORT Standard_Boolean FUN_tool_haspc(const TopoDS_Edge& E,const TopoDS_Face& F)
973 {
974   Standard_Real f,l,tol; Handle(Geom2d_Curve) C2d =  FC2D_CurveOnSurface(E,F,f,l,tol);
975   Standard_Boolean null = C2d.IsNull();
976   return !null;
977 }
978 
979 // ----------------------------------------------------------------------
FUN_tool_pcurveonF(const TopoDS_Face & F,TopoDS_Edge & E)980 Standard_EXPORT Standard_Boolean FUN_tool_pcurveonF(const TopoDS_Face& F,TopoDS_Edge& E)
981 {
982   Standard_Real f,l; Handle(Geom_Curve) C3d = BRep_Tool::Curve(E,f,l);
983   if (C3d.IsNull()) return Standard_False;
984   Standard_Real tolReached2d;
985   Handle(Geom2d_Curve) C2d =
986     TopOpeBRepTool_CurveTool::MakePCurveOnFace(F,C3d,tolReached2d,f,l);
987   if (C2d.IsNull()) return Standard_False;
988 
989   Standard_Real tolE = BRep_Tool::Tolerance(E);
990   BRep_Builder BB;
991   BB.UpdateEdge(E,C2d,F,tolE);
992   return Standard_True;
993 }
994 
995 // ----------------------------------------------------------------------
FUN_tool_pcurveonF(const TopoDS_Face & fF,TopoDS_Edge & faultyE,const Handle (Geom2d_Curve)& C2d,TopoDS_Face & newf)996 Standard_EXPORT Standard_Boolean FUN_tool_pcurveonF(const TopoDS_Face& fF,TopoDS_Edge& faultyE,
997 				       const Handle(Geom2d_Curve)& C2d,
998 				       TopoDS_Face& newf)
999 {
1000   BRep_Builder BB;
1001   TopExp_Explorer exw(fF,TopAbs_WIRE);
1002   TopTools_ListOfShape low;
1003   Standard_Boolean hasnewf = Standard_False;
1004   for (; exw.More(); exw.Next()){
1005     const TopoDS_Shape& w = exw.Current();
1006 
1007     TopTools_ListOfShape loe; Standard_Boolean hasneww = Standard_False;
1008     TopExp_Explorer exe(w,TopAbs_EDGE);
1009     for (; exe.More(); exe.Next()){
1010       const TopoDS_Edge& e = TopoDS::Edge(exe.Current());
1011       Standard_Boolean equal = e.IsEqual(faultyE);
1012       if (!equal) {loe.Append(e); continue;}
1013 
1014       Standard_Real tole = BRep_Tool::Tolerance(e);
1015       TopoDS_Vertex vf,vl; TopExp::Vertices(e,vf,vl);
1016 
1017       TopoDS_Edge newe = faultyE;
1018 //      TopoDS_Edge newe; FUN_ds_CopyEdge(e,newe); newe.Orientation(TopAbs_FORWARD);
1019 //      vf.Orientation(TopAbs_FORWARD);  BB.Add(newe,vf); FUN_ds_Parameter(newe,vf,parf);
1020 //      vl.Orientation(TopAbs_REVERSED); BB.Add(newe,vl); FUN_ds_Parameter(newe,vl,parl);
1021       BB.UpdateEdge(newe,C2d,fF,tole);
1022       newe.Orientation(e.Orientation());
1023       loe.Append(newe);
1024       hasneww = Standard_True;
1025       hasnewf = Standard_True;
1026     }
1027     if (hasneww) {
1028       TopoDS_Wire neww; Standard_Boolean ok = FUN_tool_MakeWire(loe,neww);
1029       if (!ok) return Standard_False;
1030       low.Append(neww);
1031     }
1032     else low.Append(w);
1033   } // exw
1034   if (hasnewf) {
1035     TopoDS_Shape aLocalShape = fF.EmptyCopied();
1036     newf = TopoDS::Face(aLocalShape);
1037 //    newf = TopoDS::Face(fF.EmptyCopied());
1038     for (TopTools_ListIteratorOfListOfShape itw(low); itw.More(); itw.Next()){
1039       const TopoDS_Shape w = itw.Value();
1040       BB.Add(newf,w);
1041     }
1042     return Standard_True;
1043   }
1044   return Standard_False;
1045 }
1046 
1047 // ----------------------------------------------------------------------
1048 //  shared geometry :
1049 // ----------------------------------------------------------------------
1050 
1051 // ----------------------------------------------------------------------
FUN_tool_curvesSO(const TopoDS_Edge & E1,const Standard_Real p1,const TopoDS_Edge & E2,const Standard_Real p2,Standard_Boolean & so)1052 Standard_EXPORT Standard_Boolean FUN_tool_curvesSO(const TopoDS_Edge& E1,const Standard_Real p1,const TopoDS_Edge& E2,const Standard_Real p2,
1053 				      Standard_Boolean& so)
1054 {
1055   BRepAdaptor_Curve BAC1(E1);
1056   BRepAdaptor_Curve BAC2(E2);
1057   gp_Vec tg1; Standard_Boolean ok = TopOpeBRepTool_TOOL::TggeomE(p1,E1,tg1);
1058   if (!ok) return Standard_False;//NYIRAISE
1059   gp_Vec tg2;     ok = TopOpeBRepTool_TOOL::TggeomE(p2,E2,tg2);
1060   if (!ok) return Standard_False;//NYIRAISE
1061   Standard_Real tola = Precision::Angular()*1.e3;
1062   Standard_Boolean oppo = tg1.IsOpposite(tg2,tola);
1063   Standard_Boolean samo = tg1.IsParallel(tg2,tola);
1064   if      (oppo) so = Standard_False;
1065   else if (samo) so = Standard_True;
1066   else return Standard_False;
1067   return Standard_True;
1068 }
FUN_tool_curvesSO(const TopoDS_Edge & E1,const Standard_Real p1,const TopoDS_Edge & E2,Standard_Boolean & so)1069 Standard_EXPORT Standard_Boolean FUN_tool_curvesSO(const TopoDS_Edge& E1,const Standard_Real p1,const TopoDS_Edge& E2,Standard_Boolean& so)
1070 {
1071   // prequesitory : P3d(E1,p1) is IN 1d(E2)
1072   Standard_Real p2 = 0.; Standard_Boolean ok = FUN_tool_parE(E1,p1,E2,p2);
1073   if (!ok) return Standard_False;
1074   ok = FUN_tool_curvesSO(E1,p1,E2,p2,so);
1075   return ok;
1076 }
1077 
1078 // ----------------------------------------------------------------------
FUN_tool_curvesSO(const TopoDS_Edge & E1,const TopoDS_Edge & E2,Standard_Boolean & so)1079 Standard_EXPORT Standard_Boolean FUN_tool_curvesSO(const TopoDS_Edge& E1,const TopoDS_Edge& E2,Standard_Boolean& so)
1080 {
1081   // prequesitory : E1 is IN 1d(E2)
1082   TopoDS_Vertex vf1,vl1; TopExp::Vertices(E1,vf1,vl1);
1083   Standard_Boolean closed1 = vf1.IsSame(vl1);
1084   TopoDS_Vertex vf2,vl2; TopExp::Vertices(E2,vf2,vl2);
1085   Standard_Boolean closed2 = vf2.IsSame(vl2);
1086   Standard_Boolean project = Standard_False;
1087   if (closed1 || closed2) project = Standard_True;
1088   else {
1089     if      (vf1.IsSame(vf2)) so = Standard_True;
1090     else if (vl1.IsSame(vl2)) so = Standard_True;
1091     else if (vf1.IsSame(vl2)) so = Standard_False;
1092     else if (vl1.IsSame(vf2)) so = Standard_False;
1093     else project = Standard_True;
1094   }
1095   if (project) {
1096     Standard_Real f,l; FUN_tool_bounds(E1,f,l);
1097     Standard_Real x = 0.45678; Standard_Real p1 = (1-x)*l + x*f;
1098     Standard_Boolean ok = FUN_tool_curvesSO(E1,p1,E2,so);
1099     return ok;
1100   }
1101   return Standard_True;
1102 }
1103 
1104 // ----------------------------------------------------------------------
FUN_tool_findAncestor(const TopTools_ListOfShape & lF,const TopoDS_Edge & E,TopoDS_Face & Fanc)1105 Standard_EXPORT Standard_Boolean FUN_tool_findAncestor(const TopTools_ListOfShape& lF,const TopoDS_Edge& E,TopoDS_Face& Fanc)
1106 {
1107   TopTools_ListIteratorOfListOfShape it(lF);
1108   for (; it.More(); it.Next()){
1109     const TopoDS_Face& F = TopoDS::Face(it.Value());
1110     TopAbs_Orientation dummy; Standard_Boolean found = FUN_tool_orientEinF(E,F,dummy);
1111     if (found) {Fanc = F; return Standard_True;}
1112   }
1113   return Standard_False;
1114 }
1115 
1116 // ----------------------------------------------------------------------
1117 //  new topologies :
1118 // ----------------------------------------------------------------------
1119 
1120 // FUN_ds_* methods are methods of TopOpeBRepDS_BuildTool
1121 // that cannot be called (cyclic dependencies)
1122 
1123 // ----------------------------------------------------------------------
FUN_ds_CopyEdge(const TopoDS_Shape & Ein,TopoDS_Shape & Eou)1124 Standard_EXPORT void FUN_ds_CopyEdge(const TopoDS_Shape& Ein,TopoDS_Shape& Eou)
1125 {
1126   Standard_Real f,l;
1127   TopoDS_Edge E1 = TopoDS::Edge(Ein);
1128   BRep_Tool::Range(E1,f,l);
1129   Eou = Ein.EmptyCopied();
1130   TopoDS_Edge E2 = TopoDS::Edge(Eou);
1131   BRep_Builder BB;
1132   BB.Range(E2,f,l);
1133 }
1134 
1135 // ----------------------------------------------------------------------
FUN_ds_Parameter(const TopoDS_Shape & E,const TopoDS_Shape & V,const Standard_Real P)1136 Standard_EXPORT void FUN_ds_Parameter(const TopoDS_Shape& E,const TopoDS_Shape& V,const Standard_Real P)
1137 {
1138   BRep_Builder BB;
1139   const TopoDS_Edge&   e = TopoDS::Edge(E);
1140   const TopoDS_Vertex& v = TopoDS::Vertex(V);
1141   Standard_Real p = P;
1142   TopLoc_Location loc; Standard_Real f,l;
1143   Handle(Geom_Curve) C = BRep_Tool::Curve(e,loc,f,l);
1144   if ( !C.IsNull() && C->IsPeriodic()) {
1145     Standard_Real per = C->Period();
1146 
1147     TopAbs_Orientation oV=TopAbs_FORWARD;
1148 
1149     TopExp_Explorer exV(e,TopAbs_VERTEX);
1150     for (; exV.More(); exV.Next()) {
1151       const TopoDS_Vertex& vofe = TopoDS::Vertex(exV.Current());
1152       if ( vofe.IsSame(v) ) {
1153 	oV = vofe.Orientation();
1154 	break;
1155       }
1156     }
1157     if ( exV.More() ) {
1158       if ( oV == TopAbs_REVERSED ) {
1159 	if ( p < f ) {
1160 	  Standard_Real pp = ElCLib::InPeriod(p,f,f+per);
1161 	  p = pp;
1162 	}
1163       }
1164     }
1165   }
1166   BB.UpdateVertex(v,p,e,0);
1167 }
1168 
1169 // ----------------------------------------------------------------------
FUN_tool_MakeWire(const TopTools_ListOfShape & loE,TopoDS_Wire & newW)1170 Standard_EXPORT Standard_Boolean FUN_tool_MakeWire(const TopTools_ListOfShape& loE,TopoDS_Wire& newW)
1171 {
1172   newW.Nullify();
1173   BRep_Builder BB;
1174   BB.MakeWire(newW);
1175   TopTools_ListIteratorOfListOfShape itloE(loE);
1176   for (; itloE.More(); itloE.Next()) {
1177     const TopoDS_Edge& E = TopoDS::Edge(itloE.Value());
1178     BB.Add(newW,E);
1179   }
1180   return Standard_True;
1181 }
1182 
1183