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