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