1 // Copyright (C) 2014-2022 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_Placement.h"
22 #include <GeomAlgoAPI_DFLoader.h>
23 #include <GeomAlgoAPI_ShapeTools.h>
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>
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>
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)
51 build(theSourceSolid, theDestSolid, theSourceShape, theDestShape,
52 theIsReverse, theIsCentering, theSimpleTransform);
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)
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};
73 static const double aPropEps = 1.e-4;
75 bool isCompound = false;
76 for (int i = 0; i < aNbObjects; i++) {
79 if (aShapes[i]->isCompound()) {
81 GeomAPI_ShapeIterator anIt(aShapes[i]);
82 aShape = anIt.current();
84 GeomPointPtr aPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aShapes[i]);
85 aSrcDstPoints[i].SetCoord(aPnt->x(), aPnt->y(), aPnt->z());
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());
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());
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());
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())
111 aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
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
120 hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
121 hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
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();
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();
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();
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())
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];
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())
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())
206 aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
207 aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
212 // Reverse the normal if it was not done before
213 if (!hasNormal[0] && theIsReverse)
214 aSrcDstNormals[0].Reverse();
216 // Calculate transformation
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());
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);
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);
247 this->setImpl(aBuilder);
248 this->setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
249 if(aBuilder->IsDone() != Standard_True) {
252 TopoDS_Shape aResult = aBuilder->Shape();
254 aShape.reset(new GeomAPI_Shape());
255 aShape->setImpl(new TopoDS_Shape(aResult));
256 this->setShape(aShape);
261 //=================================================================================================
262 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Placement::transformation() const