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 under
7 // the terms of the GNU Lesser General Public License 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 <Aspect_TypeOfLine.hxx>
18 #include <Aspect_TypeOfMarker.hxx>
20 #include <gce_MakeLin.hxx>
21 #include <Geom_Circle.hxx>
22 #include <Geom_Curve.hxx>
23 #include <Geom_Line.hxx>
24 #include <Geom_Surface.hxx>
25 #include <GeomAPI_ExtremaCurveCurve.hxx>
26 #include <GeomAPI_ProjectPointOnCurve.hxx>
27 #include <GeomAPI_ProjectPointOnSurf.hxx>
29 #include <gp_Circ.hxx>
31 #include <gp_Elips.hxx>
35 #include <Graphic3d_ArrayOfPoints.hxx>
36 #include <Graphic3d_AspectLine3d.hxx>
37 #include <Graphic3d_AspectMarker3d.hxx>
38 #include <Graphic3d_Group.hxx>
39 #include <Precision.hxx>
40 #include <Prs3d_Arrow.hxx>
41 #include <Prs3d_ArrowAspect.hxx>
42 #include <Prs3d_DimensionAspect.hxx>
43 #include <Prs3d_LineAspect.hxx>
44 #include <Prs3d_Presentation.hxx>
45 #include <Prs3d_Root.hxx>
46 #include <Quantity_Color.hxx>
47 #include <TCollection_AsciiString.hxx>
48 #include <TCollection_ExtendedString.hxx>
50 void DsgPrs::ComputeSymbol (const Handle(Prs3d_Presentation)& aPresentation,
51 const Handle(Prs3d_DimensionAspect)& LA,
56 const DsgPrs_ArrowSide ArrowSide,
57 const Standard_Boolean drawFromCenter)
59 Handle(Graphic3d_Group) aGroup = aPresentation->NewGroup();
61 Quantity_Color aColor = LA->LineAspect()->Aspect()->Color();
62 Handle(Graphic3d_AspectMarker3d) aMarkerAsp = new Graphic3d_AspectMarker3d (Aspect_TOM_O, aColor, 1.0);
63 aGroup->SetGroupPrimitivesAspect (LA->LineAspect()->Aspect());
70 case DsgPrs_AS_FIRSTAR:
72 Prs3d_Arrow::Draw (aGroup,
75 LA->ArrowAspect()->Angle(),
76 LA->ArrowAspect()->Length());
79 case DsgPrs_AS_LASTAR:
82 Prs3d_Arrow::Draw (aGroup,
85 LA->ArrowAspect()->Angle(),
86 LA->ArrowAspect()->Length());
90 case DsgPrs_AS_BOTHAR:
92 Prs3d_Arrow::Draw (aGroup,
95 LA->ArrowAspect()->Angle(),
96 LA->ArrowAspect()->Length());
97 Prs3d_Arrow::Draw (aGroup,
100 LA->ArrowAspect()->Angle(),
101 LA->ArrowAspect()->Length());
106 case DsgPrs_AS_FIRSTPT:
110 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
111 anArrayOfPoints->AddVertex (pt1.X(), pt1.Y(), pt1.Z());
112 Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray (anArrayOfPoints);
117 case DsgPrs_AS_LASTPT:
119 // On dessine un rond
120 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
121 anArrayOfPoints->AddVertex (pt2.X(), pt2.Y(), pt2.Z());
122 Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray (anArrayOfPoints);
126 case DsgPrs_AS_BOTHPT:
130 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints1 = new Graphic3d_ArrayOfPoints (2);
131 anArrayOfPoints1->AddVertex (pt1.X(), pt1.Y(), pt1.Z());
132 anArrayOfPoints1->AddVertex (pt2.X(), pt2.Y(), pt2.Z());
133 aGroup->SetGroupPrimitivesAspect (aMarkerAsp);
134 aGroup->AddPrimitiveArray (anArrayOfPoints1);
139 case DsgPrs_AS_FIRSTAR_LASTPT:
142 Prs3d_Arrow::Draw (aGroup,
145 LA->ArrowAspect()->Angle(),
146 LA->ArrowAspect()->Length());
148 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
149 anArrayOfPoints->AddVertex (pt2.X(), pt2.Y(), pt2.Z());
150 aGroup->SetPrimitivesAspect (aMarkerAsp);
151 aGroup->AddPrimitiveArray (anArrayOfPoints);
155 case DsgPrs_AS_FIRSTPT_LASTAR:
158 Prs3d_Arrow::Draw (aGroup,
161 LA->ArrowAspect()->Angle(),
162 LA->ArrowAspect()->Length());
167 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
168 anArrayOfPoints->AddVertex (pt1.X(), pt1.Y(), pt1.Z());
169 aGroup->SetPrimitivesAspect (aMarkerAsp);
170 aGroup->AddPrimitiveArray (anArrayOfPoints);
178 //=======================================================================
179 //function : ComputePlanarFacesLengthPresentation
181 //=======================================================================
183 void DsgPrs::ComputePlanarFacesLengthPresentation( const Standard_Real FirstArrowLength,
184 const Standard_Real SecondArrowLength,
185 const gp_Pnt& AttachmentPoint1,
186 const gp_Pnt& AttachmentPoint2,
187 const gp_Dir& DirAttach,
188 const gp_Pnt& OffsetPoint,
189 const gp_Pln& PlaneOfFaces,
190 gp_Pnt & EndOfArrow1,
191 gp_Pnt & EndOfArrow2,
192 gp_Dir & DirOfArrow1 )
194 gp_Lin FirstLin( AttachmentPoint1, DirAttach );
195 gp_Lin SecondLin( AttachmentPoint2, DirAttach );
197 EndOfArrow1 = ElCLib::Value( ElCLib::Parameter( FirstLin, OffsetPoint ), FirstLin );
198 EndOfArrow2 = ElCLib::Value( ElCLib::Parameter( SecondLin, OffsetPoint ), SecondLin );
200 if (EndOfArrow1.SquareDistance( EndOfArrow2 ) > Precision::SquareConfusion()) // not null length
202 gp_Dir LengthDir( gp_Vec( EndOfArrow1, EndOfArrow2 ) );
203 if ((FirstArrowLength + SecondArrowLength)*(FirstArrowLength + SecondArrowLength) <
204 EndOfArrow1.SquareDistance( EndOfArrow2 ))
205 DirOfArrow1 = -LengthDir;
207 DirOfArrow1 = LengthDir;
210 DirOfArrow1 = PlaneOfFaces.Axis().Direction();
213 //=======================================================================
214 //function : ComputeCurvilinearFacesLengthPresentation
216 //=======================================================================
218 void DsgPrs::ComputeCurvilinearFacesLengthPresentation( const Standard_Real FirstArrowLength,
219 const Standard_Real SecondArrowLength,
220 const Handle( Geom_Surface )& SecondSurf,
221 const gp_Pnt& AttachmentPoint1,
222 const gp_Pnt& AttachmentPoint2,
223 const gp_Dir& DirAttach,
224 gp_Pnt & EndOfArrow2,
225 gp_Dir & DirOfArrow1,
226 Handle( Geom_Curve )& VCurve,
227 Handle( Geom_Curve )& UCurve,
228 Standard_Real & FirstU,
229 Standard_Real & deltaU,
230 Standard_Real & FirstV,
231 Standard_Real & deltaV )
233 GeomAPI_ProjectPointOnSurf ProjectorOnSurface;
234 GeomAPI_ProjectPointOnCurve ProjectorOnCurve;
235 Standard_Real U1, V1, U2, V2;
236 Standard_Real LastU, LastV;
237 Standard_Real SquareTolerance = Precision::SquareConfusion();
239 ProjectorOnSurface.Init( AttachmentPoint1, SecondSurf );
240 Standard_Integer Index(1);
241 Standard_Real MinDist = RealLast();
242 Standard_Real LocalU, LocalV;
245 for (Standard_Integer i = 1; i <= ProjectorOnSurface.NbPoints(); i++)
247 ProjectorOnSurface.Parameters( i, LocalU, LocalV );
249 SecondSurf->D1( LocalU, LocalV, EndOfArrow2, D1U, D1V );
250 if (D1U.SquareMagnitude() <= SquareTolerance || D1V.SquareMagnitude() <= SquareTolerance)
251 LocalDir = gp_Dir( gp_Vec( AttachmentPoint1, ProjectorOnSurface.Point( i ) ) );
253 LocalDir = gp_Dir( D1U ^ D1V );
254 if (DirAttach.IsParallel( LocalDir, Precision::Angular() ) && ProjectorOnSurface.Distance( i ) < MinDist)
257 MinDist = ProjectorOnSurface.Distance( i );
260 EndOfArrow2 = ProjectorOnSurface.Point( Index );
261 ProjectorOnSurface.Parameters( Index, U1, V1 );
263 if ((FirstArrowLength + SecondArrowLength)*(FirstArrowLength + SecondArrowLength) <
264 AttachmentPoint1.SquareDistance( EndOfArrow2 ))
265 DirOfArrow1 = -DirAttach;
267 DirOfArrow1 = DirAttach;
269 if (EndOfArrow2.SquareDistance( AttachmentPoint2 ) > Precision::SquareConfusion())
271 VCurve = SecondSurf->VIso( V1 );
272 ProjectorOnCurve.Init( EndOfArrow2, VCurve );
273 FirstU = ProjectorOnCurve.LowerDistanceParameter();
275 ProjectorOnSurface.Init( AttachmentPoint2, SecondSurf );
276 ProjectorOnSurface.LowerDistanceParameters( U2, V2 );
277 UCurve = SecondSurf->UIso( U2 );
279 ProjectorOnCurve.Init( AttachmentPoint2, UCurve );
280 LastV = ProjectorOnCurve.LowerDistanceParameter();
282 gp_Pnt Intersection = SecondSurf->Value( U2, V1 );
283 ProjectorOnCurve.Init( Intersection, VCurve );
284 LastU = ProjectorOnCurve.LowerDistanceParameter();
285 ProjectorOnCurve.Init( Intersection, UCurve );
286 FirstV = ProjectorOnCurve.LowerDistanceParameter();
288 deltaU = LastU - FirstU;
289 deltaV = LastV - FirstV;
291 if (VCurve->IsPeriodic() && Abs( deltaU ) > VCurve->Period()/2)
293 Standard_Real Sign = (deltaU > 0.0)? -1.0 : 1.0;
294 deltaU = VCurve->Period() - Abs( deltaU );
297 if (UCurve->IsPeriodic() && Abs( deltaV ) > UCurve->Period()/2)
299 Standard_Real Sign = (deltaV > 0.0)? -1.0 : 1.0;
300 deltaV = UCurve->Period() - Abs( deltaV );
307 //=======================================================================
308 //function : ComputeFacesAnglePresentation
310 //=======================================================================
312 void DsgPrs::ComputeFacesAnglePresentation( const Standard_Real ArrowLength,
313 const Standard_Real Value,
314 const gp_Pnt& CenterPoint,
315 const gp_Pnt& AttachmentPoint1,
316 const gp_Pnt& AttachmentPoint2,
319 const gp_Dir& axisdir,
320 const Standard_Boolean isPlane,
321 const gp_Ax1& AxisOfSurf,
322 const gp_Pnt& OffsetPoint,
324 Standard_Real & FirstParAngleCirc,
325 Standard_Real & LastParAngleCirc,
326 gp_Pnt & EndOfArrow1,
327 gp_Pnt & EndOfArrow2,
328 gp_Dir & DirOfArrow1,
329 gp_Dir & DirOfArrow2,
330 gp_Pnt & ProjAttachPoint2,
331 gp_Circ & AttachCirc,
332 Standard_Real & FirstParAttachCirc,
333 Standard_Real & LastParAttachCirc )
335 if (Value > Precision::Angular() && Abs( M_PI-Value ) > Precision::Angular())
337 // Computing presentation of angle's arc
338 gp_Ax2 ax( CenterPoint, axisdir, dir1 );
339 AngleCirc.SetPosition( ax );
340 AngleCirc.SetRadius( CenterPoint.Distance( OffsetPoint ) );
342 vec1 *= AngleCirc.Radius();
343 gp_Pnt p1 = CenterPoint.Translated( vec1 );
345 vec2 *= AngleCirc.Radius();
346 gp_Pnt p2 = CenterPoint.Translated( vec2 );
348 Standard_Real Par1 = 0.;
349 Standard_Real Par2 = ElCLib::Parameter( AngleCirc, p2 );
350 Standard_Real Par0 = ElCLib::Parameter( AngleCirc, OffsetPoint );
352 gp_Vec PosVec( CenterPoint, OffsetPoint );
353 gp_Vec NormalOfPlane = vec1 ^ vec2;
355 gp_Vec Normal1 = NormalOfPlane ^ vec1;
356 gp_Vec Normal2 = NormalOfPlane ^ vec2;
357 Standard_Integer Sign1 = (PosVec * Normal1 >= 0)? 1 : -1;
358 Standard_Integer Sign2 = (PosVec * Normal2 >= 0)? 1 : -1;
359 if (Sign1 == 1 && Sign2 == -1)
361 FirstParAngleCirc = Par1;
362 LastParAngleCirc = Par2;
364 else if (Sign1 == 1 && Sign2 == 1)
366 FirstParAngleCirc = Par1;
367 LastParAngleCirc = Par0;
369 else if (Sign1 == -1 && Sign2 == 1)
373 FirstParAngleCirc = Par1;
374 LastParAngleCirc = Par2;
376 else //Sign1 == -1 && Sign2 == -1
378 AngleCirc.SetPosition( gp_Ax2( CenterPoint, axisdir, gp_Dir( PosVec ) ) );
380 Par1 = ElCLib::Parameter( AngleCirc, p1 );
381 Par2 = ElCLib::Parameter( AngleCirc, p2 );
382 FirstParAngleCirc = Par0;
383 LastParAngleCirc = Par2;
386 // Computing presentation of arrows
387 EndOfArrow1 = ElCLib::Value( Par1, AngleCirc );
388 EndOfArrow2 = ElCLib::Value( Par2, AngleCirc );
389 Standard_Real beta = 0.;
390 if (AngleCirc.Radius() > Precision::Confusion())
391 beta = ArrowLength / AngleCirc.Radius();
392 gp_Pnt OriginOfArrow1 = ElCLib::Value( Par1 + beta, AngleCirc );
393 gp_Pnt OriginOfArrow2 = ElCLib::Value( Par2 - beta, AngleCirc );
394 DirOfArrow1 = gp_Dir( gp_Vec( OriginOfArrow1, EndOfArrow1 ) );
395 DirOfArrow2 = gp_Dir( gp_Vec( OriginOfArrow2, EndOfArrow2 ) );
396 if (EndOfArrow1.SquareDistance( EndOfArrow2 ) <= (ArrowLength + ArrowLength)*(ArrowLength + ArrowLength))
398 DirOfArrow1.Reverse();
399 DirOfArrow2.Reverse();
402 else // dir1 and dir2 are parallel
404 gp_Dir ArrowDir = axisdir ^ dir1;
405 DirOfArrow1 = ArrowDir;
406 DirOfArrow2 = -ArrowDir;
407 gp_Lin DirLine( AttachmentPoint1, dir1 );
408 EndOfArrow1 = ElCLib::Value( ElCLib::Parameter( DirLine, OffsetPoint ), DirLine );
409 EndOfArrow2 = EndOfArrow1;
412 // Line or arc from AttachmentPoint2 to its "projection"
413 gp_Lin SecondLin( CenterPoint, dir2 );
414 if (SecondLin.Contains( AttachmentPoint2, Precision::Confusion() ))
415 ProjAttachPoint2 = AttachmentPoint2;
419 ProjAttachPoint2 = ElCLib::Value( ElCLib::Parameter( SecondLin, AttachmentPoint2 ), SecondLin );
422 gp_Lin LineOfAxis( AxisOfSurf );
423 gp_Pnt CenterOfArc = ElCLib::Value( ElCLib::Parameter( LineOfAxis, AttachmentPoint2 ),
426 gp_Ax2 Ax2( CenterOfArc,
427 AxisOfSurf.Direction(),
428 gp_Dir( gp_Vec( CenterOfArc, AttachmentPoint2 ) ) );
429 AttachCirc.SetPosition( Ax2 );
430 AttachCirc.SetRadius( CenterOfArc.Distance( AttachmentPoint2 ) );
432 GeomAPI_ExtremaCurveCurve Intersection( new Geom_Circle( AttachCirc ),
433 new Geom_Line( SecondLin ) );
434 Intersection.NearestPoints( ProjAttachPoint2, ProjAttachPoint2 );
436 Standard_Real U2 = ElCLib::Parameter( AttachCirc, ProjAttachPoint2 );
439 FirstParAttachCirc = 0;
440 LastParAttachCirc = U2;
444 FirstParAttachCirc = U2;
445 LastParAttachCirc = 2*M_PI;
453 void DsgPrs::ComputeFilletRadiusPresentation( const Standard_Real /*ArrowLength*/,
454 const Standard_Real Value,
455 const gp_Pnt & Position,
456 const gp_Dir & NormalDir,
457 const gp_Pnt & FirstPoint,
458 const gp_Pnt & SecondPoint,
459 const gp_Pnt & Center,
460 const gp_Pnt & BasePnt,
461 const Standard_Boolean drawRevers,
462 Standard_Boolean & SpecCase,
463 gp_Circ & FilletCirc,
464 Standard_Real & FirstParCirc,
465 Standard_Real & LastParCirc,
468 gp_Pnt & DrawPosition)
470 gp_Dir dir1(gp_Vec(Center, FirstPoint));
471 gp_Dir dir2(gp_Vec(Center, SecondPoint));
472 Standard_Real Angle = dir1.Angle(dir2);
473 if(Angle <= Precision::Angular() || ( M_PI - Angle ) <= Precision::Angular() ||
474 Value <= Precision::Confusion()) SpecCase = Standard_True;
475 else SpecCase = Standard_False;
478 // Computing presentation of fillet's arc
479 gp_Ax2 ax( Center, NormalDir, dir1 );
480 FilletCirc.SetPosition( ax );
481 FilletCirc.SetRadius( Center.Distance( FirstPoint ) ); //***
483 vec1 *= FilletCirc.Radius();
485 vec2 *= FilletCirc.Radius();
487 if(! Center.IsEqual( Position, Precision::Confusion() ))
488 PosVec.SetXYZ( gp_Vec(Center, Position).XYZ() );
490 PosVec.SetXYZ( (vec1.Added(vec2)).XYZ() );
491 gp_Vec NormalOfPlane = vec1 ^ vec2;
492 gp_Vec Normal1 = NormalOfPlane ^ vec1;
493 gp_Vec Normal2 = NormalOfPlane ^ vec2;
494 Standard_Integer Sign1 = (PosVec * Normal1 >= 0)? 1 : -1;
495 Standard_Integer Sign2 = (PosVec * Normal2 >= 0)? 1 : -1;
496 gp_Lin L1( Center, dir1 );
497 gp_Lin L2( Center, dir2 );
498 if ( Sign1 != Sign2 )
500 DrawPosition = Position; //***
501 gp_Dir direction(PosVec) ;
502 Standard_Real angle = dir1.Angle(direction) ;
503 if (( dir1 ^ direction) * NormalDir < 0.0e0) angle = -angle ;
504 if(Sign1 == -1) angle += M_PI;
505 EndOfArrow = ElCLib::Value(angle, FilletCirc); //***
510 if(L1.Distance(Position) < L2.Distance(Position))
512 EndOfArrow = FirstPoint; //***
513 DrawPosition = ElCLib::Value(ElCLib::Parameter(L1, Position), L1);
517 EndOfArrow = SecondPoint; //***
518 DrawPosition = ElCLib::Value(ElCLib::Parameter(L2, Position), L2);
521 if((dir1^dir2).IsOpposite(NormalDir, Precision::Angular()))
523 gp_Dir newdir = NormalDir.Reversed() ;
524 gp_Ax2 axnew( Center, newdir, dir1 );
525 FilletCirc.SetPosition( axnew );
527 FirstParCirc = ElCLib::Parameter( FilletCirc, FirstPoint );
528 LastParCirc = ElCLib::Parameter( FilletCirc, SecondPoint );
530 else //Angle equal 0 or PI or R = 0
532 DrawPosition = Position;
533 EndOfArrow = BasePnt;
538 gp_Vec Vd(DrawPosition, EndOfArrow);
539 DrawPosition.Translate(Vd *2);
541 DirOfArrow.SetXYZ(gp_Dir(gp_Vec(DrawPosition, EndOfArrow)).XYZ());
544 //=======================================================================
545 //function : ComputeRadiusLine
547 //=======================================================================
549 void DsgPrs::ComputeRadiusLine(const gp_Pnt & aCenter,
550 const gp_Pnt & anEndOfArrow,
551 const gp_Pnt & aPosition,
552 const Standard_Boolean drawFromCenter,
553 gp_Pnt & aRadLineOrign,
554 gp_Pnt & aRadLineEnd)
558 gp_Lin RadiusLine = gce_MakeLin( aCenter, anEndOfArrow );
559 Standard_Real PosParOnLine = ElCLib::Parameter( RadiusLine, aPosition );
560 Standard_Real EndOfArrowPar = ElCLib::Parameter( RadiusLine, anEndOfArrow );
561 if (PosParOnLine < 0.0)
563 aRadLineOrign = aPosition;
564 aRadLineEnd = anEndOfArrow;
566 else if (PosParOnLine > EndOfArrowPar)
568 aRadLineOrign = aPosition;
569 aRadLineEnd = aCenter;
573 aRadLineOrign = aCenter;
574 aRadLineEnd = anEndOfArrow;
579 aRadLineOrign = aPosition;
580 aRadLineEnd = anEndOfArrow;
585 //=======================================================================
586 //function : DistanceFromApex
588 //=======================================================================
590 Standard_Real DsgPrs::DistanceFromApex(const gp_Elips & elips,
592 const Standard_Real par)
595 Standard_Real parApex = ElCLib::Parameter ( elips, Apex );
596 if(parApex == 0.0 || parApex == M_PI)
598 if(parApex == 0.0) //pos Apex
599 dist = (par < M_PI) ? par : (2*M_PI - par);
601 dist = (par < M_PI) ? ( M_PI - par) : ( par - M_PI );
605 if(parApex == M_PI / 2) //pos Apex
607 if(par <= parApex + M_PI && par > parApex )
608 dist = par - parApex;
611 if(par > parApex + M_PI)
612 dist = 2*M_PI - par + parApex;
614 dist = parApex - par; // 0 < par < M_PI/2
617 else //neg Apex == 3/2 PI
619 if(par <= parApex && par >= M_PI/2)
620 dist = parApex - par;
624 dist = par - parApex;
626 dist = par + M_PI/2; // 0 < par < PI/2