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   // Calculate directions, which comply the normal, for vertices and edges
86   if (!hasNormal[0] || !hasNormal[1]) {
87     if (hasNormal[0] || hasNormal[1]) { // plane with line or vertex
88       if (hasDirection[0] || hasDirection[1]) { // plane - line
89         int anInd = hasDirection[0] ? 0 : 1;
90         gp_Vec aVec = aSrcDstNormals[1 - anInd].Crossed(aSrcDstDirections[anInd]);
91         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and direction are collinear
92           aVec = aSrcDstNormals[1 - anInd].Crossed(
93               gp_Vec(aSrcDstPoints[1 - anInd], aSrcDstPoints[anInd]));
94           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // normal and points direction are collinear
95             if (Abs(aSrcDstNormals[1 - anInd].Y()) >= Precision::Confusion() || 
96                 Abs(aSrcDstNormals[1 - anInd].Z()) >= Precision::Confusion())
97               aVec = gp::DX();
98             else
99               aVec = gp::DY();
100           }
101         }
102         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
103       } else { // plane - point
104         int anInd = hasNormal[0] ? 1 : 0;
105         aSrcDstNormals[anInd] = aSrcDstNormals[1 - anInd];
106       }
107     } else {
108       if (hasDirection[0] && hasDirection[1]) { // line - line
109         gp_Vec aVec = aSrcDstDirections[0].Crossed(aSrcDstDirections[1]);
110         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are parallel
111           aVec = aSrcDstDirections[0].Crossed(gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]));
112           if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // lines are equal
113             if (Abs(aSrcDstDirections[0].Y()) >= Precision::Confusion() ||
114                 Abs(aSrcDstDirections[0].Z()) >= Precision::Confusion())
115               aVec = gp::DX();
116             else
117               aVec = gp::DY();
118           }
119         }
120         aSrcDstNormals[0] = aSrcDstDirections[0].Crossed(aVec);
121         aSrcDstNormals[0].Normalize();
122         aSrcDstNormals[1] = aSrcDstDirections[1].Crossed(aVec).Reversed();
123         aSrcDstNormals[1].Normalize();
124       } else if (!hasDirection[0] && !hasDirection[1]) { // point - point
125         aSrcDstNormals[0] = gp_Vec(aSrcDstPoints[0], aSrcDstPoints[1]);
126         aSrcDstNormals[0].Normalize();
127         aSrcDstNormals[1] = -aSrcDstNormals[0];
128       } else { // line - point
129         int anInd = hasDirection[0] ? 0 : 1;
130         gp_Vec aVec(aSrcDstPoints[anInd], aSrcDstPoints[1 - anInd]);
131         aVec.Cross(aSrcDstDirections[anInd]);
132         if (aVec.SquareMagnitude() < Precision::SquareConfusion()) { // point is on line
133           if (Abs(aSrcDstDirections[1 - anInd].Y()) >= Precision::Confusion() || 
134               Abs(aSrcDstDirections[1 - anInd].Z()) >= Precision::Confusion())
135             aVec = gp::DX();
136           else
137             aVec = gp::DY();
138         }
139         aSrcDstNormals[anInd] = aSrcDstDirections[anInd].Crossed(aVec).Normalized();
140         aSrcDstNormals[1 - anInd] = aSrcDstNormals[anInd];
141       }
142     }
143   }
144
145   // Initial shapes
146   const TopoDS_Shape& aSourceShape = theSourceSolid->impl<TopoDS_Shape>();
147   const TopoDS_Shape& aDestShape = theDestSolid->impl<TopoDS_Shape>();
148
149   // Calculate transformation
150   gp_Trsf aTrsf;
151   gp_Vec aSrcDir = aSrcDstNormals[0];
152   gp_Vec aDstDir = aSrcDstNormals[1];
153   // Check the material of the solids to be on the correct side
154   BRepClass3d_SolidClassifier aClassifier;
155   static const double aTransStep = 10. * Precision::Confusion();
156   if (hasNormal[0]) {
157     aClassifier.Load(aSourceShape);
158     gp_Pnt aPoint = aSrcDstPoints[0];
159     aPoint.Translate(aSrcDir * aTransStep);
160     aClassifier.Perform(aPoint, Precision::Confusion());
161     if ((aClassifier.State() == TopAbs_OUT && !theIsReverse) ||
162         (aClassifier.State() == TopAbs_IN && theIsReverse))
163       aSrcDir.Reverse();
164   } else if (theIsReverse)
165     aSrcDir.Reverse();
166   if (hasNormal[1]) {
167     aClassifier.Load(aDestShape);
168     gp_Pnt aPoint = aSrcDstPoints[1];
169     aPoint.Translate(aDstDir * aTransStep);
170     aClassifier.Perform(aPoint, Precision::Confusion());
171     if (aClassifier.State() == TopAbs_IN)
172       aDstDir.Reverse();
173   }
174   // Calculate rotation
175   gp_Quaternion aRot(aSrcDir, aDstDir);
176   aTrsf.SetRotation(aRot);
177   // Calculate translation
178   gp_Vec aSrcLoc(aSrcDstPoints[0].XYZ());
179   gp_Vec aDstLoc(aSrcDstPoints[1].XYZ());
180   if (!theIsCentering)
181     aDstLoc = aSrcLoc + gp_Vec(aDstDir) * (aDstLoc-aSrcLoc).Dot(aDstDir);
182   aSrcLoc.Transform(aTrsf);
183   gp_Vec aTrans = aDstLoc - aSrcLoc;
184   aTrsf.SetTransformation(aRot, aTrans);
185
186   // Transform the shape with copying it
187   BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true);
188   if(aBuilder) {
189     setImpl(aBuilder);
190     myDone = aBuilder->IsDone() == Standard_True;
191     if (myDone) {
192       TopoDS_Shape aResult = aBuilder->Shape();
193       // fill data map to keep correct orientation of sub-shapes 
194       for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
195         std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
196         aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
197         myMap.bind(aCurrentShape, aCurrentShape);
198       }
199 #ifdef DEB_PLACEMENT
200           int aNum = myMap.size();
201           cout << "MAP of Oriented shapes =" << aNum <<endl;
202
203 #endif
204
205       myShape->setImpl(new TopoDS_Shape(aResult));
206       myMkShape = new GeomAlgoAPI_MakeShape (aBuilder);
207     }
208   }
209 }
210
211 //============================================================================
212 const bool GeomAlgoAPI_Placement::isValid() const
213 {
214   BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
215   return (aChecker.IsValid() == Standard_True);
216 }
217
218 //============================================================================
219 const bool GeomAlgoAPI_Placement::hasVolume() const
220 {
221   bool hasVolume(false);
222   if(isValid()) {
223     const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
224     GProp_GProps aGProp;
225     BRepGProp::VolumeProperties(aRShape, aGProp);
226     if(aGProp.Mass() > Precision::Confusion()) 
227       hasVolume = true; 
228   }
229   return hasVolume;
230 }
231
232 //============================================================================
233 const std::shared_ptr<GeomAPI_Shape>& GeomAlgoAPI_Placement::shape () const 
234 {
235   return myShape;
236 }
237
238 //============================================================================
239 void GeomAlgoAPI_Placement::mapOfShapes (GeomAPI_DataMapOfShapeShape& theMap) const
240 {
241   theMap = myMap;
242 }
243
244 //============================================================================
245 GeomAlgoAPI_MakeShape * GeomAlgoAPI_Placement::makeShape() const
246 {
247   return myMkShape;
248 }
249
250 //============================================================================
251 GeomAlgoAPI_Placement::~GeomAlgoAPI_Placement()
252 {
253   if (myImpl)
254     myMap.clear();
255 }