0023634: Eliminate Polyline and Polygon usage in drawers
[occt.git] / src / V3d / V3d_Plane.cxx
1 // Created by: GG
2 // Copyright (c) 1991-1999 Matra Datavision
3 // Copyright (c) 1999-2012 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
21
22 //-Version
23
24 //-Design
25
26 //-Warning
27
28 //-References
29
30 //-Language     C++ 2.1
31
32 #include <V3d.hxx>
33 #include <V3d_Plane.ixx>
34 #include <Viewer_BadValue.hxx>
35
36 #include <Graphic3d_Group.hxx>
37 #include <Graphic3d_ArrayOfQuadrangles.hxx>
38 #include <Graphic3d_AspectFillArea3d.hxx>
39 #include <gp_Pln.hxx>
40
41 //-Constructors
42
43 V3d_Plane::V3d_Plane(const Standard_Real A, const Standard_Real B, const Standard_Real C, const Standard_Real D)
44 {
45   Viewer_BadValue_Raise_if( sqrt(A*A + B*B + C*C) <= 0., "V3d_Plane::V3d_Plane, bad plane coefficients");
46
47   MyPlane = new Visual3d_ClipPlane(A,B,C,D) ;
48 }
49
50 //-Methods, in order
51
52 void V3d_Plane::SetPlane(const Standard_Real A, const Standard_Real B, const Standard_Real C, const Standard_Real D)
53 {
54   Viewer_BadValue_Raise_if( sqrt(A*A + B*B + C*C) <= 0., "V3d_Plane::SetPlane, bad plane coefficients");
55
56   MyPlane->SetPlane(A,B,C,D) ;
57   if( IsDisplayed() )
58     Update();
59 }
60
61 void V3d_Plane::Display(const Handle(V3d_View)& aView,
62                         const Quantity_Color& aColor)
63 {
64   Handle(V3d_Viewer) theViewer = aView->Viewer();
65   if (!MyGraphicStructure.IsNull())
66     MyGraphicStructure->Clear();
67
68   MyGraphicStructure = new Graphic3d_Structure(theViewer->Viewer());
69   Handle(Graphic3d_Group) group = new Graphic3d_Group(MyGraphicStructure);
70   Handle(Graphic3d_AspectFillArea3d) aspect = new Graphic3d_AspectFillArea3d();
71   Graphic3d_MaterialAspect plastic(Graphic3d_NOM_PLASTIC);
72   plastic.SetColor(aColor);
73   plastic.SetTransparency(0.5);
74   aView->SetTransparency(Standard_True);
75   aspect->SetFrontMaterial(plastic);
76   aspect->SetInteriorStyle (Aspect_IS_HATCH);
77   aspect->SetHatchStyle (Aspect_HS_GRID_DIAGONAL_WIDE);
78   MyGraphicStructure->SetPrimitivesAspect(aspect);
79
80   const Standard_ShortReal size = (Standard_ShortReal)(0.5*theViewer->DefaultViewSize());
81   const Standard_ShortReal offset = size/5000.F;
82
83   Handle(Graphic3d_ArrayOfQuadrangles) aPrims = new Graphic3d_ArrayOfQuadrangles(4);
84   aPrims->AddVertex(-size,-size,offset);
85   aPrims->AddVertex(-size, size,offset);
86   aPrims->AddVertex( size, size,offset);
87   aPrims->AddVertex( size,-size,offset);
88   group->AddPrimitiveArray(aPrims);
89
90   MyGraphicStructure->Display(0);
91   Update();
92 }
93
94 void V3d_Plane::Erase()
95 {
96   if (!MyGraphicStructure.IsNull()) MyGraphicStructure->Erase();
97 }
98
99 void V3d_Plane::Plane(Standard_Real& A, Standard_Real& B, Standard_Real& C, Standard_Real& D) const
100 {
101   MyPlane->Plane(A,B,C,D) ;
102 }
103
104 Handle(Visual3d_ClipPlane) V3d_Plane::Plane()const
105 {
106   return MyPlane;
107 }
108
109 Standard_Boolean V3d_Plane::IsDisplayed() const
110 {
111   if( MyGraphicStructure.IsNull() ) return Standard_False;
112   return MyGraphicStructure->IsDisplayed();
113 }
114
115 void V3d_Plane::Update()
116 {
117   if( !MyGraphicStructure.IsNull() ) {
118     TColStd_Array2OfReal matrix(1,4,1,4);
119     Standard_Real A,B,C,D;
120     MyPlane->Plane(A,B,C,D) ;
121     gp_Pln plan(A,B,C,D);
122     gp_Trsf trsf;
123     trsf.SetTransformation(plan.Position());
124     trsf.Invert();
125     for (Standard_Integer i=1; i<=3; i++){
126       for (Standard_Integer j=1; j<=4; j++){
127         matrix.SetValue(i,j,trsf.Value(i,j));
128       }
129     }
130     matrix.SetValue(4,1,0.);
131     matrix.SetValue(4,2,0.);
132     matrix.SetValue(4,3,0.);
133     matrix.SetValue(4,4,1.);
134     MyGraphicStructure->SetTransform(matrix,Graphic3d_TOC_REPLACE);
135   }
136 }