summaryrefslogtreecommitdiff
path: root/src/AIS/AIS_TexturedShape.cxx
blob: 0572aaa3e60ea10b26f738617c622bf4ffcdb873 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// File:	AIS_TexturedShape.cdl
// Created:	Mon Jul  2 11:32:59 2001
// Author:	Mathias BOSSHARD
//		<mbd@pomalox.paris1.matra-dtv.fr>
// Copyright:	 Matra Datavision 2001
//
// Modified:
//
//
//
////////////////////////////////////////////////////////////////////////


#include <AIS_TexturedShape.ixx>
#include <Standard_ErrorHandler.hxx>

#include <BRepTools.hxx>
#include <gp_Vec.hxx>
#include <Graphic3d_StructureManager.hxx>
#include <Graphic3d_Texture2Dmanual.hxx>
#include <Graphic3d_AspectFillArea3d.hxx>
#include <AIS_InteractiveContext.hxx>
#include <Prs3d_ShadingAspect.hxx>
#include <Prs3d_Root.hxx>
#include <PrsMgr_PresentationManager3d.hxx>
#include <Prs3d_Presentation.hxx>
#include <TopExp_Explorer.hxx>
#include <StdPrs_WFDeflectionShape.hxx>
#include <Graphic3d_Group.hxx>
#include <AIS_Drawer.hxx>
#include <StdPrs_WFShape.hxx>
#include <StdPrs_ShadedShape.hxx>
#include <StdPrs_ToolShadedShape.hxx>
#include <Precision.hxx>
#include <BRepMesh.hxx>
#include <Poly_Triangulation.hxx>
#include <Poly_Connect.hxx>
#include <Graphic3d_Array1OfVertexNT.hxx>
#include <Aspect_Array1OfEdge.hxx>
#include <TColgp_Array1OfDir.hxx>
#include <TColgp_Array1OfPnt2d.hxx>

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

//////////////////////////////////////////////////////////////////////
// CONSTRUCTOR / DESTRUCTOR
//////////////////////////////////////////////////////////////////////

//=======================================================================
//function : AIS_TexturedShape
//purpose  : 
//=======================================================================

AIS_TexturedShape::AIS_TexturedShape(const TopoDS_Shape& ashape):AIS_Shape(ashape),
                                     myPredefTexture(Graphic3d_NameOfTexture2D(0)),
				     myTextureFile(""),
				     DoRepeat(Standard_True),
				     myURepeat(1.0),
				     myVRepeat(1.0),
				     DoMapTexture(Standard_True),
				     DoSetTextureOrigin(Standard_True),
				     myUOrigin(0.0),
				     myVOrigin(0.0),
				     DoSetTextureScale(Standard_True),
				     myScaleU(1.0),
				     myScaleV(1.0),
				     DoShowTriangles(Standard_False),
                                     myModulate(Standard_True)
{
}

//////////////////////////////////////////////////////////////////////
// TEXTURE MAPPING MANAGEMENT METHODS
//////////////////////////////////////////////////////////////////////

//=======================================================================
//function : SetTextureFileName
//purpose  : 
//=======================================================================

void AIS_TexturedShape::SetTextureFileName(const TCollection_AsciiString& TextureFileName)
{
  if (TextureFileName.IsIntegerValue())
    {
      if(TextureFileName.IntegerValue()<Graphic3d_Texture2D::NumberOfTextures() && TextureFileName.IntegerValue()>=0)
	myPredefTexture = (Graphic3d_NameOfTexture2D)(TextureFileName.IntegerValue());
      else
	{
	  cout << "Texture "<<TextureFileName<<" doesn't exist \n"<< endl;
	  cout << "Using Texture 0 instead ...\n"<< endl;
	  myPredefTexture = (Graphic3d_NameOfTexture2D)(0);
	}
      myTextureFile = "";
    }
  else
    {
      myTextureFile = TextureFileName;
      myPredefTexture = (Graphic3d_NameOfTexture2D)(-1);
    }
}

//=======================================================================
//function : SetTextureRepeat
//purpose  : 
//=======================================================================

void AIS_TexturedShape::SetTextureRepeat(const Standard_Boolean RepeatYN,
                                         const Standard_Real URepeat,
                                         const Standard_Real VRepeat)
{
  DoRepeat = RepeatYN;
  myURepeat = URepeat;
  myVRepeat = VRepeat;
}

//=======================================================================
//function : SetTextureMapOn
//purpose  : 
//=======================================================================

void AIS_TexturedShape::SetTextureMapOn()
{
  DoMapTexture = Standard_True;
}

//=======================================================================
//function : SetTextureMapOff
//purpose  : 
//=======================================================================

void AIS_TexturedShape::SetTextureMapOff()
{
  DoMapTexture = Standard_False;
}

//=======================================================================
//function : SetTextureOrigin
//purpose  : 
//=======================================================================

void AIS_TexturedShape::SetTextureOrigin(const Standard_Boolean SetTextureOriginYN, const Standard_Real UOrigin, const Standard_Real VOrigin)
{
  DoSetTextureOrigin = SetTextureOriginYN;
  myUOrigin = UOrigin;
  myVOrigin = VOrigin;
}

//=======================================================================
//function : SetTextureScale
//purpose  : 
//=======================================================================

void AIS_TexturedShape::SetTextureScale(const Standard_Boolean SetTextureScaleYN, const Standard_Real ScaleU, const Standard_Real ScaleV)
{
  DoSetTextureScale = SetTextureScaleYN;
  myScaleU = ScaleU;
  myScaleV = ScaleV;
}

//=======================================================================
//function : TriangleIsValid
//purpose  : 
//=======================================================================

Standard_Boolean AIS_TexturedShape::TriangleIsValid(const gp_Pnt& P1, const gp_Pnt& P2, const gp_Pnt& P3) const
{ 
  gp_Vec V1(P1,P2);								// V1=(P1,P2)
  gp_Vec V2(P2,P3);								// V2=(P2,P3)
  gp_Vec V3(P3,P1);								// V3=(P3,P1)
  
  if ((V1.SquareMagnitude() > 1.e-10) && (V2.SquareMagnitude() > 1.e-10) && (V3.SquareMagnitude() > 1.e-10))
    {
      V1.Cross(V2);								// V1 = Normal	
      if (V1.SquareMagnitude() > 1.e-10)
	return Standard_True;
      else
	return Standard_False;
    }
  else
    return Standard_False;
  
}

//=======================================================================
//function : ShowTriangles
//purpose  : 
//=======================================================================

void AIS_TexturedShape::ShowTriangles(const Standard_Boolean ShowTrianglesYN)
{
  DoShowTriangles = ShowTrianglesYN;
}

//=======================================================================
//function : EnableTextureModulate
//purpose  : 
//=======================================================================

void AIS_TexturedShape::EnableTextureModulate()
{
  myModulate = Standard_True;
}

//=======================================================================
//function : DisableTextureModulate
//purpose  : 
//=======================================================================

void AIS_TexturedShape::DisableTextureModulate()
{
  myModulate = Standard_False;
}

//=======================================================================
//function : UpdateAttributes
//purpose  : 
//=======================================================================

void AIS_TexturedShape::UpdateAttributes()
{
  Handle(Graphic3d_StructureManager) aStrucMana = GetContext()->MainPrsMgr()->StructureManager();
  myAspect = (new Prs3d_ShadingAspect())->Aspect();
  Handle(Prs3d_Presentation) aPrs = Presentation();
  if (!DoMapTexture)
    {
      myAspect->SetTextureMapOff();
      return;
    }
  
  if(myPredefTexture!=-1)
    mytexture = new Graphic3d_Texture2Dmanual(aStrucMana, myPredefTexture);
  else
    mytexture = new Graphic3d_Texture2Dmanual(aStrucMana, myTextureFile.ToCString());
  
  myAspect->SetTextureMapOn();
  
  myAspect->SetTextureMap(mytexture);
  if (!mytexture->IsDone())
    {
      cout << "An error occured while building texture \n" <<endl;
      return;
    }
  
  if (DoShowTriangles)
    myAspect->SetEdgeOn();
  else
    myAspect->SetEdgeOff();
  
  Prs3d_Root::CurrentGroup(aPrs)->SetGroupPrimitivesAspect(myAspect);
}

//=======================================================================
//function : Compute
//purpose  : 
//=======================================================================

void AIS_TexturedShape::Compute(const Handle(PrsMgr_PresentationManager3d)& /*aPresentationManager*/,
				const Handle(Prs3d_Presentation)& aPrs,
				const Standard_Integer aMode)
{
  aPrs->Clear();
  
  if(myshape.IsNull()) return;
  
  Standard_Integer TheType;
  TheType = (Standard_Integer) myshape.ShapeType();
  if(TheType>4 && TheType<8)
    {
      aPrs->SetVisual(Graphic3d_TOS_ALL);
      aPrs->SetDisplayPriority(TheType+2);
    }

  if (myshape.ShapeType() == TopAbs_COMPOUND)
    {
      TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
      if (!anExplor.More()) {return;}
    }

  if (IsInfinite())
    aPrs->SetInfiniteState(Standard_True);

  switch (aMode)
    { 

    case 0: // Wireframe
      StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
      break;

    case 1: // Shading)
      {
	Standard_Real prevangle ;
	Standard_Real newangle  ; 
	Standard_Real prevcoeff = 0.0 ;
	Standard_Real newcoeff = 0.0 ; 
	
	if (OwnDeviationAngle(newangle,prevangle) || OwnDeviationCoefficient(newcoeff,prevcoeff))
	  if (Abs (newangle - prevangle) > Precision::Angular() || Abs (newcoeff - prevcoeff) > Precision::Confusion()  ) 
	    { 
	      BRepTools::Clean(myshape);
	    }
	if ((Standard_Integer) myshape.ShapeType()>4)
	  StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
	else
	  {
	    myDrawer->SetShadingAspectGlobal(Standard_False);
	    if (IsInfinite())
	      StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
	    else
	      {
		try
		  {
		  OCC_CATCH_SIGNALS
		    StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
		  }
		catch (Standard_Failure)
		  {
		    cout <<"AIS_TexturedShape::Compute() in ShadingMode failed \n" <<endl;
		    StdPrs_WFShape::Add(aPrs,myshape,myDrawer);
		  }
	      }
	  }
	break;
      }

    case 2: // Bounding box
      {
	if (IsInfinite())
	  {
	    StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
	  }
	else
	  {
	    AIS_Shape::DisplayBox(aPrs,BoundingBox(),myDrawer);
	  }
	break;
      }

    case 3: // texture mapping on triangulation 
      {
	BRepTools::Clean(myshape);
	BRepTools::Update(myshape);

	Handle(Graphic3d_StructureManager) aStrucMana = GetContext()->MainPrsMgr()->StructureManager();
	  {  
	    Handle(Prs3d_ShadingAspect) aPrs3d_ShadingAspect = new Prs3d_ShadingAspect;
	    myAspect = aPrs3d_ShadingAspect->Aspect();
	  }
	if (!DoMapTexture)
	  {
	    myAspect->SetTextureMapOff();
	    return;
	  }
	myAspect->SetTextureMapOn();
	
	if(myPredefTexture!=-1)
	  mytexture = new Graphic3d_Texture2Dmanual(aStrucMana, myPredefTexture);
	else
	  mytexture = new Graphic3d_Texture2Dmanual(aStrucMana, myTextureFile.ToCString());

	if (!mytexture->IsDone())
	  {
	    cout <<"An error occured while building texture \n" <<endl;
	    return;
	  }
	if (myModulate)
	  mytexture->EnableModulate();
	else
	  mytexture->DisableModulate();

	myAspect->SetTextureMap(mytexture);
	if (DoShowTriangles)
	  myAspect->SetEdgeOn();
	else
	  myAspect->SetEdgeOff();

	if (DoRepeat)
	  mytexture->EnableRepeat();
	else
	  mytexture->DisableRepeat();

	myDeflection = AIS_Shape::GetDeflection(myshape,myDrawer);
	BRepMesh::Mesh(myshape,myDeflection);
	// Adds a triangulation of the shape myshape to its topological data structure.
	// This triangulation is computed with the deflection myDeflection.

#ifdef DEBUG
	cout <<"Deflection =  " << myDeflection << "\n" << endl;
#endif

	StdPrs_ToolShadedShape SST;

	Standard_Integer NumFace;
	TopExp_Explorer ExpFace;

	for( NumFace=0,ExpFace.Init(myshape,TopAbs_FACE); ExpFace.More(); ExpFace.Next(),NumFace++ )
	  {
	    TopoDS_Face		myFace		= TopoDS::Face(ExpFace.Current());
	    TopLoc_Location aLocation	= myFace.Location();

#ifdef DEBUG
	    cout << "J\'explore actuellement la face " << NumFace << "\n" << endl;
#endif
	    Handle(Poly_Triangulation) myT = BRep_Tool::Triangulation(myFace, aLocation);
	    // Returns  the Triangulation of  the  face. It  is a null handle if there is no triangulation.

	    if (myT.IsNull())
	      {
#ifdef DEBUG
		cout << "Triangulation of the face "<< i <<" is null \n"<< endl;
#endif
		return;
	      }
	    Poly_Connect pc(myT);	
	    const TColgp_Array1OfPnt& Nodes = myT->Nodes();
	    const TColgp_Array1OfPnt2d& UVNodes = myT->UVNodes();
	    const Poly_Array1OfTriangle& triangles = myT->Triangles();
	    TColgp_Array1OfDir myNormal(Nodes.Lower(), Nodes.Upper());
	    
	    SST.Normal(myFace, pc, myNormal);
	    BRepTools::UVBounds(myFace,Umin, Umax, Vmin, Vmax);
	    dUmax = (Umax - Umin);
	    dVmax = (Vmax - Vmin);
	    Handle(Graphic3d_Group) mygroup = Prs3d_Root::CurrentGroup(aPrs);

	    Standard_Integer nnn = myT->NbTriangles();					// nnn : number of triangles
	    Standard_Integer nt, n1, n2, n3 = 0;						// nt  : current triangle
	    // ni  : top i of the current triangle
	    for (nt = 1; nt <= nnn; nt++)					
	      {
#ifdef DEBUG
		cout << "On traite actuellement le triangle : "<< nt <<"\n";
#endif						
		if (SST.Orientation(myFace) == TopAbs_REVERSED)			// if the face is "reversed"
		  triangles(nt).Get(n1,n3,n2);						// the triangle is n1,n3,n2
		else 
		  triangles(nt).Get(n1,n2,n3);						// the triangle is n1,n2,n3

		if (TriangleIsValid (Nodes(n1),Nodes(n2),Nodes(n3)) )
		  {	// Associates vertexNT to each node
		    Graphic3d_Array1OfVertexNT Points(1,3);
		    Aspect_Array1OfEdge aretes(1,3);

		    mygroup->BeginPrimitives();
		    {
		      gp_Pnt p = Nodes(n1).Transformed(aLocation.Transformation());
		      gp_Pnt q = Nodes(n2).Transformed(aLocation.Transformation());
		      gp_Pnt r = Nodes(n3).Transformed(aLocation.Transformation());

		      Points(1).SetCoord(p.X(), p.Y(), p.Z());
		      Points(2).SetCoord(q.X(), q.Y(), q.Z());
		      Points(3).SetCoord(r.X(), r.Y(), r.Z());

		      Points(1).SetNormal(myNormal(n1).X(), myNormal(n1).Y(), myNormal(n1).Z());
		      Points(2).SetNormal(myNormal(n2).X(), myNormal(n2).Y(), myNormal(n2).Z());
		      Points(3).SetNormal(myNormal(n3).X(), myNormal(n3).Y(), myNormal(n3).Z());

		      Points(1).SetTextureCoordinate((-myUOrigin+(myURepeat*(UVNodes(n1).X()-Umin))/dUmax)/myScaleU,
						     (-myVOrigin+(myVRepeat*(UVNodes(n1).Y()-Vmin))/dVmax)/myScaleV);
		      Points(2).SetTextureCoordinate((-myUOrigin+(myURepeat*(UVNodes(n2).X()-Umin))/dUmax)/myScaleU,
						     (-myVOrigin+(myVRepeat*(UVNodes(n2).Y()-Vmin))/dVmax)/myScaleV);
		      Points(3).SetTextureCoordinate((-myUOrigin+(myURepeat*(UVNodes(n3).X()-Umin))/dUmax)/myScaleU,
						     (-myVOrigin+(myVRepeat*(UVNodes(n3).Y()-Vmin))/dVmax)/myScaleV);
		      
		      aretes(1).SetValues(1, 2, Aspect_TOE_INVISIBLE);
		      aretes(2).SetValues(2, 3, Aspect_TOE_INVISIBLE);
		      aretes(3).SetValues(3, 1, Aspect_TOE_INVISIBLE);
		    }
		    mygroup->EndPrimitives();
		    mygroup->TriangleSet(Points, aretes, Standard_True);	

		  } // end of "if the triangle is valid
	      } // end of the "parcours" of the triangles
	    mygroup->SetGroupPrimitivesAspect(myAspect);
	  }// end of the exploration of the shape in faces
	break;
      }// end case 3

    } // end switch
  //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
}





/////////////////////////////////////////////////////////
//		QUERY METHODS
/////////////////////////////////////////////////////////



Standard_Boolean AIS_TexturedShape::TextureMapState() const
{
  return DoMapTexture;
}

Standard_Real AIS_TexturedShape::URepeat() const
{
  return myURepeat;
}

Standard_Boolean AIS_TexturedShape::TextureRepeat() const
{
  return DoRepeat;
}

Standard_Real AIS_TexturedShape::Deflection() const
{
  return myDeflection;
}

Standard_CString AIS_TexturedShape::TextureFile() const
{
  return myTextureFile.ToCString();
}

Standard_Real AIS_TexturedShape::VRepeat() const
{
  return myVRepeat;
}
Standard_Boolean AIS_TexturedShape::ShowTriangles() const
{
  return DoShowTriangles;
}
Standard_Real AIS_TexturedShape::TextureUOrigin() const
{
  return myUOrigin;
}
Standard_Real AIS_TexturedShape::TextureVOrigin() const
{
  return myVOrigin;
}
Standard_Real AIS_TexturedShape::TextureScaleU() const
{
  return myScaleU;
}
Standard_Real AIS_TexturedShape::TextureScaleV() const
{
  return myScaleV;
}
Standard_Boolean	AIS_TexturedShape::TextureScale() const
{
  return DoSetTextureScale;
}
Standard_Boolean	AIS_TexturedShape::TextureOrigin() const
{
  return DoSetTextureOrigin;
}
Standard_Boolean	AIS_TexturedShape::TextureModulate() const
{
  return myModulate;
}