Salome HOME
Updating Primitive Box.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Placement.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_Placement.cpp
4 // Created:     2 Dec 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "GeomAlgoAPI_Placement.h"
8
9 #include <GeomAlgoAPI_DFLoader.h>
10
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>
19
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>
27
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)
35 {
36   build(theSourceSolid, theDestSolid, theSourceShape, theDestShape,
37     theIsReverse, theIsCentering, theSimpleTransform);
38 }
39
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)
47 {
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};
56
57   GProp_GProps aProps;
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());
65
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());
69     }
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());
79     }
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
85       return;
86     hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
87     hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
88   }
89
90   // Initial shapes
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();
96   if (hasNormal[0]) {
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();
104   }
105   if (hasNormal[1]) {
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();
112   }
113
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()) {
121           // normal and direction are collinear
122           aVec = aSrcDstNormals[1 - anInd].Crossed(
123             gp_Vec(aSrcDstPoints[1 - anInd], aSrcDstPoints[anInd]));
124           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) {
125             // normal and points direction are collinear
126             if (Abs(aSrcDstNormals[1 - anInd].Y()) >= Precision::Confusion() ||
127               Abs(aSrcDstNormals[1 - anInd].Z()) >= Precision::Confusion())
128               aVec = gp::DX();
129             else
130               aVec = gp::DY();
131           }
132         }
133         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
134       } else { // plane - point
135         int anInd = hasNormal[0] ? 1 : 0;
136         aSrcDstNormals[anInd] = aSrcDstNormals[1 - anInd];
137       }
138     } else {
139       if (hasDirection[0] && hasDirection[1]) { // line - line
140         gp_Vec aVec = aSrcDstDirections[0].Crossed(aSrcDstDirections[1]);
141         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are parallel
142           aVec = aSrcDstDirections[0].Crossed(gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]));
143           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are equal
144             if (Abs(aSrcDstDirections[0].Y()) >= Precision::Confusion() ||
145               Abs(aSrcDstDirections[0].Z()) >= Precision::Confusion())
146               aVec = gp::DX();
147             else
148               aVec = gp::DY();
149           }
150         }
151         aSrcDstNormals[0] = aSrcDstDirections[0].Crossed(aVec);
152         aSrcDstNormals[0].Normalize();
153         aSrcDstNormals[1] = aSrcDstDirections[1].Crossed(aVec);
154         aSrcDstNormals[1].Normalize();
155         if (aSrcDstDirections[0].Dot(aSrcDstDirections[1]) < -Precision::Confusion())
156           aSrcDstNormals[1].Reverse();
157       } else if (!hasDirection[0] && !hasDirection[1]) { // point - point
158         aSrcDstNormals[0] = gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]);
159         aSrcDstNormals[0].Normalize();
160         aSrcDstNormals[1] = -aSrcDstNormals[0];
161       } else { // line - point
162         int anInd = hasDirection[0] ? 0 : 1;
163         gp_Vec aVec(aSrcDstPoints[anInd], aSrcDstPoints[1 - anInd]);
164         aVec.Cross(aSrcDstDirections[anInd]);
165         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // point is on line
166           if (Abs(aSrcDstDirections[1 - anInd].Y()) >= Precision::Confusion() ||
167             Abs(aSrcDstDirections[1 - anInd].Z()) >= Precision::Confusion())
168             aVec = gp::DX();
169           else
170             aVec = gp::DY();
171         }
172         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
173         aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
174       }
175     }
176   }
177
178   // Reverse the normal if it was not done before
179   if (!hasNormal[0] && theIsReverse)
180     aSrcDstNormals[0].Reverse();
181
182   // Calculate transformation
183   gp_Trsf aTrsf;
184   gp_Vec aSrcDir = aSrcDstNormals[0];
185   gp_Vec aDstDir = aSrcDstNormals[1];
186   // Calculate rotation
187   gp_Quaternion aRot(aSrcDir, aDstDir);
188   aTrsf.SetRotation(aRot);
189   // Calculate translation
190   gp_Vec aSrcLoc(aSrcDstPoints[0].XYZ());
191   gp_Vec aDstLoc(aSrcDstPoints[1].XYZ());
192   if (!theIsCentering)
193     aDstLoc = aSrcLoc + gp_Vec(aDstDir) * (aDstLoc-aSrcLoc).Dot(aDstDir);
194   aSrcLoc.Transform(aTrsf);
195   gp_Vec aTrans = aDstLoc - aSrcLoc;
196   aTrsf.SetTransformation(aRot, aTrans);
197
198   if (theSimpleTransform) { // just add transformation
199     TopLoc_Location aDelta(aTrsf);
200     // store the accumulated information about the result and this delta
201     myTrsf.reset(new GeomAPI_Trsf(new gp_Trsf(aTrsf)));
202     TopoDS_Shape aResult = aSourceShape.Moved(aDelta);
203     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
204     aShape->setImpl(new TopoDS_Shape(aResult));
205     this->setShape(aShape);
206     this->setDone(true); // it is allways true for simple transformation generation
207   } else { // internal rebuild of the shape
208     // Transform the shape with copying it
209     BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
210     if(!aBuilder) {
211       return;
212     }
213     this->setImpl(aBuilder);
214     this->setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
215     if(aBuilder->IsDone() != Standard_True) {
216       return;
217     }
218     TopoDS_Shape aResult = aBuilder->Shape();
219
220     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
221     aShape->setImpl(new TopoDS_Shape(aResult));
222     this->setShape(aShape);
223     this->setDone(true);
224   }
225 }
226
227 //=================================================================================================
228 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Placement::transformation() const
229 {
230   return myTrsf;
231 }