summaryrefslogtreecommitdiff
path: root/inc/Prs3d_ShadedShape.gxx
blob: 52e93916efaac43dd765defa3fa08f9a7088de60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// File:	Prs3d_ShadedShape.gxx
// Created:	Thu Sep 23 18:47:22 1993
// Author:	Jean-Louis FRENKEL
//		<jlf@stylox>

//#define BUC60488//GG_081199 Enable the SuppressBackface() ShadingAspect attribute

#define G005    //ATS,GG 04/01/01 Use ArrayOfPrimitives instead Sets of primitives
//              for performance improvment

#include <Graphic3d_Vertex.hxx>
#include <Graphic3d_VertexN.hxx>
#include <Graphic3d_Array1OfVertexN.hxx>
#include <Graphic3d_Array1OfVertex.hxx>
#include <Aspect_Edge.hxx>
#include <Aspect_Array1OfEdge.hxx>
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include <Prs3d_ShadingAspect.hxx>
#include <Graphic3d_Group.hxx>
#include <Aspect_TypeOfEdge.hxx>
#include <Bnd_Box.hxx>
#include <Graphic3d_AspectFillArea3d.hxx>
#include <BRepTools.hxx>
#include <BRep_Tool.hxx>
#include <BRep_Builder.hxx>
#include <TopoDS_Compound.hxx>
#include <Poly_Triangulation.hxx>
#include <TColgp_HArray1OfPnt.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <TColgp_Array1OfDir.hxx>
#include <Poly_Connect.hxx>
#include <TopAbs_Orientation.hxx>
#include <TColStd_MapOfInteger.hxx>
#include <TColStd_MapIteratorOfMapOfInteger.hxx>
#include <BRepMesh_FactoryError.hxx>
#include <BRepMesh_DiscretRoot.hxx>
#include <BRepMesh_DiscretFactory.hxx>
#include <BRepMesh_PDiscretRoot.hxx>
#include <gp_Vec.hxx>
#include <StdPrs_WFShape.hxx>
#include <BRepBndLib.hxx>
#include <Precision.hxx>
#ifdef G005
#include <Graphic3d_ArrayOfTriangles.hxx>
#endif


#define MAX2(X, Y)	(  Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
#define MAX3(X, Y, Z)	( MAX2 ( MAX2(X,Y) , Z) )


static Standard_Real GetDeflection(const anyShape&             aShape,
                                   const Handle(Prs3d_Drawer)& aDrawer)
{
  Standard_Real aDeflection = aDrawer->MaximalChordialDeviation();
  if (aDrawer->TypeOfDeflection() == Aspect_TOD_RELATIVE) {
    Bnd_Box B;
    BRepBndLib::Add(aShape, B, Standard_False);
    if ( ! B.IsVoid() )
    {
      Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
      B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
      aDeflection = MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) 
        * aDrawer->DeviationCoefficient()*4;    
    }
  }
  return aDeflection;
}


