Salome HOME
Fix pipe to avoid moving of the path, if it is passed through the first face.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Rotation.cpp
index 62e687d98dc9e5aea6e277d23ddd2836930d52df..c8a76a01dbeaea3485077a75d65575ea4202b693 100644 (file)
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+// Copyright (C) 2014-2017  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<mailto:webmaster.salome@opencascade.com>
+//
+
+#include "GeomAlgoAPI_Rotation.h"
+
+#include <GeomAPI_XYZ.h>
 
-// File:        GeomAlgoAPI_Rotation.cpp
-// Created:     12 May 2015
-// Author:      Dmitry Bobylev
-
-#include <GeomAlgoAPI_Rotation.h>
+#include <BRepBuilderAPI_Transform.hxx>
 
-#include <GeomAlgoAPI_ShapeTools.h>
+#include <gp_Ax1.hxx>
+#include <gp_Dir.hxx>
+#include <gp_Vec.hxx>
 
-#include <BRepBuilderAPI_Transform.hxx>
-#include <BRepCheck_Analyzer.hxx>
 #include <Precision.hxx>
-#include <TopExp_Explorer.hxx>
 
 //=================================================================================================
 GeomAlgoAPI_Rotation::GeomAlgoAPI_Rotation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
                                            std::shared_ptr<GeomAPI_Ax1>   theAxis,
                                            double                         theAngle)
-: myDone(false)
 {
-  build(theSourceShape, theAxis, theAngle);
+  myMethodType = BY_ANGLE;
+  mySourceShape = theSourceShape;
+  myAxis = theAxis;
+  myAngle = theAngle;
 }
 
+
 //=================================================================================================
-void GeomAlgoAPI_Rotation::build(std::shared_ptr<GeomAPI_Shape> theSourceShape,
-                                 std::shared_ptr<GeomAPI_Ax1>   theAxis,
-                                 double                         theAngle)
+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)
 {
-  if(!theSourceShape || !theAxis) {
-    return;
+  myMethodType = BY_POINTS;
+  mySourceShape = theSourceShape;
+  myCenterPoint = theCenterPoint;
+  myStartPoint = theStartPoint;
+  myEndPoint = theEndPoint;
+}
+
+//=================================================================================================
+bool GeomAlgoAPI_Rotation::check()
+{
+  switch (myMethodType) {
+    case BY_ANGLE: {
+      if (!myAxis) {
+        myError = "Rotation builder :: axis is not valid.";
+        return false;
+      }
+      if (!mySourceShape) {
+        myError = "Rotation builder :: source shape is not valid.";
+        return false;
+      }
+      return true;
+    }
+    case BY_POINTS: {
+      if (!myCenterPoint) {
+        myError = "Rotation builder :: center point is not valid.";
+        return false;
+      }
+      if (!myStartPoint) {
+        myError = "Rotation builder :: start point is not valid.";
+        return false;
+      }
+      if (!myEndPoint) {
+        myError = "Rotation builder :: end point is not valid.";
+        return false;
+      }
+      if (!mySourceShape) {
+        myError = "Rotation builder :: source shape is not valid.";
+        return false;
+      }
+      if(myCenterPoint->distance(myStartPoint) < Precision::Confusion()) {
+        myError = "Rotation builder :: center point and start point coincide.";
+        return false;
+      }
+      if(myCenterPoint->distance(myEndPoint) < Precision::Confusion()) {
+        myError = "Rotation builder :: center point and end point coincide.";
+        return false;
+      }
+      if(myStartPoint->distance(myEndPoint) < Precision::Confusion()) {
+        myError = "Rotation builder :: start point and end point coincide.";
+        return false;
+      }
+      std::shared_ptr<GeomAPI_XYZ> aCenterPointXYZ = myCenterPoint->xyz();
+      std::shared_ptr<GeomAPI_XYZ> aStartPointXYZ = myStartPoint->xyz();
+      std::shared_ptr<GeomAPI_XYZ> aEndPointXYZ = myEndPoint->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);
+      std::shared_ptr<GeomAPI_Pnt> aOriginPnt =
+        std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
+      std::shared_ptr<GeomAPI_XYZ> aOriginXYZ = aOriginPnt->xyz();
+
+      if (crossProduct->distance(aOriginXYZ) < Precision::Confusion()) {
+        myError = "Rotation builder :: center point, start point and end point are on a line.";
+        return false;
+      }
+      return true;
+    }
+    default: {
+      myError = "Rotation builder :: method not implemented.";
+      return false;
+    }
   }
+}
 
