0023948: Wrong intersection between a surface of revolution and a plane.
[occt.git] / src / DsgPrs / DsgPrs_LengthPresentation.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <DsgPrs_LengthPresentation.ixx>
16 #include <gp_Lin.hxx>
17 #include <gp_Dir.hxx>
18 #include <ElCLib.hxx>
19 #include <gce_MakeLin.hxx>
20 #include <Graphic3d_Group.hxx>
21 #include <Graphic3d_ArrayOfSegments.hxx>
22 #include <Graphic3d_ArrayOfPolylines.hxx>
23 #include <Prs3d_Root.hxx>
24 #include <Prs3d_Arrow.hxx>
25 #include <Prs3d_ArrowAspect.hxx>
26 #include <Prs3d_LineAspect.hxx>
27 #include <Prs3d_DimensionAspect.hxx>
28 #include <TCollection_AsciiString.hxx>
29 #include <Graphic3d_AspectLine3d.hxx>
30 #include <Prs3d_Text.hxx>
31 #include <Precision.hxx>
32 #include <Graphic3d_Vertex.hxx>
33 #include <Graphic3d_AspectMarker3d.hxx>
34 #include <Aspect_TypeOfLine.hxx>
35 #include <Aspect_TypeOfMarker.hxx>
36 #include <Aspect_AspectMarker.hxx>
37 #include <Quantity_Color.hxx>
38 #include <DsgPrs.hxx>
39
40 #include <Geom_Curve.hxx>
41
42
43 void DsgPrs_LengthPresentation::Add (const Handle(Prs3d_Presentation)& aPresentation,
44                                      const Handle(Prs3d_Drawer)& aDrawer,
45                                      const TCollection_ExtendedString& aText,
46                                      const gp_Pnt& AttachmentPoint1,
47                                      const gp_Pnt& AttachmentPoint2,
48                                      const gp_Dir& aDirection,
49                                      const gp_Pnt& OffsetPoint)
50 {
51   Handle(Prs3d_DimensionAspect) LA = aDrawer->DimensionAspect();
52   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
53
54   gp_Lin L1 (AttachmentPoint1,aDirection);
55   gp_Lin L2 (AttachmentPoint2,aDirection);
56   gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,OffsetPoint),L1);
57   gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,OffsetPoint),L2);
58   gp_Lin L3 = Proj1.IsEqual(Proj2,Precision::Confusion())? gp_Lin(Proj1,aDirection) : gce_MakeLin(Proj1,Proj2);
59   Standard_Real parmin,parmax,parcur;
60   parmin = ElCLib::Parameter(L3,Proj1);
61   parmax = parmin;
62   parcur = ElCLib::Parameter(L3,Proj2);
63   Standard_Real dist = Abs(parmin-parcur);
64   if (parcur < parmin) parmin = parcur;
65   if (parcur > parmax) parmax = parcur;
66   parcur = ElCLib::Parameter(L3,OffsetPoint);
67   gp_Pnt offp = ElCLib::Value(parcur,L3);
68
69   Standard_Boolean outside = Standard_False;
70   if (parcur < parmin) {
71     parmin = parcur;
72     outside = Standard_True;
73   }
74   if (parcur > parmax) {
75     parmax = parcur;
76     outside = Standard_True;
77   }
78
79   gp_Pnt PointMin = ElCLib::Value(parmin,L3);
80   gp_Pnt PointMax = ElCLib::Value(parmax,L3);
81
82   // face processing : 1st group
83   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(6);
84   aPrims->AddVertex(PointMin);
85   aPrims->AddVertex(PointMax);
86
87   if (dist < (LA->ArrowAspect()->Length()+LA->ArrowAspect()->Length()))
88     outside = Standard_True;
89
90   gp_Dir arrdir = L3.Direction().Reversed();
91   if (outside)
92     arrdir.Reverse();
93
94   // arrow 1 : 2nd group
95   Prs3d_Arrow::Draw(aPresentation,Proj1,arrdir, LA->ArrowAspect()->Angle(), LA->ArrowAspect()->Length());
96
97   Prs3d_Root::NewGroup(aPresentation);
98   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
99   
100   // arrow 2 : 3rd group
101   Prs3d_Arrow::Draw(aPresentation,Proj2,arrdir.Reversed(), LA->ArrowAspect()->Angle(), LA->ArrowAspect()->Length());
102
103   Prs3d_Root::NewGroup(aPresentation);
104   
105   // text : 4th group
106   Prs3d_Text::Draw(aPresentation,LA->TextAspect(),aText,offp);
107   
108   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
109
110   // processing of call 1 : 5th group
111   aPrims->AddVertex(AttachmentPoint1);
112   aPrims->AddVertex(Proj1);
113   
114   // processing of call 2 : 6th group
115   aPrims->AddVertex(AttachmentPoint2);
116   aPrims->AddVertex(Proj2);
117
118   Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
119 }
120
121 //==================================================================================
122 //function : Add
123 //purpose  : Adds presentation of length dimension between two planar faces
124 //==================================================================================
125
126 void DsgPrs_LengthPresentation::Add( const Handle(Prs3d_Presentation)& aPresentation,
127                                      const Handle(Prs3d_Drawer)& aDrawer,
128                                      const TCollection_ExtendedString& aText,
129                                      const gp_Pnt& AttachmentPoint1,
130                                      const gp_Pnt& AttachmentPoint2,
131                                      const gp_Pln& PlaneOfFaces,
132                                      const gp_Dir& aDirection,
133                                      const gp_Pnt& OffsetPoint,
134                                      const DsgPrs_ArrowSide ArrowPrs ) 
135 {
136   Handle(Prs3d_DimensionAspect) LA = aDrawer->DimensionAspect();
137   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
138
139   gp_Pnt EndOfArrow1, EndOfArrow2;
140   gp_Dir DirOfArrow1;
141   
142   DsgPrs::ComputePlanarFacesLengthPresentation( LA->ArrowAspect()->Length(),
143                                                 LA->ArrowAspect()->Length(),
144                                                 AttachmentPoint1,
145                                                 AttachmentPoint2,
146                                                 aDirection,
147                                                 OffsetPoint,
148                                                 PlaneOfFaces,
149                                                 EndOfArrow1,
150                                                 EndOfArrow2,
151                                                 DirOfArrow1 );
152
153   // Parameters for length's line
154   gp_Lin LengthLine( OffsetPoint, DirOfArrow1 );
155   Standard_Real Par1 = ElCLib::Parameter( LengthLine, EndOfArrow1 );
156   Standard_Real Par2 = ElCLib::Parameter( LengthLine, EndOfArrow2 );
157   gp_Pnt FirstPoint, LastPoint;
158   if ((Par1 > 0.0 && Par2 > 0.0) || (Par1 < 0.0 && Par2 < 0.0))
159   {
160     FirstPoint = OffsetPoint;
161     LastPoint  = (Abs( Par1 ) > Abs( Par2 ))? EndOfArrow1 : EndOfArrow2;
162   }
163   else
164   {
165     FirstPoint = EndOfArrow1;
166     LastPoint  = EndOfArrow2;
167   }
168
169   // Creating the length's line
170   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(6);
171
172   aPrims->AddVertex(FirstPoint);
173   aPrims->AddVertex(LastPoint);
174
175   // Add presentation of arrows
176   DsgPrs::ComputeSymbol( aPresentation, LA, EndOfArrow1, EndOfArrow2, DirOfArrow1, DirOfArrow1.Reversed(), ArrowPrs );
177  
178   // Drawing the text
179   Prs3d_Text::Draw( aPresentation, LA->TextAspect(), aText, OffsetPoint );
180
181   // Line from AttachmentPoint1 to end of Arrow1
182   aPrims->AddVertex(AttachmentPoint1);
183   aPrims->AddVertex(EndOfArrow1);
184
185   // Line from AttachmentPoint2 to end of Arrow2
186   aPrims->AddVertex(AttachmentPoint2);
187   aPrims->AddVertex(EndOfArrow2);
188
189   Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
190 }
191
192
193 //=========================================================================================
194 //function : Add
195 //purpose  : adds presentation of length between two edges, vertex and edge or two vertices
196 //=========================================================================================
197
198 void DsgPrs_LengthPresentation::Add (const Handle(Prs3d_Presentation)& aPresentation,
199                                      const Handle(Prs3d_Drawer)& aDrawer,
200                                      const TCollection_ExtendedString& aText,
201                                      const gp_Pnt& AttachmentPoint1,
202                                      const gp_Pnt& AttachmentPoint2,
203                                      const gp_Dir& aDirection,
204                                      const gp_Pnt& OffsetPoint,
205                                      const DsgPrs_ArrowSide ArrowPrs) 
206 {
207   Handle(Prs3d_DimensionAspect) LA = aDrawer->DimensionAspect();
208   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
209
210   gp_Lin L1 (AttachmentPoint1,aDirection);
211   gp_Lin L2 (AttachmentPoint2,aDirection);
212   gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,OffsetPoint),L1);
213   gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,OffsetPoint),L2);
214   gp_Lin L3 = Proj1.IsEqual(Proj2,Precision::Confusion())? gp_Lin(Proj1,aDirection) : gce_MakeLin(Proj1,Proj2);
215   Standard_Real parmin,parmax,parcur;
216   parmin = ElCLib::Parameter(L3,Proj1);
217   parmax = parmin;
218   parcur = ElCLib::Parameter(L3,Proj2);
219   Standard_Real dist = Abs(parmin-parcur);
220   if (parcur < parmin) parmin = parcur;
221   if (parcur > parmax) parmax = parcur;
222   parcur = ElCLib::Parameter(L3,OffsetPoint);
223   gp_Pnt offp = ElCLib::Value(parcur,L3);
224
225   Standard_Boolean outside = Standard_False;
226   if (parcur < parmin) {
227     parmin = parcur;
228     outside = Standard_True;
229   }
230   if (parcur > parmax) {
231     parmax = parcur;
232     outside = Standard_True;
233   }
234
235   gp_Pnt PointMin = ElCLib::Value(parmin,L3);
236   gp_Pnt PointMax = ElCLib::Value(parmax,L3);
237
238   // processing of face 
239   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(6);
240
241   aPrims->AddVertex(PointMin);
242   aPrims->AddVertex(PointMax);
243
244   if (dist < (LA->ArrowAspect()->Length()+LA->ArrowAspect()->Length()))
245     outside = Standard_True;
246
247   gp_Dir arrdir = L3.Direction().Reversed();
248   if (outside)
249     arrdir.Reverse();
250   
251   // processing of call  1 
252   aPrims->AddVertex(AttachmentPoint1);
253   aPrims->AddVertex(Proj1);
254   
255   // processing of call 2 
256   aPrims->AddVertex(AttachmentPoint2);
257   aPrims->AddVertex(Proj2);
258
259   Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
260
261   // text 
262   Prs3d_Text::Draw(aPresentation,LA->TextAspect(),aText,offp);
263
264   // symbols at the extremities of the face
265   DsgPrs::ComputeSymbol(aPresentation,LA,Proj1,Proj2,arrdir,arrdir.Reversed(),ArrowPrs);
266 }
267
268
269 //==================================================================================
270 //function : Add
271 //purpose  : Adds presentation of length dimension between two curvilinear faces
272 //==================================================================================
273
274 void DsgPrs_LengthPresentation::Add( const Handle(Prs3d_Presentation)& aPresentation,
275                                      const Handle(Prs3d_Drawer)& aDrawer,
276                                      const TCollection_ExtendedString& aText,
277                                      const Handle( Geom_Surface )& SecondSurf,
278                                      const gp_Pnt& AttachmentPoint1,
279                                      const gp_Pnt& AttachmentPoint2,
280                                      const gp_Dir& aDirection,
281                                      const gp_Pnt& OffsetPoint,
282                                      const DsgPrs_ArrowSide ArrowPrs ) 
283 {
284   Handle(Prs3d_DimensionAspect) LA = aDrawer->DimensionAspect();
285   Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(LA->LineAspect()->Aspect());
286
287   gp_Pnt EndOfArrow2;
288   gp_Dir DirOfArrow1;
289   Handle( Geom_Curve ) VCurve, UCurve;
290   Standard_Real FirstU, deltaU = 0.0e0, FirstV, deltaV = 0.0e0;
291   
292   DsgPrs::ComputeCurvilinearFacesLengthPresentation( LA->ArrowAspect()->Length(),
293                                                      LA->ArrowAspect()->Length(),
294                                                      SecondSurf,
295                                                      AttachmentPoint1,
296                                                      AttachmentPoint2,
297                                                      aDirection,
298                                                      EndOfArrow2,
299                                                      DirOfArrow1,
300                                                      VCurve,
301                                                      UCurve,
302                                                      FirstU, deltaU,
303                                                      FirstV, deltaV );
304                                                        
305   gp_Lin LengthLine( OffsetPoint, DirOfArrow1 );
306   Standard_Real Par1 = ElCLib::Parameter( LengthLine, AttachmentPoint1 );
307   Standard_Real Par2 = ElCLib::Parameter( LengthLine, EndOfArrow2 );
308   gp_Pnt FirstPoint, LastPoint;
309   if ((Par1 > 0.0 && Par2 > 0.0) || (Par1 < 0.0 && Par2 < 0.0))
310   {
311     FirstPoint = OffsetPoint;
312     LastPoint  = (Abs( Par1 ) > Abs( Par2 ))? AttachmentPoint1 : EndOfArrow2;
313   }
314   else
315   {
316     FirstPoint = AttachmentPoint1;
317     LastPoint  = EndOfArrow2;
318   }
319
320   // Creating the length's line
321   Handle(Graphic3d_ArrayOfPrimitives) aPrims = new Graphic3d_ArrayOfSegments(2);
322   aPrims->AddVertex(FirstPoint);
323   aPrims->AddVertex(LastPoint);
324   Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
325
326   // Add presentation of arrows
327   DsgPrs::ComputeSymbol( aPresentation, LA, AttachmentPoint1, EndOfArrow2, DirOfArrow1, DirOfArrow1.Reversed(), ArrowPrs );
328  
329   // Drawing the text
330   Prs3d_Text::Draw( aPresentation, LA->TextAspect(), aText, OffsetPoint );
331  
332   // Two curves from end of Arrow2 to AttachmentPoint2
333   Standard_Real Alpha, delta;
334   Standard_Integer NodeNumber;
335   
336   Alpha  = Abs( deltaU );
337   if (Alpha > Precision::Angular() && Alpha<Precision::Infinite())
338   {
339     NodeNumber = Max( 4 , Standard_Integer (50. * Alpha / M_PI) );
340     delta = deltaU / (Standard_Real)( NodeNumber - 1 );
341     aPrims = new Graphic3d_ArrayOfPolylines(NodeNumber);
342     for (Standard_Integer i = 1; i <= NodeNumber; i++, FirstU += delta)
343       aPrims->AddVertex(VCurve->Value( FirstU ));
344     Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
345   }
346   Alpha  = Abs( deltaV );
347   if (Alpha > Precision::Angular() && Alpha<Precision::Infinite())
348   {
349     NodeNumber = Max( 4 , Standard_Integer (50. * Alpha / M_PI) );
350     delta = deltaV / (Standard_Real)( NodeNumber - 1 );
351     aPrims = new Graphic3d_ArrayOfPolylines(NodeNumber);
352     for (Standard_Integer i = 1; i <= NodeNumber; i++, FirstV += delta)
353       aPrims->AddVertex(UCurve->Value( FirstV ));
354     Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(aPrims);
355   }
356 }
357
358
359 //================================
360 // Function:
361 // Purpose: Rob 26-mar-96
362 //=================================
363
364 void DsgPrs_LengthPresentation::Add (const Handle(Prs3d_Presentation)& aPrs,
365                                      const Handle(Prs3d_Drawer)& aDrawer,
366                                      const gp_Pnt& Pt1,
367                                      const gp_Pnt& Pt2,
368                                      const DsgPrs_ArrowSide ArrowPrs) 
369 {
370   Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
371   aPrims->AddVertex(Pt1);
372   aPrims->AddVertex(Pt2);
373   Prs3d_Root::CurrentGroup(aPrs)->AddPrimitiveArray(aPrims);
374
375   gp_Vec V ;
376   switch(ArrowPrs)
377   {
378     case DsgPrs_AS_LASTAR:
379     Prs3d_Arrow::Draw(aPrs,Pt2,gp_Dir(gp_Vec(Pt1,Pt2)), 
380                       aDrawer->DimensionAspect()->ArrowAspect()->Angle(),
381                       aDrawer->DimensionAspect()->ArrowAspect()->Length());
382     break;
383     case DsgPrs_AS_FIRSTAR:
384     Prs3d_Arrow::Draw(aPrs,Pt1,gp_Dir(gp_Vec(Pt2,Pt1)), 
385                       aDrawer->DimensionAspect()->ArrowAspect()->Angle(),
386                       aDrawer->DimensionAspect()->ArrowAspect()->Length());
387     break;
388     case DsgPrs_AS_BOTHAR:
389     V = gp_Vec(Pt1,Pt2);
390     Prs3d_Arrow::Draw(aPrs,Pt2,gp_Dir(V), 
391                       aDrawer->DimensionAspect()->ArrowAspect()->Angle(),
392                       aDrawer->DimensionAspect()->ArrowAspect()->Length());
393     Prs3d_Arrow::Draw(aPrs,Pt1,gp_Dir(V.Reversed()), 
394                       aDrawer->DimensionAspect()->ArrowAspect()->Angle(),
395                       aDrawer->DimensionAspect()->ArrowAspect()->Length());
396     break;
397     default:
398     break;
399   }
400 }