0031570: Samples - add Qt samples similar to standard MFC samples
[occt.git] / samples / OCCTOverview / code / Sample2D_Image.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of the examples of the Open CASCADE Technology software library.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
21
22 #include "Sample2D_Image.h"
23
24 #include <AIS_InteractiveContext.hxx>
25 #include <BRepBuilderAPI_MakeEdge.hxx>
26 #include <BRepBuilderAPI_MakeWire.hxx>
27 #include <BRepBuilderAPI_MakeFace.hxx>
28 #include <gp_Pnt.hxx>
29 #include <Graphic3d_Texture1D.hxx>
30 #include <Graphic3d_Texture1Dsegment.hxx>
31 #include <Graphic3d_Texture2Dmanual.hxx>
32 #include <Image_AlienPixMap.hxx>
33 #include <Prs3d_ShadingAspect.hxx>
34 #include <TopoDS_Edge.hxx>
35 #include <TopoDS_Wire.hxx>
36
37 Sample2D_Image::Sample2D_Image (const TCollection_AsciiString& theFileName,
38                                 const Standard_Real theX,
39                                 const Standard_Real theY,
40                                 const Standard_Real theScale)
41 : AIS_Shape (TopoDS_Shape()),
42   myFilename (theFileName),
43   myX (theX),
44   myY (theY),
45   myScale (theScale)
46 {
47   //
48 }
49
50 void Sample2D_Image::MakeShape()
51 {
52   Standard_Real coeff = 1.0;
53   Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
54   if (anImage->Load (myFilename))
55   {
56     coeff = Standard_Real(anImage->Height()) / Standard_Real(anImage->Width()) * myScale;
57   }
58
59   TopoDS_Edge E1 = BRepBuilderAPI_MakeEdge (gp_Pnt(myX, myY, 0.),
60                                             gp_Pnt(100 * myScale + myX, myY, 0.));
61   TopoDS_Edge E2 = BRepBuilderAPI_MakeEdge (gp_Pnt(100 * myScale + myX, myY, 0.),
62                                             gp_Pnt(100 * myScale + myX, 100 * coeff + myY, 0.));
63   TopoDS_Edge E3 = BRepBuilderAPI_MakeEdge (gp_Pnt(100 * myScale + myX, 100 * coeff + myY, 0.),
64                                             gp_Pnt(myX, 100 * coeff + myY, 0.));
65   TopoDS_Edge E4 = BRepBuilderAPI_MakeEdge (gp_Pnt(myX, 100 * coeff + myY, 0.),
66                                             gp_Pnt(myX, myY, 0.));
67   TopoDS_Wire anImageBounds = BRepBuilderAPI_MakeWire(E1, E2, E3, E4);
68   myFace = BRepBuilderAPI_MakeFace(gp_Pln(gp_Pnt(0, 0, 0), gp_Dir(0, 0, 1)), anImageBounds);
69 }
70
71 void Sample2D_Image::SetContext (const Handle(AIS_InteractiveContext)& theContext)
72 {
73   if (theContext.IsNull() || theContext->CurrentViewer().IsNull())
74   {
75     AIS_InteractiveObject::SetContext (theContext);
76     return;
77   }
78
79   AIS_InteractiveObject::SetContext (theContext);
80   MakeShape();
81   this->Set(TopoDS_Shape(myFace));
82
83   myDrawer->SetShadingAspect(new Prs3d_ShadingAspect());
84   Handle(Graphic3d_Texture2Dmanual) aTexture = new Graphic3d_Texture2Dmanual(myFilename);
85   aTexture->DisableModulate();
86   myDrawer->ShadingAspect()->Aspect()->SetTextureMap (aTexture);
87   myDrawer->ShadingAspect()->Aspect()->SetTextureMapOn();
88 }