Salome HOME
Optimization of the widget code
[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 <gp_Trsf.hxx>
23 #include <gp_Quaternion.hxx>
24 #include <Precision.hxx>
25
26 GeomAlgoAPI_Placement::GeomAlgoAPI_Placement(const std::shared_ptr<GeomAPI_Shape> theSourceSolid,
27                                              const std::shared_ptr<GeomAPI_Shape> theDestSolid,
28                                              const std::shared_ptr<GeomAPI_Shape> theSourceShape,
29                                              const std::shared_ptr<GeomAPI_Shape> theDestShape,
30                                              const bool theIsReverse,
31                                              const bool theIsCentering,
32                                              const bool theSimpleTransform)
33 {
34   build(theSourceSolid, theDestSolid, theSourceShape, theDestShape,
35     theIsReverse, theIsCentering, theSimpleTransform);
36 }
37
38 void GeomAlgoAPI_Placement::build(const std::shared_ptr<GeomAPI_Shape>& theSourceSolid,
39                                   const std::shared_ptr<GeomAPI_Shape>& theDestSolid,
40                                   const std::shared_ptr<GeomAPI_Shape>& theSourceShape,
41                                   const std::shared_ptr<GeomAPI_Shape>& theDestShape,
42                                   const bool theIsReverse,
43                                   const bool theIsCentering,
44                                   const bool theSimpleTransform)
45 {
46   // Filling the parameters of the objects
47   static const int aNbObjects = 2;
48   gp_Pnt aSrcDstPoints[aNbObjects]; // points on the selected objects (0 - source, 1 - destination)
49   gp_Vec aSrcDstNormals[aNbObjects]; // normal vectors, if planar faces are selected
50   gp_Vec aSrcDstDirections[aNbObjects]; // directions of linear edges
51   bool hasNormal[aNbObjects];
52   bool hasDirection[aNbObjects];
53   std::shared_ptr<GeomAPI_Shape> aShapes[aNbObjects] = {theSourceShape, theDestShape};
54
55   for (int i = 0; i < aNbObjects; i++) {
56     if (aShapes[i]->isFace()) {
57       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
58       std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
59       std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
60       std::shared_ptr<GeomAPI_Pnt> aLoc = aPlane->location();
61       aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
62       aSrcDstNormals[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
63     } else if (aShapes[i]->isEdge()) {
64       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
65       std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
66       std::shared_ptr<GeomAPI_Dir> aDir = aLine->direction();
67       std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
68       std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
69       std::shared_ptr<GeomAPI_XYZ> aLoc = aFirstPnt->xyz()->added(aLastPnt->xyz())->multiplied(0.5);
70       aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
71       aSrcDstDirections[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
72     } else if (aShapes[i]->isVertex()) {
73       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aShapes[i]));
74       std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
75       aSrcDstPoints[i].SetCoord(aPnt->x(), aPnt->y(), aPnt->z());
76     } else // something goes wrong
77       return;
78     hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
79     hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
80   }
81
82   // Initial shapes
83   const TopoDS_Shape& aSourceShape = theSourceSolid->impl<TopoDS_Shape>();
84   const TopoDS_Shape& aDestShape = theDestSolid->impl<TopoDS_Shape>();
85   // Check the material of the solids to be on the correct side
86   BRepClass3d_SolidClassifier aClassifier;
87   static const double aTransStep = 10. * Precision::Confusion();
88   if (hasNormal[0]) {
89     aClassifier.Load(aSourceShape);
90     gp_Pnt aPoint = aSrcDstPoints[0];
91     aPoint.Translate(aSrcDstNormals[0] * aTransStep);
92     aClassifier.Perform(aPoint, Precision::Confusion());
93     if ((aClassifier.State() == TopAbs_OUT && !theIsReverse) ||
94       (aClassifier.State() == TopAbs_IN && theIsReverse))
95       aSrcDstNormals[0].Reverse();
96   }
97   if (hasNormal[1]) {
98     aClassifier.Load(aDestShape);
99     gp_Pnt aPoint = aSrcDstPoints[1];
100     aPoint.Translate(aSrcDstNormals[1] * aTransStep);
101     aClassifier.Perform(aPoint, Precision::Confusion());
102     if (aClassifier.State() == TopAbs_IN)
103       aSrcDstNormals[1].Reverse();
104   }
105
106   // Calculate directions, which comply the normal, for vertices and edges
107   if (!hasNormal[0] || !hasNormal[1]) {
108     if (hasNormal[0] || hasNormal[1]) { // plane with line or vertex
109       if (hasDirection[0] || hasDirection[1]) { // plane - line
110         int anInd = hasDirection[0] ? 0 : 1;
111         gp_Vec aVec = aSrcDstNormals[1 - anInd].Crossed(aSrcDstDirections[anInd]);
112         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and direction are collinear
113           aVec = aSrcDstNormals[1 - anInd].Crossed(
114             gp_Vec(aSrcDstPoints[1 - anInd], aSrcDstPoints[anInd]));
115           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and points direction are collinear
116             if (Abs(aSrcDstNormals[1 - anInd].Y()) >= Precision::Confusion() || 
117               Abs(aSrcDstNormals[1 - anInd].Z()) >= Precision::Confusion())
118               aVec = gp::DX();
119             else
120               aVec = gp::DY();
121           }
122         }
123         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
124       } else { // plane - point
125         int anInd = hasNormal[0] ? 1 : 0;
126         aSrcDstNormals[anInd] = aSrcDstNormals[1 - anInd];
127       }
128     } else {
129       if (hasDirection[0] && hasDirection[1]) { // line - line
130         gp_Vec aVec = aSrcDstDirections[0].Crossed(aSrcDstDirections[1]);
131         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are parallel
132           aVec = aSrcDstDirections[0].Crossed(gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]));
133           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are equal
134             if (Abs(aSrcDstDirections[0].Y()) >= Precision::Confusion() ||
135               Abs(aSrcDstDirections[0].Z()) >= Precision::Confusion())
136               aVec = gp::DX();
137             else
138               aVec = gp::DY();
139           }
140         }
141         aSrcDstNormals[0] = aSrcDstDirections[0].Crossed(aVec);
142         aSrcDstNormals[0].Normalize();
143         aSrcDstNormals[1] = aSrcDstDirections[1].Crossed(aVec);
144         aSrcDstNormals[1].Normalize();
145         if (aSrcDstDirections[0].Dot(aSrcDstDirections[1]) < -Precision::Confusion())
146           aSrcDstNormals[1].Reverse();
147       } else if (!hasDirection[0] && !hasDirection[1]) { // point - point
148         aSrcDstNormals[0] = gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]);
149         aSrcDstNormals[0].Normalize();
150         aSrcDstNormals[1] = -aSrcDstNormals[0];
151       } else { // line - point
152         int anInd = hasDirection[0] ? 0 : 1;
153         gp_Vec aVec(aSrcDstPoints[anInd], aSrcDstPoints[1 - anInd]);
154         aVec.Cross(aSrcDstDirections[anInd]);
155         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // point is on line
156           if (Abs(aSrcDstDirections[1 - anInd].Y()) >= Precision::Confusion() || 
157             Abs(aSrcDstDirections[1 - anInd].Z()) >= Precision::Confusion())
158             aVec = gp::DX();
159           else
160             aVec = gp::DY();
161         }
162         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
163         aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
164       }
165     }
166   }
167
168   // Reverse the normal if it was not done before
169   if (!hasNormal[0] && theIsReverse)
170     aSrcDstNormals[0].Reverse();
171
172   // Calculate transformation
173   gp_Trsf aTrsf;
174   gp_Vec aSrcDir = aSrcDstNormals[0];
175   gp_Vec aDstDir = aSrcDstNormals[1];
176   // Calculate rotation
177   gp_Quaternion aRot(aSrcDir, aDstDir);
178   aTrsf.SetRotation(aRot);
179   // Calculate translation
180   gp_Vec aSrcLoc(aSrcDstPoints[0].XYZ());
181   gp_Vec aDstLoc(aSrcDstPoints[1].XYZ());
182   if (!theIsCentering)
183     aDstLoc = aSrcLoc + gp_Vec(aDstDir) * (aDstLoc-aSrcLoc).Dot(aDstDir);
184   aSrcLoc.Transform(aTrsf);
185   gp_Vec aTrans = aDstLoc - aSrcLoc;
186   aTrsf.SetTransformation(aRot, aTrans);
187
188   if (theSimpleTransform) { // just add transformation
189     TopLoc_Location aDelta(aTrsf);
190     // store the accumulated information about the result and this delta
191     //myTrsf = std::shared_ptr<GeomAPI_Trsf>(new GeomAPI_Trsf(new gp_Trsf(aTrsf * aSourceShape.Location().Transformation())));
192     myTrsf.reset(new GeomAPI_Trsf(new gp_Trsf(aTrsf)));
193     TopoDS_Shape aResult = aSourceShape.Moved(aDelta);
194     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
195     aShape->setImpl(new TopoDS_Shape(aResult));
196     this->setShape(aShape);
197     this->setDone(true); // it is allways true for simple transformation generation
198   } else { // internal rebuild of the shape
199     // Transform the shape with copying it
200     BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
201     if(!aBuilder) {
202       return;
203     }
204     this->setImpl(aBuilder);
205     this->setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
206     if(aBuilder->IsDone() != Standard_True) {
207       return;
208     }
209     TopoDS_Shape aResult = aBuilder->Shape();
210
211     std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
212     aShape->setImpl(new TopoDS_Shape(aResult));
213     this->setShape(aShape);
214     this->setDone(true);
215   }
216 }
217
218 //=================================================================================================
219 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Placement::transformation() const
220 {
221   return myTrsf;
222 }