0031909: Visualization, AIS_Trihedron - replace maps with arrays
[occt.git] / src / AIS / AIS_XRTrackedDevice.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <AIS_XRTrackedDevice.hxx>
15
16 #include <Graphic3d_ArrayOfSegments.hxx>
17 #include <Graphic3d_ArrayOfTriangles.hxx>
18 #include <Graphic3d_Group.hxx>
19 #include <Graphic3d_Texture2Dmanual.hxx>
20 #include <Image_Texture.hxx>
21 #include <Prs3d_LineAspect.hxx>
22 #include <Prs3d_ShadingAspect.hxx>
23 #include <Select3D_SensitivePrimitiveArray.hxx>
24 #include <SelectMgr_EntityOwner.hxx>
25
26 //! Texture holder.
27 class AIS_XRTrackedDevice::XRTexture : public Graphic3d_Texture2Dmanual
28 {
29 public:
30
31   //! Constructor.
32   XRTexture (const Handle(Image_Texture)& theImageSource,
33              const Graphic3d_TextureUnit theUnit = Graphic3d_TextureUnit_BaseColor)
34   : Graphic3d_Texture2Dmanual (""), myImageSource (theImageSource)
35   {
36     if (!theImageSource->TextureId().IsEmpty())
37     {
38       myTexId = theImageSource->TextureId();
39     }
40     myParams->SetTextureUnit (theUnit);
41     myIsColorMap = theUnit == Graphic3d_TextureUnit_BaseColor
42                 || theUnit == Graphic3d_TextureUnit_Emissive;
43   }
44
45   //! Image reader.
46   virtual Handle(Image_PixMap) GetImage (const Handle(Image_SupportedFormats)& theSupported) Standard_OVERRIDE
47   {
48     return myImageSource->ReadImage (theSupported);
49   }
50
51 protected:
52   Handle(Image_Texture) myImageSource;
53 };
54
55 IMPLEMENT_STANDARD_RTTIEXT(AIS_XRTrackedDevice, AIS_InteractiveObject)
56
57 //=======================================================================
58 //function : AIS_XRTrackedDevice
59 //purpose  :
60 //=======================================================================
61 AIS_XRTrackedDevice::AIS_XRTrackedDevice (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
62                                           const Handle(Image_Texture)& theTexture)
63 : myTris (theTris),
64   myLaserColor (Quantity_NOC_BLUE),
65   myLaserLength (0.0f),
66   myUnitFactor (1.0f),
67   myRole (Aspect_XRTrackedDeviceRole_Other),
68   myToShowAxes (false)
69 {
70   myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
71   myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NameOfMaterial_DEFAULT);
72   myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
73   if (!theTexture.IsNull())
74   {
75     myDrawer->ShadingAspect()->Aspect()->SetTextureMap (new XRTexture (theTexture));
76     myDrawer->ShadingAspect()->Aspect()->SetTextureMapOn (true);
77   }
78 }
79
80 //=======================================================================
81 //function : AIS_XRTrackedDevice
82 //purpose  :
83 //=======================================================================
84 AIS_XRTrackedDevice::AIS_XRTrackedDevice()
85 : myLaserColor (Quantity_NOC_BLUE),
86   myLaserLength (0.0f),
87   myUnitFactor (1.0f),
88   myRole (Aspect_XRTrackedDeviceRole_Other),
89   myToShowAxes (true)
90 {
91   //
92 }
93
94 //=======================================================================
95 //function : SetLaserColor
96 //purpose  :
97 //=======================================================================
98 void AIS_XRTrackedDevice::SetLaserColor (const Quantity_Color& theColor)
99 {
100   if (!myLaserColor.IsEqual (theColor))
101   {
102     myLaserColor = theColor;
103     computeLaserRay();
104   }
105 }
106
107 //=======================================================================
108 //function : SetLaserLength
109 //purpose  :
110 //=======================================================================
111 void AIS_XRTrackedDevice::SetLaserLength (Standard_ShortReal theLength)
112 {
113   if (myLaserLength != theLength)
114   {
115     myLaserLength = theLength;
116     computeLaserRay();
117   }
118 }
119
120 //=======================================================================
121 //function : computeLaserRay
122 //purpose  :
123 //=======================================================================
124 void AIS_XRTrackedDevice::computeLaserRay()
125 {
126   if (myRayGroup.IsNull())
127   {
128     return;
129   }
130
131   if (!myRayGroup->IsEmpty())
132   {
133     myRayGroup->Clear();
134   }
135   if (myLaserLength <= 0.0f)
136   {
137     return;
138   }
139
140   Handle(Graphic3d_ArrayOfPrimitives) aLines = new Graphic3d_ArrayOfSegments (2, 0, Graphic3d_ArrayFlags_VertexColor);
141   aLines->AddVertex (gp_Pnt (0.0, 0.0, 0.0), myLaserColor);
142   aLines->AddVertex (gp_Pnt (0.0, 0.0, -myLaserLength), myLaserColor);
143   myRayGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
144   myRayGroup->AddPrimitiveArray (aLines, false); // do not extend camera frustum by ray
145 }
146
147 //=======================================================================
148 //function : Compute
149 //purpose  :
150 //=======================================================================
151 void AIS_XRTrackedDevice::Compute (const Handle(PrsMgr_PresentationManager3d)& ,
152                                    const Handle(Prs3d_Presentation)& thePrs,
153                                    const Standard_Integer theMode)
154 {
155   if (theMode != 0)
156   {
157     return;
158   }
159
160   thePrs->SetInfiniteState (myInfiniteState);
161   Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
162   if (!myTris.IsNull())
163   {
164     aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
165     aGroup->AddPrimitiveArray (myTris);
166   }
167
168   if (myToShowAxes || myTris.IsNull())
169   {
170     const float aSize = 0.1f * myUnitFactor;
171     aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
172     Handle(Graphic3d_ArrayOfPrimitives) aLines = new Graphic3d_ArrayOfSegments (6, 0, Graphic3d_ArrayFlags_VertexColor);
173     aLines->AddVertex (gp_Pnt (0.0,   0.0, 0.0),   Quantity_Color (Quantity_NOC_RED));
174     aLines->AddVertex (gp_Pnt (aSize, 0.0, 0.0),   Quantity_Color (Quantity_NOC_RED));
175     aLines->AddVertex (gp_Pnt (0.0,   0.0, 0.0),   Quantity_Color (Quantity_NOC_GREEN));
176     aLines->AddVertex (gp_Pnt (0.0, aSize, 0.0),   Quantity_Color (Quantity_NOC_GREEN));
177     aLines->AddVertex (gp_Pnt (0.0,   0.0, 0.0),   Quantity_Color (Quantity_NOC_BLUE));
178     aLines->AddVertex (gp_Pnt (0.0,   0.0, aSize), Quantity_Color (Quantity_NOC_BLUE));
179     aGroup->AddPrimitiveArray (aLines);
180   }
181
182   myRayGroup = thePrs->NewGroup();
183   computeLaserRay();
184 }
185
186 //=======================================================================
187 //function : ComputeSelection
188 //purpose  :
189 //=======================================================================
190 void AIS_XRTrackedDevice::ComputeSelection (const Handle(SelectMgr_Selection)& theSel,
191                                             const Standard_Integer theMode)
192 {
193   if (theMode != 0)
194   {
195     return;
196   }
197
198   if (!myTris.IsNull())
199   {
200     Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
201     Handle(Select3D_SensitivePrimitiveArray) aSensitive = new Select3D_SensitivePrimitiveArray (anOwner);
202     aSensitive->InitTriangulation (myTris->Attributes(), myTris->Indices(), TopLoc_Location(), true);
203     theSel->Add (aSensitive);
204   }
205 }