1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_Placement.cpp
5 // Author: Artem ZHIDKOV
7 #include <GeomAlgoAPI_Placement.h>
8 #include <GeomAlgoAPI_DFLoader.h>
10 #include <GeomAPI_Edge.h>
11 #include <GeomAPI_Lin.h>
12 #include <GeomAPI_Pnt.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_Vertex.h>
15 #include <GeomAPI_XYZ.h>
17 #include <BRepBuilderAPI_Transform.hxx>
18 #include <gp_Trsf.hxx>
19 #include <gp_Quaternion.hxx>
20 #include <TopExp_Explorer.hxx>
21 #include <BRepCheck_Analyzer.hxx>
22 #include <BRepClass3d_SolidClassifier.hxx>
23 #include <GProp_GProps.hxx>
24 #include <BRepGProp.hxx>
25 #include <Precision.hxx>
27 #define DEB_PLACEMENT 1
28 GeomAlgoAPI_Placement::GeomAlgoAPI_Placement(
29 std::shared_ptr<GeomAPI_Shape> theSourceSolid,
30 std::shared_ptr<GeomAPI_Shape> theDestSolid,
31 std::shared_ptr<GeomAPI_Shape> theSourceShape,
32 std::shared_ptr<GeomAPI_Shape> theDestShape,
36 myShape(new GeomAPI_Shape())
38 build(theSourceSolid, theDestSolid, theSourceShape, theDestShape, theIsReverse, theIsCentering);
41 void GeomAlgoAPI_Placement::build(
42 const std::shared_ptr<GeomAPI_Shape>& theSourceSolid,
43 const std::shared_ptr<GeomAPI_Shape>& theDestSolid,
44 const std::shared_ptr<GeomAPI_Shape>& theSourceShape,
45 const std::shared_ptr<GeomAPI_Shape>& theDestShape,
49 // Filling the parameters of the objects
50 static const int aNbObjects = 2;
51 gp_Pnt aSrcDstPoints[aNbObjects]; // points on the selected objects (0 - source, 1 - destination)
52 gp_Vec aSrcDstNormals[aNbObjects]; // normal vectors, if planar faces are selected
53 gp_Vec aSrcDstDirections[aNbObjects]; // directions of linear edges
54 bool hasNormal[aNbObjects];
55 bool hasDirection[aNbObjects];
56 std::shared_ptr<GeomAPI_Shape> aShapes[aNbObjects] = {theSourceShape, theDestShape};
58 for (int i = 0; i < aNbObjects; i++) {
59 if (aShapes[i]->isFace()) {
60 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
61 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
62 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
63 std::shared_ptr<GeomAPI_Pnt> aLoc = aPlane->location();
64 aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
65 aSrcDstNormals[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
66 } else if (aShapes[i]->isEdge()) {
67 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
68 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
69 std::shared_ptr<GeomAPI_Dir> aDir = aLine->direction();
70 std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
71 std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
72 std::shared_ptr<GeomAPI_XYZ> aLoc = aFirstPnt->xyz()->added(aLastPnt->xyz())->multiplied(0.5);
73 aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
74 aSrcDstDirections[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
75 } else if (aShapes[i]->isVertex()) {
76 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aShapes[i]));
77 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
78 aSrcDstPoints[i].SetCoord(aPnt->x(), aPnt->y(), aPnt->z());
79 } else // something goes wrong
81 hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
82 hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
85 // Calculate directions, which comply the normal, for vertices and edges
86 if (!hasNormal[0] || !hasNormal[1]) {
87 if (hasNormal[0] || hasNormal[1]) { // plane with line or vertex
88 if (hasDirection[0] || hasDirection[1]) { // plane - line
89 int anInd = hasDirection[0] ? 0 : 1;
90 gp_Vec aVec = aSrcDstNormals[1 - anInd].Crossed(aSrcDstDirections[anInd]);
91 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and direction are collinear
92 aVec = aSrcDstNormals[1 - anInd].Crossed(
93 gp_Vec(aSrcDstPoints[1 - anInd], aSrcDstPoints[anInd]));
94 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and points direction are collinear
95 if (Abs(aSrcDstNormals[1 - anInd].Y()) >= Precision::Confusion() ||
96 Abs(aSrcDstNormals[1 - anInd].Z()) >= Precision::Confusion())
102 aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
103 } else { // plane - point
104 int anInd = hasNormal[0] ? 1 : 0;
105 aSrcDstNormals[anInd] = aSrcDstNormals[1 - anInd];
108 if (hasDirection[0] && hasDirection[1]) { // line - line
109 gp_Vec aVec = aSrcDstDirections[0].Crossed(aSrcDstDirections[1]);
110 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are parallel
111 aVec = aSrcDstDirections[0].Crossed(gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]));
112 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are equal
113 if (Abs(aSrcDstDirections[0].Y()) >= Precision::Confusion() ||
114 Abs(aSrcDstDirections[0].Z()) >= Precision::Confusion())
120 aSrcDstNormals[0] = aSrcDstDirections[0].Crossed(aVec);
121 aSrcDstNormals[0].Normalize();
122 aSrcDstNormals[1] = aSrcDstDirections[1].Crossed(aVec).Reversed();
123 aSrcDstNormals[1].Normalize();
124 } else if (!hasDirection[0] && !hasDirection[1]) { // point - point
125 aSrcDstNormals[0] = gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]);
126 aSrcDstNormals[0].Normalize();
127 aSrcDstNormals[1] = -aSrcDstNormals[0];
128 } else { // line - point
129 int anInd = hasDirection[0] ? 0 : 1;
130 gp_Vec aVec(aSrcDstPoints[anInd], aSrcDstPoints[1 - anInd]);
131 aVec.Cross(aSrcDstDirections[anInd]);
132 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // point is on line
133 if (Abs(aSrcDstDirections[1 - anInd].Y()) >= Precision::Confusion() ||
134 Abs(aSrcDstDirections[1 - anInd].Z()) >= Precision::Confusion())
139 aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
140 aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
146 const TopoDS_Shape& aSourceShape = theSourceSolid->impl<TopoDS_Shape>();
147 const TopoDS_Shape& aDestShape = theDestSolid->impl<TopoDS_Shape>();
149 // Calculate transformation
151 gp_Vec aSrcDir = aSrcDstNormals[0];
152 gp_Vec aDstDir = aSrcDstNormals[1];
153 // Check the material of the solids to be on the correct side
154 BRepClass3d_SolidClassifier aClassifier;
155 static const double aTransStep = 10. * Precision::Confusion();
157 aClassifier.Load(aSourceShape);
158 gp_Pnt aPoint = aSrcDstPoints[0];
159 aPoint.Translate(aSrcDir * aTransStep);
160 aClassifier.Perform(aPoint, Precision::Confusion());
161 if ((aClassifier.State() == TopAbs_OUT && !theIsReverse) ||
162 (aClassifier.State() == TopAbs_IN && theIsReverse))
164 } else if (theIsReverse)
167 aClassifier.Load(aDestShape);
168 gp_Pnt aPoint = aSrcDstPoints[1];
169 aPoint.Translate(aDstDir * aTransStep);
170 aClassifier.Perform(aPoint, Precision::Confusion());
171 if (aClassifier.State() == TopAbs_IN)
174 // Calculate rotation
175 gp_Quaternion aRot(aSrcDir, aDstDir);
176 aTrsf.SetRotation(aRot);
177 // Calculate translation
178 gp_Vec aSrcLoc(aSrcDstPoints[0].XYZ());
179 gp_Vec aDstLoc(aSrcDstPoints[1].XYZ());
181 aDstLoc = aSrcLoc + gp_Vec(aDstDir) * (aDstLoc-aSrcLoc).Dot(aDstDir);
182 aSrcLoc.Transform(aTrsf);
183 gp_Vec aTrans = aDstLoc - aSrcLoc;
184 aTrsf.SetTransformation(aRot, aTrans);
186 // Transform the shape with copying it
187 BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
190 myDone = aBuilder->IsDone() == Standard_True;
192 TopoDS_Shape aResult = aBuilder->Shape();
193 // fill data map to keep correct orientation of sub-shapes
194 for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
195 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
196 aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
197 myMap.bind(aCurrentShape, aCurrentShape);
200 int aNum = myMap.size();
201 cout << "MAP of Oriented shapes =" << aNum <<endl;
205 myShape->setImpl(new TopoDS_Shape(aResult));
206 myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
211 //============================================================================
212 const bool GeomAlgoAPI_Placement::isValid() const
214 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
215 return (aChecker.IsValid() == Standard_True);
218 //============================================================================
219 const bool GeomAlgoAPI_Placement::hasVolume() const
221 bool hasVolume(false);
223 const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
225 BRepGProp::VolumeProperties(aRShape, aGProp);
226 if(aGProp.Mass() > Precision::Confusion())
232 //============================================================================
233 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Placement::shape () const
238 //============================================================================
239 void GeomAlgoAPI_Placement::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
244 //============================================================================
245 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const
250 //============================================================================
251 GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement()