Salome HOME
0020907: Werror in GEOM: integrate patch from Erwan ADAM
[modules/geom.git] / src / GEOMImpl / GEOMImpl_RotateDriver.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_RotateDriver.hxx>
26 #include <GEOMImpl_IRotate.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29 #include <gp_Trsf.hxx>
30 #include <gp_Pnt.hxx>
31 #include <gp_Vec.hxx>
32 #include <gp_Dir.hxx>
33 #include <gp_Ax1.hxx>
34 #include <BRepBuilderAPI_Transform.hxx>
35 #include <TopoDS.hxx>
36 #include <TopoDS_Vertex.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <TopoDS_Compound.hxx>
39 #include <TopAbs.hxx>
40 #include <TopExp.hxx>
41 #include <TopoDS_Vertex.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <BRep_Tool.hxx>
44 #include <BRep_Builder.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <Geom_Line.hxx>
47 #include <GProp_GProps.hxx>
48 #include <BRepGProp.hxx>
49 #include <Precision.hxx>
50
51 //=======================================================================
52 //function : GetID
53 //purpose  :
54 //=======================================================================
55 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
56 {
57   static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
58   return aRotateDriver;
59 }
60
61
62 //=======================================================================
63 //function : GEOMImpl_RotateDriver
64 //purpose  :
65 //=======================================================================
66
67 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
68 {
69 }
70
71 //=======================================================================
72 //function : Execute
73 //purpose  :
74 //=======================================================================
75 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
76 {
77   if (Label().IsNull()) return 0;
78   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
79
80   if (aFunction.IsNull()) return 0;
81
82   GEOMImpl_IRotate RI(aFunction);
83   gp_Trsf aTrsf;
84   gp_Pnt aCP, aP1, aP2;
85   Standard_Integer aType = aFunction->GetType();
86   Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
87   if (anOriginalFunction.IsNull()) return 0;
88   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
89   if (anOriginal.IsNull()) return 0;
90
91   if (aType == ROTATE || aType == ROTATE_COPY) {
92     Handle(GEOM_Function) anAxis = RI.GetAxis();
93     if (anAxis.IsNull()) return 0;
94     TopoDS_Shape A = anAxis->GetValue();
95     if (A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
96     TopoDS_Edge anEdge = TopoDS::Edge(A);
97
98     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
99     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
100     gp_Dir aDir(gp_Vec(aP1, aP2));
101     gp_Ax1 anAx1(aP1, aDir);
102     Standard_Real anAngle = RI.GetAngle();
103     if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665,19769
104     aTrsf.SetRotation(anAx1, anAngle);
105
106     //NPAL18620: performance problem: multiple locations are accumulated
107     //           in shape and need a great time to process
108     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
109     //aShape = aTransformation.Shape();
110     TopLoc_Location aLocOrig = anOriginal.Location();
111     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
112     //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
113     aTrsfOrig.PreMultiply(aTrsf);
114     TopLoc_Location aLocRes (aTrsfOrig);
115     aShape = anOriginal.Located(aLocRes);
116   }
117   else if (aType ==  ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
118     Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
119     Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
120     Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
121     if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
122     TopoDS_Shape aCV = aCentPoint->GetValue();
123     TopoDS_Shape aV1 = aPoint1->GetValue();
124     TopoDS_Shape aV2 = aPoint2->GetValue();
125     if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
126     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
127     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
128
129     aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
130     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
131     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
132
133     gp_Vec aVec1 (aCP, aP1);
134     gp_Vec aVec2 (aCP, aP2);
135     gp_Dir aDir (aVec1 ^ aVec2);
136     gp_Ax1 anAx1 (aCP, aDir);
137     Standard_Real anAngle = aVec1.Angle(aVec2);
138     if (fabs(anAngle) < Precision::Angular()) anAngle += 2*PI; // NPAL19665
139     aTrsf.SetRotation(anAx1, anAngle);
140     //NPAL18620: performance problem: multiple locations are accumulated
141     //           in shape and need a great time to process
142     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
143     //aShape = aTransformation.Shape();
144     TopLoc_Location aLocOrig = anOriginal.Location();
145     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
146     //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
147     aTrsfOrig.PreMultiply(aTrsf);
148     TopLoc_Location aLocRes (aTrsfOrig);
149     aShape = anOriginal.Located(aLocRes);
150   }
151   else if (aType == ROTATE_1D) {
152     //Get direction
153     Handle(GEOM_Function) anAxis = RI.GetAxis();
154     if(anAxis.IsNull()) return 0;
155     TopoDS_Shape A = anAxis->GetValue();
156     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
157     TopoDS_Edge anEdge = TopoDS::Edge(A);
158
159     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
160     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
161     gp_Dir D(gp_Vec(aP1, aP2));
162
163     gp_Ax1 AX1(aP1, D);
164
165     Standard_Integer nbtimes = RI.GetNbIter1();
166     Standard_Real angle = 360.0/nbtimes;
167
168     TopoDS_Compound aCompound;
169     BRep_Builder B;
170     B.MakeCompound( aCompound );
171
172     TopLoc_Location aLocOrig = anOriginal.Location();
173     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
174
175     for (int i = 0; i < nbtimes; i++ ) {
176       if (i == 0) { // NPAL19665
177         B.Add(aCompound, anOriginal);
178       }
179       else {
180         aTrsf.SetRotation(AX1, i*angle*PI180);
181         //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
182         gp_Trsf aTrsfNew (aTrsfOrig);
183         aTrsfNew.PreMultiply(aTrsf);
184         TopLoc_Location aLocRes (aTrsfNew);
185         B.Add(aCompound, anOriginal.Located(aLocRes));
186       }
187       //NPAL18620: performance problem: multiple locations are accumulated
188       //           in shape and need a great time to process
189       //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
190       //B.Add(aCompound, aBRepTransformation.Shape());
191     }
192
193     aShape = aCompound;
194   }
195   else if (aType == ROTATE_2D) {
196     //Get direction
197     Handle(GEOM_Function) anAxis = RI.GetAxis();
198     if(anAxis.IsNull()) return 0;
199     TopoDS_Shape A = anAxis->GetValue();
200     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
201     TopoDS_Edge anEdge = TopoDS::Edge(A);
202     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
203     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
204     gp_Dir D(gp_Vec(aP1, aP2));
205
206     gp_Ax1 AX1(aP1, D);
207
208
209     gp_Trsf aTrsf1;
210     gp_Trsf aTrsf2;
211     gp_Pnt P1;
212     GProp_GProps System;
213
214     if (anOriginal.ShapeType() == TopAbs_VERTEX) {
215       P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
216     }
217     else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
218       BRepGProp::LinearProperties(anOriginal, System);
219       P1 = System.CentreOfMass();
220     }
221     else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
222       BRepGProp::SurfaceProperties(anOriginal, System);
223       P1 = System.CentreOfMass();
224     }
225     else {
226       BRepGProp::VolumeProperties(anOriginal, System);
227       P1 = System.CentreOfMass();
228     }
229
230     Handle(Geom_Line) Line = new Geom_Line(AX1);
231     GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
232     gp_Pnt P2 = aPrjTool.NearestPoint();
233
234     if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
235
236     gp_Vec Vec (P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
237     Vec.Normalize();
238
239     Standard_Integer nbtimes2 = RI.GetNbIter2();
240     Standard_Integer nbtimes1 = RI.GetNbIter1();
241     Standard_Real step = RI.GetStep();
242     Standard_Real ang = RI.GetAngle();
243
244     TopLoc_Location aLocOrig = anOriginal.Location();
245     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
246
247     gp_Vec aVec;
248     TopoDS_Compound aCompound;
249     BRep_Builder B;
250     B.MakeCompound( aCompound );
251
252     Standard_Real DX, DY, DZ;
253
254     for (int i = 0; i < nbtimes2; i++ ) {
255       DX = i * step * Vec.X();
256       DY = i * step * Vec.Y();
257       DZ = i * step * Vec.Z();
258       aVec.SetCoord( DX, DY, DZ );
259       aTrsf1.SetTranslation(aVec);
260
261       for (int j = 0; j < nbtimes1; j++ ) {
262         if (j == 0) { // NPAL19665
263           TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
264           B.Add(aCompound, anOriginal.Located(aLocRes));
265         }
266         else {
267           aTrsf2.SetRotation(AX1, j*ang*PI180);
268           //TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig); // gp_Trsf::Multiply() has a bug
269           gp_Trsf aTrsfNew (aTrsfOrig);
270           aTrsfNew.PreMultiply(aTrsf1);
271           aTrsfNew.PreMultiply(aTrsf2);
272           TopLoc_Location aLocRes (aTrsfNew);
273           B.Add(aCompound, anOriginal.Located(aLocRes));
274         }
275         //NPAL18620: performance problem: multiple locations are accumulated
276         //           in shape and need a great time to process
277         //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
278         //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
279         //B.Add(aCompound, aBRepTrsf2.Shape());
280       }
281     }
282
283     aShape = aCompound;
284   }
285   else return 0;
286
287
288   if (aShape.IsNull()) return 0;
289
290   aFunction->SetValue(aShape);
291
292   log.SetTouched(Label());
293
294   return 1;
295 }
296
297
298 //=======================================================================
299 //function :  GEOMImpl_RotateDriver_Type_
300 //purpose  :
301 //=======================================================================
302 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
303 {
304   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
305   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
306   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
307   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
308   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
309   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
310
311   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
312   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
313                                                          sizeof(GEOMImpl_RotateDriver),
314                                                          1,
315                                                          (Standard_Address)_Ancestors,
316                                                          (Standard_Address)NULL);
317
318   return _aType;
319 }
320
321 //=======================================================================
322 //function : DownCast
323 //purpose  :
324 //=======================================================================
325 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
326 {
327   Handle(GEOMImpl_RotateDriver) _anOtherObject;
328
329   if (!AnObject.IsNull()) {
330      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
331        _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
332      }
333   }
334
335   return _anOtherObject;
336 }