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