Salome HOME
bde61cc26e94a1f596d93eae37f6dbc30c1c001c
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Scale.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAlgoAPI_Scale.h>
21
22 #include <GeomAPI_Pnt.h>
23
24 #include <BRepBuilderAPI_GTransform.hxx>
25
26 //=================================================================================================
27 GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
28                                      std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
29                                      double theScaleFactor)
30 {
31   if (fabs(theScaleFactor) < Precision::Confusion()) {
32     myError = "Scale builder :: the scale factor is null.";
33     return;
34   }
35
36   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
37   aTrsf->setScale(theCenterPoint, theScaleFactor);
38
39   build(theSourceShape, aTrsf);
40 }
41
42 //=================================================================================================
43 GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
44                                      std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
45                                      double theScaleFactorX,
46                                      double theScaleFactorY,
47                                      double theScaleFactorZ)
48 {
49   if (fabs(theScaleFactorX) < Precision::Confusion()) {
50     myError = "Scale builder :: the scale factor in X is null.";
51     return;
52   }
53   if (fabs(theScaleFactorY) < Precision::Confusion()) {
54     myError = "Scale builder :: the scale factor in Y is null.";
55     return;
56   }
57   if (fabs(theScaleFactorZ) < Precision::Confusion()) {
58     myError = "Scale builder :: the scale factor in Z is null.";
59     return;
60   }
61
62   buildByDimensions(theSourceShape, theCenterPoint,
63                     theScaleFactorX, theScaleFactorY, theScaleFactorZ);
64 }
65
66 //=================================================================================================
67 void GeomAlgoAPI_Scale::buildByDimensions(std::shared_ptr<GeomAPI_Shape> theSourceShape,
68                                           std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
69                                           double                         theScaleFactorX,
70                                           double                         theScaleFactorY,
71                                           double                         theScaleFactorZ)
72 {
73   const gp_Pnt& aCenterPoint = theCenterPoint->impl<gp_Pnt>();
74
75   // Perform the rotation matrix
76   gp_Mat aMatRot(theScaleFactorX, 0., 0.,
77                  0., theScaleFactorY, 0.,
78                  0., 0., theScaleFactorZ);
79
80   // Perform the tranformation
81   gp_GTrsf aGTrsf;
82   gp_GTrsf aGTrsfP0;
83   gp_GTrsf aGTrsf0P;
84   aGTrsfP0.SetTranslationPart(gp::Origin().XYZ() - aCenterPoint.XYZ());
85   aGTrsf0P.SetTranslationPart(aCenterPoint.XYZ());
86   aGTrsf.SetVectorialPart(aMatRot);
87   aGTrsf = aGTrsf0P.Multiplied(aGTrsf);
88   aGTrsf = aGTrsf.Multiplied(aGTrsfP0);
89
90   const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
91
92   if(aSourceShape.IsNull()) {
93     myError = "Scale builder :: source shape does not contain any actual shape.";
94     return;
95   }
96
97   // Transform the shape while copying it.
98   BRepBuilderAPI_GTransform* aBuilder = new BRepBuilderAPI_GTransform(aSourceShape, aGTrsf, true);
99   if(!aBuilder) {
100     myError = "Scale builder :: transform initialization failed.";
101     return;
102   }
103
104   setImpl(aBuilder);
105   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
106
107   if(!aBuilder->IsDone()) {
108     myError = "Scale builder :: algorithm failed.";
109     return;
110   }
111
112   TopoDS_Shape aResult = aBuilder->Shape();
113
114   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
115   aShape->setImpl(new TopoDS_Shape(aResult));
116   setShape(aShape);
117   setDone(true);
118 }