static Standard_Boolean ShadeFromShape(const anyShape&                    aShape,
                                       const Standard_Real                /*defle*/,
                                       const Standard_Boolean             /*share*/,
                                       const Handle (Prs3d_Presentation)& aPresentation,
                                       const Handle (Prs3d_Drawer)&       aDrawer) 
{
  anyShadedShapeTool SST;
  Handle(Poly_Triangulation) T;
  TopLoc_Location loc;
  gp_Pnt p;
  Standard_Integer i,j,k,decal ;
  Standard_Integer t[3], n[3];
  Standard_Integer nbTriangles = 0, nbVertices = 0;

  // precision for compare square distances
  double dPreci = Precision::Confusion()*Precision::Confusion();

  if ( !aDrawer->ShadingAspectGlobal() ) {

    Handle(Graphic3d_AspectFillArea3d) Asp = aDrawer->ShadingAspect()->Aspect();
    if(anyShadedShapeTool::IsClosed(aShape)) {
      Asp->SuppressBackFace();
    } else {
      Asp->AllowBackFace();
    }
    Prs3d_Root::CurrentGroup(aPresentation)->SetPrimitivesAspect(Asp);
  }

#ifdef G005
  if( Graphic3d_ArrayOfPrimitives::IsEnable() ) {

    for (SST.Init(aShape); SST.MoreFace(); SST.NextFace()) {
      const anyTopFace& F = SST.CurrentFace();
      T = SST.Triangulation(F, loc);
      if (!T.IsNull()) {
        nbTriangles += T->NbTriangles();
        nbVertices += T->NbNodes();
      }
    } 

    if (nbVertices > 2 && nbTriangles > 0) {
      Handle(Graphic3d_ArrayOfTriangles) parray =
        new Graphic3d_ArrayOfTriangles(nbVertices,3*nbTriangles,
          Standard_True,Standard_False,Standard_False,Standard_True);
      for (SST.Init(aShape); SST.MoreFace(); SST.NextFace()) {
        const anyTopFace& F = SST.CurrentFace();
        T = SST.Triangulation(F, loc);
        if (!T.IsNull()) {
          const gp_Trsf& trsf = loc.Transformation();
          Poly_Connect pc(T);
          // Extracts vertices & normals from nodes 
          const TColgp_Array1OfPnt& Nodes = T->Nodes();
          TColgp_Array1OfDir NORMAL(Nodes.Lower(), Nodes.Upper());
          SST.Normal(F, pc, NORMAL);

          decal = parray->VertexNumber();
          for (i= Nodes.Lower(); i<= Nodes.Upper(); i++) {
            p = Nodes(i);
            if( !loc.IsIdentity() ) {
              p.Transform(trsf);
              NORMAL(i).Transform(trsf);
            }
            parray->AddVertex(p,NORMAL(i));
          }
  
          // Fill parray with vertex and edge visibillity info
          const Poly_Array1OfTriangle& triangles = T->Triangles();
          for (i = 1; i <= T->NbTriangles(); i++) {
            pc.Triangles(i,t[0],t[1],t[2]);
            if (SST.Orientation(F) == TopAbs_REVERSED) 
              triangles(i).Get(n[0],n[2],n[1]);
            else 
              triangles(i).Get(n[0],n[1],n[2]);
            gp_Pnt P1 = Nodes(n[0]);
            gp_Pnt P2 = Nodes(n[1]);
            gp_Pnt P3 = Nodes(n[2]);
            gp_Vec V1(P1,P2);
            if ( V1.SquareMagnitude() > dPreci ) {
              gp_Vec V2(P2,P3);
              if ( V2.SquareMagnitude() > dPreci ) {
                gp_Vec V3(P3,P1);
                if ( V3.SquareMagnitude() > dPreci ) {
                  V1.Normalize();
                  V2.Normalize();
                  V1.Cross(V2);
                  if ( V1.SquareMagnitude() > dPreci ) {
                    parray->AddEdge(n[0]+decal,t[0] == 0);
                    parray->AddEdge(n[1]+decal,t[1] == 0);
                    parray->AddEdge(n[2]+decal,t[2] == 0);
                  }
                }
              }
            }
          }
        }
      }    
      Prs3d_Root::CurrentGroup(aPresentation)->BeginPrimitives();
      Prs3d_Root::CurrentGroup(aPresentation)->AddPrimitiveArray(parray);
      Prs3d_Root::CurrentGroup(aPresentation)->EndPrimitives();
    }
    return Standard_True;
  }
#endif

  // phase de comptage:
  Standard_Integer nt, nnn, n1, n2, n3, nnv, EI;
  static Standard_Integer plus1mod3[3] = {1, 2, 0};
  for (SST.Init(aShape); SST.MoreFace(); SST.NextFace()) {
    const anyTopFace& F = SST.CurrentFace();
    T = SST.Triangulation(F, loc);
    if (!T.IsNull()) {
      nnn = T->NbTriangles();
      const TColgp_Array1OfPnt& Nodes = T->Nodes();
      const Poly_Array1OfTriangle& triangles = T->Triangles();
      for (nt = 1; nt <= nnn; nt++) {
        if (SST.Orientation(F) == TopAbs_REVERSED) 
          triangles(nt).Get(n1,n3,n2);
        else 
          triangles(nt).Get(n1,n2,n3);
        const gp_Pnt& P1 = Nodes(n1);
        const gp_Pnt& P2 = Nodes(n2);
        const gp_Pnt& P3 = Nodes(n3);
        gp_Vec V1(P1,P2);
        if ( V1.SquareMagnitude() > dPreci ) {
          gp_Vec V2(P2,P3);
          if (V2.SquareMagnitude() > dPreci ) {
            gp_Vec V3(P3,P1);
            if (V3.SquareMagnitude() > dPreci ) {
              V1.Normalize();
              V2.Normalize();
              V1.Cross(V2);
              if (V1.SquareMagnitude() > dPreci ) {
                nbTriangles++;
              }
            }
          }
        }
      }
      nbVertices += T->NbNodes();
    }
  }      

  if (nbVertices > 2 && nbTriangles > 0) {
    Graphic3d_Array1OfVertexN AVN(1, nbVertices);
    Aspect_Array1OfEdge AE(1, 3*nbTriangles);
    
    EI = 1;
    nnv = 1;
    
    for (SST.Init(aShape); SST.MoreFace(); SST.NextFace()) {
      const anyTopFace& F = SST.CurrentFace();
      T = SST.Triangulation(F, loc);
      if (!T.IsNull()) {
        Poly_Connect pc(T);
        // 1- les noeuds.
        const TColgp_Array1OfPnt& Nodes = T->Nodes();
        TColgp_Array1OfDir NORMAL(Nodes.Lower(), Nodes.Upper());
        SST.Normal(F, pc, NORMAL);
        decal = nnv-1;
       
        for (j= Nodes.Lower(); j<= Nodes.Upper(); j++) {
          p = Nodes(j).Transformed(loc.Transformation());
          AVN(nnv).SetCoord(p.X(), p.Y(), p.Z());
          AVN(nnv).SetNormal(NORMAL(j).X(), NORMAL(j).Y(), NORMAL(j).Z());
          nnv++;
        }
        // 2- les edges.
        nbTriangles = T->NbTriangles();
        const Poly_Array1OfTriangle& triangles = T->Triangles();
        
        for (i = 1; i <= nbTriangles; i++) {
          pc.Triangles(i,t[0],t[1],t[2]);
          if (SST.Orientation(F) == TopAbs_REVERSED) 
            triangles(i).Get(n[0],n[2],n[1]);
          else 
            triangles(i).Get(n[0],n[1],n[2]);
          const gp_Pnt& P1 = Nodes(n[0]);
          const gp_Pnt& P2 = Nodes(n[1]);
          const gp_Pnt& P3 = Nodes(n[2]);
          gp_Vec V1(P1,P2);
          if (V1.SquareMagnitude() > 1.e-10) {
            gp_Vec V2(P2,P3);
            if (V2.SquareMagnitude() > 1.e-10) {
              gp_Vec V3(P3,P1);
              if (V3.SquareMagnitude() > 1.e-10) {
                V1.Normalize();
                V2.Normalize();
                V1.Cross(V2);
                if (V1.SquareMagnitude() > 1.e-10) {
                  for (j = 0; j < 3; j++) {
                    k = plus1mod3[j];
                    if (t[j] == 0)
                      AE(EI).SetValues(n[j]+decal, n[k]+decal, Aspect_TOE_VISIBLE);
                    else
                      AE(EI).SetValues(n[j]+decal, n[k]+decal, Aspect_TOE_INVISIBLE);
                    EI++;
                  }
                }
              }
            }
          }
        }    
      }
    }
    Prs3d_Root::CurrentGroup(aPresentation)->TriangleSet(AVN, AE);
  }
  return Standard_True;
}



