0029915: Porting to VC 2017 : Regressions in Modeling Algorithms on VC 2017
[occt.git] / src / Prs3d / Prs3d_Arrow.cxx
1 // Copyright (c) 1995-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 <Prs3d_Arrow.hxx>
16
17 #include <gp_Ax3.hxx>
18 #include <gp_Dir.hxx>
19 #include <gp_Pnt.hxx>
20 #include <gp_Trsf.hxx>
21 #include <Graphic3d_ArrayOfPolylines.hxx>
22 #include <Graphic3d_ArrayOfSegments.hxx>
23 #include <Graphic3d_Group.hxx>
24 #include <Prs3d_Presentation.hxx>
25 #include <Prs3d_ToolCylinder.hxx>
26 #include <Prs3d_ToolDisk.hxx>
27 #include <Prs3d_ToolSphere.hxx>
28
29 //=======================================================================
30 //function : Draw
31 //purpose  : 
32 //=======================================================================
33 void Prs3d_Arrow::Draw(const Handle(Graphic3d_Group)& theGroup,
34                        const gp_Pnt& theLocation,
35                        const gp_Dir& theDirection,
36                        const Standard_Real theAngle,
37                        const Standard_Real theLength)
38 {
39   Handle(Graphic3d_ArrayOfSegments) aPrimitives = Prs3d_Arrow::DrawSegments(theLocation,
40                                                   theDirection, theAngle, theLength, 15);
41   theGroup->AddPrimitiveArray (aPrimitives);
42 }
43
44 //=======================================================================
45 //function : DrawSegments
46 //purpose  :
47 //=======================================================================
48 Handle(Graphic3d_ArrayOfSegments) Prs3d_Arrow::DrawSegments (const gp_Pnt& theLocation,
49                                                              const gp_Dir& theDir,
50                                                              const Standard_Real theAngle,
51                                                              const Standard_Real theLength,
52                                                              const Standard_Integer theNbSegments)
53 {
54   Handle(Graphic3d_ArrayOfSegments) aSegments = new Graphic3d_ArrayOfSegments (theNbSegments + 1, 2 * (2 * theNbSegments));
55
56   // center of the base circle of the arrow
57   const gp_XYZ aC = theLocation.XYZ() + theDir.XYZ() * (-theLength);
58
59   // construction of i,j mark for the circle
60   gp_Dir aN;
61   if (Abs(theDir.X()) <= Abs(theDir.Y())
62    && Abs(theDir.X()) <= Abs(theDir.Z()))
63   {
64     aN = gp::DX();
65   }
66   else if (Abs(theDir.Y()) <= Abs(theDir.Z())
67         && Abs(theDir.Y()) <= Abs(theDir.X()))
68   {
69     aN = gp::DY();
70   }
71   else
72   {
73     aN = gp::DZ();
74   }
75
76   const gp_Dir anXYZi = theDir.Crossed (aN.XYZ());
77   const gp_XYZ anXYZj = theDir.XYZ().Crossed (anXYZi.XYZ());
78   aSegments->AddVertex (theLocation);
79
80   const Standard_Real Tg = Tan (theAngle);
81   for (Standard_Integer aVertIter = 1; aVertIter <= theNbSegments; ++aVertIter)
82   {
83     const Standard_Real aCos = Cos (2.0 * M_PI / theNbSegments * (aVertIter - 1));
84     const Standard_Real aSin = Sin (2.0 * M_PI / theNbSegments * (aVertIter - 1));
85
86     const gp_Pnt pp(aC.X() + (aCos * anXYZi.X() + aSin * anXYZj.X()) * theLength * Tg,
87                     aC.Y() + (aCos * anXYZi.Y() + aSin * anXYZj.Y()) * theLength * Tg,
88                     aC.Z() + (aCos * anXYZi.Z() + aSin * anXYZj.Z()) * theLength * Tg);
89
90     aSegments->AddVertex (pp);
91   }
92
93   Standard_Integer aNbVertices = theNbSegments + 1;
94   Standard_Integer aFirstContourVertex = 2;
95   Standard_Integer anEdgeCount = 0;
96   for (Standard_Integer aVertIter = aFirstContourVertex; aVertIter <= aNbVertices; ++aVertIter)
97   {
98     aSegments->AddEdge (1);
99     aSegments->AddEdge (aVertIter);
100     ++anEdgeCount;
101   }
102   aSegments->AddEdge (aNbVertices);
103   aSegments->AddEdge (aFirstContourVertex);
104   ++anEdgeCount;
105
106   for (Standard_Integer aVertIter = aFirstContourVertex; aVertIter <= aNbVertices - 1; ++aVertIter)
107   {
108     aSegments->AddEdge (aVertIter);
109     aSegments->AddEdge (aVertIter + 1);
110     ++anEdgeCount;
111   }
112   return aSegments;
113 }
114
115 // ============================================================================
116 // function : DrawShaded
117 // purpose  :
118 // ============================================================================
119 Handle(Graphic3d_ArrayOfTriangles) Prs3d_Arrow::DrawShaded (const gp_Ax1&          theAxis,
120                                                             const Standard_Real    theTubeRadius,
121                                                             const Standard_Real    theAxisLength,
122                                                             const Standard_Real    theConeRadius,
123                                                             const Standard_Real    theConeLength,
124                                                             const Standard_Integer theNbFacettes)
125 {
126   const Standard_Real aTubeLength = Max (0.0, theAxisLength - theConeLength);
127   const Standard_Integer aNbTrisTube = (theTubeRadius > 0.0 && aTubeLength > 0.0)
128                                      ? Prs3d_ToolCylinder::TrianglesNb (theNbFacettes, 1)
129                                      : 0;
130   const Standard_Integer aNbTrisCone = (theConeRadius > 0.0 && theConeLength > 0.0)
131                                      ? (Prs3d_ToolDisk    ::TrianglesNb (theNbFacettes, 1)
132                                       + Prs3d_ToolCylinder::TrianglesNb (theNbFacettes, 1))
133                                      : 0;
134
135   const Standard_Integer aNbTris = aNbTrisTube + aNbTrisCone;
136   if (aNbTris == 0)
137   {
138     return Handle(Graphic3d_ArrayOfTriangles)();
139   }
140
141   Handle(Graphic3d_ArrayOfTriangles) anArray = new Graphic3d_ArrayOfTriangles (aNbTris * 3, 0, Standard_True);
142   if (aNbTrisTube != 0)
143   {
144     gp_Ax3  aSystem (theAxis.Location(), theAxis.Direction());
145     gp_Trsf aTrsf;
146     aTrsf.SetTransformation (aSystem, gp_Ax3());
147
148     Prs3d_ToolCylinder aTool (theTubeRadius, theTubeRadius, aTubeLength, theNbFacettes, 1);
149     aTool.FillArray (anArray, aTrsf);
150   }
151
152   if (aNbTrisCone != 0)
153   {
154     gp_Pnt aConeOrigin = theAxis.Location().Translated (gp_Vec (theAxis.Direction().X() * aTubeLength,
155                                                                 theAxis.Direction().Y() * aTubeLength,
156                                                                 theAxis.Direction().Z() * aTubeLength));
157     gp_Ax3  aSystem (aConeOrigin, theAxis.Direction());
158     gp_Trsf aTrsf;
159     aTrsf.SetTransformation (aSystem, gp_Ax3());
160     {
161       Prs3d_ToolDisk aTool (0.0, theConeRadius, theNbFacettes, 1);
162       aTool.FillArray (anArray, aTrsf);
163     }
164     {
165       Prs3d_ToolCylinder aTool (theConeRadius, 0.0, theConeLength, theNbFacettes, 1);
166       aTool.FillArray (anArray, aTrsf);
167     }
168   }
169
170   return anArray;
171 }