0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / Prs3d / Prs3d_ToolQuadric.cxx
1 // Created on: 2016-02-04
2 // Created by: Anastasia BORISOVA
3 // Copyright (c) 2016 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <Prs3d_ToolQuadric.hxx>
17
18 #include <Graphic3d_ArrayOfTriangles.hxx>
19 #include <Poly_Array1OfTriangle.hxx>
20
21 //=======================================================================
22 //function : FIllArray
23 //purpose  :
24 //=======================================================================
25 void Prs3d_ToolQuadric::FillArray (Handle(Graphic3d_ArrayOfTriangles)& theArray,
26                                    const gp_Trsf& theTrsf) const
27 {
28   if (theArray.IsNull())
29   {
30     theArray = new Graphic3d_ArrayOfTriangles (VerticesNb(), TrianglesNb() * 3, Graphic3d_ArrayFlags_VertexNormal);
31   }
32
33   const Standard_Real aStepU = 1.0f / mySlicesNb;
34   const Standard_Real aStepV = 1.0f / myStacksNb;
35   if (theArray->EdgeNumberAllocated() > 0)
36   {
37     // indexed array
38     for (Standard_Integer aU = 0; aU <= mySlicesNb; ++aU)
39     {
40       const Standard_Real aParamU = aU * aStepU;
41       for (Standard_Integer aV = 0; aV <= myStacksNb; ++aV)
42       {
43         const Standard_Real aParamV = aV * aStepV;
44         const gp_Pnt aVertex = Vertex (aParamU, aParamV).Transformed (theTrsf);
45         const gp_Dir aNormal = Normal (aParamU, aParamV).Transformed (theTrsf);
46         theArray->AddVertex (aVertex, aNormal);
47
48         if (aU != 0 && aV != 0)
49         {
50           const int aVertId = theArray->VertexNumber();
51           theArray->AddTriangleEdges (aVertId, aVertId - myStacksNb - 2, aVertId - 1);
52           theArray->AddTriangleEdges (aVertId - myStacksNb - 2, aVertId, aVertId - myStacksNb - 1);
53         }
54       }
55     }
56   }
57   else
58   {
59     // non-indexed array
60     for (Standard_Integer aU = 0; aU < mySlicesNb; ++aU)
61     {
62       const Standard_Real aParamU = aU * aStepU;
63       for (Standard_Integer aV = 0; aV < myStacksNb; ++aV)
64       {
65         const Standard_Real aParamV = aV * aStepV;
66         theArray->AddVertex (Vertex (aParamU, aParamV).Transformed (theTrsf),
67                              Normal (aParamU, aParamV).Transformed (theTrsf));
68         theArray->AddVertex (Vertex (aParamU + aStepU, aParamV).Transformed (theTrsf),
69                              Normal (aParamU + aStepU, aParamV).Transformed (theTrsf));
70         theArray->AddVertex (Vertex (aParamU + aStepU, aParamV + aStepV).Transformed (theTrsf),
71                              Normal (aParamU + aStepU, aParamV + aStepV).Transformed (theTrsf));
72         theArray->AddVertex (Vertex (aParamU + aStepU, aParamV + aStepV).Transformed (theTrsf),
73                              Normal (aParamU + aStepU, aParamV + aStepV).Transformed (theTrsf));
74         theArray->AddVertex (Vertex (aParamU, aParamV + aStepV).Transformed (theTrsf),
75                              Normal (aParamU, aParamV + aStepV).Transformed (theTrsf));
76         theArray->AddVertex (Vertex (aParamU, aParamV).Transformed (theTrsf),
77                              Normal (aParamU, aParamV).Transformed (theTrsf));
78       }
79     }
80   }
81 }
82
83 //=======================================================================
84 //function : CreateTriangulation
85 //purpose  :
86 //=======================================================================
87 Handle(Graphic3d_ArrayOfTriangles) Prs3d_ToolQuadric::CreateTriangulation (const gp_Trsf& theTrsf) const
88 {
89   Handle(Graphic3d_ArrayOfTriangles) aTriangulation;
90   FillArray (aTriangulation, theTrsf);
91   return aTriangulation;
92 }
93
94 //=======================================================================
95 //function : CreatePolyTriangulation
96 //purpose  :
97 //=======================================================================
98 Handle(Poly_Triangulation) Prs3d_ToolQuadric::CreatePolyTriangulation (const gp_Trsf& theTrsf) const
99 {
100   Handle(Poly_Triangulation) aTriangulation = new Poly_Triangulation (VerticesNb(), TrianglesNb(), Standard_False);
101   Standard_ShortReal aStepU = 1.0f / mySlicesNb;
102   Standard_ShortReal aStepV = 1.0f / myStacksNb;
103
104   // Fill triangles
105   for (Standard_Integer aU = 0, anIndex = 0; aU <= mySlicesNb; ++aU)
106   {
107     const Standard_Real aParamU = aU * aStepU;
108     for (Standard_Integer aV = 0; aV <= myStacksNb; ++aV)
109     {
110       const Standard_ShortReal aParamV = aV * aStepV;
111       const Standard_Integer   aVertId = aU * (myStacksNb + 1) + (aV + 1);
112       gp_Pnt aVertex = Vertex (aParamU, aParamV).Transformed (theTrsf);
113
114       aTriangulation->SetNode (aVertId, aVertex);
115       if (aU != 0 && aV != 0)
116       {
117         aTriangulation->SetTriangle (++anIndex, Poly_Triangle (aVertId, aVertId - myStacksNb - 2, aVertId - 1));
118         aTriangulation->SetTriangle (++anIndex, Poly_Triangle (aVertId - myStacksNb - 2, aVertId, aVertId - myStacksNb - 1));
119       }
120     }
121   }
122   return aTriangulation;
123 }
124
125 //=======================================================================
126 //function : FillArray
127 //purpose  :
128 //=======================================================================
129 void Prs3d_ToolQuadric::FillArray (Handle(Graphic3d_ArrayOfTriangles)& theArray,
130                                    Handle(Poly_Triangulation)& theTriangulation,
131                                    const gp_Trsf& theTrsf) const
132 {
133   theArray = CreateTriangulation (theTrsf);
134   theTriangulation = CreatePolyTriangulation (theTrsf);
135 }