Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Scale.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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 (!theCenterPoint) {
32     myError = "Scale builder :: center point is not valid.";
33     return;
34   }
35   if (fabs(theScaleFactor) < Precision::Confusion()) {
36     myError = "Scale builder :: the scale factor is null.";
37     return;
38   }
39
40   GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
41   aTrsf->setScale(theCenterPoint, theScaleFactor);
42
43   build(theSourceShape, aTrsf);
44 }
45
46 //=================================================================================================
47 GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
48                                      std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
49                                      double theScaleFactorX,
50                                      double theScaleFactorY,
51                                      double theScaleFactorZ)
52 {
53   if (!theCenterPoint) {
54     myError = "Scale builder :: center point is not valid.";
55     return;
56   }
57   if (fabs(theScaleFactorX) < Precision::Confusion()) {
58     myError = "Scale builder :: the scale factor in X is null.";
59     return;
60   }
61   if (fabs(theScaleFactorY) < Precision::Confusion()) {
62     myError = "Scale builder :: the scale factor in Y is null.";
63     return;
64   }
65   if (fabs(theScaleFactorZ) < Precision::Confusion()) {
66     myError = "Scale builder :: the scale factor in Z is null.";
67     return;
68   }
69
70   buildByDimensions(theSourceShape, theCenterPoint,
71                     theScaleFactorX, theScaleFactorY, theScaleFactorZ);
72 }
73
74 //=================================================================================================
75 void GeomAlgoAPI_Scale::buildByDimensions(std::shared_ptr<GeomAPI_Shape> theSourceShape,
76                                           std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
77                                           double                         theScaleFactorX,
78                                           double                         theScaleFactorY,
79                                           double                         theScaleFactorZ)
80 {
81   if (!theSourceShape) {
82     myError = "Scale builder :: source shape is not valid.";
83     return;
84   }
85
86   const gp_Pnt& aCenterPoint = theCenterPoint->impl<gp_Pnt>();
87
88   // Perform the rotation matrix
89   gp_Mat aMatRot(theScaleFactorX, 0., 0.,
90                  0., theScaleFactorY, 0.,
91                  0., 0., theScaleFactorZ);
92
93   // Perform the tranformation
94   gp_GTrsf aGTrsf;
95   gp_GTrsf aGTrsfP0;
96   gp_GTrsf aGTrsf0P;
97   aGTrsfP0.SetTranslationPart(gp::Origin().XYZ() - aCenterPoint.XYZ());
98   aGTrsf0P.SetTranslationPart(aCenterPoint.XYZ());
99   aGTrsf.SetVectorialPart(aMatRot);
100   aGTrsf = aGTrsf0P.Multiplied(aGTrsf);
101   aGTrsf = aGTrsf.Multiplied(aGTrsfP0);
102
103   const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
104
105   if(aSourceShape.IsNull()) {
106     myError = "Scale builder :: source shape does not contain any actual shape.";
107     return;
108   }
109
110   // Transform the shape while copying it.
111   BRepBuilderAPI_GTransform* aBuilder = new BRepBuilderAPI_GTransform(aSourceShape, aGTrsf, true);
112   if(!aBuilder) {
113     myError = "Scale builder :: transform initialization failed.";
114     return;
115   }
116
117   setImpl(aBuilder);
118   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
119
120   if(!aBuilder->IsDone()) {
121     myError = "Scale builder :: algorithm failed.";
122     return;
123   }
124
125   TopoDS_Shape aResult = aBuilder->Shape();
126
127   std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
128   aShape->setImpl(new TopoDS_Shape(aResult));
129   setShape(aShape);
130   setDone(true);
131 }