0024947: Redesign OCCT legacy type system -- automatic
[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 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <V3d_Plane.hxx>
17 #include <Graphic3d_Group.hxx>
18 #include <Graphic3d_AspectFillArea3d.hxx>
19 #include <Graphic3d_ArrayOfQuadrangles.hxx>
20 #include <gp_Pln.hxx>
21
22
23 // =======================================================================
24 // function : V3d_Plane
25 // purpose  :
26 // =======================================================================
27 V3d_Plane::V3d_Plane (const Standard_Real theA,
28                       const Standard_Real theB,
29                       const Standard_Real theC,
30                       const Standard_Real theD)
31 : myGraphicStructure(),
32   myPlane (new Graphic3d_ClipPlane (gp_Pln (theA, theB, theC, theD)))
33 {
34 }
35
36 // =======================================================================
37 // function : V3d_Plane
38 // purpose  :
39 // =======================================================================
40 void V3d_Plane::SetPlane (const Standard_Real theA,
41                           const Standard_Real theB,
42                           const Standard_Real theC,
43                           const Standard_Real theD)
44 {
45   myPlane->SetEquation (gp_Pln (theA, theB, theC, theD));
46   if (IsDisplayed())
47   {
48     Update();
49   }
50 }
51
52 // =======================================================================
53 // function : Display
54 // purpose  :
55 // =======================================================================
56 void V3d_Plane::Display (const Handle(V3d_View)& theView,
57                          const Quantity_Color& theColor)
58 {
59   Handle(V3d_Viewer) aViewer = theView->Viewer();
60   if (!myGraphicStructure.IsNull())
61   {
62     myGraphicStructure->Clear();
63   }
64
65   myGraphicStructure = new Graphic3d_Structure (aViewer->Viewer());
66   Handle(Graphic3d_Group)            aGroup = myGraphicStructure->NewGroup();
67   Handle(Graphic3d_AspectFillArea3d) anAsp  = new Graphic3d_AspectFillArea3d();
68   Graphic3d_MaterialAspect aPlastic (Graphic3d_NOM_PLASTIC);
69   aPlastic.SetColor (theColor);
70   aPlastic.SetTransparency (0.5);
71   anAsp->SetFrontMaterial (aPlastic);
72   anAsp->SetInteriorStyle (Aspect_IS_HATCH);
73   anAsp->SetHatchStyle (Aspect_HS_GRID_DIAGONAL_WIDE);
74   myGraphicStructure->SetPrimitivesAspect (anAsp);
75
76   const Standard_ShortReal aSize = (Standard_ShortReal)(0.5*aViewer->DefaultViewSize());
77   const Standard_ShortReal anOffset = aSize/5000.0f;
78
79   Handle(Graphic3d_ArrayOfQuadrangles) aPrims = new Graphic3d_ArrayOfQuadrangles(4);
80   aPrims->AddVertex (-aSize,-aSize, anOffset);
81   aPrims->AddVertex (-aSize, aSize, anOffset);
82   aPrims->AddVertex ( aSize, aSize, anOffset);
83   aPrims->AddVertex ( aSize,-aSize, anOffset);
84   aGroup->AddPrimitiveArray(aPrims);
85
86   myGraphicStructure->SetDisplayPriority (0);
87   myGraphicStructure->Display();
88   Update();
89 }
90
91 // =======================================================================
92 // function : Erase
93 // purpose  :
94 // =======================================================================
95 void V3d_Plane::Erase()
96 {
97   if (!myGraphicStructure.IsNull())
98   {
99     myGraphicStructure->Erase();
100   }
101 }
102
103 // =======================================================================
104 // function : Plane
105 // purpose  :
106 // =======================================================================
107 void V3d_Plane::Plane (Standard_Real& theA, Standard_Real& theB, Standard_Real& theC, Standard_Real& theD) const
108 {
109   const Graphic3d_ClipPlane::Equation& anEquation = myPlane->GetEquation();
110   theA = anEquation[0];
111   theB = anEquation[1];
112   theC = anEquation[2];
113   theD = anEquation[3];
114 }
115
116 // =======================================================================
117 // function : IsDisplayed
118 // purpose  :
119 // =======================================================================
120 Standard_Boolean V3d_Plane::IsDisplayed() const
121 {
122   if (myGraphicStructure.IsNull())
123   {
124     return Standard_False;
125   }
126
127   return myGraphicStructure->IsDisplayed();
128 }
129
130 // =======================================================================
131 // function : Update
132 // purpose  :
133 // =======================================================================
134 void V3d_Plane::Update()
135 {
136   if(!myGraphicStructure.IsNull())
137   {
138     TColStd_Array2OfReal aMatrix (1, 4, 1, 4);
139     Standard_Real theA, theB, theC, theD;
140     this->Plane(theA, theB, theC, theD);
141     gp_Pln aGeomPln (theA, theB, theC, theD);
142     gp_Trsf aTransform;
143     aTransform.SetTransformation (aGeomPln.Position());
144     aTransform.Invert();
145     for (Standard_Integer i = 1; i <= 3; i++)
146     {
147       for (Standard_Integer j = 1; j <= 4; j++)
148       {
149         aMatrix.SetValue (i, j, aTransform.Value (i,j));
150       }
151     }
152
153     aMatrix.SetValue (4,1,0.);
154     aMatrix.SetValue (4,2,0.);
155     aMatrix.SetValue (4,3,0.);
156     aMatrix.SetValue (4,4,1.);
157     myGraphicStructure->SetTransform (aMatrix, Graphic3d_TOC_REPLACE);
158   }
159 }