0026754: Visualization - provide API to display AIS_Trihedron presentation without...
[occt.git] / src / DsgPrs / DsgPrs_DiameterPresentation.cxx
1 // Created on: 1996-08-21
2 // Created by: Jacques MINOT
3 // Copyright (c) 1996-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 //              modified 12-january-98
18 //              Sergey ZARITCHNY
19 //              szy
20
21 #include <Aspect_AspectMarker.hxx>
22 #include <Aspect_TypeOfLine.hxx>
23 #include <Aspect_TypeOfMarker.hxx>
24 #include <DsgPrs.hxx>
25 #include <DsgPrs_DiameterPresentation.hxx>
26 #include <ElCLib.hxx>
27 #include <gp_Circ.hxx>
28 #include <gp_Dir.hxx>
29 #include <gp_Pnt.hxx>
30 #include <gp_Vec.hxx>
31 #include <Graphic3d_ArrayOfSegments.hxx>
32 #include <Graphic3d_AspectLine3d.hxx>
33 #include <Graphic3d_AspectMarker3d.hxx>
34 #include <Graphic3d_Group.hxx>
35 #include <Graphic3d_Vertex.hxx>
36 #include <Prs3d_Arrow.hxx>
37 #include <Prs3d_ArrowAspect.hxx>
38 #include <Prs3d_DimensionAspect.hxx>
39 #include <Prs3d_LineAspect.hxx>
40 #include <Prs3d_Presentation.hxx>
41 #include <Prs3d_Text.hxx>
42 #include <Prs3d_TextAspect.hxx>
43 #include <Quantity_Color.hxx>
44 #include <TCollection_ExtendedString.hxx>
45
46 //==========================================================================
47 // function : DsgPrs_DiameterPresentation::Add
48 // purpose  : it is possible to choose the symbol of extremities of the face (arrow, point ...)
49 //==========================================================================
50 void DsgPrs_DiameterPresentation::Add (const Handle(Prs3d_Presentation)& aPresentation,
51                                        const Handle(Prs3d_Drawer)& aDrawer,
52                                        const TCollection_ExtendedString& aText,
53                                        const gp_Pnt& AttachmentPoint,
54                                        const gp_Circ& aCircle,
55                                        const DsgPrs_ArrowSide ArrowPrs,
56                                        const Standard_Boolean IsDiamSymbol )
57 {
58   Handle(Prs3d_DimensionAspect) LA = aDrawer->DimensionAspect();
59   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
60
61   Standard_Real parat    = ElCLib::Parameter(aCircle, AttachmentPoint);
62   gp_Pnt        ptoncirc = ElCLib::Value    (parat, aCircle);
63
64   // sideline
65   gp_Pnt        center  = aCircle.Location();
66   gp_Vec        vecrap  (ptoncirc,center);
67
68   Standard_Real dist    = center.Distance(AttachmentPoint);
69   Standard_Real aRadius = aCircle.Radius();
70   Standard_Boolean inside = (dist < aRadius);
71
72   gp_Pnt pt1 = AttachmentPoint;
73   if (inside) {
74     pt1 = ptoncirc;
75     dist = aRadius;
76   }
77   vecrap.Normalize();
78   vecrap *= (dist+aRadius);
79   gp_Pnt OppositePoint = pt1.Translated(vecrap);
80   
81   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
82   aPrims->AddVertex(pt1);
83   aPrims->AddVertex(OppositePoint);
84   Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
85
86   // value
87   TCollection_ExtendedString Text = aText;
88   if(IsDiamSymbol) 
89     Text = TCollection_ExtendedString("\330  ") +  aText; // VRO (2007-05-17) inserted a blank.
90   Prs3d_Text::Draw(aPresentation, LA->TextAspect(), Text, AttachmentPoint);
91
92   // arrows
93   gp_Dir arrdir (vecrap);
94   if (inside) arrdir.Reverse();
95
96   gp_Vec vecrap2 = vecrap; 
97   gp_Pnt ptoncirc2 = ptoncirc;
98   gp_Dir arrdir2 = arrdir;
99   vecrap2.Normalize();
100   vecrap2 *= (aCircle.Radius() * 2.);
101   ptoncirc2.Translate (vecrap2);
102   arrdir2.Reverse();
103
104   DsgPrs::ComputeSymbol(aPresentation,LA,ptoncirc,ptoncirc2,arrdir,arrdir2,ArrowPrs);
105 }
106
107
108 static Standard_Boolean DsgPrs_InDomain(const Standard_Real fpar,
109                                         const Standard_Real lpar,
110                                         const Standard_Real para) 
111 {
112  if (fpar >= 0.) {
113     if(lpar > fpar)
114       return ((para >= fpar) && (para <= lpar));
115     else { // fpar > lpar
116       Standard_Real delta = 2.*M_PI-fpar;
117       Standard_Real lp, par, fp;
118       lp = lpar + delta;
119       par = para + delta;
120       while(lp > 2*M_PI) lp-=2*M_PI;
121       while(par > 2*M_PI) par-=2*M_PI;
122       fp = 0.;
123       return ((par >= fp) && (par <= lp));
124     }
125   }
126   if (para >= (fpar+2*M_PI)) return Standard_True;
127   if (para <= lpar) return Standard_True;
128   return Standard_False;
129 }
130
131
132 //=======================================================================
133 //function : DsgPrs_DiameterPresentation::Add
134 //purpose  : SZY 12-february-98
135 //=======================================================================
136
137 void DsgPrs_DiameterPresentation::Add (const Handle(Prs3d_Presentation)& aPresentation,
138                                        const Handle(Prs3d_Drawer)& aDrawer,
139                                        const TCollection_ExtendedString& aText,
140                                        const gp_Pnt& AttachmentPoint,
141                                        const gp_Circ& aCircle,
142                                        const Standard_Real uFirst,
143                                        const Standard_Real uLast,
144                                        const DsgPrs_ArrowSide ArrowPrs,//ArrowSide
145                                        const Standard_Boolean IsDiamSymbol )
146 {
147   Standard_Real fpara = uFirst;
148   Standard_Real lpara = uLast;
149   while (lpara > 2.*M_PI) {
150     fpara -= 2.*M_PI;
151     lpara -= 2.*M_PI;
152   }
153
154   Handle(Prs3d_DimensionAspect) LA = aDrawer->DimensionAspect();
155   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
156   Standard_Real parEndOfArrow = ElCLib::Parameter(aCircle,AttachmentPoint);
157   gp_Pnt EndOfArrow;
158   gp_Pnt DrawPosition = AttachmentPoint;// point of attachment
159
160   gp_Pnt Center = aCircle.Location();
161   gp_Pnt FirstPoint = ElCLib::Value(uFirst, aCircle);
162   gp_Pnt SecondPoint = ElCLib::Value(uLast, aCircle);
163
164   if ( !DsgPrs_InDomain(fpara,lpara,parEndOfArrow)) {
165     Standard_Real otherpar = parEndOfArrow + M_PI;// not in domain
166     if (otherpar > 2*M_PI) otherpar -= 2*M_PI;
167     if (DsgPrs_InDomain(fpara,lpara,otherpar)) {
168       parEndOfArrow = otherpar; // parameter on circle
169       EndOfArrow = ElCLib::Value(parEndOfArrow, aCircle);
170     }
171     else {
172       gp_Dir dir1(gp_Vec(Center, FirstPoint));
173       gp_Dir dir2(gp_Vec(Center, SecondPoint));
174       gp_Lin L1( Center, dir1 );
175       gp_Lin L2( Center, dir2 );
176       if(L1.Distance(AttachmentPoint) < L2.Distance(AttachmentPoint))
177       {
178         EndOfArrow = FirstPoint; //***
179         DrawPosition = ElCLib::Value(ElCLib::Parameter( L1, AttachmentPoint ), L1);     
180       }
181       else
182       {
183         EndOfArrow = SecondPoint; //***
184         DrawPosition = ElCLib::Value(ElCLib::Parameter( L2, AttachmentPoint ), L2);
185       }      
186     }
187   } 
188   else {
189     EndOfArrow   = ElCLib::Value(parEndOfArrow, aCircle);
190     DrawPosition = AttachmentPoint;
191   }
192
193   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
194   aPrims->AddVertex(DrawPosition);
195   aPrims->AddVertex(EndOfArrow);
196   Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
197
198   // text
199   TCollection_ExtendedString Text = aText;
200   if(IsDiamSymbol)
201     Text = TCollection_ExtendedString("\330 ") +  Text;//  => \330 | \370?
202   Prs3d_Text::Draw(aPresentation,LA->TextAspect(),Text,DrawPosition);
203
204   // Add presentation of arrow 
205   gp_Dir DirOfArrow(gp_Vec(DrawPosition, EndOfArrow).XYZ()); 
206   DsgPrs::ComputeSymbol(aPresentation, LA, EndOfArrow, EndOfArrow, DirOfArrow, DirOfArrow, ArrowPrs);
207 }