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