Salome HOME
Porting to OCCT development version: Standard_PI -> M_PI
[modules/geom.git] / src / GEOMImpl / GEOMImpl_RotateDriver.cxx
1 // Copyright (C) 2007-2011  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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_RotateDriver.hxx>
25 #include <GEOMImpl_IRotate.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28 #include <gp_Trsf.hxx>
29 #include <gp_Pnt.hxx>
30 #include <gp_Vec.hxx>
31 #include <gp_Dir.hxx>
32 #include <gp_Ax1.hxx>
33 #include <BRepBuilderAPI_Transform.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Compound.hxx>
38 #include <TopAbs.hxx>
39 #include <TopExp.hxx>
40 #include <TopoDS_Vertex.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <BRep_Tool.hxx>
43 #include <BRep_Builder.hxx>
44 #include <GeomAPI_ProjectPointOnCurve.hxx>
45 #include <Geom_Line.hxx>
46 #include <GProp_GProps.hxx>
47 #include <BRepGProp.hxx>
48 #include <Precision.hxx>
49
50 //=======================================================================
51 //function : GetID
52 //purpose  :
53 //=======================================================================
54 const Standard_GUID& GEOMImpl_RotateDriver::GetID()
55 {
56   static Standard_GUID aRotateDriver("FF1BBB56-5D14-4df2-980B-3A668264EA16");
57   return aRotateDriver;
58 }
59
60
61 //=======================================================================
62 //function : GEOMImpl_RotateDriver
63 //purpose  :
64 //=======================================================================
65
66 GEOMImpl_RotateDriver::GEOMImpl_RotateDriver()
67 {
68 }
69
70 //=======================================================================
71 //function : Execute
72 //purpose  :
73 //=======================================================================
74 Standard_Integer GEOMImpl_RotateDriver::Execute(TFunction_Logbook& log) const
75 {
76   if (Label().IsNull()) return 0;
77   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
78
79   if (aFunction.IsNull()) return 0;
80
81   GEOMImpl_IRotate RI(aFunction);
82   gp_Trsf aTrsf;
83   gp_Pnt aCP, aP1, aP2;
84   Standard_Integer aType = aFunction->GetType();
85   Handle(GEOM_Function) anOriginalFunction = RI.GetOriginal();
86   if (anOriginalFunction.IsNull()) return 0;
87   TopoDS_Shape aShape, anOriginal = anOriginalFunction->GetValue();
88   if (anOriginal.IsNull()) return 0;
89
90   if (aType == ROTATE || aType == ROTATE_COPY) {
91     Handle(GEOM_Function) anAxis = RI.GetAxis();
92     if (anAxis.IsNull()) return 0;
93     TopoDS_Shape A = anAxis->GetValue();
94     if (A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
95     TopoDS_Edge anEdge = TopoDS::Edge(A);
96
97     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
98     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
99     gp_Dir aDir(gp_Vec(aP1, aP2));
100     gp_Ax1 anAx1(aP1, aDir);
101     Standard_Real anAngle = RI.GetAngle();
102     if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI; // NPAL19665,19769
103     aTrsf.SetRotation(anAx1, anAngle);
104
105     //NPAL18620: performance problem: multiple locations are accumulated
106     //           in shape and need a great time to process
107     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
108     //aShape = aTransformation.Shape();
109     TopLoc_Location aLocOrig = anOriginal.Location();
110     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
111     //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
112     aTrsfOrig.PreMultiply(aTrsf);
113     TopLoc_Location aLocRes (aTrsfOrig);
114     aShape = anOriginal.Located(aLocRes);
115   }
116   else if (aType ==  ROTATE_THREE_POINTS || aType == ROTATE_THREE_POINTS_COPY) {
117     Handle(GEOM_Function) aCentPoint = RI.GetCentPoint();
118     Handle(GEOM_Function) aPoint1 = RI.GetPoint1();
119     Handle(GEOM_Function) aPoint2 = RI.GetPoint2();
120     if(aCentPoint.IsNull() || aPoint1.IsNull() || aPoint2.IsNull()) return 0;
121     TopoDS_Shape aCV = aCentPoint->GetValue();
122     TopoDS_Shape aV1 = aPoint1->GetValue();
123     TopoDS_Shape aV2 = aPoint2->GetValue();
124     if(aCV.IsNull() || aCV.ShapeType() != TopAbs_VERTEX) return 0;
125     if(aV1.IsNull() || aV1.ShapeType() != TopAbs_VERTEX) return 0;
126     if(aV2.IsNull() || aV2.ShapeType() != TopAbs_VERTEX) return 0;
127
128     aCP = BRep_Tool::Pnt(TopoDS::Vertex(aCV));
129     aP1 = BRep_Tool::Pnt(TopoDS::Vertex(aV1));
130     aP2 = BRep_Tool::Pnt(TopoDS::Vertex(aV2));
131
132     gp_Vec aVec1 (aCP, aP1);
133     gp_Vec aVec2 (aCP, aP2);
134     gp_Dir aDir (aVec1 ^ aVec2);
135     gp_Ax1 anAx1 (aCP, aDir);
136     Standard_Real anAngle = aVec1.Angle(aVec2);
137     if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI; // NPAL19665
138     aTrsf.SetRotation(anAx1, anAngle);
139     //NPAL18620: performance problem: multiple locations are accumulated
140     //           in shape and need a great time to process
141     //BRepBuilderAPI_Transform aTransformation(anOriginal, aTrsf, Standard_False);
142     //aShape = aTransformation.Shape();
143     TopLoc_Location aLocOrig = anOriginal.Location();
144     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
145     //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
146     aTrsfOrig.PreMultiply(aTrsf);
147     TopLoc_Location aLocRes (aTrsfOrig);
148     aShape = anOriginal.Located(aLocRes);
149   }
150   else if (aType == ROTATE_1D) {
151     //Get direction
152     Handle(GEOM_Function) anAxis = RI.GetAxis();
153     if(anAxis.IsNull()) return 0;
154     TopoDS_Shape A = anAxis->GetValue();
155     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
156     TopoDS_Edge anEdge = TopoDS::Edge(A);
157
158     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
159     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
160     gp_Dir D(gp_Vec(aP1, aP2));
161
162     gp_Ax1 AX1(aP1, D);
163
164     Standard_Integer nbtimes = RI.GetNbIter1();
165     Standard_Real angle = 360.0/nbtimes;
166
167     TopoDS_Compound aCompound;
168     BRep_Builder B;
169     B.MakeCompound( aCompound );
170
171     TopLoc_Location aLocOrig = anOriginal.Location();
172     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
173
174     for (int i = 0; i < nbtimes; i++ ) {
175       if (i == 0) { // NPAL19665
176         B.Add(aCompound, anOriginal);
177       }
178       else {
179         aTrsf.SetRotation(AX1, i*angle*M_PI/180.);
180         //TopLoc_Location aLocRes (aTrsf * aTrsfOrig); // gp_Trsf::Multiply() has a bug
181         gp_Trsf aTrsfNew (aTrsfOrig);
182         aTrsfNew.PreMultiply(aTrsf);
183         TopLoc_Location aLocRes (aTrsfNew);
184         B.Add(aCompound, anOriginal.Located(aLocRes));
185       }
186       //NPAL18620: performance problem: multiple locations are accumulated
187       //           in shape and need a great time to process
188       //BRepBuilderAPI_Transform aBRepTransformation(anOriginal, aTrsf, Standard_False);
189       //B.Add(aCompound, aBRepTransformation.Shape());
190     }
191
192     aShape = aCompound;
193   }
194   else if (aType == ROTATE_2D) {
195     //Get direction
196     Handle(GEOM_Function) anAxis = RI.GetAxis();
197     if(anAxis.IsNull()) return 0;
198     TopoDS_Shape A = anAxis->GetValue();
199     if(A.IsNull() || A.ShapeType() != TopAbs_EDGE) return 0;
200     TopoDS_Edge anEdge = TopoDS::Edge(A);
201     gp_Pnt aP1 = BRep_Tool::Pnt(TopExp::FirstVertex(anEdge));
202     gp_Pnt aP2 = BRep_Tool::Pnt(TopExp::LastVertex(anEdge));
203     gp_Dir D(gp_Vec(aP1, aP2));
204
205     gp_Ax1 AX1(aP1, D);
206
207     gp_Trsf aTrsf1;
208     gp_Trsf aTrsf2;
209
210     gp_XYZ aDir2 = RI.GetDir2(); // can be set by previous execution
211     if (aDir2.Modulus() < gp::Resolution()) {
212       // Calculate direction as vector from the axis to the shape's center
213       gp_Pnt P1;
214       GProp_GProps System;
215
216       if (anOriginal.ShapeType() == TopAbs_VERTEX) {
217         P1 = BRep_Tool::Pnt(TopoDS::Vertex( anOriginal ));
218       }
219       else if ( anOriginal.ShapeType() == TopAbs_EDGE || anOriginal.ShapeType() == TopAbs_WIRE ) {
220         BRepGProp::LinearProperties(anOriginal, System);
221         P1 = System.CentreOfMass();
222       }
223       else if ( anOriginal.ShapeType() == TopAbs_FACE || anOriginal.ShapeType() == TopAbs_SHELL ) {
224         BRepGProp::SurfaceProperties(anOriginal, System);
225         P1 = System.CentreOfMass();
226       }
227       else {
228         BRepGProp::VolumeProperties(anOriginal, System);
229         P1 = System.CentreOfMass();
230       }
231
232       Handle(Geom_Line) Line = new Geom_Line(AX1);
233       GeomAPI_ProjectPointOnCurve aPrjTool( P1, Line );
234       gp_Pnt P2 = aPrjTool.NearestPoint();
235
236       if ( P1.IsEqual(P2, Precision::Confusion() ) ) return 0;
237
238       aDir2 = gp_XYZ(P1.X()-P2.X(), P1.Y()-P2.Y(), P1.Z()-P2.Z());
239
240       // Attention: this abnormal action is done for good working of
241       // TransformLikeOther(), used by RestoreSubShapes functionality
242       RI.SetDir2(aDir2);
243     }
244
245     gp_Vec Vec (aDir2);
246     Vec.Normalize();
247
248     Standard_Integer nbtimes2 = RI.GetNbIter2();
249     Standard_Integer nbtimes1 = RI.GetNbIter1();
250     Standard_Real step = RI.GetStep();
251     Standard_Real ang = RI.GetAngle();
252
253     TopLoc_Location aLocOrig = anOriginal.Location();
254     gp_Trsf aTrsfOrig = aLocOrig.Transformation();
255
256     gp_Vec aVec;
257     TopoDS_Compound aCompound;
258     BRep_Builder B;
259     B.MakeCompound( aCompound );
260
261     Standard_Real DX, DY, DZ;
262
263     for (int i = 0; i < nbtimes2; i++ ) {
264       DX = i * step * Vec.X();
265       DY = i * step * Vec.Y();
266       DZ = i * step * Vec.Z();
267       aVec.SetCoord( DX, DY, DZ );
268       aTrsf1.SetTranslation(aVec);
269
270       for (int j = 0; j < nbtimes1; j++ ) {
271         if (j == 0) { // NPAL19665
272           TopLoc_Location aLocRes (aTrsf1 * aTrsfOrig);
273           B.Add(aCompound, anOriginal.Located(aLocRes));
274         }
275         else {
276           aTrsf2.SetRotation(AX1, j*ang*M_PI/180.);
277           //TopLoc_Location aLocRes (aTrsf2 * aTrsf1 * aTrsfOrig); // gp_Trsf::Multiply() has a bug
278           gp_Trsf aTrsfNew (aTrsfOrig);
279           aTrsfNew.PreMultiply(aTrsf1);
280           aTrsfNew.PreMultiply(aTrsf2);
281           TopLoc_Location aLocRes (aTrsfNew);
282           B.Add(aCompound, anOriginal.Located(aLocRes));
283         }
284         //NPAL18620: performance problem: multiple locations are accumulated
285         //           in shape and need a great time to process
286         //BRepBuilderAPI_Transform aBRepTrsf1 (anOriginal, aTrsf1, Standard_False);
287         //BRepBuilderAPI_Transform aBRepTrsf2 (aBRepTrsf1.Shape(), aTrsf2, Standard_False);
288         //B.Add(aCompound, aBRepTrsf2.Shape());
289       }
290     }
291
292     aShape = aCompound;
293   }
294   else return 0;
295
296
297   if (aShape.IsNull()) return 0;
298
299   aFunction->SetValue(aShape);
300
301   log.SetTouched(Label());
302
303   return 1;
304 }
305
306
307 //=======================================================================
308 //function :  GEOMImpl_RotateDriver_Type_
309 //purpose  :
310 //=======================================================================
311 Standard_EXPORT Handle_Standard_Type& GEOMImpl_RotateDriver_Type_()
312 {
313   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
314   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
315   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
316   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
317   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
318   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
319
320   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
321   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_RotateDriver",
322                                                          sizeof(GEOMImpl_RotateDriver),
323                                                          1,
324                                                          (Standard_Address)_Ancestors,
325                                                          (Standard_Address)NULL);
326
327   return _aType;
328 }
329
330 //=======================================================================
331 //function : DownCast
332 //purpose  :
333 //=======================================================================
334 const Handle(GEOMImpl_RotateDriver) Handle(GEOMImpl_RotateDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
335 {
336   Handle(GEOMImpl_RotateDriver) _anOtherObject;
337
338   if (!AnObject.IsNull()) {
339      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_RotateDriver))) {
340        _anOtherObject = Handle(GEOMImpl_RotateDriver)((Handle(GEOMImpl_RotateDriver)&)AnObject);
341      }
342   }
343
344   return _anOtherObject;
345 }