Salome HOME
d8fd7a7498cdb9f884d0fb362ddf0e81fbcd9b0e
[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 #include <GeomAlgoAPI_DFLoader.h>
9
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>
16
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>
26
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,
32   bool theIsReverse,
33   bool theIsCentering, 
34   bool theSimpleTransform)
35   : myDone(false),
36   myShape(new GeomAPI_Shape())
37 {
38   build(theSourceSolid, theDestSolid, theSourceShape, theDestShape, 
39     theIsReverse, theIsCentering, theSimpleTransform);
40 }
41
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,
47   bool theIsReverse,
48   bool theIsCentering,
49   bool theSimpleTransform)
50 {
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};
59
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
82       return;
83     hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
84     hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
85   }
86
87   // Initial shapes
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();
93   if (hasNormal[0]) {
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();
101   }
102   if (hasNormal[1]) {
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();
109   }
110
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())
123               aVec = gp::DX();
124             else
125               aVec = gp::DY();
126           }
127         }
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];
132       }
133     } else {
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())
141               aVec = gp::DX();
142             else
143               aVec = gp::DY();
144           }
145         }
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())
163             aVec = gp::DX();
164           else
165             aVec = gp::DY();
166         }
167         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
168         aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
169       }
170     }
171   }
172
173   // Reverse the normal if it was not done before
174   if (!hasNormal[0] && theIsReverse)
175     aSrcDstNormals[0].Reverse();
176
177   // Calculate transformation
178   gp_Trsf aTrsf;
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());
187   if (!theIsCentering)
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);
192
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::make_shared<GeomAPI_Trsf>(
199       new gp_Trsf(aTrsf * aSourceShape.Location().Transformation()));
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);
204     if(aBuilder) {
205       setImpl(aBuilder);
206       myDone = aBuilder->IsDone() == Standard_True;
207       if (myDone) {
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);
214         }
215         myShape->setImpl(new TopoDS_Shape(aResult));
216         myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
217       }
218     }
219   }
220 }
221
222 //============================================================================
223 const bool GeomAlgoAPI_Placement::isValid() const
224 {
225   if (myShape.get()) { // only for not-simple transform
226     BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
227     return (aChecker.IsValid() == Standard_True);
228   }
229   return true;
230 }
231
232 //============================================================================
233 const bool GeomAlgoAPI_Placement::hasVolume() const
234 {
235   bool hasVolume(false);
236   if(isValid()) {
237     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
238     GProp_GProps aGProp;
239     BRepGProp::VolumeProperties(aRShape, aGProp);
240     if(aGProp.Mass() > Precision::Confusion()) 
241       hasVolume = true; 
242   }
243   return hasVolume;
244 }
245
246 //============================================================================
247 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Placement::shape () const 
248 {
249   return myShape;
250 }
251
252 //============================================================================
253 void GeomAlgoAPI_Placement::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
254 {
255   theMap = myMap;
256 }
257
258 //============================================================================
259 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const
260 {
261   return myMkShape;
262 }
263
264 std::shared_ptr<GeomAPI_Trsf> GeomAlgoAPI_Placement::transformation() const
265 {
266   return myTrsf;
267 }
268
269 //============================================================================
270 GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement()
271 {
272   if (!empty())
273     myMap.clear();
274 }