-  const TopoDS_Shape& aSourceShape = theSourceShape->impl<TopoDS_Shape>();
-  const gp_Ax1& anAxis = theAxis->impl<gp_Ax1>();
+//=================================================================================================
+void GeomAlgoAPI_Rotation::build()
+{
+  gp_Trsf* aTrsf = new gp_Trsf();
+
+  switch (myMethodType) {
+    case BY_ANGLE: {
+      const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
+      aTrsf->SetRotation(anAxis, myAngle/180.0*M_PI);
+      break;
+    }
+    case BY_POINTS: {
+      const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
+      const gp_Pnt& aStartPoint = myStartPoint->impl<gp_Pnt>();
+      const gp_Pnt& aEndPoint = myEndPoint->impl<gp_Pnt>();
+      gp_Vec aVec1(aCenterPoint, aStartPoint);
+      gp_Vec aVec2(aCenterPoint, aEndPoint);
+      gp_Dir aDir(aVec1^aVec2);
+      gp_Ax1 anAxis(aCenterPoint, aDir);
+      double anAngle = aVec1.Angle(aVec2);
+      if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI;
+      aTrsf->SetRotation(anAxis, anAngle);
+      break;
+    }
+    default: {
+      myError = "Rotation builder :: method not supported";
+      return;
+    }
+  }
+
+  const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
 
   if(aSourceShape.IsNull()) {
+    myError = "Rotation builder :: source shape does not contain any actual shape.";
     return;
   }
 
-  gp_Trsf* aTrsf = new gp_Trsf();
-  aTrsf->SetRotation(anAxis, theAngle / 180.0 * M_PI);
-  myTrsf.reset(new GeomAPI_Trsf(aTrsf));
-
-  // Transform the shape with copying it.
+  // Transform the shape while copying it.
   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
   if(!aBuilder) {
+    myError = "Rotation builder :: transform initialization failed.";
     return;
   }
-  myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder));
 
-  myDone = aBuilder->IsDone() == Standard_True;
+  setImpl(aBuilder);
+  setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
 
-  if(!myDone) {
+  if(!aBuilder->IsDone()) {
+    myError = "Rotation builder :: algorithm failed.";
     return;
   }
 
   TopoDS_Shape aResult = aBuilder->Shape();
-  // Fill data map to keep correct orientation of sub-shapes.
-  myMap.reset(new GeomAPI_DataMapOfShapeShape());
-  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);
-  }
 
-  myShape.reset(new GeomAPI_Shape());
-  myShape->setImpl(new TopoDS_Shape(aResult));
-}
-
-//=================================================================================================
-const bool GeomAlgoAPI_Rotation::isValid() const
-{
-  BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
-  return (aChecker.IsValid() == Standard_True);
-}
-
-//=================================================================================================
-const bool GeomAlgoAPI_Rotation::hasVolume() const
-{
-  bool hasVolume(false);
-  if(isValid() && (GeomAlgoAPI_ShapeTools::volume(myShape) > Precision::Confusion())) {
-    hasVolume = true;
-  }
-  return hasVolume;
-}
-
-//=================================================================================================
-const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Rotation::shape() const
-{
-  return myShape;
-}
-
-//=================================================================================================
-std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Rotation::mapOfShapes() const
-{
-  return myMap;
-}
-
-//=================================================================================================
-std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Rotation::makeShape() const
-{
-  return myMkShape;
-}
-
-//=================================================================================================
-std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Rotation::transformation() const
-{
-  return myTrsf;
+  std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
+  aShape->setImpl(new TopoDS_Shape(aResult));
+  setShape(aShape);
+  setDone(true);
 }