0024131: TKOpenGL redesign GPU memory management for markers presentation
[occt.git] / src / DsgPrs / DsgPrs_RadiusPresentation.cxx
1 // Created on: 1995-03-01
2 // Created by: Arnaud BOUZY
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21 //modified      20-feb-98 by <SZY>
22 //              Sergei Zaritchny
23
24 #include <DsgPrs_RadiusPresentation.ixx>
25 #include <gp_Lin.hxx>
26 #include <gp_Dir.hxx>
27 #include <gp_Circ.hxx>
28 #include <ElCLib.hxx>
29 #include <Graphic3d_Group.hxx>
30 #include <Graphic3d_ArrayOfSegments.hxx>
31 #include <Prs3d_Arrow.hxx>
32 #include <Prs3d_ArrowAspect.hxx>
33 #include <Prs3d_LineAspect.hxx>
34 #include <Prs3d_LengthAspect.hxx>
35 #include <Prs3d_Text.hxx>
36
37 #include <Graphic3d_Vertex.hxx>
38 #include <Graphic3d_AspectMarker3d.hxx>
39 #include <Graphic3d_AspectLine3d.hxx>
40 #include <Aspect_TypeOfLine.hxx>
41 #include <Aspect_TypeOfMarker.hxx>
42 #include <Aspect_AspectMarker.hxx>
43 #include <Quantity_Color.hxx>
44 #include <DsgPrs.hxx>
45 #include <Precision.hxx>
46 #include <gce_MakeLin.hxx>
47 #include <gce_MakeDir.hxx>
48
49
50 static Standard_Boolean DsgPrs_InDomain(const Standard_Real fpar,
51                                         const Standard_Real lpar,
52                                         const Standard_Real para)
53 {
54   if (fpar >= 0.)
55     return ((para >= fpar) && (para <= lpar));
56   if (para >= (fpar+2.*M_PI)) return Standard_True;
57   if (para <= lpar) return Standard_True;
58   return Standard_False;
59 }
60
61 //=======================================================================
62 //function : Add
63 //purpose  : 
64 //=======================================================================
65
66 void DsgPrs_RadiusPresentation::Add (const Handle(Prs3d_Presentation)& aPresentation,
67                                      const Handle(Prs3d_Drawer)& aDrawer,
68                                      const TCollection_ExtendedString& aText,
69                                      const gp_Pnt& AttachmentPoint,
70                                      const gp_Circ& aCircle,
71                                      const Standard_Real firstparam,
72                                      const Standard_Real lastparam,
73                                      const Standard_Boolean drawFromCenter,
74                                      const Standard_Boolean reverseArrow) 
75 {
76   Standard_Real fpara = firstparam;
77   Standard_Real lpara = lastparam;
78   while (lpara > 2.*M_PI) {
79     fpara -= 2.*M_PI;
80     lpara -= 2.*M_PI;
81   }
82   Handle(Prs3d_LengthAspect) LA = aDrawer->LengthAspect();
83   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
84
85   Standard_Real parat = ElCLib::Parameter(aCircle,AttachmentPoint);
86   gp_Pnt attpoint = AttachmentPoint;
87   Standard_Boolean otherside = Standard_False;
88   if ( !DsgPrs_InDomain(fpara,lpara,parat)) {
89     Standard_Real otherpar = parat + M_PI;
90     if (otherpar > 2.*M_PI) otherpar -= 2.*M_PI;
91     if (DsgPrs_InDomain(fpara,lpara,otherpar)) {
92       parat = otherpar;
93       otherside = Standard_True;
94     }
95     else {
96       const Standard_Real ecartpar = Min(Abs(fpara-parat),Abs(lpara-parat));
97       const Standard_Real ecartoth = Min(Abs(fpara-otherpar),Abs(lpara-otherpar));
98       if (ecartpar <= ecartoth) {
99         parat = (parat < fpara)? fpara : lpara;
100       }
101       else {
102         otherside = Standard_True;
103         parat = (otherpar < fpara)? fpara : lpara;
104       }
105       gp_Pnt ptdir = ElCLib::Value(parat,aCircle);
106       gp_Lin lsup(aCircle.Location(),gp_Dir(ptdir.XYZ()-aCircle.Location().XYZ()));
107       const Standard_Real parpos = ElCLib::Parameter(lsup,AttachmentPoint);
108       attpoint = ElCLib::Value(parpos,lsup);
109     }
110   }
111   gp_Pnt ptoncirc = ElCLib::Value(parat,aCircle);
112   gp_Lin L (aCircle.Location(),gp_Dir(attpoint.XYZ()-aCircle.Location().XYZ()));
113   gp_Pnt firstpoint = attpoint;
114   gp_Pnt drawtopoint = ptoncirc;
115   if (drawFromCenter && !otherside) {
116     const Standard_Real uatt = ElCLib::Parameter(L,attpoint);
117     const Standard_Real uptc = ElCLib::Parameter(L,ptoncirc);
118     if (Abs(uatt) > Abs(uptc))
119       drawtopoint = aCircle.Location();
120     else
121       firstpoint = aCircle.Location();
122   }
123
124   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
125   aPrims->AddVertex(firstpoint);
126   aPrims->AddVertex(drawtopoint);
127   Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
128
129   gp_Dir arrdir = L.Direction();
130   if (reverseArrow)
131     arrdir.Reverse();
132
133   // fleche
134   Prs3d_Arrow::Draw(aPresentation,ptoncirc,arrdir,LA->Arrow1Aspect()->Angle(),LA->Arrow1Aspect()->Length());
135
136   // texte
137   Prs3d_Text::Draw(aPresentation,LA->TextAspect(),aText,attpoint);
138 }
139
140 //=======================================================================
141 //function : DsgPrs_RadiusPresentation::Add
142 //purpose  : SZY 20-february-98 
143 //         : adds radius representation according drawFromCenter value
144 //=======================================================================
145
146 void DsgPrs_RadiusPresentation::Add( const Handle(Prs3d_Presentation)& aPresentation,
147                                      const Handle(Prs3d_Drawer)& aDrawer,
148                                      const TCollection_ExtendedString& aText,
149                                      const gp_Pnt& AttachmentPoint,
150                                      const gp_Pnt& Center,
151                                      const gp_Pnt& EndOfArrow,
152                                      const DsgPrs_ArrowSide ArrowPrs,
153                                      const Standard_Boolean drawFromCenter,
154                                      const Standard_Boolean reverseArrow)
155 {
156   Handle( Prs3d_LengthAspect ) LA = aDrawer->LengthAspect();
157   Prs3d_Root::CurrentGroup( aPresentation )->SetPrimitivesAspect( LA->LineAspect()->Aspect() );
158
159   gp_Pnt LineOrigin, LineEnd;
160   DsgPrs::ComputeRadiusLine(Center, EndOfArrow, AttachmentPoint, drawFromCenter, LineOrigin, LineEnd);
161
162   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
163   aPrims->AddVertex(LineOrigin);
164   aPrims->AddVertex(LineEnd);
165   Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
166
167   // text
168   Prs3d_Text::Draw( aPresentation, LA->TextAspect(), aText, AttachmentPoint );
169
170   gp_Dir ArrowDir = gce_MakeDir( LineOrigin , LineEnd );
171   if (reverseArrow)
172     ArrowDir.Reverse();
173   DsgPrs::ComputeSymbol( aPresentation, LA, Center, EndOfArrow, ArrowDir.Reversed(), ArrowDir, ArrowPrs, drawFromCenter );
174 }