0025194: It is necessary to orthogonalize transformation matrix in gp_Trsf and gp_Trs...
[occt.git] / src / Prs3d / Prs3d_Presentation.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 // Modified:     22/03/04 ; SAN : OCC4895 High-level interface for controlling polygon offsets
16
17 #define IMP020200       //GG Add Transformation() method
18
19 #include <Prs3d_Presentation.ixx>
20 #include <TColStd_Array2OfReal.hxx>
21 #include <Graphic3d_Structure.hxx>
22 #include <Aspect_TypeOfHighlightMethod.hxx>
23 #include <gp_Pnt.hxx>
24 #include <gp_Dir.hxx>
25 #include <gp_Ax1.hxx>
26 #include <gp_Trsf.hxx>
27 #include <gp_Vec.hxx>
28 #include <Standard_Real.hxx>
29 #include <Aspect_InteriorStyle.hxx>
30 #include <Aspect_TypeOfLine.hxx>
31 // OCC4895 SAN 22/03/04 High-level interface for controlling polygon offsets 
32 #include <Aspect_PolygonOffsetMode.hxx>
33 // OCC4895 SAN 22/03/04 High-level interface for controlling polygon offsets
34 #include <Graphic3d_NameOfMaterial.hxx>
35 #include <Graphic3d_AspectFillArea3d.hxx>
36
37
38 static void MakeGraphicTrsf (const Handle(Geom_Transformation)& aGeomTrsf,
39                              TColStd_Array2OfReal& Array){
40   for (Standard_Integer i=1; i<=3; i++){
41     for (Standard_Integer j=1; j<=4; j++){
42       Array.SetValue(i,j,aGeomTrsf->Value(i,j));
43     }
44   }
45   Array.SetValue(4,1,0.);
46   Array.SetValue(4,2,0.);
47   Array.SetValue(4,3,0.);
48   Array.SetValue(4,4,1.);
49 }
50
51 //=======================================================================
52 //function : Prs3d_Presentation
53 //purpose  :
54 //=======================================================================
55 Prs3d_Presentation::Prs3d_Presentation (const Handle(Graphic3d_StructureManager)& theViewer,
56                                         const Standard_Boolean                    theToInit)
57 : Graphic3d_Structure (theViewer)
58 {
59   if (!theToInit)
60   {
61     return;
62   }
63
64   Graphic3d_MaterialAspect aMat (Graphic3d_NOM_BRASS);
65   Quantity_Color aColor = aMat.AmbientColor();
66   // It is necessary to set default polygon offsets for a new presentation
67   Handle(Graphic3d_AspectFillArea3d) aDefAspect =
68     new Graphic3d_AspectFillArea3d (Aspect_IS_SOLID,
69                                     aColor,
70                                     aColor,
71                                     Aspect_TOL_SOLID,
72                                     1.0,
73                                     Graphic3d_NOM_BRASS,
74                                     Graphic3d_NOM_BRASS);
75   aDefAspect->SetPolygonOffsets (Aspect_POM_Fill, 1.0f, 0.0f);
76   SetPrimitivesAspect (aDefAspect);
77 }
78
79 //=======================================================================
80 //function : Prs3d_Presentation
81 //purpose  :
82 //=======================================================================
83 Prs3d_Presentation::Prs3d_Presentation (const Handle(Graphic3d_StructureManager)& theViewer,
84                                         const Handle(Prs3d_Presentation)&         thePrs)
85 : Graphic3d_Structure (theViewer, thePrs)
86 {
87   Graphic3d_MaterialAspect aMat (Graphic3d_NOM_BRASS);
88   Quantity_Color aColor = aMat.AmbientColor();
89   // It is necessary to set default polygon offsets for a new presentation
90   Handle(Graphic3d_AspectFillArea3d) aDefAspect =
91     new Graphic3d_AspectFillArea3d (Aspect_IS_SOLID,
92                                     aColor,
93                                     aColor,
94                                     Aspect_TOL_SOLID,
95                                     1.0,
96                                     Graphic3d_NOM_BRASS,
97                                     Graphic3d_NOM_BRASS);
98   aDefAspect->SetPolygonOffsets (Aspect_POM_Fill, 1.0f, 0.0f);
99   SetPrimitivesAspect (aDefAspect);
100 }
101
102 //=======================================================================
103 //function : Highlight
104 //purpose  : 
105 //=======================================================================
106 void Prs3d_Presentation::Highlight()
107 {
108   SetHighlightColor(Quantity_Color(Quantity_NOC_GRAY99));
109   Aspect_TypeOfHighlightMethod Method = Aspect_TOHM_COLOR;
110   Graphic3d_Structure::Highlight(Method);
111 }
112
113 //=======================================================================
114 //function : Color
115 //purpose  : 
116 //=======================================================================
117 void Prs3d_Presentation::Color(const Quantity_NameOfColor aColor) 
118 {
119   SetHighlightColor(Quantity_Color(aColor));
120   Graphic3d_Structure::Highlight(Aspect_TOHM_COLOR);
121 }
122
123 //=======================================================================
124 //function : BoundBox
125 //purpose  : 
126 //=======================================================================
127 void Prs3d_Presentation::BoundBox()
128
129   SetHighlightColor(Quantity_Color(Quantity_NOC_GRAY99));
130   Graphic3d_Structure::Highlight(Aspect_TOHM_BOUNDBOX);
131 }
132
133
134 //=======================================================================
135 //function : SetShadingAspect
136 //purpose  : 
137 //=======================================================================
138 void Prs3d_Presentation::SetShadingAspect(const Handle(Prs3d_ShadingAspect)& aShadingAspect) 
139
140   SetPrimitivesAspect(aShadingAspect->Aspect());
141 }
142
143 //=======================================================================
144 //function : IsPickable
145 //purpose  : 
146 //=======================================================================
147 Standard_Boolean Prs3d_Presentation::IsPickable () const 
148 {
149   return Graphic3d_Structure::IsSelectable();
150 }
151
152 //=======================================================================
153 //function : SetPickable
154 //purpose  : 
155 //=======================================================================
156
157 void Prs3d_Presentation::SetPickable() 
158 {
159   SetPick(Standard_True);
160 }
161
162 //=======================================================================
163 //function : SetUnPickable
164 //purpose  : 
165 //=======================================================================
166 void Prs3d_Presentation::SetUnPickable() 
167 {
168   SetPick(Standard_False);
169 }
170
171 //=======================================================================
172 //function : Transform
173 //purpose  : 
174 //=======================================================================
175
176 void Prs3d_Presentation::Transform(const Handle(Geom_Transformation)& aTransformation) 
177 {
178   TColStd_Array2OfReal Array (1,4,1,4);
179   MakeGraphicTrsf(aTransformation, Array);
180   SetTransform(Array, Graphic3d_TOC_REPLACE);
181 }
182
183 #ifdef IMP020200
184 //=======================================================================
185 //function : Transformation
186 //purpose  : 
187 //=======================================================================
188
189 Handle(Geom_Transformation) Prs3d_Presentation::Transformation() const {
190 TColStd_Array2OfReal matrix(1,4,1,4);
191
192   Graphic3d_Structure::Transform(matrix);
193
194   gp_Trsf trsf;
195   trsf.SetValues(
196         matrix.Value(1,1),matrix.Value(1,2),matrix.Value(1,3),matrix.Value(1,4),
197         matrix.Value(2,1),matrix.Value(2,2),matrix.Value(2,3),matrix.Value(2,4),
198         matrix.Value(3,1),matrix.Value(3,2),matrix.Value(3,3),matrix.Value(3,4));  
199   Handle(Geom_Transformation) gtrsf = new Geom_Transformation(trsf); 
200
201   return gtrsf;
202 }
203 #endif
204
205 //=======================================================================
206 //function : Place
207 //purpose  : 
208 //=======================================================================
209 void Prs3d_Presentation::Place (const Quantity_Length X,
210                                 const Quantity_Length Y,
211                                 const Quantity_Length Z) 
212 {
213   Handle(Geom_Transformation) aTransformation = new Geom_Transformation;
214   aTransformation->SetTranslation(gp_Vec(X,Y,Z));
215   TColStd_Array2OfReal Array (1,4,1,4);
216   MakeGraphicTrsf(aTransformation, Array);
217   SetTransform(Array, Graphic3d_TOC_REPLACE);
218 }
219
220 //=======================================================================
221 //function : Multiply
222 //purpose  : 
223 //=======================================================================
224 void Prs3d_Presentation::Multiply(const Handle(Geom_Transformation)& aTransformation) 
225 {
226   TColStd_Array2OfReal Array (1,4,1,4);
227   MakeGraphicTrsf(aTransformation, Array);
228   SetTransform(Array, Graphic3d_TOC_POSTCONCATENATE);
229 }
230
231 //=======================================================================
232 //function : Move
233 //purpose  : 
234 //=======================================================================
235 void Prs3d_Presentation::Move  (const Quantity_Length X,
236                                 const Quantity_Length Y,
237                                 const Quantity_Length Z) 
238 {
239   Handle(Geom_Transformation) aTransformation = new Geom_Transformation;
240   aTransformation->SetTranslation(gp_Vec(X,Y,Z));
241   TColStd_Array2OfReal Array (1,4,1,4);
242   MakeGraphicTrsf(aTransformation, Array);
243   SetTransform(Array, Graphic3d_TOC_POSTCONCATENATE);
244 }
245
246 //=======================================================================
247 //function : Connect
248 //purpose  : 
249 //=======================================================================
250 void Prs3d_Presentation::Connect
251   ( const Handle(Prs3d_Presentation)& aPresentation) 
252 {
253   Graphic3d_Structure::Connect(aPresentation, Graphic3d_TOC_DESCENDANT);
254 }
255
256
257 //=======================================================================
258 //function : Remove
259 //purpose  : 
260 //=======================================================================
261 void Prs3d_Presentation::Remove (const Handle(Prs3d_Presentation)& aPresentation) 
262 {
263   Disconnect(aPresentation);
264 }
265
266 //=======================================================================
267 //function : RemoveAll
268 //purpose  : 
269 //=======================================================================
270 void Prs3d_Presentation::RemoveAll () 
271 {
272   DisconnectAll(Graphic3d_TOC_DESCENDANT);
273 }
274
275
276 //=======================================================================
277 //function : CurrentGroup
278 //purpose  : 
279 //=======================================================================
280 Handle(Graphic3d_Group) Prs3d_Presentation::CurrentGroup () const 
281 {
282   if (Groups().IsEmpty())
283   {
284     return const_cast<Prs3d_Presentation* >(this)->NewGroup();
285   }
286   return Groups().Last();
287 }
288
289 //=======================================================================
290 //=======================================================================
291 //function : SetIsForHighlight
292 //purpose  :
293 //=======================================================================
294 void Prs3d_Presentation::SetIsForHighlight (const Standard_Boolean isForHighlight)
295 {
296   Graphic3d_Structure::SetIsForHighlight (isForHighlight);
297 }
298
299 //function : Compute
300 //purpose  : 
301 //=======================================================================
302
303 Handle(Graphic3d_Structure) Prs3d_Presentation::
304        Compute(const Handle(Graphic3d_DataStructureManager)& /*aProjector*/) 
305 {
306   return this;
307 }
308
309 //=======================================================================
310 //function : Compute
311 //purpose  : 
312 //=======================================================================
313
314 void Prs3d_Presentation::Compute(const Handle(Graphic3d_DataStructureManager)& aDataStruct, 
315                                  Handle(Graphic3d_Structure)& aStruct)
316 {
317  Graphic3d_Structure::Compute(aDataStruct,aStruct );
318 }
319
320 //=======================================================================
321 //function : Compute
322 //purpose  : 
323 //=======================================================================
324
325 Handle(Graphic3d_Structure) Prs3d_Presentation::Compute(const Handle(Graphic3d_DataStructureManager)& aDataStruc, 
326                                                        const TColStd_Array2OfReal& anArray)
327 {
328  return Graphic3d_Structure::Compute(aDataStruc,anArray);
329 }
330
331 //=======================================================================
332 //function : Compute
333 //purpose  : 
334 //=======================================================================
335
336 void Prs3d_Presentation::Compute(const Handle(Graphic3d_DataStructureManager)& aDataStruc,
337                                  const TColStd_Array2OfReal& anArray,
338                                  Handle(Graphic3d_Structure)& aStruc)
339 {
340  Graphic3d_Structure::Compute(aDataStruc,anArray,aStruc);
341 }