1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_Placement.cpp
5 // Author: Artem ZHIDKOV
7 #include "GeomAlgoAPI_Placement.h"
9 #include <GeomAlgoAPI_DFLoader.h>
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_Edge.h>
13 #include <GeomAPI_Face.h>
14 #include <GeomAPI_Lin.h>
15 #include <GeomAPI_Pnt.h>
16 #include <GeomAPI_Pln.h>
17 #include <GeomAPI_Vertex.h>
18 #include <GeomAPI_XYZ.h>
20 #include <BRepBuilderAPI_Transform.hxx>
21 #include <BRepClass3d_SolidClassifier.hxx>
22 #include <BRepGProp.hxx>
23 #include <gp_Trsf.hxx>
24 #include <gp_Quaternion.hxx>
25 #include <GProp_GProps.hxx>
26 #include <Precision.hxx>
28 GeomAlgoAPI_Placement::GeomAlgoAPI_Placement(const std::shared_ptr<GeomAPI_Shape> theSourceSolid,
29 const std::shared_ptr<GeomAPI_Shape> theDestSolid,
30 const std::shared_ptr<GeomAPI_Shape> theSourceShape,
31 const std::shared_ptr<GeomAPI_Shape> theDestShape,
32 const bool theIsReverse,
33 const bool theIsCentering,
34 const bool theSimpleTransform)
36 build(theSourceSolid, theDestSolid, theSourceShape, theDestShape,
37 theIsReverse, theIsCentering, theSimpleTransform);
40 void GeomAlgoAPI_Placement::build(const std::shared_ptr<GeomAPI_Shape>& theSourceSolid,
41 const std::shared_ptr<GeomAPI_Shape>& theDestSolid,
42 const std::shared_ptr<GeomAPI_Shape>& theSourceShape,
43 const std::shared_ptr<GeomAPI_Shape>& theDestShape,
44 const bool theIsReverse,
45 const bool theIsCentering,
46 const bool theSimpleTransform)
48 // Filling the parameters of the objects
49 static const int aNbObjects = 2;
50 gp_Pnt aSrcDstPoints[aNbObjects]; // points on the selected objects (0 - source, 1 - destination)
51 gp_Vec aSrcDstNormals[aNbObjects]; // normal vectors, if planar faces are selected
52 gp_Vec aSrcDstDirections[aNbObjects]; // directions of linear edges
53 bool hasNormal[aNbObjects];
54 bool hasDirection[aNbObjects];
55 std::shared_ptr<GeomAPI_Shape> aShapes[aNbObjects] = {theSourceShape, theDestShape};
58 static const double aPropEps = 1.e-4;
59 for (int i = 0; i < aNbObjects; i++) {
60 if (aShapes[i]->isFace()) {
61 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
62 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
63 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
64 aSrcDstNormals[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
66 BRepGProp::SurfaceProperties(aFace->impl<TopoDS_Face>(), aProps, aPropEps);
67 gp_Pnt aLoc = aProps.CentreOfMass();
68 aSrcDstPoints[i].SetCoord(aLoc.X(), aLoc.Y(), aLoc.Z());
70 else if (aShapes[i]->isEdge()) {
71 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
72 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
73 std::shared_ptr<GeomAPI_Dir> aDir = aLine->direction();
74 std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
75 std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
76 std::shared_ptr<GeomAPI_XYZ> aLoc = aFirstPnt->xyz()->added(aLastPnt->xyz())->multiplied(0.5);
77 aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
78 aSrcDstDirections[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
80 else if (aShapes[i]->isVertex()) {
81 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aShapes[i]));
82 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
83 aSrcDstPoints[i].SetCoord(aPnt->x(), aPnt->y(), aPnt->z());
84 } else // something goes wrong
86 hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
87 hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
91 const TopoDS_Shape& aSourceShape = theSourceSolid->impl<TopoDS_Shape>();
92 const TopoDS_Shape& aDestShape = theDestSolid->impl<TopoDS_Shape>();
93 // Check the material of the solids to be on the correct side
94 BRepClass3d_SolidClassifier aClassifier;
95 static const double aTransStep = 10. * Precision::Confusion();
97 aClassifier.Load(aSourceShape);
98 gp_Pnt aPoint = aSrcDstPoints[0];
99 aPoint.Translate(aSrcDstNormals[0] * aTransStep);
100 aClassifier.Perform(aPoint, Precision::Confusion());
101 if ((aClassifier.State() == TopAbs_OUT && !theIsReverse) ||
102 (aClassifier.State() == TopAbs_IN && theIsReverse))
103 aSrcDstNormals[0].Reverse();
106 aClassifier.Load(aDestShape);
107 gp_Pnt aPoint = aSrcDstPoints[1];
108 aPoint.Translate(aSrcDstNormals[1] * aTransStep);
109 aClassifier.Perform(aPoint, Precision::Confusion());
110 if (aClassifier.State() == TopAbs_IN)
111 aSrcDstNormals[1].Reverse();
114 // Calculate directions, which comply the normal, for vertices and edges
115 if (!hasNormal[0] || !hasNormal[1]) {
116 if (hasNormal[0] || hasNormal[1]) { // plane with line or vertex
117 if (hasDirection[0] || hasDirection[1]) { // plane - line
118 int anInd = hasDirection[0] ? 0 : 1;
119 gp_Vec aVec = aSrcDstNormals[1 - anInd].Crossed(aSrcDstDirections[anInd]);
120 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and direction are collinear
121 aVec = aSrcDstNormals[1 - anInd].Crossed(
122 gp_Vec(aSrcDstPoints[1 - anInd], aSrcDstPoints[anInd]));
123 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and points direction are collinear
124 if (Abs(aSrcDstNormals[1 - anInd].Y()) >= Precision::Confusion() ||
125 Abs(aSrcDstNormals[1 - anInd].Z()) >= Precision::Confusion())
131 aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
132 } else { // plane - point
133 int anInd = hasNormal[0] ? 1 : 0;
134 aSrcDstNormals[anInd] = aSrcDstNormals[1 - anInd];
137 if (hasDirection[0] && hasDirection[1]) { // line - line
138 gp_Vec aVec = aSrcDstDirections[0].Crossed(aSrcDstDirections[1]);
139 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are parallel
140 aVec = aSrcDstDirections[0].Crossed(gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]));
141 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are equal
142 if (Abs(aSrcDstDirections[0].Y()) >= Precision::Confusion() ||
143 Abs(aSrcDstDirections[0].Z()) >= Precision::Confusion())
149 aSrcDstNormals[0] = aSrcDstDirections[0].Crossed(aVec);
150 aSrcDstNormals[0].Normalize();
151 aSrcDstNormals[1] = aSrcDstDirections[1].Crossed(aVec);
152 aSrcDstNormals[1].Normalize();
153 if (aSrcDstDirections[0].Dot(aSrcDstDirections[1]) < -Precision::Confusion())
154 aSrcDstNormals[1].Reverse();
155 } else if (!hasDirection[0] && !hasDirection[1]) { // point - point
156 aSrcDstNormals[0] = gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]);
157 aSrcDstNormals[0].Normalize();
158 aSrcDstNormals[1] = -aSrcDstNormals[0];
159 } else { // line - point
160 int anInd = hasDirection[0] ? 0 : 1;
161 gp_Vec aVec(aSrcDstPoints[anInd], aSrcDstPoints[1 - anInd]);
162 aVec.Cross(aSrcDstDirections[anInd]);
163 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // point is on line
164 if (Abs(aSrcDstDirections[1 - anInd].Y()) >= Precision::Confusion() ||
165 Abs(aSrcDstDirections[1 - anInd].Z()) >= Precision::Confusion())
170 aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
171 aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
176 // Reverse the normal if it was not done before
177 if (!hasNormal[0] && theIsReverse)
178 aSrcDstNormals[0].Reverse();
180 // Calculate transformation
182 gp_Vec aSrcDir = aSrcDstNormals[0];
183 gp_Vec aDstDir = aSrcDstNormals[1];
184 // Calculate rotation
185 gp_Quaternion aRot(aSrcDir, aDstDir);
186 aTrsf.SetRotation(aRot);
187 // Calculate translation
188 gp_Vec aSrcLoc(aSrcDstPoints[0].XYZ());
189 gp_Vec aDstLoc(aSrcDstPoints[1].XYZ());
191 aDstLoc = aSrcLoc + gp_Vec(aDstDir) * (aDstLoc-aSrcLoc).Dot(aDstDir);
192 aSrcLoc.Transform(aTrsf);
193 gp_Vec aTrans = aDstLoc - aSrcLoc;
194 aTrsf.SetTransformation(aRot, aTrans);
196 if (theSimpleTransform) { // just add transformation
197 TopLoc_Location aDelta(aTrsf);
198 // store the accumulated information about the result and this delta
199 //myTrsf = std::shared_ptr<GeomAPI_Trsf>(new GeomAPI_Trsf(new gp_Trsf(aTrsf * aSourceShape.Location().Transformation())));
200 myTrsf.reset(new GeomAPI_Trsf(new gp_Trsf(aTrsf)));
201 TopoDS_Shape aResult = aSourceShape.Moved(aDelta);
202 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
203 aShape->setImpl(new TopoDS_Shape(aResult));
204 this->setShape(aShape);
205 this->setDone(true); // it is allways true for simple transformation generation
206 } else { // internal rebuild of the shape
207 // Transform the shape with copying it
208 BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
212 this->setImpl(aBuilder);
213 this->setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
214 if(aBuilder->IsDone() != Standard_True) {
217 TopoDS_Shape aResult = aBuilder->Shape();
219 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
220 aShape->setImpl(new TopoDS_Shape(aResult));
221 this->setShape(aShape);
226 //=================================================================================================
227 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Placement::transformation() const