Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Rotation.cpp
index 81c3ca6e334a4437cd96dd53cac39a819636df1b..9815341fdd1be9378b4c2bc2828c19bd2dbebf16 100644 (file)
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        GeomAlgoAPI_Rotation.cpp
-// Created:     12 May 2015
-// Author:      Dmitry Bobylev
+// Copyright (C) 2014-2023  CEA, EDF
+//
+// 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
+//
 
 #include <GeomAlgoAPI_Rotation.h>
 
-#include <GeomAlgoAPI_ShapeProps.h>
+#include <GeomAPI_Ax1.h>
+#include <GeomAPI_Pnt.h>
+#include <GeomAPI_XYZ.h>
 
-#include <BRepBuilderAPI_Transform.hxx>
-#include <BRepCheck_Analyzer.hxx>
 #include <Precision.hxx>
-#include <TopExp_Explorer.hxx>
+
+// Verify points are applicable to build the rotation transformation
+static bool checkPoints(GeomPointPtr theCenterPoint,
+                        GeomPointPtr theStartPoint,
+                        GeomPointPtr theEndPoint,
+                        std::string& theError);
 
 //=================================================================================================
 GeomAlgoAPI_Rotation::GeomAlgoAPI_Rotation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
                                            std::shared_ptr<GeomAPI_Ax1>   theAxis,
                                            double                         theAngle)
-: myDone(false),
-  myShape(new GeomAPI_Shape()),
-  myMap(new GeomAPI_DataMapOfShapeShape()),
-  myMkShape(new GeomAlgoAPI_MakeShape())
-{
-  build(theSourceShape, theAxis, theAngle);
-}
-
-//=================================================================================================
-void GeomAlgoAPI_Rotation::build(std::shared_ptr<GeomAPI_Shape> theSourceShape,
-                                 std::shared_ptr<GeomAPI_Ax1>   theAxis,
-                                 double                         theAngle)
 {
-  if(!theSourceShape || !theAxis) {
-    return;
-  }
-
-  const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
-  const gp_Ax1& anAxis = theAxis->impl<gp_Ax1>();
-
-  if(aSourceShape.IsNull()) {
+  if (!theAxis) {
+    myError = "Rotation builder :: axis is not valid.";
     return;
   }
 
-  gp_Trsf aTrsf;
-  aTrsf.SetRotation(anAxis, theAngle / 180.0 * M_PI);
-
-  // Transform the shape with copying it.
-  BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
-  if(!aBuilder) {
-    return;
-  }
-
-  myDone = aBuilder->IsDone() == Standard_True;
-
-  if(!myDone) {
-    return;
-  }
-
-  TopoDS_Shape aResult = aBuilder->Shape();
-  // Fill data map to keep correct orientation of sub-shapes.
-  for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) {
-    std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
-    aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current()));
-    myMap->bind(aCurrentShape, aCurrentShape);
-  }
+  GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
+  aTrsf->setRotation(theAxis, theAngle);
 
-  myShape->setImpl(new TopoDS_Shape(aResult));
-  myMkShape->setImpl(aBuilder);
+  build(theSourceShape, aTrsf);
 }
 
-//=================================================================================================
-const bool GeomAlgoAPI_Rotation::isValid() const
-{
-  BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
-  return (aChecker.IsValid() == Standard_True);
-}
 
 //=================================================================================================
-const bool GeomAlgoAPI_Rotation::hasVolume() const
+GeomAlgoAPI_Rotation::GeomAlgoAPI_Rotation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
+                                           std::shared_ptr<GeomAPI_Pnt>   theCenterPoint,
+                                           std::shared_ptr<GeomAPI_Pnt>   theStartPoint,
+                                           std::shared_ptr<GeomAPI_Pnt>   theEndPoint)
 {
-  bool hasVolume(false);
-  if(isValid() && (GeomAlgoAPI_ShapeProps::volume(myShape) > Precision::Confusion())) {
-    hasVolume = true;
-  }
-  return hasVolume;
-}
+  if (!checkPoints(theCenterPoint, theStartPoint, theEndPoint, myError))
+    return;
 
-//=================================================================================================
-const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Rotation::shape() const
-{
-  return myShape;
-}
+  GeomTrsfPtr aTrsf(new GeomAPI_Trsf);
+  aTrsf->setRotation(theCenterPoint, theStartPoint, theEndPoint);
 
-//=================================================================================================
-std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Rotation::mapOfShapes() const
-{
-  return myMap;
+  build(theSourceShape, aTrsf);
 }
 
 //=================================================================================================
-std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Rotation::makeShape() const
+bool checkPoints(GeomPointPtr theCenterPoint,
+                 GeomPointPtr theStartPoint,
+                 GeomPointPtr theEndPoint,
+                 std::string& theError)
 {
-  return myMkShape;
+  if (!theCenterPoint) {
+    theError = "Rotation builder :: center point is not valid.";
+    return false;
+  }
+  if (!theStartPoint) {
+    theError = "Rotation builder :: start point is not valid.";
+    return false;
+  }
+  if (!theEndPoint) {
+    theError = "Rotation builder :: end point is not valid.";
+    return false;
+  }
+  if (theCenterPoint->distance(theStartPoint) < Precision::Confusion()) {
+    theError = "Rotation builder :: center point and start point coincide.";
+    return false;
+  }
+  if (theCenterPoint->distance(theEndPoint) < Precision::Confusion()) {
+    theError = "Rotation builder :: center point and end point coincide.";
+    return false;
+  }
+  if (theStartPoint->distance(theEndPoint) < Precision::Confusion()) {
+    theError = "Rotation builder :: start point and end point coincide.";
+    return false;
+  }
+  std::shared_ptr<GeomAPI_XYZ> aCenterPointXYZ = theCenterPoint->xyz();
+  std::shared_ptr<GeomAPI_XYZ> aStartPointXYZ = theStartPoint->xyz();
+  std::shared_ptr<GeomAPI_XYZ> aEndPointXYZ = theEndPoint->xyz();
+  std::shared_ptr<GeomAPI_XYZ> vectCenterPointStartPoint =
+      aStartPointXYZ->decreased(aCenterPointXYZ);
+  std::shared_ptr<GeomAPI_XYZ> vectCenterPointEndPoint =
+      aEndPointXYZ->decreased(aCenterPointXYZ);
+  std::shared_ptr<GeomAPI_XYZ> crossProduct =
+      vectCenterPointStartPoint->cross(vectCenterPointEndPoint);
+
+  if (crossProduct->squareModulus() < Precision::SquareConfusion()) {
+    theError = "Rotation builder :: center point, start point and end point are on a line.";
+    return false;
+  }
+  return true;
 }