1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_Rotation.cpp
4 // Created: 12 May 2015
5 // Author: Dmitry Bobylev
7 #include "GeomAlgoAPI_Rotation.h"
9 #include <GeomAPI_XYZ.h>
11 #include <BRepBuilderAPI_Transform.hxx>
17 #include <Precision.hxx>
19 //=================================================================================================
20 GeomAlgoAPI_Rotation::GeomAlgoAPI_Rotation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
21 std::shared_ptr<GeomAPI_Ax1> theAxis,
24 myMethodType = BY_ANGLE;
25 mySourceShape = theSourceShape;
31 //=================================================================================================
32 GeomAlgoAPI_Rotation::GeomAlgoAPI_Rotation(std::shared_ptr<GeomAPI_Shape> theSourceShape,
33 std::shared_ptr<GeomAPI_Pnt> theCenterPoint,
34 std::shared_ptr<GeomAPI_Pnt> theStartPoint,
35 std::shared_ptr<GeomAPI_Pnt> theEndPoint)
37 myMethodType = BY_POINTS;
38 mySourceShape = theSourceShape;
39 myCenterPoint = theCenterPoint;
40 myStartPoint = theStartPoint;
41 myEndPoint = theEndPoint;
44 //=================================================================================================
45 bool GeomAlgoAPI_Rotation::check()
47 switch (myMethodType) {
50 myError = "Rotation builder :: axis is not valid.";
54 myError = "Rotation builder :: source shape is not valid.";
57 if (myAngle < -360.) {
58 myError = "Rotation builder :: angle smaller than -360 degrees.";
62 myError = "Rotation builder :: angle greater than 360 degrees.";
69 myError = "Rotation builder :: center point is not valid.";
73 myError = "Rotation builder :: start point is not valid.";
77 myError = "Rotation builder :: end point is not valid.";
81 myError = "Rotation builder :: source shape is not valid.";
84 if(myCenterPoint->distance(myStartPoint) < Precision::Confusion()) {
85 myError = "Rotation builder :: center point and start point coincide.";
88 if(myCenterPoint->distance(myEndPoint) < Precision::Confusion()) {
89 myError = "Rotation builder :: center point and end point coincide.";
92 if(myStartPoint->distance(myEndPoint) < Precision::Confusion()) {
93 myError = "Rotation builder :: start point and end point coincide.";
96 std::shared_ptr<GeomAPI_XYZ> aCenterPointXYZ = myCenterPoint->xyz();
97 std::shared_ptr<GeomAPI_XYZ> aStartPointXYZ = myStartPoint->xyz();
98 std::shared_ptr<GeomAPI_XYZ> aEndPointXYZ = myEndPoint->xyz();
99 std::shared_ptr<GeomAPI_XYZ> vectCenterPointStartPoint =
100 aStartPointXYZ->decreased(aCenterPointXYZ);
101 std::shared_ptr<GeomAPI_XYZ> vectCenterPointEndPoint =
102 aEndPointXYZ->decreased(aCenterPointXYZ);
103 std::shared_ptr<GeomAPI_XYZ> crossProduct =
104 vectCenterPointStartPoint->cross(vectCenterPointEndPoint);
105 std::shared_ptr<GeomAPI_Pnt> aOriginPnt =
106 std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(0.,0.,0.));
107 std::shared_ptr<GeomAPI_XYZ> aOriginXYZ = aOriginPnt->xyz();
109 if (crossProduct->distance(aOriginXYZ) < Precision::Confusion()) {
110 myError = "Rotation builder :: center point, start point and end point are on a line.";
116 myError = "Rotation builder :: method not implemented.";
122 //=================================================================================================
123 void GeomAlgoAPI_Rotation::build()
125 gp_Trsf* aTrsf = new gp_Trsf();
127 switch (myMethodType) {
129 const gp_Ax1& anAxis = myAxis->impl<gp_Ax1>();
130 aTrsf->SetRotation(anAxis, myAngle/180.0*M_PI);
134 const gp_Pnt& aCenterPoint = myCenterPoint->impl<gp_Pnt>();
135 const gp_Pnt& aStartPoint = myStartPoint->impl<gp_Pnt>();
136 const gp_Pnt& aEndPoint = myEndPoint->impl<gp_Pnt>();
137 gp_Vec aVec1(aCenterPoint, aStartPoint);
138 gp_Vec aVec2(aCenterPoint, aEndPoint);
139 gp_Dir aDir(aVec1^aVec2);
140 gp_Ax1 anAxis(aCenterPoint, aDir);
141 double anAngle = aVec1.Angle(aVec2);
142 if (fabs(anAngle) < Precision::Angular()) anAngle += 2.*M_PI;
143 aTrsf->SetRotation(anAxis, anAngle);
147 myError = "Rotation builder :: method not supported";
152 const TopoDS_Shape& aSourceShape = mySourceShape->impl<TopoDS_Shape>();
154 if(aSourceShape.IsNull()) {
155 myError = "Rotation builder :: source shape does not contain any actual shape.";
159 // Transform the shape while copying it.
160 BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true);
162 myError = "Rotation builder :: transform initialization failed.";
167 setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
169 if(!aBuilder->IsDone()) {
170 myError = "Rotation builder :: algorithm failed.";
174 TopoDS_Shape aResult = aBuilder->Shape();
176 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
177 aShape->setImpl(new TopoDS_Shape(aResult));