Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Placement.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_Placement.h"
22
23 #include <GeomAlgoAPI_DFLoader.h>
24
25 #include <GeomAPI_Dir.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Face.h>
28 #include <GeomAPI_Lin.h>
29 #include <GeomAPI_Pnt.h>
30 #include <GeomAPI_Pln.h>
31 #include <GeomAPI_Vertex.h>
32 #include <GeomAPI_XYZ.h>
33
34 #include <BRepBuilderAPI_Transform.hxx>
35 #include <BRepClass3d_SolidClassifier.hxx>
36 #include <BRepGProp.hxx>
37 #include <gp_Trsf.hxx>
38 #include <gp_Quaternion.hxx>
39 #include <GProp_GProps.hxx>
40 #include <Precision.hxx>
41
42 GeomAlgoAPI_Placement::GeomAlgoAPI_Placement(const std::shared_ptr<GeomAPI_Shape> theSourceSolid,
43                                              const std::shared_ptr<GeomAPI_Shape> theDestSolid,
44                                              const std::shared_ptr<GeomAPI_Shape> theSourceShape,
45                                              const std::shared_ptr<GeomAPI_Shape> theDestShape,
46                                              const bool theIsReverse,
47                                              const bool theIsCentering,
48                                              const bool theSimpleTransform)
49 {
50   build(theSourceSolid, theDestSolid, theSourceShape, theDestShape,
51     theIsReverse, theIsCentering, theSimpleTransform);
52 }
53
54 void GeomAlgoAPI_Placement::build(const std::shared_ptr<GeomAPI_Shape>& theSourceSolid,
55                                   const std::shared_ptr<GeomAPI_Shape>& theDestSolid,
56                                   const std::shared_ptr<GeomAPI_Shape>& theSourceShape,
57                                   const std::shared_ptr<GeomAPI_Shape>& theDestShape,
58                                   const bool theIsReverse,
59                                   const bool theIsCentering,
60                                   const bool theSimpleTransform)
61 {
62   // Filling the parameters of the objects
63   static const int aNbObjects = 2;
64   gp_Pnt aSrcDstPoints[aNbObjects]; // points on the selected objects (0 - source, 1 - destination)
65   gp_Vec aSrcDstNormals[aNbObjects]; // normal vectors, if planar faces are selected
66   gp_Vec aSrcDstDirections[aNbObjects]; // directions of linear edges
67   bool hasNormal[aNbObjects];
68   bool hasDirection[aNbObjects];
69   std::shared_ptr<GeomAPI_Shape> aShapes[aNbObjects] = {theSourceShape, theDestShape};
70
71   GProp_GProps aProps;
72   static const double aPropEps = 1.e-4;
73   for (int i = 0; i < aNbObjects; i++) {
74     if (aShapes[i]->isFace()) {
75       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
76       std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
77       std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
78       aSrcDstNormals[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
79
80       BRepGProp::SurfaceProperties(aFace->impl<TopoDS_Face>(), aProps, aPropEps);
81       gp_Pnt aLoc = aProps.CentreOfMass();
82       aSrcDstPoints[i].SetCoord(aLoc.X(), aLoc.Y(), aLoc.Z());
83     }
84     else if (aShapes[i]->isEdge()) {
85       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
86       std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
87       std::shared_ptr<GeomAPI_Dir> aDir = aLine->direction();
88       std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
89       std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
90       std::shared_ptr<GeomAPI_XYZ> aLoc = aFirstPnt->xyz()->added(aLastPnt->xyz())->multiplied(0.5);
91       aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
92       aSrcDstDirections[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
93     }
94     else if (aShapes[i]->isVertex()) {
95       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aShapes[i]));
96       std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
97       aSrcDstPoints[i].SetCoord(aPnt->x(), aPnt->y(), aPnt->z());
98     } else // something goes wrong
99       return;
100     hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
101     hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
102   }
103
104   // Initial shapes
105   const TopoDS_Shape& aSourceShape = theSourceSolid->impl<TopoDS_Shape>();
106   const TopoDS_Shape& aDestShape = theDestSolid->impl<TopoDS_Shape>();
107   // Check the material of the solids to be on the correct side
108   BRepClass3d_SolidClassifier aClassifier;
109   static const double aTransStep = 10. * Precision::Confusion();
110   if (hasNormal[0]) {
111     aClassifier.Load(aSourceShape);
112     gp_Pnt aPoint = aSrcDstPoints[0];
113     aPoint.Translate(aSrcDstNormals[0] * aTransStep);
114     aClassifier.Perform(aPoint, Precision::Confusion());
115     if ((aClassifier.State() == TopAbs_OUT && !theIsReverse) ||
116       (aClassifier.State() == TopAbs_IN && theIsReverse))
117       aSrcDstNormals[0].Reverse();
118   }
119   if (hasNormal[1]) {
120     aClassifier.Load(aDestShape);
121     gp_Pnt aPoint = aSrcDstPoints[1];
122     aPoint.Translate(aSrcDstNormals[1] * aTransStep);
123     aClassifier.Perform(aPoint, Precision::Confusion());
124     if (aClassifier.State() == TopAbs_IN)
125       aSrcDstNormals[1].Reverse();
126   }
127
128   // Calculate directions, which comply the normal, for vertices and edges
129   if (!hasNormal[0] || !hasNormal[1]) {
130     if (hasNormal[0] || hasNormal[1]) { // plane with line or vertex
131       if (hasDirection[0] || hasDirection[1]) { // plane - line
132         int anInd = hasDirection[0] ? 0 : 1;
133         gp_Vec aVec = aSrcDstNormals[1 - anInd].Crossed(aSrcDstDirections[anInd]);
134         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) {
135           // normal and direction are collinear
136           aVec = aSrcDstNormals[1 - anInd].Crossed(
137             gp_Vec(aSrcDstPoints[1 - anInd], aSrcDstPoints[anInd]));
138           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) {
139             // normal and points direction are collinear
140             if (Abs(aSrcDstNormals[1 - anInd].Y()) >= Precision::Confusion() ||
141               Abs(aSrcDstNormals[1 - anInd].Z()) >= Precision::Confusion())
142               aVec = gp::DX();
143             else
144               aVec = gp::DY();
145           }
146         }
147         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
148       } else { // plane - point
149         int anInd = hasNormal[0] ? 1 : 0;
150         aSrcDstNormals[anInd] = aSrcDstNormals[1 - anInd];
151       }
152     } else {
153       if (hasDirection[0] && hasDirection[1]) { // line - line
154         gp_Vec aVec = aSrcDstDirections[0].Crossed(aSrcDstDirections[1]);
155         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are parallel
156           aVec = aSrcDstDirections[0].Crossed(gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]));
157           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are equal
158             if (Abs(aSrcDstDirections[0].Y()) >= Precision::Confusion() ||
159               Abs(aSrcDstDirections[0].Z()) >= Precision::Confusion())
160               aVec = gp::DX();
161             else
162               aVec = gp::DY();
163           }
164         }
165         aSrcDstNormals[0] = aSrcDstDirections[0].Crossed(aVec);
166         aSrcDstNormals[0].Normalize();
167         aSrcDstNormals[1] = aSrcDstDirections[1].Crossed(aVec);
168         aSrcDstNormals[1].Normalize();
169         if (aSrcDstDirections[0].Dot(aSrcDstDirections[1]) < -Precision::Confusion())
170           aSrcDstNormals[1].Reverse();
171       } else if (!hasDirection[0] && !hasDirection[1]) { // point - point
172         aSrcDstNormals[0] = gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]);
173         aSrcDstNormals[0].Normalize();
174         aSrcDstNormals[1] = -aSrcDstNormals[0];
175       } else { // line - point
176         int anInd = hasDirection[0] ? 0 : 1;
177         gp_Vec aVec(aSrcDstPoints[anInd], aSrcDstPoints[1 - anInd]);
178         aVec.Cross(aSrcDstDirections[anInd]);
179         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // point is on line
180           if (Abs(aSrcDstDirections[1 - anInd].Y()) >= Precision::Confusion() ||
181             Abs(aSrcDstDirections[1 - anInd].Z()) >= Precision::Confusion())
182             aVec = gp::DX();
183           else
184             aVec = gp::DY();
185         }
186         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
187         aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
188       }
189     }
190   }
191
192   // Reverse the normal if it was not done before
193   if (!hasNormal[0] && theIsReverse)
194     aSrcDstNormals[0].Reverse();
195
196   // Calculate transformation
197   gp_Trsf aTrsf;
198   gp_Vec aSrcDir = aSrcDstNormals[0];
199   gp_Vec aDstDir = aSrcDstNormals[1];
200   // Calculate rotation
201   gp_Quaternion aRot(aSrcDir, aDstDir);
202   aTrsf.SetRotation(aRot);
203   // Calculate translation
204   gp_Vec aSrcLoc(aSrcDstPoints[0].XYZ());
205   gp_Vec aDstLoc(aSrcDstPoints[1].XYZ());
206   if (!theIsCentering)
207     aDstLoc = aSrcLoc + gp_Vec(aDstDir) * (aDstLoc-aSrcLoc).Dot(aDstDir);
208   aSrcLoc.Transform(aTrsf);
209   gp_Vec aTrans = aDstLoc - aSrcLoc;
210   aTrsf.SetTransformation(aRot, aTrans);
211
212   if (theSimpleTransform) { // just add transformation
213     TopLoc_Location aDelta(aTrsf);
214     // store the accumulated information about the result and this delta
215     myTrsf.reset(new GeomAPI_Trsf(new gp_Trsf(aTrsf)));
216     TopoDS_Shape aResult = aSourceShape.Moved(aDelta);
217     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
218     aShape->setImpl(new TopoDS_Shape(aResult));
219     this->setShape(aShape);
220     this->setDone(true); // it is allways true for simple transformation generation
221   } else { // internal rebuild of the shape
222     // Transform the shape with copying it
223     BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
224     if(!aBuilder) {
225       return;
226     }
227     this->setImpl(aBuilder);
228     this->setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
229     if(aBuilder->IsDone() != Standard_True) {
230       return;
231     }
232     TopoDS_Shape aResult = aBuilder->Shape();
233
234     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
235     aShape->setImpl(new TopoDS_Shape(aResult));
236     this->setShape(aShape);
237     this->setDone(true);
238   }
239 }
240
241 //=================================================================================================
242 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Placement::transformation() const
243 {
244   return myTrsf;
245 }