0024070: OpenGL capped object-level clipping planes
[occt.git] / src / V3d / V3d_Plane.cxx
1 // Created by: GG
2 // Copyright (c) 1991-1999 Matra Datavision
3 // Copyright (c) 1999-2013 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 #include <V3d_Plane.hxx>
21 #include <Graphic3d_Group.hxx>
22 #include <Graphic3d_AspectFillArea3d.hxx>
23 #include <Graphic3d_ArrayOfQuadrangles.hxx>
24 #include <gp_Pln.hxx>
25
26 IMPLEMENT_STANDARD_HANDLE(V3d_Plane, MMgt_TShared)
27 IMPLEMENT_STANDARD_RTTIEXT(V3d_Plane, MMgt_TShared)
28
29 // =======================================================================
30 // function : V3d_Plane
31 // purpose  :
32 // =======================================================================
33 V3d_Plane::V3d_Plane (const Standard_Real theA,
34                       const Standard_Real theB,
35                       const Standard_Real theC,
36                       const Standard_Real theD)
37 : myGraphicStructure(),
38   myPlane (new Graphic3d_ClipPlane (gp_Pln (theA, theB, theC, theD)))
39 {
40 }
41
42 // =======================================================================
43 // function : V3d_Plane
44 // purpose  :
45 // =======================================================================
46 void V3d_Plane::SetPlane (const Standard_Real theA,
47                           const Standard_Real theB,
48                           const Standard_Real theC,
49                           const Standard_Real theD)
50 {
51   myPlane->SetEquation (gp_Pln (theA, theB, theC, theD));
52   if (IsDisplayed())
53   {
54     Update();
55   }
56 }
57
58 // =======================================================================
59 // function : Display
60 // purpose  :
61 // =======================================================================
62 void V3d_Plane::Display (const Handle(V3d_View)& theView,
63                          const Quantity_Color& theColor)
64 {
65   Handle(V3d_Viewer) aViewer = theView->Viewer();
66   if (!myGraphicStructure.IsNull())
67   {
68     myGraphicStructure->Clear();
69   }
70
71   myGraphicStructure = new Graphic3d_Structure (aViewer->Viewer());
72   Handle(Graphic3d_Group) aGroup = new Graphic3d_Group (myGraphicStructure);
73   Handle(Graphic3d_AspectFillArea3d) anAsp = new Graphic3d_AspectFillArea3d();
74   Graphic3d_MaterialAspect aPlastic (Graphic3d_NOM_PLASTIC);
75   aPlastic.SetColor (theColor);
76   aPlastic.SetTransparency (0.5);
77   theView->SetTransparency (Standard_True);
78   anAsp->SetFrontMaterial (aPlastic);
79   anAsp->SetInteriorStyle (Aspect_IS_HATCH);
80   anAsp->SetHatchStyle (Aspect_HS_GRID_DIAGONAL_WIDE);
81   myGraphicStructure->SetPrimitivesAspect (anAsp);
82
83   const Standard_ShortReal aSize = (Standard_ShortReal)(0.5*aViewer->DefaultViewSize());
84   const Standard_ShortReal anOffset = aSize/5000.0f;
85
86   Handle(Graphic3d_ArrayOfQuadrangles) aPrims = new Graphic3d_ArrayOfQuadrangles(4);
87   aPrims->AddVertex (-aSize,-aSize, anOffset);
88   aPrims->AddVertex (-aSize, aSize, anOffset);
89   aPrims->AddVertex ( aSize, aSize, anOffset);
90   aPrims->AddVertex ( aSize,-aSize, anOffset);
91   aGroup->AddPrimitiveArray(aPrims);
92
93   myGraphicStructure->Display(0);
94   Update();
95 }
96
97 // =======================================================================
98 // function : Erase
99 // purpose  :
100 // =======================================================================
101 void V3d_Plane::Erase()
102 {
103   if (!myGraphicStructure.IsNull())
104   {
105     myGraphicStructure->Erase();
106   }
107 }
108
109 // =======================================================================
110 // function : Plane
111 // purpose  :
112 // =======================================================================
113 void V3d_Plane::Plane (Standard_Real& theA, Standard_Real& theB, Standard_Real& theC, Standard_Real& theD) const
114 {
115   const Graphic3d_ClipPlane::Equation& anEquation = myPlane->GetEquation();
116   theA = anEquation[0];
117   theB = anEquation[1];
118   theC = anEquation[2];
119   theD = anEquation[3];
120 }
121
122 // =======================================================================
123 // function : IsDisplayed
124 // purpose  :
125 // =======================================================================
126 Standard_Boolean V3d_Plane::IsDisplayed() const
127 {
128   if (myGraphicStructure.IsNull())
129   {
130     return Standard_False;
131   }
132
133   return myGraphicStructure->IsDisplayed();
134 }
135
136 // =======================================================================
137 // function : Update
138 // purpose  :
139 // =======================================================================
140 void V3d_Plane::Update()
141 {
142   if(!myGraphicStructure.IsNull())
143   {
144     TColStd_Array2OfReal aMatrix (1, 4, 1, 4);
145     Standard_Real theA, theB, theC, theD;
146     this->Plane(theA, theB, theC, theD);
147     gp_Pln aGeomPln (theA, theB, theC, theD);
148     gp_Trsf aTransform;
149     aTransform.SetTransformation (aGeomPln.Position());
150     aTransform.Invert();
151     for (Standard_Integer i = 1; i <= 3; i++)
152     {
153       for (Standard_Integer j = 1; j <= 4; j++)
154       {
155         aMatrix.SetValue (i, j, aTransform.Value (i,j));
156       }
157     }
158
159     aMatrix.SetValue (4,1,0.);
160     aMatrix.SetValue (4,2,0.);
161     aMatrix.SetValue (4,3,0.);
162     aMatrix.SetValue (4,4,1.);
163     myGraphicStructure->SetTransform (aMatrix, Graphic3d_TOC_REPLACE);
164   }
165 }