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,
35 bool theSimpleTransform)
37 myShape(new GeomAPI_Shape())
39 build(theSourceSolid, theDestSolid, theSourceShape, theDestShape,
40 theIsReverse, theIsCentering, theSimpleTransform);
43 void GeomAlgoAPI_Placement::build(
44 const std::shared_ptr<GeomAPI_Shape>& theSourceSolid,
45 const std::shared_ptr<GeomAPI_Shape>& theDestSolid,
46 const std::shared_ptr<GeomAPI_Shape>& theSourceShape,
47 const std::shared_ptr<GeomAPI_Shape>& theDestShape,
50 bool theSimpleTransform)
52 // Filling the parameters of the objects
53 static const int aNbObjects = 2;
54 gp_Pnt aSrcDstPoints[aNbObjects]; // points on the selected objects (0 - source, 1 - destination)
55 gp_Vec aSrcDstNormals[aNbObjects]; // normal vectors, if planar faces are selected
56 gp_Vec aSrcDstDirections[aNbObjects]; // directions of linear edges
57 bool hasNormal[aNbObjects];
58 bool hasDirection[aNbObjects];
59 std::shared_ptr<GeomAPI_Shape> aShapes[aNbObjects] = {theSourceShape, theDestShape};
61 for (int i = 0; i < aNbObjects; i++) {
62 if (aShapes[i]->isFace()) {
63 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
64 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
65 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
66 std::shared_ptr<GeomAPI_Pnt> aLoc = aPlane->location();
67 aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
68 aSrcDstNormals[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
69 } else if (aShapes[i]->isEdge()) {
70 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
71 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
72 std::shared_ptr<GeomAPI_Dir> aDir = aLine->direction();
73 std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
74 std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
75 std::shared_ptr<GeomAPI_XYZ> aLoc = aFirstPnt->xyz()->added(aLastPnt->xyz())->multiplied(0.5);
76 aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
77 aSrcDstDirections[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
78 } else if (aShapes[i]->isVertex()) {
79 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aShapes[i]));
80 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
81 aSrcDstPoints[i].SetCoord(aPnt->x(), aPnt->y(), aPnt->z());
82 } else // something goes wrong
84 hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
85 hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
89 const TopoDS_Shape& aSourceShape = theSourceSolid->impl<TopoDS_Shape>();
90 const TopoDS_Shape& aDestShape = theDestSolid->impl<TopoDS_Shape>();
91 // Check the material of the solids to be on the correct side
92 BRepClass3d_SolidClassifier aClassifier;
93 static const double aTransStep = 10. * Precision::Confusion();
95 aClassifier.Load(aSourceShape);
96 gp_Pnt aPoint = aSrcDstPoints[0];
97 aPoint.Translate(aSrcDstNormals[0] * aTransStep);
98 aClassifier.Perform(aPoint, Precision::Confusion());
99 if ((aClassifier.State() == TopAbs_OUT && !theIsReverse) ||
100 (aClassifier.State() == TopAbs_IN && theIsReverse))
101 aSrcDstNormals[0].Reverse();
104 aClassifier.Load(aDestShape);
105 gp_Pnt aPoint = aSrcDstPoints[1];
106 aPoint.Translate(aSrcDstNormals[1] * aTransStep);
107 aClassifier.Perform(aPoint, Precision::Confusion());
108 if (aClassifier.State() == TopAbs_IN)
109 aSrcDstNormals[1].Reverse();
112 // Calculate directions, which comply the normal, for vertices and edges
113 if (!hasNormal[0] || !hasNormal[1]) {
114 if (hasNormal[0] || hasNormal[1]) { // plane with line or vertex
115 if (hasDirection[0] || hasDirection[1]) { // plane - line
116 int anInd = hasDirection[0] ? 0 : 1;
117 gp_Vec aVec = aSrcDstNormals[1 - anInd].Crossed(aSrcDstDirections[anInd]);
118 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and direction are collinear
119 aVec = aSrcDstNormals[1 - anInd].Crossed(
120 gp_Vec(aSrcDstPoints[1 - anInd], aSrcDstPoints[anInd]));
121 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and points direction are collinear
122 if (Abs(aSrcDstNormals[1 - anInd].Y()) >= Precision::Confusion() ||
123 Abs(aSrcDstNormals[1 - anInd].Z()) >= Precision::Confusion())
129 aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
130 } else { // plane - point
131 int anInd = hasNormal[0] ? 1 : 0;
132 aSrcDstNormals[anInd] = aSrcDstNormals[1 - anInd];
135 if (hasDirection[0] && hasDirection[1]) { // line - line
136 gp_Vec aVec = aSrcDstDirections[0].Crossed(aSrcDstDirections[1]);
137 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are parallel
138 aVec = aSrcDstDirections[0].Crossed(gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]));
139 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are equal
140 if (Abs(aSrcDstDirections[0].Y()) >= Precision::Confusion() ||
141 Abs(aSrcDstDirections[0].Z()) >= Precision::Confusion())
147 aSrcDstNormals[0] = aSrcDstDirections[0].Crossed(aVec);
148 aSrcDstNormals[0].Normalize();
149 aSrcDstNormals[1] = aSrcDstDirections[1].Crossed(aVec);
150 aSrcDstNormals[1].Normalize();
151 if (aSrcDstDirections[0].Dot(aSrcDstDirections[1]) < -Precision::Confusion())
152 aSrcDstNormals[1].Reverse();
153 } else if (!hasDirection[0] && !hasDirection[1]) { // point - point
154 aSrcDstNormals[0] = gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]);
155 aSrcDstNormals[0].Normalize();
156 aSrcDstNormals[1] = -aSrcDstNormals[0];
157 } else { // line - point
158 int anInd = hasDirection[0] ? 0 : 1;
159 gp_Vec aVec(aSrcDstPoints[anInd], aSrcDstPoints[1 - anInd]);
160 aVec.Cross(aSrcDstDirections[anInd]);
161 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // point is on line
162 if (Abs(aSrcDstDirections[1 - anInd].Y()) >= Precision::Confusion() ||
163 Abs(aSrcDstDirections[1 - anInd].Z()) >= Precision::Confusion())
168 aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
169 aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
174 // Reverse the normal if it was not done before
175 if (!hasNormal[0] && theIsReverse)
176 aSrcDstNormals[0].Reverse();
178 // Calculate transformation
180 gp_Vec aSrcDir = aSrcDstNormals[0];
181 gp_Vec aDstDir = aSrcDstNormals[1];
182 // Calculate rotation
183 gp_Quaternion aRot(aSrcDir, aDstDir);
184 aTrsf.SetRotation(aRot);
185 // Calculate translation
186 gp_Vec aSrcLoc(aSrcDstPoints[0].XYZ());
187 gp_Vec aDstLoc(aSrcDstPoints[1].XYZ());
189 aDstLoc = aSrcLoc + gp_Vec(aDstDir) * (aDstLoc-aSrcLoc).Dot(aDstDir);
190 aSrcLoc.Transform(aTrsf);
191 gp_Vec aTrans = aDstLoc - aSrcLoc;
192 aTrsf.SetTransformation(aRot, aTrans);
194 if (theSimpleTransform) { // just add transformation
195 TopLoc_Location aDelta(aTrsf);
196 TopoDS_Shape aResult = aSourceShape.Moved(aDelta);
197 myShape->setImpl(new TopoDS_Shape(aResult));
198 } else { // internal rebuild of the shape
199 // Transform the shape with copying it
200 BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
203 myDone = aBuilder->IsDone() == Standard_True;
205 TopoDS_Shape aResult = aBuilder->Shape();
206 // fill data map to keep correct orientation of sub-shapes
207 for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
208 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
209 aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
210 myMap.bind(aCurrentShape, aCurrentShape);
213 int aNum = myMap.size();
214 cout << "MAP of Oriented shapes =" << aNum <<endl;
218 myShape->setImpl(new TopoDS_Shape(aResult));
219 myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
225 //============================================================================
226 const bool GeomAlgoAPI_Placement::isValid() const
228 if (myShape.get()) { // only for not-simple transform
229 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
230 return (aChecker.IsValid() == Standard_True);
235 //============================================================================
236 const bool GeomAlgoAPI_Placement::hasVolume() const
238 bool hasVolume(false);
240 const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
242 BRepGProp::VolumeProperties(aRShape, aGProp);
243 if(aGProp.Mass() > Precision::Confusion())
249 //============================================================================
250 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Placement::shape () const
255 //============================================================================
256 void GeomAlgoAPI_Placement::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
261 //============================================================================
262 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const
267 //============================================================================
268 GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement()