Salome HOME
Merge remote-tracking branch 'remotes/origin/cgt/NormalToFace' into master
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Scale.cpp
index 28484c4d409b463bd7f6b7c2b199d709c33aee45..776ef1960107ce9eee389b6e3510f1fa84a2ff2f 100644 (file)
-// Copyright (C) 2014-201x CEA/DEN, EDF R&D
+// Copyright (C) 2014-2021  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
-// File:        GeomAlgoAPI_Scale.cpp
-// Created:     23 Jan 2017
-// Author:      Clarisse Genrault (CEA)
+#include <GeomAlgoAPI_Scale.h>
 
-#include "GeomAlgoAPI_Scale.h"
+#include <GeomAPI_Pnt.h>
 
-#include <BRepBuilderAPI_Transform.hxx>
-#include <Precision.hxx>
+#include <BRepBuilderAPI_GTransform.hxx>
 
 //=================================================================================================
 GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
                                      std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
                                      double theScaleFactor)
 {
-  mySourceShape = theSourceShape;
-  myCenterPoint = theCenterPoint;
-  myScaleFactor = theScaleFactor;
+  if (!theCenterPoint) {
+    myError = "Scale builder :: center point is not valid.";
+    return;
+  }
+  if (fabs(theScaleFactor) < Precision::Confusion()) {
+    myError = "Scale builder :: the scale factor is null.";
+    return;
+  }
+
+  GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
+  aTrsf->setScale(theCenterPoint, theScaleFactor);
+
+  build(theSourceShape, aTrsf);
 }
 
 //=================================================================================================
-bool GeomAlgoAPI_Scale::check()
+GeomAlgoAPI_Scale::GeomAlgoAPI_Scale(std::shared_ptr<GeomAPI_Shape> theSourceShape,
+                                     std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
+                                     double theScaleFactorX,
+                                     double theScaleFactorY,
+                                     double theScaleFactorZ)
 {
-  if (!mySourceShape) {
-    myError = "Scale builder :: source shape is invalid.";
-    return false;
+  if (!theCenterPoint) {
+    myError = "Scale builder :: center point is not valid.";
+    return;
   }
-  if (!myCenterPoint) {
-    myError = "Scale builder :: center point is invalid.";
-    return false;
+  if (fabs(theScaleFactorX) < Precision::Confusion()) {
+    myError = "Scale builder :: the scale factor in X is null.";
+    return;
   }
-  if (fabs(myScaleFactor) < Precision::Confusion()) {
-    myError = "Scale builder :: the scale factor is null.";
-    return false;
+  if (fabs(theScaleFactorY) < Precision::Confusion()) {
+    myError = "Scale builder :: the scale factor in Y is null.";
+    return;
   }
-  return true;
+  if (fabs(theScaleFactorZ) < Precision::Confusion()) {
+    myError = "Scale builder :: the scale factor in Z is null.";
+    return;
+  }
+
+  buildByDimensions(theSourceShape, theCenterPoint,
+                    theScaleFactorX, theScaleFactorY, theScaleFactorZ);
 }
 
 //=================================================================================================
-void GeomAlgoAPI_Scale::build()
+void GeomAlgoAPI_Scale::buildByDimensions(std::shared_ptr<GeomAPI_Shape> theSourceShape,
+                                          std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
+                                          double                         theScaleFactorX,
+                                          double                         theScaleFactorY,
+                                          double                         theScaleFactorZ)
 {
-  const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
-  gp_Trsf* aTrsf = new gp_Trsf();
-  aTrsf->SetScale(aCenterPoint, myScaleFactor);
+  if (!theSourceShape) {
+    myError = "Scale builder :: source shape is not valid.";
+    return;
+  }
+
+  const gp_Pnt& aCenterPoint = theCenterPoint->impl<gp_Pnt>();
+
+  // Perform the rotation matrix
+  gp_Mat aMatRot(theScaleFactorX, 0., 0.,
+                 0., theScaleFactorY, 0.,
+                 0., 0., theScaleFactorZ);
+
+  // Perform the tranformation
+  gp_GTrsf aGTrsf;
+  gp_GTrsf aGTrsfP0;
+  gp_GTrsf aGTrsf0P;
+  aGTrsfP0.SetTranslationPart(gp::Origin().XYZ() - aCenterPoint.XYZ());
+  aGTrsf0P.SetTranslationPart(aCenterPoint.XYZ());
+  aGTrsf.SetVectorialPart(aMatRot);
+  aGTrsf = aGTrsf0P.Multiplied(aGTrsf);
+  aGTrsf = aGTrsf.Multiplied(aGTrsfP0);
 
-  const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
+  const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
 
   if(aSourceShape.IsNull()) {
     myError = "Scale builder :: source shape does not contain any actual shape.";
@@ -52,7 +108,7 @@ void GeomAlgoAPI_Scale::build()
   }
 
   // Transform the shape while copying it.
-  BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
+  BRepBuilderAPI_GTransform* aBuilder = new BRepBuilderAPI_GTransform(aSourceShape, aGTrsf, true);
   if(!aBuilder) {
     myError = "Scale builder :: transform initialization failed.";
     return;