1 // Created on: 2018-12-12
2 // Created by: Olga SURYANINOVA
3 // Copyright (c) 2018 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <AIS_CameraFrustum.hxx>
18 #include <AIS_DisplayMode.hxx>
19 #include <Graphic3d_ArrayOfTriangles.hxx>
20 #include <Graphic3d_ArrayOfSegments.hxx>
21 #include <Prs3d_LineAspect.hxx>
22 #include <Prs3d_ShadingAspect.hxx>
23 #include <Select3D_SensitiveGroup.hxx>
24 #include <Select3D_SensitivePrimitiveArray.hxx>
25 #include <Select3D_SensitiveSegment.hxx>
26 #include <SelectMgr_EntityOwner.hxx>
28 IMPLEMENT_STANDARD_RTTIEXT(AIS_CameraFrustum, AIS_InteractiveObject)
32 static const Standard_ShortReal THE_DEFAULT_TRANSPARENCY = 0.7f;
33 static const Quantity_Color THE_DEFAULT_COLOR = Quantity_NOC_WHITE;
36 //=======================================================================
37 //function : Constructor
39 //=======================================================================
40 AIS_CameraFrustum::AIS_CameraFrustum()
41 : myPoints (0, Graphic3d_Camera::FrustumVerticesNB)
43 myDrawer->SetLineAspect (new Prs3d_LineAspect (THE_DEFAULT_COLOR, Aspect_TOL_SOLID, 1.0));
45 Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
46 aShadingAspect->SetMaterial (Graphic3d_NameOfMaterial_Plastified);
47 aShadingAspect->Aspect()->SetAlphaMode (Graphic3d_AlphaMode_Blend);
48 aShadingAspect->SetTransparency (THE_DEFAULT_TRANSPARENCY);
49 aShadingAspect->SetColor (THE_DEFAULT_COLOR);
50 myDrawer->SetShadingAspect (aShadingAspect);
52 myDrawer->SetTransparency (THE_DEFAULT_TRANSPARENCY);
53 SetDisplayMode (AIS_Shaded);
56 //=======================================================================
57 //function : AcceptDisplayMode
59 //=======================================================================
60 Standard_Boolean AIS_CameraFrustum::AcceptDisplayMode (const Standard_Integer theMode) const
62 return theMode == AIS_Shaded || theMode == AIS_WireFrame;
65 //=======================================================================
66 //function : SetCameraFrustum
68 //=======================================================================
69 void AIS_CameraFrustum::SetCameraFrustum (const Handle(Graphic3d_Camera)& theCamera)
71 if (theCamera.IsNull())
76 theCamera->FrustumPoints (myPoints);
84 //=======================================================================
87 //=======================================================================
88 void AIS_CameraFrustum::SetColor (const Quantity_Color& theColor)
90 AIS_InteractiveObject::SetColor (theColor);
91 myDrawer->ShadingAspect()->SetColor (theColor);
92 myDrawer->LineAspect()->SetColor (theColor);
96 //=======================================================================
97 //function : UnsetColor
99 //=======================================================================
100 void AIS_CameraFrustum::UnsetColor()
107 AIS_InteractiveObject::UnsetColor();
109 myDrawer->ShadingAspect()->SetColor (THE_DEFAULT_COLOR);
110 myDrawer->LineAspect()->SetColor (THE_DEFAULT_COLOR);
111 SynchronizeAspects();
114 //=======================================================================
115 //function : UnsetColor
117 //=======================================================================
118 void AIS_CameraFrustum::UnsetTransparency()
120 myDrawer->ShadingAspect()->SetTransparency (0.0f);
121 myDrawer->SetTransparency (0.0f);
122 SynchronizeAspects();
125 //=======================================================================
126 //function : fillTriangles
128 //=======================================================================
129 void AIS_CameraFrustum::fillTriangles()
131 if (myTriangles.IsNull())
133 const Standard_Integer aPlaneTriangleVertsNb = 2 * 3;
134 const Standard_Integer aPlanesNb = 3 * 2;
136 myTriangles = new Graphic3d_ArrayOfTriangles (Graphic3d_Camera::FrustumVerticesNB, aPlaneTriangleVertsNb * aPlanesNb);
137 myTriangles->SetVertice (Graphic3d_Camera::FrustumVerticesNB, gp_Pnt (0.0, 0.0, 0.0));
139 // Triangles go in order (clockwise vertices traversing for correct normal):
140 // (0, 2, 1), (3, 1, 2)
141 const Standard_Integer aLookup1_clockwise[] = { 0, 1, 0, 1, 0, 1 };
142 const Standard_Integer aLookup2_clockwise[] = { 0, 0, 1, 1, 1, 0 };
143 // Triangles go in order (counterclockwise vertices traversing for correct normal):
144 // (1, 2, 0), (2, 1, 3)
145 const Standard_Integer aLookup1_anticlockwise[] = { 0, 1, 0, 1, 0, 1 };
146 const Standard_Integer aLookup2_anticlockwise[] = { 1, 0, 0, 0, 1, 1 };
147 Standard_Integer aShifts[] = { 0, 0, 0 };
149 // Planes go in order:
150 // LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR
151 for (Standard_Integer aFaceIdx = 0; aFaceIdx < 3; ++aFaceIdx)
153 for (Standard_Integer i = 0; i < 2; ++i)
155 for (Standard_Integer aPntIter = 0; aPntIter < aPlaneTriangleVertsNb; ++aPntIter)
157 aShifts[aFaceIdx] = i;
160 aShifts[(aFaceIdx + 1) % 3] = aLookup1_clockwise[aPntIter];
161 aShifts[(aFaceIdx + 2) % 3] = aLookup2_clockwise[aPntIter];
165 aShifts[(aFaceIdx + 1) % 3] = aLookup1_anticlockwise[aPntIter];
166 aShifts[(aFaceIdx + 2) % 3] = aLookup2_anticlockwise[aPntIter];
169 Standard_Integer anIndex = aShifts[0] * 2 * 2 + aShifts[1] * 2 + aShifts[2];
170 myTriangles->AddEdge (anIndex + 1);
176 for (Standard_Integer aPointIter = 0; aPointIter < Graphic3d_Camera::FrustumVerticesNB; ++aPointIter)
178 const Graphic3d_Vec3d aPnt = myPoints[aPointIter];
179 myTriangles->SetVertice (aPointIter + 1, gp_Pnt (aPnt.x(), aPnt.y(), aPnt.z()));
183 //=======================================================================
184 //function : fillBorders
186 //=======================================================================
187 void AIS_CameraFrustum::fillBorders()
189 if (myBorders.IsNull())
191 const Standard_Integer aPlaneSegmVertsNb = 2 * 4;
192 const Standard_Integer aPlanesNb = 3 * 2;
193 myBorders = new Graphic3d_ArrayOfSegments (Graphic3d_Camera::FrustumVerticesNB, aPlaneSegmVertsNb * aPlanesNb);
194 myBorders->SetVertice (Graphic3d_Camera::FrustumVerticesNB, gp_Pnt (0.0, 0.0, 0.0));
196 // Segments go in order:
197 // (0, 2), (2, 3), (3, 1), (1, 0)
198 const Standard_Integer aLookup1[] = { 0, 1, 1, 1, 1, 0, 0, 0 };
199 const Standard_Integer aLookup2[] = { 0, 0, 0, 1, 1, 1, 1, 0 };
200 Standard_Integer aShifts[] = { 0, 0, 0 };
202 // Planes go in order:
203 // LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR
204 for (Standard_Integer aFaceIdx = 0; aFaceIdx < 3; ++aFaceIdx)
206 for (Standard_Integer i = 0; i < 2; ++i)
208 for (Standard_Integer aSegmVertIter = 0; aSegmVertIter < aPlaneSegmVertsNb; ++aSegmVertIter)
210 aShifts[aFaceIdx] = i;
211 aShifts[(aFaceIdx + 1) % 3] = aLookup1[aSegmVertIter];
212 aShifts[(aFaceIdx + 2) % 3] = aLookup2[aSegmVertIter];
214 Standard_Integer anIndex = aShifts[0] * 2 * 2 + aShifts[1] * 2 + aShifts[2];
215 myBorders->AddEdge (anIndex + 1);
221 for (Standard_Integer aPointIter = 0; aPointIter < Graphic3d_Camera::FrustumVerticesNB; ++aPointIter)
223 const Graphic3d_Vec3d aPnt = myPoints[aPointIter];
224 myBorders->SetVertice (aPointIter + 1, gp_Pnt (aPnt.x(), aPnt.y(), aPnt.z()));
228 //=======================================================================
231 //=======================================================================
232 void AIS_CameraFrustum::Compute (const Handle(PrsMgr_PresentationManager3d)& ,
233 const Handle(Prs3d_Presentation)& thePrs,
234 const Standard_Integer theMode)
236 thePrs->SetInfiniteState (true);
237 if (myTriangles.IsNull())
246 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
247 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
248 aGroup->AddPrimitiveArray (myTriangles);
253 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
254 aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
255 aGroup->AddPrimitiveArray (myBorders);
261 //=======================================================================
262 //function : ComputeSelection
264 //=======================================================================
265 void AIS_CameraFrustum::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
266 const Standard_Integer theMode)
268 Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
271 case SelectionMode_Edges:
273 Handle(Select3D_SensitiveGroup) aSensitiveEntity = new Select3D_SensitiveGroup (anOwner);
274 for (Standard_Integer anIter = 1; anIter <= myBorders->EdgeNumber(); anIter += 2)
276 aSensitiveEntity->Add (new Select3D_SensitiveSegment (anOwner, myBorders->Vertice (myBorders->Edge (anIter)), myBorders->Vertice(myBorders->Edge (anIter + 1))));
278 theSelection->Add (aSensitiveEntity);
281 case SelectionMode_Volume:
283 Handle(Select3D_SensitivePrimitiveArray) aSelArray = new Select3D_SensitivePrimitiveArray (anOwner);
284 aSelArray->InitTriangulation (myTriangles->Attributes(), myTriangles->Indices(), TopLoc_Location());
285 theSelection->Add (aSelArray);