e2d129a8b8e75119c8bbb18e6693eacca0b86f99
[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_Array1OfVertex.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   }
57   if (para >= (fpar+2*M_PI)) return Standard_True;
58   if (para <= lpar) return Standard_True;
59   return Standard_False;
60 }
61
62 //=======================================================================
63 //function : Add
64 //purpose  : 
65 //=======================================================================
66
67 void DsgPrs_RadiusPresentation::Add (const Handle(Prs3d_Presentation)& aPresentation,
68                                      const Handle(Prs3d_Drawer)& aDrawer,
69                                      const TCollection_ExtendedString& aText,
70                                      const gp_Pnt& AttachmentPoint,
71                                      const gp_Circ& aCircle,
72                                      const Standard_Real firstparam,
73                                      const Standard_Real lastparam,
74                                      const Standard_Boolean drawFromCenter,
75                                      const Standard_Boolean reverseArrow) 
76 {
77   Standard_Real fpara = firstparam;
78   Standard_Real lpara = lastparam;
79   while (lpara > 2*M_PI) {
80     fpara -= 2*M_PI;
81     lpara -= 2*M_PI;
82   }
83   Handle(Prs3d_LengthAspect) LA = aDrawer->LengthAspect();
84   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
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       Standard_Real ecartpar = Min(Abs(fpara-parat),
97                                    Abs(lpara-parat));
98       Standard_Real ecartoth = Min(Abs(fpara-otherpar),
99                                    Abs(lpara-otherpar));
100       if (ecartpar <= ecartoth) {
101         if (parat < fpara) {
102           parat = fpara;
103         }
104         else {
105           parat = lpara;
106         }
107       }
108       else {
109         otherside = Standard_True;
110         if (otherpar < fpara) {
111           parat = fpara;
112         }
113         else {
114           parat = lpara;
115         }
116       }
117       gp_Pnt ptdir = ElCLib::Value(parat,aCircle);
118       gp_Lin lsup(aCircle.Location(),
119                   gp_Dir(ptdir.XYZ()-aCircle.Location().XYZ()));
120       Standard_Real parpos = ElCLib::Parameter(lsup,AttachmentPoint);
121       attpoint = ElCLib::Value(parpos,lsup);
122     }
123   }
124   gp_Pnt ptoncirc = ElCLib::Value(parat,aCircle);
125   gp_Lin L (aCircle.Location(),gp_Dir(attpoint.XYZ()-aCircle.Location().XYZ()));
126   gp_Pnt firstpoint = attpoint;
127   gp_Pnt drawtopoint = ptoncirc;
128   if (drawFromCenter && !otherside) {
129     Standard_Real uatt = ElCLib::Parameter(L,attpoint);
130     Standard_Real uptc = ElCLib::Parameter(L,ptoncirc);
131     if (Abs(uatt) > Abs(uptc)) {
132       drawtopoint = aCircle.Location();
133     }
134     else {
135       firstpoint = aCircle.Location();
136     }
137   }
138
139   Graphic3d_Array1OfVertex V(1,2);
140
141   Quantity_Length X,Y,Z;
142
143   firstpoint.Coord(X,Y,Z);
144   V(1).SetCoord(X,Y,Z);
145
146   drawtopoint.Coord(X,Y,Z);
147   V(2).SetCoord(X,Y,Z);
148
149   Prs3d_Root::CurrentGroup(aPresentation)->Polyline(V);
150
151   gp_Dir arrdir = L.Direction();
152   if (reverseArrow) {
153     arrdir.Reverse();
154   }
155   // fleche
156   Prs3d_Arrow::Draw(aPresentation,ptoncirc,arrdir,
157                     LA->Arrow1Aspect()->Angle(),
158                     LA->Arrow1Aspect()->Length());
159
160   // texte
161   Prs3d_Text::Draw(aPresentation,LA->TextAspect(),aText,attpoint);
162   
163 }
164
165 //=======================================================================
166 //function : DsgPrs_RadiusPresentation::Add
167 //purpose  : SZY 20-february-98 
168 //         : adds radius representation according drawFromCenter value
169 //=======================================================================
170
171 void DsgPrs_RadiusPresentation::Add( const Handle(Prs3d_Presentation)& aPresentation,
172                                      const Handle(Prs3d_Drawer)& aDrawer,
173                                      const TCollection_ExtendedString& aText,
174                                      const gp_Pnt& AttachmentPoint,
175                                      const gp_Pnt& Center,
176                                      const gp_Pnt& EndOfArrow,
177                                      const DsgPrs_ArrowSide ArrowPrs,
178                                      const Standard_Boolean drawFromCenter,
179                                      const Standard_Boolean reverseArrow)
180 {
181   Handle( Prs3d_LengthAspect ) LA = aDrawer->LengthAspect();
182   Prs3d_Root::CurrentGroup( aPresentation )->SetPrimitivesAspect( LA->LineAspect()->Aspect() );
183
184   Graphic3d_Array1OfVertex VertexArray( 1, 2 );
185   gp_Pnt  LineOrigin, LineEnd;
186   Quantity_Length X,Y,Z;
187
188   DsgPrs::ComputeRadiusLine( Center, EndOfArrow, AttachmentPoint, drawFromCenter,
189                             LineOrigin, LineEnd);
190 //
191   LineOrigin.Coord( X, Y, Z );
192   VertexArray(1).SetCoord( X, Y, Z );
193
194   LineEnd.Coord( X, Y, Z );
195   VertexArray(2).SetCoord( X, Y, Z );
196
197   Prs3d_Root::CurrentGroup( aPresentation )->Polyline( VertexArray );
198   // text
199   Prs3d_Text::Draw( aPresentation, LA->TextAspect(), aText, AttachmentPoint );
200
201   gp_Dir ArrowDir = gce_MakeDir( LineOrigin , LineEnd );
202   if (reverseArrow)
203     ArrowDir.Reverse();
204   DsgPrs::ComputeSymbol( aPresentation, LA, Center, EndOfArrow, ArrowDir.Reversed(), ArrowDir,
205                         ArrowPrs, drawFromCenter );
206 }
207