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 GeomAlgoAPI_Placement::GeomAlgoAPI_Placement(
28 std::shared_ptr<GeomAPI_Shape> theSourceSolid,
29 std::shared_ptr<GeomAPI_Shape> theDestSolid,
30 std::shared_ptr<GeomAPI_Shape> theSourceShape,
31 std::shared_ptr<GeomAPI_Shape> theDestShape,
34 bool theSimpleTransform)
36 myShape(new GeomAPI_Shape())
38 build(theSourceSolid, theDestSolid, theSourceShape, theDestShape,
39 theIsReverse, theIsCentering, theSimpleTransform);
42 void GeomAlgoAPI_Placement::build(
43 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,
49 bool theSimpleTransform)
51 // Filling the parameters of the objects
52 static const int aNbObjects = 2;
53 gp_Pnt aSrcDstPoints[aNbObjects]; // points on the selected objects (0 - source, 1 - destination)
54 gp_Vec aSrcDstNormals[aNbObjects]; // normal vectors, if planar faces are selected
55 gp_Vec aSrcDstDirections[aNbObjects]; // directions of linear edges
56 bool hasNormal[aNbObjects];
57 bool hasDirection[aNbObjects];
58 std::shared_ptr<GeomAPI_Shape> aShapes[aNbObjects] = {theSourceShape, theDestShape};
60 for (int i = 0; i < aNbObjects; i++) {
61 if (aShapes[i]->isFace()) {
62 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
63 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
64 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
65 std::shared_ptr<GeomAPI_Pnt> aLoc = aPlane->location();
66 aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
67 aSrcDstNormals[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
68 } else if (aShapes[i]->isEdge()) {
69 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
70 std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
71 std::shared_ptr<GeomAPI_Dir> aDir = aLine->direction();
72 std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
73 std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
74 std::shared_ptr<GeomAPI_XYZ> aLoc = aFirstPnt->xyz()->added(aLastPnt->xyz())->multiplied(0.5);
75 aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
76 aSrcDstDirections[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
77 } else if (aShapes[i]->isVertex()) {
78 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aShapes[i]));
79 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
80 aSrcDstPoints[i].SetCoord(aPnt->x(), aPnt->y(), aPnt->z());
81 } else // something goes wrong
83 hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
84 hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
88 const TopoDS_Shape& aSourceShape = theSourceSolid->impl<TopoDS_Shape>();
89 const TopoDS_Shape& aDestShape = theDestSolid->impl<TopoDS_Shape>();
90 // Check the material of the solids to be on the correct side
91 BRepClass3d_SolidClassifier aClassifier;
92 static const double aTransStep = 10. * Precision::Confusion();
94 aClassifier.Load(aSourceShape);
95 gp_Pnt aPoint = aSrcDstPoints[0];
96 aPoint.Translate(aSrcDstNormals[0] * aTransStep);
97 aClassifier.Perform(aPoint, Precision::Confusion());
98 if ((aClassifier.State() == TopAbs_OUT && !theIsReverse) ||
99 (aClassifier.State() == TopAbs_IN && theIsReverse))
100 aSrcDstNormals[0].Reverse();
103 aClassifier.Load(aDestShape);
104 gp_Pnt aPoint = aSrcDstPoints[1];
105 aPoint.Translate(aSrcDstNormals[1] * aTransStep);
106 aClassifier.Perform(aPoint, Precision::Confusion());
107 if (aClassifier.State() == TopAbs_IN)
108 aSrcDstNormals[1].Reverse();
111 // Calculate directions, which comply the normal, for vertices and edges
112 if (!hasNormal[0] || !hasNormal[1]) {
113 if (hasNormal[0] || hasNormal[1]) { // plane with line or vertex
114 if (hasDirection[0] || hasDirection[1]) { // plane - line
115 int anInd = hasDirection[0] ? 0 : 1;
116 gp_Vec aVec = aSrcDstNormals[1 - anInd].Crossed(aSrcDstDirections[anInd]);
117 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and direction are collinear
118 aVec = aSrcDstNormals[1 - anInd].Crossed(
119 gp_Vec(aSrcDstPoints[1 - anInd], aSrcDstPoints[anInd]));
120 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and points direction are collinear
121 if (Abs(aSrcDstNormals[1 - anInd].Y()) >= Precision::Confusion() ||
122 Abs(aSrcDstNormals[1 - anInd].Z()) >= Precision::Confusion())
128 aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
129 } else { // plane - point
130 int anInd = hasNormal[0] ? 1 : 0;
131 aSrcDstNormals[anInd] = aSrcDstNormals[1 - anInd];
134 if (hasDirection[0] && hasDirection[1]) { // line - line
135 gp_Vec aVec = aSrcDstDirections[0].Crossed(aSrcDstDirections[1]);
136 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are parallel
137 aVec = aSrcDstDirections[0].Crossed(gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]));
138 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are equal
139 if (Abs(aSrcDstDirections[0].Y()) >= Precision::Confusion() ||
140 Abs(aSrcDstDirections[0].Z()) >= Precision::Confusion())
146 aSrcDstNormals[0] = aSrcDstDirections[0].Crossed(aVec);
147 aSrcDstNormals[0].Normalize();
148 aSrcDstNormals[1] = aSrcDstDirections[1].Crossed(aVec);
149 aSrcDstNormals[1].Normalize();
150 if (aSrcDstDirections[0].Dot(aSrcDstDirections[1]) < -Precision::Confusion())
151 aSrcDstNormals[1].Reverse();
152 } else if (!hasDirection[0] && !hasDirection[1]) { // point - point
153 aSrcDstNormals[0] = gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]);
154 aSrcDstNormals[0].Normalize();
155 aSrcDstNormals[1] = -aSrcDstNormals[0];
156 } else { // line - point
157 int anInd = hasDirection[0] ? 0 : 1;
158 gp_Vec aVec(aSrcDstPoints[anInd], aSrcDstPoints[1 - anInd]);
159 aVec.Cross(aSrcDstDirections[anInd]);
160 if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // point is on line
161 if (Abs(aSrcDstDirections[1 - anInd].Y()) >= Precision::Confusion() ||
162 Abs(aSrcDstDirections[1 - anInd].Z()) >= Precision::Confusion())
167 aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
168 aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
173 // Reverse the normal if it was not done before
174 if (!hasNormal[0] && theIsReverse)
175 aSrcDstNormals[0].Reverse();
177 // Calculate transformation
179 gp_Vec aSrcDir = aSrcDstNormals[0];
180 gp_Vec aDstDir = aSrcDstNormals[1];
181 // Calculate rotation
182 gp_Quaternion aRot(aSrcDir, aDstDir);
183 aTrsf.SetRotation(aRot);
184 // Calculate translation
185 gp_Vec aSrcLoc(aSrcDstPoints[0].XYZ());
186 gp_Vec aDstLoc(aSrcDstPoints[1].XYZ());
188 aDstLoc = aSrcLoc + gp_Vec(aDstDir) * (aDstLoc-aSrcLoc).Dot(aDstDir);
189 aSrcLoc.Transform(aTrsf);
190 gp_Vec aTrans = aDstLoc - aSrcLoc;
191 aTrsf.SetTransformation(aRot, aTrans);
193 if (theSimpleTransform) { // just add transformation
194 TopLoc_Location aDelta(aTrsf);
195 TopoDS_Shape aResult = aSourceShape.Moved(aDelta);
196 myShape->setImpl(new TopoDS_Shape(aResult));
197 // store the accumulated information about the result and this delta
198 //myTrsf = std::shared_ptr<GeomAPI_Trsf>(new GeomAPI_Trsf(new gp_Trsf(aTrsf * aSourceShape.Location().Transformation())));
199 myTrsf = std::shared_ptr<GeomAPI_Trsf>(new GeomAPI_Trsf(new gp_Trsf(aTrsf)));
200 myDone = true; // it is allways true for simple transformation generation
201 } else { // internal rebuild of the shape
202 // Transform the shape with copying it
203 BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
206 myDone = aBuilder->IsDone() == Standard_True;
208 TopoDS_Shape aResult = aBuilder->Shape();
209 // fill data map to keep correct orientation of sub-shapes
210 for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
211 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
212 aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
213 myMap.bind(aCurrentShape, aCurrentShape);
215 myShape->setImpl(new TopoDS_Shape(aResult));
216 myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
222 //============================================================================
223 const bool GeomAlgoAPI_Placement::isValid() const
225 if (myShape.get()) { // only for not-simple transform
226 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
227 return (aChecker.IsValid() == Standard_True);
232 //============================================================================
233 const bool GeomAlgoAPI_Placement::hasVolume() const
235 bool hasVolume(false);
237 const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
239 BRepGProp::VolumeProperties(aRShape, aGProp);
240 if(aGProp.Mass() > Precision::Confusion())
246 //============================================================================
247 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Placement::shape () const
252 //============================================================================
253 void GeomAlgoAPI_Placement::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
258 //============================================================================
259 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const
264 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Placement::transformation() const
269 //============================================================================
270 GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement()