0025266: Debug statements in the source are getting flushed on to the console
[occt.git] / src / AIS / AIS_TexturedShape.cxx
CommitLineData
b311480e 1// Created on: 2001-07-02
2// Created by: Mathias BOSSHARD
973c2be1 3// Copyright (c) 2001-2014 OPEN CASCADE SAS
7fd59977 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
7fd59977 6//
d5f74e42 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
973c2be1 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.
7fd59977 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
f376ac72 16#include <AIS_TexturedShape.hxx>
7fd59977 17
2bd4c032 18#include <AIS_Drawer.hxx>
735dc1d2 19#include <AIS_GraphicTool.hxx>
2bd4c032 20#include <AIS_InteractiveContext.hxx>
7fd59977 21#include <BRepTools.hxx>
2bd4c032 22#include <gp_Pnt2d.hxx>
23#include <Graphic3d_AspectFillArea3d.hxx>
24#include <Graphic3d_Group.hxx>
7fd59977 25#include <Graphic3d_StructureManager.hxx>
26#include <Graphic3d_Texture2Dmanual.hxx>
2bd4c032 27#include <Precision.hxx>
28#include <Prs3d_Presentation.hxx>
735dc1d2 29#include <PrsMgr_ModedPresentation.hxx>
7fd59977 30#include <Prs3d_Root.hxx>
735dc1d2 31#include <Prs3d_LineAspect.hxx>
2bd4c032 32#include <Prs3d_ShadingAspect.hxx>
7fd59977 33#include <PrsMgr_PresentationManager3d.hxx>
f376ac72 34#include <Standard_ErrorHandler.hxx>
2bd4c032 35#include <StdPrs_ShadedShape.hxx>
8e3ebc7a 36#include <StdPrs_ToolShadedShape.hxx>
7fd59977 37#include <StdPrs_WFDeflectionShape.hxx>
7fd59977 38#include <StdPrs_WFShape.hxx>
2bd4c032 39#include <TopExp_Explorer.hxx>
7fd59977 40
f376ac72 41IMPLEMENT_STANDARD_HANDLE (AIS_TexturedShape, AIS_Shape)
42IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape, AIS_Shape)
43
7fd59977 44//=======================================================================
45//function : AIS_TexturedShape
2bd4c032 46//purpose :
7fd59977 47//=======================================================================
2bd4c032 48AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
f376ac72 49: AIS_Shape (theShape),
50 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
51 myToMapTexture (Standard_True),
52 myModulate (Standard_True),
53 myUVOrigin (0.0, 0.0),
54 myIsCustomOrigin (Standard_True),
55 myUVRepeat (1.0, 1.0),
56 myToRepeat (Standard_True),
57 myUVScale (1.0, 1.0),
58 myToScale (Standard_True),
59 myToShowTriangles (Standard_False)
7fd59977 60{
61}
62
7fd59977 63//=======================================================================
64//function : SetTextureFileName
2bd4c032 65//purpose :
7fd59977 66//=======================================================================
2bd4c032 67void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
7fd59977 68{
f376ac72 69 myTexturePixMap.Nullify();
70
2bd4c032 71 if (theTextureFileName.IsIntegerValue())
72 {
f376ac72 73 const Standard_Integer aValue = theTextureFileName.IntegerValue();
74 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
75 && aValue >= 0)
7fd59977 76 {
f376ac72 77 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
7fd59977 78 }
2bd4c032 79 else
7fd59977 80 {
63c629aa 81#ifdef AIS_DEB
f376ac72 82 std::cout << "Texture " << theTextureFileName << " doesn't exist\n";
2bd4c032 83 std::cout << "Using Texture 0 instead ...\n";
63c629aa 84#endif
2bd4c032 85 myPredefTexture = Graphic3d_NameOfTexture2D (0);
7fd59977 86 }
2bd4c032 87 myTextureFile = "";
88 }
89 else
90 {
91 myTextureFile = theTextureFileName;
f376ac72 92 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
2bd4c032 93 }
7fd59977 94}
95
f376ac72 96//=======================================================================
97//function : SetTexturePixMap
98//purpose :
99//=======================================================================
100void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
101{
102 myTextureFile = "";
103 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
104 myTexturePixMap = theTexturePixMap;
105}
106
7fd59977 107//=======================================================================
108//function : SetTextureRepeat
2bd4c032 109//purpose :
7fd59977 110//=======================================================================
111
f376ac72 112void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
2bd4c032 113 const Standard_Real theURepeat,
114 const Standard_Real theVRepeat)
7fd59977 115{
f376ac72 116 myToRepeat = theToRepeat;
117 myUVRepeat.SetCoord (theURepeat, theVRepeat);
7fd59977 118}
119
120//=======================================================================
121//function : SetTextureMapOn
2bd4c032 122//purpose :
7fd59977 123//=======================================================================
124
125void AIS_TexturedShape::SetTextureMapOn()
126{
f376ac72 127 myToMapTexture = Standard_True;
7fd59977 128}
129
130//=======================================================================
131//function : SetTextureMapOff
2bd4c032 132//purpose :
7fd59977 133//=======================================================================
134
135void AIS_TexturedShape::SetTextureMapOff()
136{
f376ac72 137 myToMapTexture = Standard_False;
7fd59977 138}
139
140//=======================================================================
141//function : SetTextureOrigin
2bd4c032 142//purpose :
7fd59977 143//=======================================================================
144
f376ac72 145void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
2bd4c032 146 const Standard_Real theUOrigin,
147 const Standard_Real theVOrigin)
7fd59977 148{
f376ac72 149 myIsCustomOrigin = theToSetTextureOrigin;
150 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
7fd59977 151}
152
153//=======================================================================
154//function : SetTextureScale
2bd4c032 155//purpose :
7fd59977 156//=======================================================================
157
f376ac72 158void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
2bd4c032 159 const Standard_Real theScaleU,
160 const Standard_Real theScaleV)
7fd59977 161{
f376ac72 162 myToScale = theToSetTextureScale;
163 myUVScale.SetCoord (theScaleU, theScaleV);
7fd59977 164}
165
166//=======================================================================
167//function : ShowTriangles
2bd4c032 168//purpose :
7fd59977 169//=======================================================================
170
f376ac72 171void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
7fd59977 172{
f376ac72 173 myToShowTriangles = theToShowTriangles;
7fd59977 174}
175
176//=======================================================================
177//function : EnableTextureModulate
2bd4c032 178//purpose :
7fd59977 179//=======================================================================
180
181void AIS_TexturedShape::EnableTextureModulate()
182{
183 myModulate = Standard_True;
184}
185
186//=======================================================================
187//function : DisableTextureModulate
bf75be98 188//purpose :
7fd59977 189//=======================================================================
190
191void AIS_TexturedShape::DisableTextureModulate()
192{
193 myModulate = Standard_False;
194}
195
7389f96d 196//=======================================================================
197//function : SetColor
198//purpose :
199//=======================================================================
200
201void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
202{
203 AIS_Shape::SetColor (theColor);
204
735dc1d2 205 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
7389f96d 206 {
735dc1d2 207 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
208
209 if (aPrsModed.Mode() != 3)
210 continue;
211
212 updateAttributes (aPrsModed.Presentation()->Presentation());
213 }
214}
215
216//=======================================================================
217//function : UnsetColor
218//purpose :
219//=======================================================================
220
221void AIS_TexturedShape::UnsetColor()
222{
223 AIS_Shape::UnsetColor();
224
225 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
226 {
227 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
228
229 if (aPrsModed.Mode() != 3)
230 continue;
231
232 Handle(Prs3d_Presentation) aPrs = aPrsModed.Presentation()->Presentation();
233 Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup (aPrs);
234
235 Handle(Graphic3d_AspectFillArea3d) anAreaAsp = myDrawer->Link()->ShadingAspect()->Aspect();
236 Handle(Graphic3d_AspectLine3d) aLineAsp = myDrawer->Link()->LineAspect()->Aspect();
237 Quantity_Color aColor;
238 AIS_GraphicTool::GetInteriorColor (myDrawer->Link(), aColor);
239 anAreaAsp->SetInteriorColor (aColor);
240 aPrs->SetPrimitivesAspect (anAreaAsp);
241 aPrs->SetPrimitivesAspect (aLineAsp);
242 // Check if aspect of given type is set for the group,
243 // because setting aspect for group with no already set aspect
244 // can lead to loss of presentation data
245 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
7389f96d 246 {
735dc1d2 247 aGroup->SetGroupPrimitivesAspect (anAreaAsp);
7389f96d 248 }
735dc1d2 249 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_LINE))
250 {
251 aGroup->SetGroupPrimitivesAspect (aLineAsp);
252 }
253
254 updateAttributes (aPrs);
255 }
256}
257
258//=======================================================================
259//function : SetMaterial
260//purpose :
261//=======================================================================
262
263void AIS_TexturedShape::SetMaterial (const Graphic3d_MaterialAspect& theMat)
264{
265 AIS_Shape::SetMaterial (theMat);
266
267 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
268 {
269 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
270
271 if (aPrsModed.Mode() != 3)
272 continue;
273
274 updateAttributes (aPrsModed.Presentation()->Presentation());
275 }
276}
277
278//=======================================================================
279//function : UnsetMaterial
280//purpose :
281//=======================================================================
282void AIS_TexturedShape::UnsetMaterial()
283{
284 AIS_Shape::UnsetMaterial();
285
286 for (Standard_Integer aPrsIt = 1; aPrsIt <= Presentations().Length(); ++aPrsIt)
287 {
288 const PrsMgr_ModedPresentation& aPrsModed = Presentations().Value (aPrsIt);
289
290 if (aPrsModed.Mode() != 3)
291 continue;
292
293 updateAttributes (aPrsModed.Presentation()->Presentation());
7389f96d 294 }
295}
296
7fd59977 297//=======================================================================
298//function : UpdateAttributes
2bd4c032 299//purpose :
7fd59977 300//=======================================================================
301
302void AIS_TexturedShape::UpdateAttributes()
8e3ebc7a 303{
304 updateAttributes (Presentation());
305}
306
307//=======================================================================
308//function : updateAttributes
309//purpose :
310//=======================================================================
311
312void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
7fd59977 313{
7389f96d 314 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
8e3ebc7a 315 if (HasPolygonOffsets())
316 {
317 // Issue 23115: copy polygon offset settings passed through myDrawer
318 Standard_Integer aMode;
319 Standard_ShortReal aFactor, aUnits;
320 PolygonOffsets (aMode, aFactor, aUnits);
321 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
322 }
323
f376ac72 324 if (!myToMapTexture)
2bd4c032 325 {
326 myAspect->SetTextureMapOff();
327 return;
328 }
329
f376ac72 330 if (!myTexturePixMap.IsNull())
331 {
332 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
333 }
334 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
335 {
336 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
337 }
7fd59977 338 else
f376ac72 339 {
340 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
341 }
2bd4c032 342
7fd59977 343 myAspect->SetTextureMapOn();
f376ac72 344 myAspect->SetTextureMap (myTexture);
345 if (!myTexture->IsDone())
2bd4c032 346 {
63c629aa 347#ifdef AIS_DEB
8e3ebc7a 348 std::cout << "An error occurred while building texture\n";
63c629aa 349#endif
8e3ebc7a 350 myAspect->SetTextureMapOff();
2bd4c032 351 return;
352 }
353
d399d3c3 354 if (myModulate)
355 myTexture->EnableModulate();
356 else
357 myTexture->DisableModulate();
358
f376ac72 359 if (myToShowTriangles)
7fd59977 360 myAspect->SetEdgeOn();
361 else
362 myAspect->SetEdgeOff();
2bd4c032 363
8e3ebc7a 364 // manage back face culling in consistent way (as in StdPrs_ShadedShape::Add())
365 if (StdPrs_ToolShadedShape::IsClosed (myshape))
366 {
367 myAspect->SuppressBackFace();
368 }
369 else
370 {
371 myAspect->AllowBackFace();
372 }
373
735dc1d2 374 // Go through all groups to change fill aspect for all primitives
375 for (Graphic3d_SequenceOfGroup::Iterator aGroupIt (thePrs->Groups()); aGroupIt.More(); aGroupIt.Next())
8e3ebc7a 376 {
735dc1d2 377 const Handle(Graphic3d_Group)& aGroup = aGroupIt.Value();
378
379 if (aGroup->IsGroupPrimitivesAspectSet (Graphic3d_ASPECT_FILL_AREA))
380 {
381 aGroup->SetGroupPrimitivesAspect (myAspect);
382 }
8e3ebc7a 383 }
7fd59977 384}
385
386//=======================================================================
387//function : Compute
2bd4c032 388//purpose :
7fd59977 389//=======================================================================
390
f376ac72 391void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
2bd4c032 392 const Handle(Prs3d_Presentation)& thePrs,
393 const Standard_Integer theMode)
7fd59977 394{
2bd4c032 395 thePrs->Clear();
396
397 if (myshape.IsNull())
398 {
399 return;
400 }
401
402 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
403 {
404 thePrs->SetVisual (Graphic3d_TOS_ALL);
405 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
406 }
7fd59977 407
408 if (myshape.ShapeType() == TopAbs_COMPOUND)
2bd4c032 409 {
410 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
411 if (!anExplor.More())
7fd59977 412 {
2bd4c032 413 return;
7fd59977 414 }
2bd4c032 415 }
7fd59977 416
417 if (IsInfinite())
2bd4c032 418 {
419 thePrs->SetInfiniteState (Standard_True);
420 }
7fd59977 421
2bd4c032 422 switch (theMode)
423 {
f376ac72 424 case AIS_WireFrame:
2bd4c032 425 {
426 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 427 break;
2bd4c032 428 }
f376ac72 429 case AIS_Shaded:
50b830a0 430 case 3: // texture mapping on triangulation
2bd4c032 431 {
432 Standard_Real prevangle;
433 Standard_Real newangle;
434 Standard_Real prevcoeff;
435 Standard_Real newcoeff;
7fd59977 436
50b830a0 437 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
bbf847ad
P
438 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
439 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
440 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
441 BRepTools::Clean (myshape);
2bd4c032 442 }
50b830a0 443
2bd4c032 444 if (myshape.ShapeType() > TopAbs_FACE)
7fd59977 445 {
2bd4c032 446 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
447 break;
7fd59977 448 }
50b830a0 449
2bd4c032 450 myDrawer->SetShadingAspectGlobal (Standard_False);
451 if (IsInfinite())
452 {
453 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
454 break;
455 }
456 try
457 {
458 OCC_CATCH_SIGNALS
50b830a0 459 if (theMode == AIS_Shaded)
460 {
461 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
462 }
463 else
464 {
465 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
466 Standard_True,
467 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
468 myUVRepeat,
469 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
470 updateAttributes (thePrs);
471 }
2bd4c032 472 }
473 catch (Standard_Failure)
474 {
63c629aa 475#ifdef AIS_DEB
2bd4c032 476 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
63c629aa 477#endif
2bd4c032 478 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
479 }
480 break;
481 }
7fd59977 482 case 2: // Bounding box
2bd4c032 483 {
484 if (IsInfinite())
7fd59977 485 {
2bd4c032 486 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 487 }
2bd4c032 488 else
7fd59977 489 {
d33222c1 490 StdPrs_WFDeflectionRestrictedFace::AddBox (thePrs, BoundingBox(), myDrawer);
2bd4c032 491 }
492 break;
493 }
2bd4c032 494 }
495}