Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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 #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,
33   bool theIsReverse,
34   bool theIsCentering)
35   : myDone(false),
36   myShape(new GeomAPI_Shape())
37 {
38   build(theSourceSolid, theDestSolid, theSourceShape, theDestShape, theIsReverse, theIsCentering);
39 }
40
41 void GeomAlgoAPI_Placement::build(
42   const std::shared_ptr<GeomAPI_Shape>& theSourceSolid,
43   const std::shared_ptr<GeomAPI_Shape>& theDestSolid,
44   const std::shared_ptr<GeomAPI_Shape>& theSourceShape,
45   const std::shared_ptr<GeomAPI_Shape>& theDestShape,
46   bool theIsReverse,
47   bool theIsCentering)
48 {
49   // Filling the parameters of the objects
50   static const int aNbObjects = 2;
51   gp_Pnt aSrcDstPoints[aNbObjects]; // points on the selected objects (0 - source, 1 - destination)
52   gp_Vec aSrcDstNormals[aNbObjects]; // normal vectors, if planar faces are selected
53   gp_Vec aSrcDstDirections[aNbObjects]; // directions of linear edges
54   bool hasNormal[aNbObjects];
55   bool hasDirection[aNbObjects];
56   std::shared_ptr<GeomAPI_Shape> aShapes[aNbObjects] = {theSourceShape, theDestShape};
57
58   for (int i = 0; i < aNbObjects; i++) {
59     if (aShapes[i]->isFace()) {
60       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aShapes[i]));
61       std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
62       std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
63       std::shared_ptr<GeomAPI_Pnt> aLoc = aPlane->location();
64       aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
65       aSrcDstNormals[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
66     } else if (aShapes[i]->isEdge()) {
67       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShapes[i]));
68       std::shared_ptr<GeomAPI_Lin> aLine = anEdge->line();
69       std::shared_ptr<GeomAPI_Dir> aDir = aLine->direction();
70       std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
71       std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
72       std::shared_ptr<GeomAPI_XYZ> aLoc = aFirstPnt->xyz()->added(aLastPnt->xyz())->multiplied(0.5);
73       aSrcDstPoints[i].SetCoord(aLoc->x(), aLoc->y(), aLoc->z());
74       aSrcDstDirections[i].SetCoord(aDir->x(), aDir->y(), aDir->z());
75     } else if (aShapes[i]->isVertex()) {
76       std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(aShapes[i]));
77       std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
78       aSrcDstPoints[i].SetCoord(aPnt->x(), aPnt->y(), aPnt->z());
79     } else // something goes wrong
80       return;
81     hasNormal[i] = aSrcDstNormals[i].SquareMagnitude() >= Precision::SquareConfusion();
82     hasDirection[i] = aSrcDstDirections[i].SquareMagnitude() >= Precision::SquareConfusion();
83   }
84
85   // Initial shapes
86   const TopoDS_Shape& aSourceShape = theSourceSolid->impl<TopoDS_Shape>();
87   const TopoDS_Shape& aDestShape = theDestSolid->impl<TopoDS_Shape>();
88   // Check the material of the solids to be on the correct side
89   BRepClass3d_SolidClassifier aClassifier;
90   static const double aTransStep = 10. * Precision::Confusion();
91   if (hasNormal[0]) {
92     aClassifier.Load(aSourceShape);
93     gp_Pnt aPoint = aSrcDstPoints[0];
94     aPoint.Translate(aSrcDstNormals[0] * aTransStep);
95     aClassifier.Perform(aPoint, Precision::Confusion());
96     if ((aClassifier.State() == TopAbs_OUT && !theIsReverse) ||
97       (aClassifier.State() == TopAbs_IN && theIsReverse))
98       aSrcDstNormals[0].Reverse();
99   }
100   if (hasNormal[1]) {
101     aClassifier.Load(aDestShape);
102     gp_Pnt aPoint = aSrcDstPoints[1];
103     aPoint.Translate(aSrcDstNormals[1] * aTransStep);
104     aClassifier.Perform(aPoint, Precision::Confusion());
105     if (aClassifier.State() == TopAbs_IN)
106       aSrcDstNormals[1].Reverse();
107   }
108
109   // Calculate directions, which comply the normal, for vertices and edges
110   if (!hasNormal[0] || !hasNormal[1]) {
111     if (hasNormal[0] || hasNormal[1]) { // plane with line or vertex
112       if (hasDirection[0] || hasDirection[1]) { // plane - line
113         int anInd = hasDirection[0] ? 0 : 1;
114         gp_Vec aVec = aSrcDstNormals[1 - anInd].Crossed(aSrcDstDirections[anInd]);
115         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and direction are collinear
116           aVec = aSrcDstNormals[1 - anInd].Crossed(
117             gp_Vec(aSrcDstPoints[1 - anInd], aSrcDstPoints[anInd]));
118           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and points direction are collinear
119             if (Abs(aSrcDstNormals[1 - anInd].Y()) >= Precision::Confusion() || 
120               Abs(aSrcDstNormals[1 - anInd].Z()) >= Precision::Confusion())
121               aVec = gp::DX();
122             else
123               aVec = gp::DY();
124           }
125         }
126         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
127       } else { // plane - point
128         int anInd = hasNormal[0] ? 1 : 0;
129         aSrcDstNormals[anInd] = aSrcDstNormals[1 - anInd];
130       }
131     } else {
132       if (hasDirection[0] && hasDirection[1]) { // line - line
133         gp_Vec aVec = aSrcDstDirections[0].Crossed(aSrcDstDirections[1]);
134         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are parallel
135           aVec = aSrcDstDirections[0].Crossed(gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]));
136           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are equal
137             if (Abs(aSrcDstDirections[0].Y()) >= Precision::Confusion() ||
138               Abs(aSrcDstDirections[0].Z()) >= Precision::Confusion())
139               aVec = gp::DX();
140             else
141               aVec = gp::DY();
142           }
143         }
144         aSrcDstNormals[0] = aSrcDstDirections[0].Crossed(aVec);
145         aSrcDstNormals[0].Normalize();
146         aSrcDstNormals[1] = aSrcDstDirections[1].Crossed(aVec);
147         aSrcDstNormals[1].Normalize();
148         if (aSrcDstDirections[0].Dot(aSrcDstDirections[1]) < -Precision::Confusion())
149           aSrcDstNormals[1].Reverse();
150       } else if (!hasDirection[0] && !hasDirection[1]) { // point - point
151         aSrcDstNormals[0] = gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]);
152         aSrcDstNormals[0].Normalize();
153         aSrcDstNormals[1] = -aSrcDstNormals[0];
154       } else { // line - point
155         int anInd = hasDirection[0] ? 0 : 1;
156         gp_Vec aVec(aSrcDstPoints[anInd], aSrcDstPoints[1 - anInd]);
157         aVec.Cross(aSrcDstDirections[anInd]);
158         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // point is on line
159           if (Abs(aSrcDstDirections[1 - anInd].Y()) >= Precision::Confusion() || 
160             Abs(aSrcDstDirections[1 - anInd].Z()) >= Precision::Confusion())
161             aVec = gp::DX();
162           else
163             aVec = gp::DY();
164         }
165         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
166         aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
167       }
168     }
169   }
170
171   // Reverse the normal if it was not done before
172   if (!hasNormal[0] && theIsReverse)
173     aSrcDstNormals[0].Reverse();
174
175   // Calculate transformation
176   gp_Trsf aTrsf;
177   gp_Vec aSrcDir = aSrcDstNormals[0];
178   gp_Vec aDstDir = aSrcDstNormals[1];
179   // Calculate rotation
180   gp_Quaternion aRot(aSrcDir, aDstDir);
181   aTrsf.SetRotation(aRot);
182   // Calculate translation
183   gp_Vec aSrcLoc(aSrcDstPoints[0].XYZ());
184   gp_Vec aDstLoc(aSrcDstPoints[1].XYZ());
185   if (!theIsCentering)
186     aDstLoc = aSrcLoc + gp_Vec(aDstDir) * (aDstLoc-aSrcLoc).Dot(aDstDir);
187   aSrcLoc.Transform(aTrsf);
188   gp_Vec aTrans = aDstLoc - aSrcLoc;
189   aTrsf.SetTransformation(aRot, aTrans);
190
191   // Transform the shape with copying it
192   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
193   if(aBuilder) {
194     setImpl(aBuilder);
195     myDone = aBuilder->IsDone() == Standard_True;
196     if (myDone) {
197       TopoDS_Shape aResult = aBuilder->Shape();
198       // fill data map to keep correct orientation of sub-shapes 
199       for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
200         std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
201         aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
202         myMap.bind(aCurrentShape, aCurrentShape);
203       }
204 #ifdef DEB_PLACEMENT
205       int aNum = myMap.size();
206       cout << "MAP of Oriented shapes =" << aNum <<endl;
207
208 #endif
209
210       myShape->setImpl(new TopoDS_Shape(aResult));
211       myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
212     }
213   }
214 }
215
216 //============================================================================
217 const bool GeomAlgoAPI_Placement::isValid() const
218 {
219   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
220   return (aChecker.IsValid() == Standard_True);
221 }
222
223 //============================================================================
224 const bool GeomAlgoAPI_Placement::hasVolume() const
225 {
226   bool hasVolume(false);
227   if(isValid()) {
228     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
229     GProp_GProps aGProp;
230     BRepGProp::VolumeProperties(aRShape, aGProp);
231     if(aGProp.Mass() > Precision::Confusion()) 
232       hasVolume = true; 
233   }
234   return hasVolume;
235 }
236
237 //============================================================================
238 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Placement::shape () const 
239 {
240   return myShape;
241 }
242
243 //============================================================================
244 void GeomAlgoAPI_Placement::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
245 {
246   theMap = myMap;
247 }
248
249 //============================================================================
250 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const
251 {
252   return myMkShape;
253 }
254
255 //============================================================================
256 GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement()
257 {
258   if (myImpl)
259     myMap.clear();
260 }