0031490: Foundation Classes, Poly_Connect - speed up temporary allocations
[occt.git] / src / StdPrs / StdPrs_ShadedSurface.cxx
1 // Created on: 1995-07-27
2 // Created by: Modelistation
3 // Copyright (c) 1995-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 <Adaptor3d_Surface.hxx>
19 #include <gp_Pnt.hxx>
20 #include <gp_Vec.hxx>
21 #include <Graphic3d_ArrayOfTriangleStrips.hxx>
22 #include <Graphic3d_AspectFillArea3d.hxx>
23 #include <Graphic3d_Group.hxx>
24 #include <Precision.hxx>
25 #include <Prs3d_IsoAspect.hxx>
26 #include <Prs3d_Presentation.hxx>
27 #include <Prs3d_ShadingAspect.hxx>
28 #include <StdPrs_ShadedSurface.hxx>
29 #include <TColStd_Array1OfReal.hxx>
30
31 //=======================================================================
32 //function : Add
33 //purpose  :
34 //=======================================================================
35 void StdPrs_ShadedSurface::Add (const Handle(Prs3d_Presentation)& thePrs,
36                                 const Adaptor3d_Surface&          theSurface,
37                                 const Handle(Prs3d_Drawer)&       theDrawer)
38 {
39   Standard_Integer N1 = theDrawer->UIsoAspect()->Number();
40   Standard_Integer N2 = theDrawer->VIsoAspect()->Number();
41   N1 = N1 < 3 ? 3 : N1;
42   N2 = N2 < 3 ? 3 : N2;
43
44   // If the surface is closed, the faces from back-side are not traced:
45   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (thePrs);
46   aGroup->SetGroupPrimitivesAspect (theDrawer->ShadingAspect()->Aspect());
47   aGroup->SetClosed (theSurface.IsUClosed() && theSurface.IsVClosed());
48
49   Standard_Integer aNBUintv = theSurface.NbUIntervals (GeomAbs_C1);
50   Standard_Integer aNBVintv = theSurface.NbVIntervals (GeomAbs_C1);
51   TColStd_Array1OfReal anInterU (1, aNBUintv + 1);
52   TColStd_Array1OfReal anInterV (1, aNBVintv + 1);
53
54   theSurface.UIntervals (anInterU, GeomAbs_C1);
55   theSurface.VIntervals (anInterV, GeomAbs_C1);
56
57   Standard_Real U1, U2, V1, V2, DU, DV;
58
59   gp_Pnt P1, P2;
60   gp_Vec D1U, D1V, D1, D2;
61
62   for (Standard_Integer NU = 1; NU <= aNBUintv; ++NU)
63   {
64     for (Standard_Integer NV = 1; NV <= aNBVintv; ++NV)
65     {
66       U1 = anInterU (NU); U2 = anInterU (NU + 1);
67       V1 = anInterV (NV); V2 = anInterV (NV + 1);
68
69       U1 = (Precision::IsNegativeInfinite (U1)) ? - theDrawer->MaximalParameterValue() : U1;
70       U2 = (Precision::IsPositiveInfinite (U2)) ?   theDrawer->MaximalParameterValue() : U2;
71
72       V1 = (Precision::IsNegativeInfinite (V1)) ? - theDrawer->MaximalParameterValue() : V1;
73       V2 = (Precision::IsPositiveInfinite (V2)) ?   theDrawer->MaximalParameterValue() : V2;
74
75       DU = (U2 - U1) / N1;
76       DV = (V2 - V1) / N2;
77
78       Handle(Graphic3d_ArrayOfTriangleStrips) aPArray
79         = new Graphic3d_ArrayOfTriangleStrips (2 * (N1 + 1) * (N2 + 1), N1 + 1,
80                                                Standard_True, Standard_False, Standard_False, Standard_False);
81       for (Standard_Integer i = 1; i <= N1 + 1; ++i)
82       {
83         aPArray->AddBound (N2 + 1);
84         for (Standard_Integer j = 1; j <= N2 + 1; ++j)
85         {
86           theSurface.D1 (U1 + DU * (i - 1), V1 + DV * (j - 1), P2, D1U, D1V);
87           D1 = D1U ^ D1V;
88           D1.Normalize();
89           theSurface.D1 (U1 + DU * i, V1 + DV * (j - 1), P2, D1U, D1V);
90           D2 = D1U ^ D1V;
91           D2.Normalize();
92           aPArray->AddVertex (P1, D1);
93           aPArray->AddVertex (P2, D2);
94         }
95       }
96       aGroup->AddPrimitiveArray (aPArray);
97     }
98   }
99 }