void Prs3d_ShadedShape::Add(const Handle (Prs3d_Presentation)& aPresentation,
                            const anyShape&                    aShape,
                            const Handle (Prs3d_Drawer)&       aDrawer)
{

  if (aShape.IsNull()) return;

  TopAbs_ShapeEnum E = aShape.ShapeType();
  if (E == TopAbs_COMPOUND) {
    TopExp_Explorer ex;

    ex.Init(aShape, TopAbs_FACE);
    if (ex.More()) {
      TopoDS_Compound CO;
      BRep_Builder B;
      B.MakeCompound(CO);
      Standard_Boolean haselement = Standard_False;

      // il faut presenter les edges  isoles.
      for (ex.Init(aShape, TopAbs_EDGE, TopAbs_FACE); ex.More(); ex.Next()) {
        haselement = Standard_True;
        B.Add(CO, ex.Current());
      }
      // il faut presenter les vertex isoles.
      for (ex.Init(aShape, TopAbs_VERTEX, TopAbs_EDGE); ex.More(); ex.Next()) {
        haselement = Standard_True;
        B.Add(CO, ex.Current());
      }
      if (haselement) StdPrs_WFShape::Add(aPresentation, CO, aDrawer);
    }
    else {
      StdPrs_WFShape::Add(aPresentation, aShape, aDrawer);
    }
  }
  Standard_Real aDeflection = GetDeflection(aShape, aDrawer);

  // Check if it is possible to avoid unnecessary recomputation 
  // of shape triangulation
  if (!BRepTools::Triangulation (aShape, aDeflection))
  {
    BRepTools::Clean (aShape);

    // retrieve meshing tool from Factory
    Handle(BRepMesh_DiscretRoot) aMeshAlgo = BRepMesh_DiscretFactory::Get().Discret (aShape,
                                                                                     aDeflection,
                                                                                     aDrawer->HLRAngle());
    if (!aMeshAlgo.IsNull())
      aMeshAlgo->Perform();
  }

  ShadeFromShape(aShape, aDeflection, Standard_True, aPresentation, aDrawer);
}