0028990: Coding Rules - deprecate redundant class Prs3d_Root
[occt.git] / src / DsgPrs / DsgPrs_EqualDistancePresentation.cxx
1 // Created on: 1998-01-27
2 // Created by: Julia GERASIMOVA
3 // Copyright (c) 1998-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
18 #include <DsgPrs.hxx>
19 #include <DsgPrs_EqualDistancePresentation.hxx>
20 #include <ElCLib.hxx>
21 #include <gce_MakeDir.hxx>
22 #include <Geom_Plane.hxx>
23 #include <gp_Circ.hxx>
24 #include <gp_Dir.hxx>
25 #include <gp_Pln.hxx>
26 #include <gp_Pnt.hxx>
27 #include <gp_Vec.hxx>
28 #include <Graphic3d_ArrayOfPolylines.hxx>
29 #include <Graphic3d_ArrayOfSegments.hxx>
30 #include <Graphic3d_Group.hxx>
31 #include <Precision.hxx>
32 #include <Prs3d_DimensionAspect.hxx>
33 #include <Prs3d_LineAspect.hxx>
34 #include <Prs3d_Presentation.hxx>
35 #include <Prs3d_Text.hxx>
36 #include <TCollection_ExtendedString.hxx>
37
38 //=================================================================================
39 //function  : Add
40 //=================================================================================
41 void DsgPrs_EqualDistancePresentation::Add( const Handle( Prs3d_Presentation )& aPresentation,
42                                             const Handle( Prs3d_Drawer )& aDrawer,
43                                             const gp_Pnt& Point1,
44                                             const gp_Pnt& Point2,
45                                             const gp_Pnt& Point3,
46                                             const gp_Pnt& Point4,
47                                             const Handle( Geom_Plane )& Plane )
48 {
49   Handle( Prs3d_DimensionAspect ) LA = aDrawer->DimensionAspect();
50   aPresentation->CurrentGroup()->SetPrimitivesAspect( LA->LineAspect()->Aspect() );
51
52   // Line between two middles
53   gp_Pnt Middle12( (Point1.XYZ() + Point2.XYZ()) * 0.5 ), Middle34( (Point3.XYZ() + Point4.XYZ()) * 0.5 );
54
55   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
56   aPrims->AddVertex(Middle12);
57   aPrims->AddVertex(Middle34);
58   aPresentation->CurrentGroup()->AddPrimitiveArray(aPrims);
59
60   // Add presentation of arrows (points)
61   gp_Dir aDir( 0, 0, 1 );
62   DsgPrs::ComputeSymbol(aPresentation, LA, Middle12, Middle34, aDir, aDir, DsgPrs_AS_BOTHPT );
63 // ota -- begin --  
64   // Two small lines in the middle of this line
65   gp_Pnt Middle( (Middle12.XYZ() + Middle34.XYZ()) * 0.5 ), aTextPos;
66   Standard_Real Dist = Middle12.Distance( Middle34 );
67   Standard_Real SmallDist;
68   gp_Dir LineDir, OrtDir;
69   gp_Vec LineVec, OrtVec;
70
71   if (Dist > Precision::Confusion())
72   {
73     SmallDist = Dist * 0.05; // 1/20.0 part
74     if (SmallDist <= Precision::Confusion())
75       SmallDist = Dist;
76     LineDir = gce_MakeDir( Middle12, Middle34 );
77     OrtDir  = Plane->Pln().Axis().Direction() ^ LineDir;
78     LineVec = gp_Vec( LineDir ) * SmallDist;
79     OrtVec  = gp_Vec( OrtDir ) * SmallDist;
80
81     aTextPos = Middle.Translated( OrtVec );
82   }
83   else
84   {
85     gp_Vec Vec1( Middle, Point1 );
86
87     if (Vec1.SquareMagnitude() > Precision::Confusion()*Precision::Confusion())
88         {
89           Standard_Real Angle = gp_Vec( Middle, Point1 ).Angle( gp_Vec( Middle, Point3 ) );
90           gp_Pnt MidPnt = Point1.Rotated( Plane->Pln().Axis(), Angle*0.5 );
91           OrtDir  = gce_MakeDir( Middle, MidPnt );
92           LineDir = OrtDir ^ Plane->Pln().Axis().Direction();
93           
94           Standard_Real Distance = Point1.Distance( Point2 );
95           SmallDist = Distance * 0.05; // 1/20.0
96           if (SmallDist <= Precision::Confusion())
97             SmallDist = Distance;
98           
99           OrtVec = gp_Vec( OrtDir ) * SmallDist;
100           LineVec = gp_Vec( LineDir ) * SmallDist;
101         }
102     else
103         {
104           SmallDist = 5.0;
105           OrtVec = gp_Vec( Plane->Pln().XAxis().Direction() ) * SmallDist;
106           LineVec = gp_Vec( Plane->Pln().YAxis().Direction() ) * SmallDist;
107         }
108     aTextPos =  Middle.Translated (OrtVec);
109   }
110
111   TCollection_ExtendedString aText("==");
112
113   //Draw the text
114   Prs3d_Text::Draw (aPresentation->CurrentGroup(),LA->TextAspect(), aText, aTextPos);
115 }
116
117
118 //==================================================================================
119 //function  : AddInterval
120 //purpose   : is used for presentation of interval between two lines or two points, 
121 //            or between one line and one point.
122 //==================================================================================
123  void DsgPrs_EqualDistancePresentation::AddInterval(const Handle(Prs3d_Presentation)& aPresentation,
124                                                     const Handle(Prs3d_Drawer)& aDrawer,
125                                                     const gp_Pnt& aPoint1,
126                                                     const gp_Pnt& aPoint2,
127                                                     const gp_Dir& aDirection,
128                                                     const gp_Pnt& aPosition,
129                                                     const DsgPrs_ArrowSide anArrowSide,
130                                                     gp_Pnt& aProj1,
131                                                     gp_Pnt& aProj2) 
132 {
133   const Handle(Prs3d_DimensionAspect) LA = aDrawer->DimensionAspect();
134   aPresentation->CurrentGroup()->SetPrimitivesAspect(LA->LineAspect()->Aspect());
135   
136   gp_Lin L1 (aPoint1,aDirection);
137   gp_Lin L2 (aPoint2,aDirection);
138   aProj1 = ElCLib::Value(ElCLib::Parameter(L1, aPosition),L1);
139   aProj2 = ElCLib::Value(ElCLib::Parameter(L2, aPosition),L2);
140
141   Handle(Graphic3d_ArrayOfPolylines) aPrims = new Graphic3d_ArrayOfPolylines(4);
142   aPrims->AddVertex(aPoint1);
143   aPrims->AddVertex(aProj1);
144   aPrims->AddVertex(aProj2);
145   aPrims->AddVertex(aPoint2);
146   aPresentation->CurrentGroup()->AddPrimitiveArray(aPrims);
147
148   //add arrows presentation
149   gp_Dir aDir(aProj2.XYZ() - aProj1.XYZ());
150   
151   DsgPrs::ComputeSymbol(aPresentation, LA, aProj1, aProj2, aDir.Reversed(), aDir, anArrowSide);
152 }
153
154
155 //========================================================================
156 // function : AddIntervalBetweenTwoArcs 
157 // purpose  : is used for presentation of interval between two arcs. One
158 //            of the arcs can have a zero radius (being a point really) 
159 //========================================================================
160  void 
161   DsgPrs_EqualDistancePresentation::AddIntervalBetweenTwoArcs(const Handle(Prs3d_Presentation)& aPresentation,
162                                                               const Handle(Prs3d_Drawer)& aDrawer,
163                                                               const gp_Circ& aCirc1,
164                                                               const gp_Circ& aCirc2,
165                                                               const gp_Pnt& aPoint1,
166                                                               const gp_Pnt& aPoint2,
167                                                               const gp_Pnt& aPoint3,
168                                                               const gp_Pnt& aPoint4,
169                                                               const DsgPrs_ArrowSide anArrowSide) 
170 {
171   const Handle(Prs3d_DimensionAspect) LA = aDrawer->DimensionAspect();
172   aPresentation->CurrentGroup()->SetPrimitivesAspect(LA->LineAspect()->Aspect());
173
174   Standard_Real aPar11, aPar12, aPar21, aPar22;
175   if(aCirc1.Radius() > Precision::Confusion()){
176     aPar11 = ElCLib::Parameter (aCirc1, aPoint1);
177     aPar12 = ElCLib::Parameter(aCirc1, aPoint2);
178   }
179   else {
180     aPar11 = M_PI;
181     aPar12 = M_PI;
182   }
183   if (aCirc2.Radius() > Precision::Confusion()){
184     aPar21 = ElCLib::Parameter(aCirc2, aPoint3 );
185     aPar22 = ElCLib::Parameter(aCirc2, aPoint4);
186   }
187   else {
188     aPar21 = M_PI;
189     aPar22 = M_PI;
190   }
191
192   Handle(Graphic3d_ArrayOfPrimitives) aPrims = new Graphic3d_ArrayOfSegments(2);
193   aPrims->AddVertex(aPoint2);
194   aPrims->AddVertex(aPoint4);
195   aPresentation->CurrentGroup()->AddPrimitiveArray(aPrims);
196
197   Standard_Integer i, aNodeNb;
198   Standard_Real aDelta, aCurPar;
199   if(aPar12 < aPar11 ) aPar12 += 2.*M_PI;
200   if (Abs(aPar12 - aPar11) > Precision::Confusion())
201   {
202     aNodeNb = Standard_Integer(Max(Abs(aPar12 - aPar11)*50./M_PI + 0.5, 4.));
203     aDelta = (aPar12 - aPar11)/aNodeNb;
204     aCurPar= aPar11;
205
206     aPrims = new Graphic3d_ArrayOfPolylines(aNodeNb+1);
207     for (i = 1; i<= aNodeNb; aCurPar += aDelta, i++)
208       aPrims->AddVertex(ElCLib::Value( aCurPar, aCirc1));
209     aPrims->AddVertex(aPoint2);
210     aPresentation->CurrentGroup()->AddPrimitiveArray(aPrims);
211   }
212   if (aPar22 < aPar21) aPar22 += 2.*M_PI;
213   if ( Abs(aPar22 - aPar21) > Precision::Confusion())
214   {
215     aNodeNb = Standard_Integer(Max(Abs(aPar22 - aPar21)*50./M_PI + 0.5, 4.));
216     aDelta = (aPar22 - aPar21)/aNodeNb;
217     aCurPar= aPar21;
218
219     aPrims = new Graphic3d_ArrayOfPolylines(aNodeNb+1);
220     for (i = 1; i<= aNodeNb; aCurPar += aDelta, i++)
221       aPrims->AddVertex(ElCLib::Value( aCurPar, aCirc2));
222     aPrims->AddVertex(aPoint4);
223     aPresentation->CurrentGroup()->AddPrimitiveArray(aPrims);
224   }
225
226   //get the direction of interval
227   gp_Dir DirOfArrow;
228   if(aPoint4.Distance(aPoint2) > Precision::Confusion())
229   {
230     DirOfArrow.SetXYZ(aPoint4.XYZ() - aPoint2.XYZ());
231   }
232   else
233   {
234     //Let's take the radius direction
235     gp_Pnt aCenter = aCirc1.Location();
236     if(aPoint4.Distance(aCenter) < Precision::Confusion())
237       return;
238     DirOfArrow.SetXYZ(aPoint4.XYZ() - aCenter.XYZ());
239   }
240
241   // Add presentation of arrows
242   DsgPrs::ComputeSymbol( aPresentation, LA, aPoint2, aPoint4, DirOfArrow.Reversed(), DirOfArrow, anArrowSide );
243 }
244 //-- ota -- end