\feff0025129: Visualization - add interactive object for Points Cloud objects
[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>
19#include <AIS_InteractiveContext.hxx>
7fd59977 20#include <BRepTools.hxx>
2bd4c032 21#include <gp_Pnt2d.hxx>
22#include <Graphic3d_AspectFillArea3d.hxx>
23#include <Graphic3d_Group.hxx>
7fd59977 24#include <Graphic3d_StructureManager.hxx>
25#include <Graphic3d_Texture2Dmanual.hxx>
2bd4c032 26#include <Precision.hxx>
27#include <Prs3d_Presentation.hxx>
7fd59977 28#include <Prs3d_Root.hxx>
2bd4c032 29#include <Prs3d_ShadingAspect.hxx>
7fd59977 30#include <PrsMgr_PresentationManager3d.hxx>
f376ac72 31#include <Standard_ErrorHandler.hxx>
2bd4c032 32#include <StdPrs_ShadedShape.hxx>
8e3ebc7a 33#include <StdPrs_ToolShadedShape.hxx>
7fd59977 34#include <StdPrs_WFDeflectionShape.hxx>
7fd59977 35#include <StdPrs_WFShape.hxx>
2bd4c032 36#include <TopExp_Explorer.hxx>
7fd59977 37
f376ac72 38IMPLEMENT_STANDARD_HANDLE (AIS_TexturedShape, AIS_Shape)
39IMPLEMENT_STANDARD_RTTIEXT(AIS_TexturedShape, AIS_Shape)
40
7fd59977 41//=======================================================================
42//function : AIS_TexturedShape
2bd4c032 43//purpose :
7fd59977 44//=======================================================================
2bd4c032 45AIS_TexturedShape::AIS_TexturedShape (const TopoDS_Shape& theShape)
f376ac72 46: AIS_Shape (theShape),
47 myPredefTexture (Graphic3d_NameOfTexture2D(0)),
48 myToMapTexture (Standard_True),
49 myModulate (Standard_True),
50 myUVOrigin (0.0, 0.0),
51 myIsCustomOrigin (Standard_True),
52 myUVRepeat (1.0, 1.0),
53 myToRepeat (Standard_True),
54 myUVScale (1.0, 1.0),
55 myToScale (Standard_True),
56 myToShowTriangles (Standard_False)
7fd59977 57{
58}
59
7fd59977 60//=======================================================================
61//function : SetTextureFileName
2bd4c032 62//purpose :
7fd59977 63//=======================================================================
2bd4c032 64void AIS_TexturedShape::SetTextureFileName (const TCollection_AsciiString& theTextureFileName)
7fd59977 65{
f376ac72 66 myTexturePixMap.Nullify();
67
2bd4c032 68 if (theTextureFileName.IsIntegerValue())
69 {
f376ac72 70 const Standard_Integer aValue = theTextureFileName.IntegerValue();
71 if (aValue < Graphic3d_Texture2D::NumberOfTextures()
72 && aValue >= 0)
7fd59977 73 {
f376ac72 74 myPredefTexture = Graphic3d_NameOfTexture2D (aValue);
7fd59977 75 }
2bd4c032 76 else
7fd59977 77 {
f376ac72 78 std::cout << "Texture " << theTextureFileName << " doesn't exist\n";
2bd4c032 79 std::cout << "Using Texture 0 instead ...\n";
80 myPredefTexture = Graphic3d_NameOfTexture2D (0);
7fd59977 81 }
2bd4c032 82 myTextureFile = "";
83 }
84 else
85 {
86 myTextureFile = theTextureFileName;
f376ac72 87 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
2bd4c032 88 }
7fd59977 89}
90
91//=======================================================================
f376ac72 92//function : SetTexturePixMap
93//purpose :
94//=======================================================================
95void AIS_TexturedShape::SetTexturePixMap (const Handle(Image_PixMap)& theTexturePixMap)
96{
97 myTextureFile = "";
98 myPredefTexture = Graphic3d_NOT_2D_UNKNOWN;
99 myTexturePixMap = theTexturePixMap;
100}
101
102//=======================================================================
7fd59977 103//function : SetTextureRepeat
2bd4c032 104//purpose :
7fd59977 105//=======================================================================
106
f376ac72 107void AIS_TexturedShape::SetTextureRepeat (const Standard_Boolean theToRepeat,
2bd4c032 108 const Standard_Real theURepeat,
109 const Standard_Real theVRepeat)
7fd59977 110{
f376ac72 111 myToRepeat = theToRepeat;
112 myUVRepeat.SetCoord (theURepeat, theVRepeat);
7fd59977 113}
114
115//=======================================================================
116//function : SetTextureMapOn
2bd4c032 117//purpose :
7fd59977 118//=======================================================================
119
120void AIS_TexturedShape::SetTextureMapOn()
121{
f376ac72 122 myToMapTexture = Standard_True;
7fd59977 123}
124
125//=======================================================================
126//function : SetTextureMapOff
2bd4c032 127//purpose :
7fd59977 128//=======================================================================
129
130void AIS_TexturedShape::SetTextureMapOff()
131{
f376ac72 132 myToMapTexture = Standard_False;
7fd59977 133}
134
135//=======================================================================
136//function : SetTextureOrigin
2bd4c032 137//purpose :
7fd59977 138//=======================================================================
139
f376ac72 140void AIS_TexturedShape::SetTextureOrigin (const Standard_Boolean theToSetTextureOrigin,
2bd4c032 141 const Standard_Real theUOrigin,
142 const Standard_Real theVOrigin)
7fd59977 143{
f376ac72 144 myIsCustomOrigin = theToSetTextureOrigin;
145 myUVOrigin.SetCoord (theUOrigin, theVOrigin);
7fd59977 146}
147
148//=======================================================================
149//function : SetTextureScale
2bd4c032 150//purpose :
7fd59977 151//=======================================================================
152
f376ac72 153void AIS_TexturedShape::SetTextureScale (const Standard_Boolean theToSetTextureScale,
2bd4c032 154 const Standard_Real theScaleU,
155 const Standard_Real theScaleV)
7fd59977 156{
f376ac72 157 myToScale = theToSetTextureScale;
158 myUVScale.SetCoord (theScaleU, theScaleV);
7fd59977 159}
160
161//=======================================================================
162//function : ShowTriangles
2bd4c032 163//purpose :
7fd59977 164//=======================================================================
165
f376ac72 166void AIS_TexturedShape::ShowTriangles (const Standard_Boolean theToShowTriangles)
7fd59977 167{
f376ac72 168 myToShowTriangles = theToShowTriangles;
7fd59977 169}
170
171//=======================================================================
172//function : EnableTextureModulate
2bd4c032 173//purpose :
7fd59977 174//=======================================================================
175
176void AIS_TexturedShape::EnableTextureModulate()
177{
178 myModulate = Standard_True;
179}
180
181//=======================================================================
182//function : DisableTextureModulate
bf75be98 183//purpose :
7fd59977 184//=======================================================================
185
186void AIS_TexturedShape::DisableTextureModulate()
187{
188 myModulate = Standard_False;
189}
190
191//=======================================================================
7389f96d 192//function : SetColor
193//purpose :
194//=======================================================================
195
196void AIS_TexturedShape::SetColor (const Quantity_Color& theColor)
197{
198 AIS_Shape::SetColor (theColor);
199
200 if (!GetContext().IsNull())
201 {
202 if (GetContext()->MainPrsMgr()->HasPresentation (this, 3))
203 {
204 updateAttributes (GetContext()->MainPrsMgr()->Presentation (this, 3)->Presentation());
205 }
206 }
207}
208
209//=======================================================================
7fd59977 210//function : UpdateAttributes
2bd4c032 211//purpose :
7fd59977 212//=======================================================================
213
214void AIS_TexturedShape::UpdateAttributes()
215{
8e3ebc7a 216 updateAttributes (Presentation());
217}
218
219//=======================================================================
220//function : updateAttributes
221//purpose :
222//=======================================================================
223
224void AIS_TexturedShape::updateAttributes (const Handle(Prs3d_Presentation)& thePrs)
225{
7389f96d 226 myAspect = new Graphic3d_AspectFillArea3d (*myDrawer->ShadingAspect()->Aspect());
8e3ebc7a 227 if (HasPolygonOffsets())
228 {
229 // Issue 23115: copy polygon offset settings passed through myDrawer
230 Standard_Integer aMode;
231 Standard_ShortReal aFactor, aUnits;
232 PolygonOffsets (aMode, aFactor, aUnits);
233 myAspect->SetPolygonOffsets (aMode, aFactor, aUnits);
234 }
235
f376ac72 236 if (!myToMapTexture)
2bd4c032 237 {
238 myAspect->SetTextureMapOff();
239 return;
240 }
241
f376ac72 242 if (!myTexturePixMap.IsNull())
243 {
244 myTexture = new Graphic3d_Texture2Dmanual (myTexturePixMap);
245 }
246 else if (myPredefTexture != Graphic3d_NOT_2D_UNKNOWN)
247 {
248 myTexture = new Graphic3d_Texture2Dmanual (myPredefTexture);
249 }
7fd59977 250 else
f376ac72 251 {
252 myTexture = new Graphic3d_Texture2Dmanual (myTextureFile.ToCString());
253 }
2bd4c032 254
7fd59977 255 myAspect->SetTextureMapOn();
f376ac72 256 myAspect->SetTextureMap (myTexture);
257 if (!myTexture->IsDone())
2bd4c032 258 {
8e3ebc7a 259 std::cout << "An error occurred while building texture\n";
260 myAspect->SetTextureMapOff();
2bd4c032 261 return;
262 }
263
d399d3c3 264 if (myModulate)
265 myTexture->EnableModulate();
266 else
267 myTexture->DisableModulate();
268
f376ac72 269 if (myToShowTriangles)
7fd59977 270 myAspect->SetEdgeOn();
271 else
272 myAspect->SetEdgeOff();
2bd4c032 273
8e3ebc7a 274 // manage back face culling in consistent way (as in StdPrs_ShadedShape::Add())
275 if (StdPrs_ToolShadedShape::IsClosed (myshape))
276 {
277 myAspect->SuppressBackFace();
278 }
279 else
280 {
281 myAspect->AllowBackFace();
282 }
283
284 if (!thePrs.IsNull())
285 {
286 Prs3d_Root::CurrentGroup (thePrs)->SetGroupPrimitivesAspect (myAspect);
287 }
7fd59977 288}
289
290//=======================================================================
291//function : Compute
2bd4c032 292//purpose :
7fd59977 293//=======================================================================
294
f376ac72 295void AIS_TexturedShape::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
2bd4c032 296 const Handle(Prs3d_Presentation)& thePrs,
297 const Standard_Integer theMode)
7fd59977 298{
2bd4c032 299 thePrs->Clear();
300
301 if (myshape.IsNull())
302 {
303 return;
304 }
305
306 if (myshape.ShapeType() > TopAbs_FACE && myshape.ShapeType() < TopAbs_SHAPE)
307 {
308 thePrs->SetVisual (Graphic3d_TOS_ALL);
309 thePrs->SetDisplayPriority (myshape.ShapeType() + 2);
310 }
7fd59977 311
312 if (myshape.ShapeType() == TopAbs_COMPOUND)
2bd4c032 313 {
314 TopExp_Explorer anExplor (myshape, TopAbs_VERTEX);
315 if (!anExplor.More())
7fd59977 316 {
2bd4c032 317 return;
7fd59977 318 }
2bd4c032 319 }
7fd59977 320
321 if (IsInfinite())
2bd4c032 322 {
323 thePrs->SetInfiniteState (Standard_True);
324 }
7fd59977 325
2bd4c032 326 switch (theMode)
327 {
f376ac72 328 case AIS_WireFrame:
2bd4c032 329 {
330 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 331 break;
2bd4c032 332 }
f376ac72 333 case AIS_Shaded:
2bd4c032 334 {
335 Standard_Real prevangle;
336 Standard_Real newangle;
337 Standard_Real prevcoeff;
338 Standard_Real newcoeff;
7fd59977 339
bbf847ad
P
340 Standard_Boolean isOwnDeviationAngle = OwnDeviationAngle(newangle,prevangle);
341 Standard_Boolean isOwnDeviationCoefficient = OwnDeviationCoefficient(newcoeff,prevcoeff);
342 if (((Abs (newangle - prevangle) > Precision::Angular()) && isOwnDeviationAngle) ||
343 ((Abs (newcoeff - prevcoeff) > Precision::Confusion()) && isOwnDeviationCoefficient)) {
344 BRepTools::Clean (myshape);
2bd4c032 345 }
346 if (myshape.ShapeType() > TopAbs_FACE)
7fd59977 347 {
2bd4c032 348 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
349 break;
7fd59977 350 }
2bd4c032 351 myDrawer->SetShadingAspectGlobal (Standard_False);
352 if (IsInfinite())
353 {
354 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
355 break;
356 }
357 try
358 {
359 OCC_CATCH_SIGNALS
360 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer);
361 }
362 catch (Standard_Failure)
363 {
364 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed \n";
365 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
366 }
367 break;
368 }
7fd59977 369 case 2: // Bounding box
2bd4c032 370 {
371 if (IsInfinite())
7fd59977 372 {
2bd4c032 373 StdPrs_WFDeflectionShape::Add (thePrs, myshape, myDrawer);
7fd59977 374 }
2bd4c032 375 else
7fd59977 376 {
d33222c1 377 StdPrs_WFDeflectionRestrictedFace::AddBox (thePrs, BoundingBox(), myDrawer);
2bd4c032 378 }
379 break;
380 }
2bd4c032 381 case 3: // texture mapping on triangulation
382 {
f376ac72 383 BRepTools::Clean (myshape);
2bd4c032 384 BRepTools::Update (myshape);
2bd4c032 385 try
386 {
387 OCC_CATCH_SIGNALS
388 StdPrs_ShadedShape::Add (thePrs, myshape, myDrawer,
f376ac72 389 Standard_True,
390 myIsCustomOrigin ? myUVOrigin : gp_Pnt2d (0.0, 0.0),
391 myUVRepeat,
392 myToScale ? myUVScale : gp_Pnt2d (1.0, 1.0));
8e3ebc7a 393
394 updateAttributes (thePrs);
2bd4c032 395 }
396 catch (Standard_Failure)
397 {
8e3ebc7a 398 std::cout << "AIS_TexturedShape::Compute() in ShadingMode failed\n";
2bd4c032 399 StdPrs_WFShape::Add (thePrs, myshape, myDrawer);
400 }
401 break;
402 }
403 }
404}