1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and / or modify it
7 // under the terms of the GNU Lesser General Public version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
17 #include <TCollection_AsciiString.hxx>
18 #include <TCollection_ExtendedString.hxx>
19 #include <Prs3d_Arrow.hxx>
20 #include <Prs3d_Root.hxx>
21 #include <Prs3d_LineAspect.hxx>
22 #include <Prs3d_ArrowAspect.hxx>
24 #include <Graphic3d_Group.hxx>
25 #include <Graphic3d_Vertex.hxx>
26 #include <Graphic3d_AspectMarker3d.hxx>
27 #include <Graphic3d_AspectLine3d.hxx>
28 #include <Graphic3d_ArrayOfPoints.hxx>
29 #include <Aspect_TypeOfLine.hxx>
30 #include <Aspect_TypeOfMarker.hxx>
31 #include <Aspect_AspectMarker.hxx>
32 #include <Quantity_Color.hxx>
34 #include <Precision.hxx>
38 #include <Geom_Line.hxx>
39 #include <Geom_Circle.hxx>
40 #include <GeomAPI_ExtremaCurveCurve.hxx>
41 #include <GeomAPI_ProjectPointOnSurf.hxx>
42 #include <GeomAPI_ProjectPointOnCurve.hxx>
44 #include <gce_MakeLin.hxx>
46 void DsgPrs::ComputeSymbol (const Handle(Prs3d_Presentation)& aPresentation,
47 const Handle(Prs3d_DimensionAspect)& LA,
52 const DsgPrs_ArrowSide ArrowSide,
53 const Standard_Boolean drawFromCenter)
55 Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
57 Quantity_Color aColor;
58 Aspect_TypeOfLine aType;
60 LA->LineAspect()->Aspect()->Values (aColor, aType, aWidth);
61 Handle(Graphic3d_AspectMarker3d) aMarkerAsp = new Graphic3d_AspectMarker3d (Aspect_TOM_O, aColor, 1.0);
62 Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect (aMarkerAsp);
63 Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
70 case DsgPrs_AS_FIRSTAR:
73 Prs3d_Arrow::Draw(aPresentation,
76 LA->ArrowAspect()->Angle(),
77 LA->ArrowAspect()->Length());
80 case DsgPrs_AS_LASTAR:
83 Prs3d_Arrow::Draw(aPresentation,
86 LA->ArrowAspect()->Angle(),
87 LA->ArrowAspect()->Length());
91 case DsgPrs_AS_BOTHAR:
93 Prs3d_Arrow::Draw(aPresentation,
96 LA->ArrowAspect()->Angle(),
97 LA->ArrowAspect()->Length());
98 Prs3d_Arrow::Draw(aPresentation,
101 LA->ArrowAspect()->Angle(),
102 LA->ArrowAspect()->Length());
107 case DsgPrs_AS_FIRSTPT:
111 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
112 anArrayOfPoints->AddVertex (pt1.X(), pt1.Y(), pt1.Z());
113 Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray (anArrayOfPoints);
118 case DsgPrs_AS_LASTPT:
120 // On dessine un rond
121 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
122 anArrayOfPoints->AddVertex (pt2.X(), pt2.Y(), pt2.Z());
123 Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray (anArrayOfPoints);
127 case DsgPrs_AS_BOTHPT:
131 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints1 = new Graphic3d_ArrayOfPoints (1);
132 anArrayOfPoints1->AddVertex (pt1.X(), pt1.Y(), pt1.Z());
133 Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray (anArrayOfPoints1);
137 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints2 = new Graphic3d_ArrayOfPoints (1);
138 anArrayOfPoints2->AddVertex (pt2.X(), pt2.Y(), pt2.Z());
139 Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray (anArrayOfPoints2);
144 case DsgPrs_AS_FIRSTAR_LASTPT:
147 Prs3d_Arrow::Draw(aPresentation,
150 LA->ArrowAspect()->Angle(),
151 LA->ArrowAspect()->Length());
153 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
154 anArrayOfPoints->AddVertex (pt2.X(), pt2.Y(), pt2.Z());
155 Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray (anArrayOfPoints);
159 case DsgPrs_AS_FIRSTPT_LASTAR:
164 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
165 anArrayOfPoints->AddVertex (pt1.X(), pt1.Y(), pt1.Z());
166 Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray (anArrayOfPoints);
169 Prs3d_Arrow::Draw(aPresentation,
172 LA->ArrowAspect()->Angle(),
173 LA->ArrowAspect()->Length());
180 //=======================================================================
181 //function : ComputePlanarFacesLengthPresentation
183 //=======================================================================
185 void DsgPrs::ComputePlanarFacesLengthPresentation( const Standard_Real FirstArrowLength,
186 const Standard_Real SecondArrowLength,
187 const gp_Pnt& AttachmentPoint1,
188 const gp_Pnt& AttachmentPoint2,
189 const gp_Dir& DirAttach,
190 const gp_Pnt& OffsetPoint,
191 const gp_Pln& PlaneOfFaces,
192 gp_Pnt & EndOfArrow1,
193 gp_Pnt & EndOfArrow2,
194 gp_Dir & DirOfArrow1 )
196 gp_Lin FirstLin( AttachmentPoint1, DirAttach );
197 gp_Lin SecondLin( AttachmentPoint2, DirAttach );
199 EndOfArrow1 = ElCLib::Value( ElCLib::Parameter( FirstLin, OffsetPoint ), FirstLin );
200 EndOfArrow2 = ElCLib::Value( ElCLib::Parameter( SecondLin, OffsetPoint ), SecondLin );
202 if (EndOfArrow1.SquareDistance( EndOfArrow2 ) > Precision::SquareConfusion()) // not null length
204 gp_Dir LengthDir( gp_Vec( EndOfArrow1, EndOfArrow2 ) );
205 if ((FirstArrowLength + SecondArrowLength)*(FirstArrowLength + SecondArrowLength) <
206 EndOfArrow1.SquareDistance( EndOfArrow2 ))
207 DirOfArrow1 = -LengthDir;
209 DirOfArrow1 = LengthDir;
212 DirOfArrow1 = PlaneOfFaces.Axis().Direction();
215 //=======================================================================
216 //function : ComputeCurvilinearFacesLengthPresentation
218 //=======================================================================
220 void DsgPrs::ComputeCurvilinearFacesLengthPresentation( const Standard_Real FirstArrowLength,
221 const Standard_Real SecondArrowLength,
222 const Handle( Geom_Surface )& SecondSurf,
223 const gp_Pnt& AttachmentPoint1,
224 const gp_Pnt& AttachmentPoint2,
225 const gp_Dir& DirAttach,
226 gp_Pnt & EndOfArrow2,
227 gp_Dir & DirOfArrow1,
228 Handle( Geom_Curve )& VCurve,
229 Handle( Geom_Curve )& UCurve,
230 Standard_Real & FirstU,
231 Standard_Real & deltaU,
232 Standard_Real & FirstV,
233 Standard_Real & deltaV )
235 GeomAPI_ProjectPointOnSurf ProjectorOnSurface;
236 GeomAPI_ProjectPointOnCurve ProjectorOnCurve;
237 Quantity_Parameter U1, V1, U2, V2;
238 Standard_Real LastU, LastV;
239 Standard_Real SquareTolerance = Precision::SquareConfusion();
241 ProjectorOnSurface.Init( AttachmentPoint1, SecondSurf );
242 Standard_Integer Index(1);
243 Quantity_Length MinDist = RealLast();
244 Quantity_Parameter LocalU, LocalV;
247 for (Standard_Integer i = 1; i <= ProjectorOnSurface.NbPoints(); i++)
249 ProjectorOnSurface.Parameters( i, LocalU, LocalV );
251 SecondSurf->D1( LocalU, LocalV, EndOfArrow2, D1U, D1V );
252 if (D1U.SquareMagnitude() <= SquareTolerance || D1V.SquareMagnitude() <= SquareTolerance)
253 LocalDir = gp_Dir( gp_Vec( AttachmentPoint1, ProjectorOnSurface.Point( i ) ) );
255 LocalDir = gp_Dir( D1U ^ D1V );
256 if (DirAttach.IsParallel( LocalDir, Precision::Angular() ) && ProjectorOnSurface.Distance( i ) < MinDist)
259 MinDist = ProjectorOnSurface.Distance( i );
262 EndOfArrow2 = ProjectorOnSurface.Point( Index );
263 ProjectorOnSurface.Parameters( Index, U1, V1 );
265 if ((FirstArrowLength + SecondArrowLength)*(FirstArrowLength + SecondArrowLength) <
266 AttachmentPoint1.SquareDistance( EndOfArrow2 ))
267 DirOfArrow1 = -DirAttach;
269 DirOfArrow1 = DirAttach;
271 if (EndOfArrow2.SquareDistance( AttachmentPoint2 ) > Precision::SquareConfusion())
273 VCurve = SecondSurf->VIso( V1 );
274 ProjectorOnCurve.Init( EndOfArrow2, VCurve );
275 FirstU = ProjectorOnCurve.LowerDistanceParameter();
277 ProjectorOnSurface.Init( AttachmentPoint2, SecondSurf );
278 ProjectorOnSurface.LowerDistanceParameters( U2, V2 );
279 UCurve = SecondSurf->UIso( U2 );
281 ProjectorOnCurve.Init( AttachmentPoint2, UCurve );
282 LastV = ProjectorOnCurve.LowerDistanceParameter();
284 gp_Pnt Intersection = SecondSurf->Value( U2, V1 );
285 ProjectorOnCurve.Init( Intersection, VCurve );
286 LastU = ProjectorOnCurve.LowerDistanceParameter();
287 ProjectorOnCurve.Init( Intersection, UCurve );
288 FirstV = ProjectorOnCurve.LowerDistanceParameter();
290 deltaU = LastU - FirstU;
291 deltaV = LastV - FirstV;
293 if (VCurve->IsPeriodic() && Abs( deltaU ) > VCurve->Period()/2)
295 Standard_Real Sign = (deltaU > 0.0)? -1.0 : 1.0;
296 deltaU = VCurve->Period() - Abs( deltaU );
299 if (UCurve->IsPeriodic() && Abs( deltaV ) > UCurve->Period()/2)
301 Standard_Real Sign = (deltaV > 0.0)? -1.0 : 1.0;
302 deltaV = UCurve->Period() - Abs( deltaV );
309 //=======================================================================
310 //function : ComputeFacesAnglePresentation
312 //=======================================================================
314 void DsgPrs::ComputeFacesAnglePresentation( const Standard_Real ArrowLength,
315 const Standard_Real Value,
316 const gp_Pnt& CenterPoint,
317 const gp_Pnt& AttachmentPoint1,
318 const gp_Pnt& AttachmentPoint2,
321 const gp_Dir& axisdir,
322 const Standard_Boolean isPlane,
323 const gp_Ax1& AxisOfSurf,
324 const gp_Pnt& OffsetPoint,
326 Standard_Real & FirstParAngleCirc,
327 Standard_Real & LastParAngleCirc,
328 gp_Pnt & EndOfArrow1,
329 gp_Pnt & EndOfArrow2,
330 gp_Dir & DirOfArrow1,
331 gp_Dir & DirOfArrow2,
332 gp_Pnt & ProjAttachPoint2,
333 gp_Circ & AttachCirc,
334 Standard_Real & FirstParAttachCirc,
335 Standard_Real & LastParAttachCirc )
337 if (Value > Precision::Angular() && Abs( M_PI-Value ) > Precision::Angular())
339 // Computing presentation of angle's arc
340 gp_Ax2 ax( CenterPoint, axisdir, dir1 );
341 AngleCirc.SetPosition( ax );
342 AngleCirc.SetRadius( CenterPoint.Distance( OffsetPoint ) );
344 vec1 *= AngleCirc.Radius();
345 gp_Pnt p1 = CenterPoint.Translated( vec1 );
347 vec2 *= AngleCirc.Radius();
348 gp_Pnt p2 = CenterPoint.Translated( vec2 );
350 Standard_Real Par1 = 0.;
351 Standard_Real Par2 = ElCLib::Parameter( AngleCirc, p2 );
352 Standard_Real Par0 = ElCLib::Parameter( AngleCirc, OffsetPoint );
354 gp_Vec PosVec( CenterPoint, OffsetPoint );
355 gp_Vec NormalOfPlane = vec1 ^ vec2;
357 gp_Vec Normal1 = NormalOfPlane ^ vec1;
358 gp_Vec Normal2 = NormalOfPlane ^ vec2;
359 Standard_Integer Sign1 = (PosVec * Normal1 >= 0)? 1 : -1;
360 Standard_Integer Sign2 = (PosVec * Normal2 >= 0)? 1 : -1;
361 if (Sign1 == 1 && Sign2 == -1)
363 FirstParAngleCirc = Par1;
364 LastParAngleCirc = Par2;
366 else if (Sign1 == 1 && Sign2 == 1)
368 FirstParAngleCirc = Par1;
369 LastParAngleCirc = Par0;
371 else if (Sign1 == -1 && Sign2 == 1)
375 FirstParAngleCirc = Par1;
376 LastParAngleCirc = Par2;
378 else //Sign1 == -1 && Sign2 == -1
380 AngleCirc.SetPosition( gp_Ax2( CenterPoint, axisdir, gp_Dir( PosVec ) ) );
382 Par1 = ElCLib::Parameter( AngleCirc, p1 );
383 Par2 = ElCLib::Parameter( AngleCirc, p2 );
384 FirstParAngleCirc = Par0;
385 LastParAngleCirc = Par2;
388 // Computing presentation of arrows
389 EndOfArrow1 = ElCLib::Value( Par1, AngleCirc );
390 EndOfArrow2 = ElCLib::Value( Par2, AngleCirc );
391 Standard_Real beta = 0.;
392 if (AngleCirc.Radius() > Precision::Confusion())
393 beta = ArrowLength / AngleCirc.Radius();
394 gp_Pnt OriginOfArrow1 = ElCLib::Value( Par1 + beta, AngleCirc );
395 gp_Pnt OriginOfArrow2 = ElCLib::Value( Par2 - beta, AngleCirc );
396 DirOfArrow1 = gp_Dir( gp_Vec( OriginOfArrow1, EndOfArrow1 ) );
397 DirOfArrow2 = gp_Dir( gp_Vec( OriginOfArrow2, EndOfArrow2 ) );
398 if (EndOfArrow1.SquareDistance( EndOfArrow2 ) <= (ArrowLength + ArrowLength)*(ArrowLength + ArrowLength))
400 DirOfArrow1.Reverse();
401 DirOfArrow2.Reverse();
404 else // dir1 and dir2 are parallel
406 gp_Dir ArrowDir = axisdir ^ dir1;
407 DirOfArrow1 = ArrowDir;
408 DirOfArrow2 = -ArrowDir;
409 gp_Lin DirLine( AttachmentPoint1, dir1 );
410 EndOfArrow1 = ElCLib::Value( ElCLib::Parameter( DirLine, OffsetPoint ), DirLine );
411 EndOfArrow2 = EndOfArrow1;
414 // Line or arc from AttachmentPoint2 to its "projection"
415 gp_Lin SecondLin( CenterPoint, dir2 );
416 if (SecondLin.Contains( AttachmentPoint2, Precision::Confusion() ))
417 ProjAttachPoint2 = AttachmentPoint2;
421 ProjAttachPoint2 = ElCLib::Value( ElCLib::Parameter( SecondLin, AttachmentPoint2 ), SecondLin );
424 gp_Lin LineOfAxis( AxisOfSurf );
425 gp_Pnt CenterOfArc = ElCLib::Value( ElCLib::Parameter( LineOfAxis, AttachmentPoint2 ),
428 gp_Ax2 Ax2( CenterOfArc,
429 AxisOfSurf.Direction(),
430 gp_Dir( gp_Vec( CenterOfArc, AttachmentPoint2 ) ) );
431 AttachCirc.SetPosition( Ax2 );
432 AttachCirc.SetRadius( CenterOfArc.Distance( AttachmentPoint2 ) );
434 GeomAPI_ExtremaCurveCurve Intersection( new Geom_Circle( AttachCirc ),
435 new Geom_Line( SecondLin ) );
436 Intersection.NearestPoints( ProjAttachPoint2, ProjAttachPoint2 );
438 Standard_Real U2 = ElCLib::Parameter( AttachCirc, ProjAttachPoint2 );
441 FirstParAttachCirc = 0;
442 LastParAttachCirc = U2;
446 FirstParAttachCirc = U2;
447 LastParAttachCirc = 2*M_PI;
455 void DsgPrs::ComputeFilletRadiusPresentation( const Standard_Real /*ArrowLength*/,
456 const Standard_Real Value,
457 const gp_Pnt & Position,
458 const gp_Dir & NormalDir,
459 const gp_Pnt & FirstPoint,
460 const gp_Pnt & SecondPoint,
461 const gp_Pnt & Center,
462 const gp_Pnt & BasePnt,
463 const Standard_Boolean drawRevers,
464 Standard_Boolean & SpecCase,
465 gp_Circ & FilletCirc,
466 Standard_Real & FirstParCirc,
467 Standard_Real & LastParCirc,
470 gp_Pnt & DrawPosition)
472 gp_Dir dir1(gp_Vec(Center, FirstPoint));
473 gp_Dir dir2(gp_Vec(Center, SecondPoint));
474 Standard_Real Angle = dir1.Angle(dir2);
475 if(Angle <= Precision::Angular() || ( M_PI - Angle ) <= Precision::Angular() ||
476 Value <= Precision::Confusion()) SpecCase = Standard_True;
477 else SpecCase = Standard_False;
480 // Computing presentation of fillet's arc
481 gp_Ax2 ax( Center, NormalDir, dir1 );
482 FilletCirc.SetPosition( ax );
483 FilletCirc.SetRadius( Center.Distance( FirstPoint ) ); //***
485 vec1 *= FilletCirc.Radius();
489 Center.Translated( vec1 );
491 vec2 *= FilletCirc.Radius();
495 Center.Translated( vec2 );
497 if(! Center.IsEqual( Position, Precision::Confusion() ))
498 PosVec.SetXYZ( gp_Vec(Center, Position).XYZ() );
500 PosVec.SetXYZ( (vec1.Added(vec2)).XYZ() );
501 gp_Vec NormalOfPlane = vec1 ^ vec2;
502 gp_Vec Normal1 = NormalOfPlane ^ vec1;
503 gp_Vec Normal2 = NormalOfPlane ^ vec2;
504 Standard_Integer Sign1 = (PosVec * Normal1 >= 0)? 1 : -1;
505 Standard_Integer Sign2 = (PosVec * Normal2 >= 0)? 1 : -1;
506 gp_Lin L1( Center, dir1 );
507 gp_Lin L2( Center, dir2 );
508 if ( Sign1 != Sign2 )
510 DrawPosition = Position; //***
511 gp_Dir direction(PosVec) ;
512 Standard_Real angle = dir1.Angle(direction) ;
513 if (( dir1 ^ direction) * NormalDir < 0.0e0) angle = -angle ;
514 if(Sign1 == -1) angle += M_PI;
515 EndOfArrow = ElCLib::Value(angle, FilletCirc); //***
520 if(L1.Distance(Position) < L2.Distance(Position))
522 EndOfArrow = FirstPoint; //***
523 DrawPosition = ElCLib::Value(ElCLib::Parameter(L1, Position), L1);
527 EndOfArrow = SecondPoint; //***
528 DrawPosition = ElCLib::Value(ElCLib::Parameter(L2, Position), L2);
531 if((dir1^dir2).IsOpposite(NormalDir, Precision::Angular()))
533 gp_Dir newdir = NormalDir.Reversed() ;
534 gp_Ax2 axnew( Center, newdir, dir1 );
535 FilletCirc.SetPosition( axnew );
537 FirstParCirc = ElCLib::Parameter( FilletCirc, FirstPoint );
538 LastParCirc = ElCLib::Parameter( FilletCirc, SecondPoint );
543 else //Angle equal 0 or PI or R = 0
545 DrawPosition = Position;
546 EndOfArrow = BasePnt;
552 gp_Vec Vd(DrawPosition, EndOfArrow);
553 DrawPosition.Translate(Vd *2);
555 DirOfArrow.SetXYZ(gp_Dir(gp_Vec(DrawPosition, EndOfArrow)).XYZ());
558 //=======================================================================
559 //function : ComputeRadiusLine
561 //=======================================================================
563 void DsgPrs::ComputeRadiusLine(const gp_Pnt & aCenter,
564 const gp_Pnt & anEndOfArrow,
565 const gp_Pnt & aPosition,
566 const Standard_Boolean drawFromCenter,
567 gp_Pnt & aRadLineOrign,
568 gp_Pnt & aRadLineEnd)
572 gp_Lin RadiusLine = gce_MakeLin( aCenter, anEndOfArrow );
573 Standard_Real PosParOnLine = ElCLib::Parameter( RadiusLine, aPosition );
574 Standard_Real EndOfArrowPar = ElCLib::Parameter( RadiusLine, anEndOfArrow );
575 if (PosParOnLine < 0.0)
577 aRadLineOrign = aPosition;
578 aRadLineEnd = anEndOfArrow;
580 else if (PosParOnLine > EndOfArrowPar)
582 aRadLineOrign = aPosition;
583 aRadLineEnd = aCenter;
587 aRadLineOrign = aCenter;
588 aRadLineEnd = anEndOfArrow;
593 aRadLineOrign = aPosition;
594 aRadLineEnd = anEndOfArrow;
599 //=======================================================================
600 //function : DistanceFromApex
602 //=======================================================================
604 Standard_Real DsgPrs::DistanceFromApex(const gp_Elips & elips,
606 const Standard_Real par)
609 Standard_Real parApex = ElCLib::Parameter ( elips, Apex );
610 if(parApex == 0.0 || parApex == M_PI)
612 if(parApex == 0.0) //pos Apex
613 dist = (par < M_PI) ? par : (2*M_PI - par);
615 dist = (par < M_PI) ? ( M_PI - par) : ( par - M_PI );
619 if(parApex == M_PI / 2) //pos Apex
621 if(par <= parApex + M_PI && par > parApex )
622 dist = par - parApex;
625 if(par > parApex + M_PI)
626 dist = 2*M_PI - par + parApex;
628 dist = parApex - par; // 0 < par < M_PI/2
631 else //neg Apex == 3/2 PI
633 if(par <= parApex && par >= M_PI/2)
634 dist = parApex - par;
638 dist = par - parApex;
640 dist = par + M_PI/2; // 0 < par < PI/2