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