1 //abv 06.01.99 fix of misprint
2 //:p6 abv 26.02.99: make ConvertToPeriodic() return Null if nothing done
3 #include <ShapeCustom_Surface.ixx>
9 #include <gp_Cylinder.hxx>
12 #include <TColgp_Array1OfPnt.hxx>
13 #include <TColStd_Array1OfReal.hxx>
14 #include <TColgp_Array2OfPnt.hxx>
15 #include <TColStd_Array2OfReal.hxx>
16 #include <TColStd_Array1OfInteger.hxx>
18 #include <Geom_Curve.hxx>
19 #include <Geom_Plane.hxx>
20 #include <Geom_BSplineSurface.hxx>
21 #include <Geom_BezierSurface.hxx>
22 #include <Geom_SphericalSurface.hxx>
23 #include <Geom_CylindricalSurface.hxx>
24 #include <Geom_ConicalSurface.hxx>
25 #include <Geom_ToroidalSurface.hxx>
26 #include <GeomAdaptor_HSurface.hxx>
27 #include <GeomAdaptor_Surface.hxx>
28 #include <GeomAbs_SurfaceType.hxx>
30 #include <ShapeAnalysis_Geom.hxx>
31 #include <ShapeAnalysis_Surface.hxx>
33 //=======================================================================
34 //function : ShapeCustom_Surface
36 //=======================================================================
38 ShapeCustom_Surface::ShapeCustom_Surface() : myGap (0)
42 //=======================================================================
43 //function : ShapeCustom_Surface
45 //=======================================================================
47 ShapeCustom_Surface::ShapeCustom_Surface (const Handle(Geom_Surface)& S)
53 //=======================================================================
56 //=======================================================================
58 void ShapeCustom_Surface::Init (const Handle(Geom_Surface)& S)
63 //=======================================================================
64 //function : ConvertToAnalytical
66 //=======================================================================
68 Handle(Geom_Surface) ShapeCustom_Surface::ConvertToAnalytical (const Standard_Real tol,
69 const Standard_Boolean substitute)
71 Handle(Geom_Surface) newSurf;
73 Standard_Integer nUP, nVP, nCP, i, j , UDeg, VDeg;
74 Standard_Real U1, U2, V1, V2, C1, C2, DU, DV, U=0, V=0;
75 Handle(Geom_Curve) iso;
76 Standard_Boolean uClosed = Standard_True;
78 // seuls cas traites : BSpline et Bezier
79 Handle(Geom_BSplineSurface) theBSplneS =
80 Handle(Geom_BSplineSurface)::DownCast(mySurf);
81 if (theBSplneS.IsNull()) {
82 Handle(Geom_BezierSurface) theBezierS =
83 Handle(Geom_BezierSurface)::DownCast(mySurf);
84 if (!theBezierS.IsNull()) { // Bezier :
85 nUP = theBezierS->NbUPoles();
86 nVP = theBezierS->NbVPoles();
87 UDeg = theBezierS->UDegree();
88 VDeg = theBezierS->VDegree();
90 else return newSurf; // non reconnu : terminus
93 nUP = theBSplneS->NbUPoles();
94 nVP = theBSplneS->NbVPoles();
95 UDeg = theBSplneS->UDegree();
96 VDeg = theBSplneS->VDegree();
100 mySurf->Bounds(U1, U2, V1, V2);
101 // mySurf->Bounds(U1, U2, V1, V2);
102 TColgp_Array1OfPnt p1(1, 3), p2(1, 3), p3(1, 3);
103 TColStd_Array1OfReal R(1,3);
104 gp_Pnt origPnt, resPnt;
105 gp_Vec origD1U, resD1U, resD1V;
107 Standard_Boolean aCySpCo = Standard_False;
108 Standard_Boolean aToroid = Standard_False;
109 Standard_Boolean aPlanar = Standard_False;
111 if (nUP == 2 && nVP == 2) {
112 if (UDeg == 1 && VDeg == 1) aPlanar = Standard_True;
113 } else if (mySurf->IsUClosed()) { // VRAI IsUClosed
114 if (mySurf->IsVClosed()) aToroid = Standard_True;
115 else aCySpCo = Standard_True;
117 if(mySurf->IsVClosed()) { // VRAI IsVClosed
118 aCySpCo = Standard_True;
119 uClosed = Standard_False;
125 TColgp_Array1OfPnt Pnts(1,4);
126 Pnts.SetValue(1,mySurf->Value(U1,V1));
127 Pnts.SetValue(2,mySurf->Value(U2,V1));
128 Pnts.SetValue(3,mySurf->Value(U1,V2));
129 Pnts.SetValue(4,mySurf->Value(U2,V2));
130 gp_Pln aPln;// Standard_Real Dmax;
131 Standard_Integer It = ShapeAnalysis_Geom::NearestPlane (Pnts,aPln,myGap/*Dmax*/);
133 // ICI, on fabrique le plan, et zou
134 if (It == 0 || myGap/*Dmax*/ > tol) return newSurf; // pas un plan
136 // IL RESTE a verifier l orientation ...
137 // On regarde sur chaque surface les vecteurs P(U0->U1),P(V0->V1)
138 // On prend la normale : les deux normales doivent etre dans le meme sens
139 // Sinon, inverser la normale (pas le Pln entier !) et refaire la Plane
140 newSurf = new Geom_Plane (aPln);
141 gp_Vec uold (Pnts(1),Pnts(2));
142 gp_Vec vold (Pnts(1),Pnts(3));
143 gp_Vec nold = uold.Crossed (vold);
144 gp_Vec unew (newSurf->Value(U1,V1), newSurf->Value(U2,V1));
145 gp_Vec vnew (newSurf->Value(U1,V1), newSurf->Value(U1,V2));
146 gp_Vec nnew = unew.Crossed (vnew);
147 if (nold.Dot (nnew) < 0.0) {
148 gp_Ax3 ax3 = aPln.Position();
152 newSurf = new Geom_Plane (aPln);
161 } else if (aCySpCo) {
166 nCP = nUP; nUP = nVP; nVP = nCP;
169 for (i=1; i<=3; i++) {
171 else if (i==2) V = V2;
172 else if (i==3) V = 0.5*(V1+V2);
174 if(uClosed) iso = mySurf->VIso(V);
175 else iso = mySurf->UIso(V);
178 iso->D0(0.5*(U1+U2), p2(i));
179 p3(i).SetCoord(0.5*(p1(i).X()+p2(i).X()),
180 0.5*(p1(i).Y()+p2(i).Y()),
181 0.5*(p1(i).Z()+p2(i).Z()));
182 R(i) = p3(i).Distance(p1(i));
183 // cout<<"sphere, i="<<i<<" V="<<V<<" R="<<R(i)<<" p1="<<p1(i).X()<<","<<p1(i).Y()<<","<<p1(i).Z()<<" p2="<<p2(i).X()<<","<<p2(i).Y()<<","<<p2(i).Z()<<" p3="<<p3(i).X()<<","<<p3(i).Y()<<","<<p3(i).Z()<<endl;
186 iso->D1 (0.,origPnt,origD1U);
187 gp_Vec xVec(p3(3), p1(3));
188 gp_Vec aVec(p3(1), p3(2));
189 // gp_Dir xDir(xVec); ne sert pas. Null si R3 = 0
191 gp_Ax3 aAx3 (p3(1),aDir,xVec);
192 // CKY 3-FEV-1997 : verification du sens de description
193 //gp_Dir AXY = aAx3.YDirection(); // AXY not used (skl)
194 if (aAx3.YDirection().Dot (origD1U) < 0) {
196 cout<<" Surface Analytique : sens a inverser"<<endl;
198 aAx3.YReverse(); // mais X reste !
202 if ((Abs(R(1)) < tol) &&
205 // deja fait gp_Ax3 aAx3(p3(1), aDir, xVec);
206 //gp_Ax3 aAx3(p3(3), aDir);
207 Handle(Geom_SphericalSurface) anObject =
208 new Geom_SphericalSurface(aAx3, R(3));
209 if (!uClosed) anObject->UReverse();
215 // deja fait gp_Ax3 aAx3(p3(1), aDir, xVec);
216 //gp_Ax3 aAx3(p3(1), aDir);
218 if (Abs(R(2)-R(1)) < tol) {
219 Handle(Geom_CylindricalSurface) anObject =
220 new Geom_CylindricalSurface(aAx3, R(1));
221 if (!uClosed) anObject->UReverse();
225 gp_Vec aVec2(p1(1), p1(2));
226 Standard_Real angle = aVec.Angle(aVec2);
228 Handle(Geom_ConicalSurface) anObject =
229 new Geom_ConicalSurface(aAx3, angle, R(1));
230 //if (!uClosed) anObject->UReverse();
231 anObject->UReverse();
236 gp_Vec anotherXVec(p3(2), p1(2));
237 gp_Dir anotherXDir(anotherXVec);
238 gp_Ax3 anotherAx3(p3(2), aDir, anotherXDir);
239 Handle(Geom_ConicalSurface) anObject =
240 new Geom_ConicalSurface(anotherAx3, angle, R(2));
241 //if (!uClosed) anObject->UReverse();
242 anObject->UReverse();
249 // test by iso U and isoV
250 Standard_Boolean isFound = Standard_False;
251 for (j=1; (j<=2) && !isFound; j++) {
257 for (i=1; i<=3; i++) {
259 else if (i==2) U = 0.5*(U1+U2);
260 else if (i==3) U = 0.25*(U1+U2);
262 iso = mySurf->UIso(U);
265 iso->D0(0.5*(V1+V2), p2(i));
266 p3(i).SetCoord(0.5*(p1(i).X()+p2(i).X()),
267 0.5*(p1(i).Y()+p2(i).Y()),
268 0.5*(p1(i).Z()+p2(i).Z()));
269 R(i) = p3(i).Distance(p1(i));
271 if ((Abs(R(1)-R(2))< tol) &&
272 (Abs(R(1)-R(3))< tol)) {
273 gp_Pnt p10(0.5*(p3(1).X()+p3(2).X()),
274 0.5*(p3(1).Y()+p3(2).Y()),
275 0.5*(p3(1).Z()+p3(2).Z()));
276 gp_Vec aVec(p10, p3(1));
277 gp_Vec aVec2(p10, p3(3));
278 Standard_Real RR1 = R(1), RR2 = R(2), RR3;
281 if (aVec.Magnitude() <= gp::Resolution()) aVec.SetCoord(0., 0., 1.);
285 gp_Ax3 aAx3(p10, aDir);
286 RR1 = p10.Distance(p3(1));
287 // modif empirique (pourtant NON DEMONTREE) : inverser roles RR1,RR2
289 if (RR1 < RR2) { RR3 = RR1; RR1 = RR2; RR2 = RR3; }
290 Handle(Geom_ToroidalSurface) anObject =
291 new Geom_ToroidalSurface(aAx3, RR1, RR2);
292 if (j==2) anObject->UReverse();
293 anObject->D1 (0.,0.,resPnt,resD1U,resD1V);
295 if (resD1U.Dot(origD1U) < 0 && j != 2)
296 cout<<" Tore a inverser !"<<endl;
299 isFound = Standard_True;
303 if (newSurf.IsNull()) return newSurf;
305 //---------------------------------------------------------------------
307 //---------------------------------------------------------------------
309 Handle(GeomAdaptor_HSurface) NHS = new GeomAdaptor_HSurface (newSurf);
310 GeomAdaptor_Surface& SurfAdapt = NHS->ChangeSurface();
312 const Standard_Integer NP = 21;
313 Standard_Real S, T; // U,V deja fait
315 Standard_Boolean onSurface = Standard_True;
317 Standard_Real dis; myGap = 0.;
321 for (j=1; (j<=NP) && onSurface; j++) {
324 if(uClosed) iso = mySurf->VIso(V);
325 else iso = mySurf->UIso(V);
327 for (i=1; i<=NP; i++) {
330 switch (SurfAdapt.GetType()){
332 case GeomAbs_Cylinder :
334 gp_Cylinder Cylinder = SurfAdapt.Cylinder();
335 ElSLib::Parameters( Cylinder, P3d, S, T);
340 gp_Cone Cone = SurfAdapt.Cone();
341 ElSLib::Parameters( Cone, P3d, S, T);
344 case GeomAbs_Sphere :
346 gp_Sphere Sphere = SurfAdapt.Sphere();
347 ElSLib::Parameters( Sphere, P3d, S, T);
352 gp_Torus Torus = SurfAdapt.Torus();
353 ElSLib::Parameters( Torus, P3d, S, T);
360 newSurf->D0(S, T, P3d2);
362 dis = P3d.Distance(P3d2);
363 if (dis > myGap) myGap = dis;
366 onSurface = Standard_False;
368 // The presumption is rejected
373 if (substitute && !NHS.IsNull()) {
379 //%pdn 30 Nov 98: converting bspline surfaces with degree+1 at ends to periodic
380 // UKI60591, entity 48720
381 Handle(Geom_Surface) ShapeCustom_Surface::ConvertToPeriodic (const Standard_Boolean substitute,
382 const Standard_Real preci)
384 Handle(Geom_Surface) newSurf;
385 Handle(Geom_BSplineSurface) BSpl = Handle(Geom_BSplineSurface)::DownCast(mySurf);
386 if (BSpl.IsNull()) return newSurf;
388 ShapeAnalysis_Surface sas(mySurf);
389 Standard_Boolean uclosed = sas.IsUClosed(preci);
390 Standard_Boolean vclosed = sas.IsVClosed(preci);
392 if ( ! uclosed && ! vclosed ) return newSurf;
394 Standard_Boolean converted = Standard_False; //:p6
396 if ( uclosed && ! BSpl->IsUPeriodic() && BSpl->NbUPoles() >3 ) {
397 Standard_Boolean set = Standard_True;
398 // if degree+1 at ends, first change it to 1 by rearranging knots
399 if ( BSpl->UMultiplicity(1) == BSpl->UDegree() + 1 &&
400 BSpl->UMultiplicity(BSpl->NbUKnots()) == BSpl->UDegree() + 1 ) {
401 Standard_Integer nbUPoles = BSpl->NbUPoles();
402 Standard_Integer nbVPoles = BSpl->NbVPoles();
403 TColgp_Array2OfPnt oldPoles(1,nbUPoles,1,nbVPoles);
404 TColStd_Array2OfReal oldWeights(1,nbUPoles,1,nbVPoles);
405 Standard_Integer nbUKnots = BSpl->NbUKnots();
406 Standard_Integer nbVKnots = BSpl->NbVKnots();
407 TColStd_Array1OfReal oldUKnots(1,nbUKnots);
408 TColStd_Array1OfReal oldVKnots(1,nbVKnots);
409 TColStd_Array1OfInteger oldUMults(1,nbUKnots);
410 TColStd_Array1OfInteger oldVMults(1,nbVKnots);
412 BSpl->Poles(oldPoles);
413 BSpl->Weights(oldWeights);
414 BSpl->UKnots(oldUKnots);
415 BSpl->VKnots(oldVKnots);
416 BSpl->UMultiplicities(oldUMults);
417 BSpl->VMultiplicities(oldVMults);
419 TColStd_Array1OfReal newUKnots (1,nbUKnots+2);
420 TColStd_Array1OfInteger newUMults(1,nbUKnots+2);
421 Standard_Real a = 0.5 * ( BSpl->UKnot(2) - BSpl->UKnot(1) +
422 BSpl->UKnot(nbUKnots) - BSpl->UKnot(nbUKnots-1) );
424 newUKnots(1) = oldUKnots(1) - a;
425 newUKnots(nbUKnots+2) = oldUKnots(nbUKnots) + a;
426 newUMults(1) = newUMults(nbUKnots+2) = 1;
427 for (Standard_Integer i = 2; i<=nbUKnots+1; i++) {
428 newUKnots(i) = oldUKnots(i-1);
429 newUMults(i) = oldUMults(i-1);
431 newUMults(2) = newUMults(nbUKnots+1) = BSpl->UDegree();
432 Handle(Geom_BSplineSurface) res = new Geom_BSplineSurface(oldPoles,
436 BSpl->UDegree(),BSpl->VDegree(),
437 BSpl->IsUPeriodic(),BSpl->IsVPeriodic());
440 else if ( BSpl->UMultiplicity(1) > BSpl->UDegree() ||
441 BSpl->UMultiplicity(BSpl->NbUKnots()) > BSpl->UDegree() + 1 ) set = Standard_False;
443 BSpl->SetUPeriodic(); // make periodic
444 converted = Standard_True;
448 if ( vclosed && ! BSpl->IsVPeriodic() && BSpl->NbVPoles() >3 ) {
449 Standard_Boolean set = Standard_True;
450 // if degree+1 at ends, first change it to 1 by rearranging knots
451 if ( BSpl->VMultiplicity(1) == BSpl->VDegree() + 1 &&
452 BSpl->VMultiplicity(BSpl->NbVKnots()) == BSpl->VDegree() + 1 ) {
453 Standard_Integer nbUPoles = BSpl->NbUPoles();
454 Standard_Integer nbVPoles = BSpl->NbVPoles();
455 TColgp_Array2OfPnt oldPoles(1,nbUPoles,1,nbVPoles);
456 TColStd_Array2OfReal oldWeights(1,nbUPoles,1,nbVPoles);
457 Standard_Integer nbUKnots = BSpl->NbUKnots();
458 Standard_Integer nbVKnots = BSpl->NbVKnots();
459 TColStd_Array1OfReal oldUKnots(1,nbUKnots);
460 TColStd_Array1OfReal oldVKnots(1,nbVKnots);
461 TColStd_Array1OfInteger oldUMults(1,nbUKnots);
462 TColStd_Array1OfInteger oldVMults(1,nbVKnots);
464 BSpl->Poles(oldPoles);
465 BSpl->Weights(oldWeights);
466 BSpl->UKnots(oldUKnots);
467 BSpl->VKnots(oldVKnots);
468 BSpl->UMultiplicities(oldUMults);
469 BSpl->VMultiplicities(oldVMults);
471 TColStd_Array1OfReal newVKnots (1,nbVKnots+2);
472 TColStd_Array1OfInteger newVMults(1,nbVKnots+2);
473 Standard_Real a = 0.5 * ( BSpl->VKnot(2) - BSpl->VKnot(1) +
474 BSpl->VKnot(nbVKnots) - BSpl->VKnot(nbVKnots-1) );
476 newVKnots(1) = oldVKnots(1) - a;
477 newVKnots(nbVKnots+2) = oldVKnots(nbVKnots) + a;
478 newVMults(1) = newVMults(nbVKnots+2) = 1;
479 for (Standard_Integer i = 2; i<=nbVKnots+1; i++) {
480 newVKnots(i) = oldVKnots(i-1);
481 newVMults(i) = oldVMults(i-1);
483 newVMults(2) = newVMults(nbVKnots+1) = BSpl->VDegree();
484 Handle(Geom_BSplineSurface) res = new Geom_BSplineSurface(oldPoles,
488 BSpl->UDegree(),BSpl->VDegree(),
489 BSpl->IsUPeriodic(),BSpl->IsVPeriodic());
492 else if ( BSpl->VMultiplicity(1) > BSpl->VDegree() ||
493 BSpl->VMultiplicity(BSpl->NbVKnots()) > BSpl->VDegree() + 1 ) set = Standard_False;
495 BSpl->SetVPeriodic(); // make periodic
496 converted = Standard_True;
501 cout << "Warning: ShapeCustom_Surface: Closed BSplineSurface is caused to be periodic" << endl;
503 if ( ! converted ) return newSurf;
505 if ( substitute ) mySurf = newSurf;