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