Salome HOME
Fix compilation problems on Mandriva64 and Mandriva
[modules/smesh.git] / src / StdMeshers / StdMeshers_Projection_3D.cxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //
23 //
24 // File      : StdMeshers_Projection_3D.cxx
25 // Module    : SMESH
26 // Created   : Fri Oct 20 11:37:07 2006
27 // Author    : Edward AGAPOV (eap)
28
29
30 #include "StdMeshers_Projection_3D.hxx"
31 #include "StdMeshers_ProjectionSource3D.hxx"
32
33 #include "StdMeshers_ProjectionUtils.hxx"
34
35 #include "SMESHDS_Hypothesis.hxx"
36 #include "SMESHDS_SubMesh.hxx"
37 #include "SMESH_Block.hxx"
38 #include "SMESH_Gen.hxx"
39 #include "SMESH_Mesh.hxx"
40 #include "SMESH_MeshEditor.hxx"
41 #include "SMESH_Pattern.hxx"
42 #include "SMESH_subMesh.hxx"
43 #include "SMESH_subMeshEventListener.hxx"
44 #include "SMESH_MesherHelper.hxx"
45 #include "SMESH_Comment.hxx"
46 #include "SMDS_VolumeTool.hxx"
47 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
48
49 #include "utilities.h"
50
51 #define RETURN_BAD_RESULT(msg) { MESSAGE(msg); return false; }
52 #define gpXYZ(n) gp_XYZ(n->X(),n->Y(),n->Z())
53 #define SHOWYXZ(msg, xyz) // {\
54 // gp_Pnt p (xyz); \
55 // cout << msg << " ("<< p.X() << "; " <<p.Y() << "; " <<p.Z() << ") " <<endl;\
56 // }
57
58 typedef StdMeshers_ProjectionUtils TAssocTool;
59
60
61 //=======================================================================
62 //function : StdMeshers_Projection_3D
63 //purpose  : 
64 //=======================================================================
65
66 StdMeshers_Projection_3D::StdMeshers_Projection_3D(int hypId, int studyId, SMESH_Gen* gen)
67   :SMESH_3D_Algo(hypId, studyId, gen)
68 {
69   _name = "Projection_3D";
70   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);       // 1 bit per shape type
71
72   _compatibleHypothesis.push_back("ProjectionSource3D");
73   _sourceHypo = 0;
74 }
75
76 //================================================================================
77 /*!
78  * \brief Destructor
79  */
80 //================================================================================
81
82 StdMeshers_Projection_3D::~StdMeshers_Projection_3D()
83 {}
84
85 //=======================================================================
86 //function : CheckHypothesis
87 //purpose  : 
88 //=======================================================================
89
90 bool StdMeshers_Projection_3D::CheckHypothesis(SMESH_Mesh&                          aMesh,
91                                                const TopoDS_Shape&                  aShape,
92                                                SMESH_Hypothesis::Hypothesis_Status& aStatus)
93 {
94   // check aShape that must be a 6 faces block
95   if ( TAssocTool::Count( aShape, TopAbs_SHELL, 1 ) != 1 ||
96        TAssocTool::Count( aShape, TopAbs_FACE , 1 ) != 6 ||
97        TAssocTool::Count( aShape, TopAbs_EDGE , 1 ) != 12 ||
98        TAssocTool::Count( aShape, TopAbs_WIRE , 1 ) != 6 )
99   {
100     aStatus = HYP_BAD_GEOMETRY;
101     return false;
102   }
103
104   list <const SMESHDS_Hypothesis * >::const_iterator itl;
105
106   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
107   if ( hyps.size() == 0 )
108   {
109     aStatus = SMESH_Hypothesis::HYP_MISSING;
110     return false;  // can't work with no hypothesis
111   }
112
113   if ( hyps.size() > 1 )
114   {
115     aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
116     return false;
117   }
118
119   const SMESHDS_Hypothesis *theHyp = hyps.front();
120
121   string hypName = theHyp->GetName();
122
123   aStatus = SMESH_Hypothesis::HYP_OK;
124
125   if (hypName == "ProjectionSource3D")
126   {
127     _sourceHypo = static_cast<const StdMeshers_ProjectionSource3D *>(theHyp);
128     // Check hypo parameters
129
130     SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
131     SMESH_Mesh* tgtMesh = & aMesh;
132     if ( !srcMesh )
133       srcMesh = tgtMesh;
134
135     // check vertices
136     if ( _sourceHypo->HasVertexAssociation() )
137     {
138       // source vertices
139       TopoDS_Shape edge = TAssocTool::GetEdgeByVertices
140         ( srcMesh, _sourceHypo->GetSourceVertex(1), _sourceHypo->GetSourceVertex(2) );
141       if ( edge.IsNull() ||
142            !TAssocTool::IsSubShape( edge, srcMesh ) ||
143            !TAssocTool::IsSubShape( edge, _sourceHypo->GetSource3DShape() ))
144       {
145         SCRUTE((edge.IsNull()));
146         SCRUTE((TAssocTool::IsSubShape( edge, srcMesh )));
147         SCRUTE((TAssocTool::IsSubShape( edge, _sourceHypo->GetSource3DShape() )));
148         aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
149       }
150       else
151       {
152         // target vertices
153         edge = TAssocTool::GetEdgeByVertices
154           ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
155         if ( edge.IsNull() ||
156              !TAssocTool::IsSubShape( edge, tgtMesh ) ||
157              !TAssocTool::IsSubShape( edge, aShape ))
158         {
159           SCRUTE((edge.IsNull()));
160           SCRUTE((TAssocTool::IsSubShape( edge, tgtMesh )));
161           SCRUTE((TAssocTool::IsSubShape( edge, aShape )));
162           aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
163         }
164       }
165     }
166     // check a source shape
167     if ( !TAssocTool::IsSubShape( _sourceHypo->GetSource3DShape(), srcMesh ) ||
168          ( srcMesh == tgtMesh && aShape == _sourceHypo->GetSource3DShape()))
169     {
170       SCRUTE((TAssocTool::IsSubShape( _sourceHypo->GetSource3DShape(), srcMesh)));
171       SCRUTE((srcMesh == tgtMesh));
172       SCRUTE((aShape == _sourceHypo->GetSource3DShape()));
173       aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
174     }
175   }
176   else
177   {
178     aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
179   }
180   return ( aStatus == HYP_OK );
181 }
182
183 //=======================================================================
184 //function : Compute
185 //purpose  : 
186 //=======================================================================
187
188 bool StdMeshers_Projection_3D::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
189 {
190   if ( !_sourceHypo )
191     return false;
192
193   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
194   SMESH_Mesh * tgtMesh = & aMesh;
195   if ( !srcMesh )
196     srcMesh = tgtMesh;
197
198   SMESHDS_Mesh * srcMeshDS = srcMesh->GetMeshDS();
199   SMESHDS_Mesh * tgtMeshDS = tgtMesh->GetMeshDS();
200
201   // get shell from shape3D
202   TopoDS_Shell srcShell, tgtShell;
203   TopExp_Explorer exp( _sourceHypo->GetSource3DShape(), TopAbs_SHELL );
204   int nbShell;
205   for ( nbShell = 0; exp.More(); exp.Next(), ++nbShell )
206     srcShell = TopoDS::Shell( exp.Current() );
207   if ( nbShell != 1 )
208     return error(COMPERR_BAD_SHAPE,
209                  SMESH_Comment("Shape must have 1 shell but not") << nbShell);
210
211   exp.Init( aShape, TopAbs_SHELL );
212   for ( nbShell = 0; exp.More(); exp.Next(), ++nbShell )
213     tgtShell = TopoDS::Shell( exp.Current() );
214   if ( nbShell != 1 )
215     return error(COMPERR_BAD_SHAPE,
216                  SMESH_Comment("Shape must have 1 shell but not") << nbShell);
217
218   // Assure that mesh on a source shape is computed
219
220   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( _sourceHypo->GetSource3DShape() );
221   //SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( aShape );
222
223   if ( tgtMesh == srcMesh && !aShape.IsSame( _sourceHypo->GetSource3DShape() )) {
224     if ( !TAssocTool::MakeComputed( srcSubMesh ))
225       return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
226   }
227   else {
228     if ( !srcSubMesh->IsMeshComputed() )
229       return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
230   }
231
232   // Find 2 pairs of corresponding vertices
233
234   TopoDS_Vertex tgtV000, tgtV100, srcV000, srcV100;
235   TAssocTool::TShapeShapeMap shape2ShapeMap;
236
237   if ( _sourceHypo->HasVertexAssociation() )
238   {
239     tgtV000 = _sourceHypo->GetTargetVertex(1);
240     tgtV100 = _sourceHypo->GetTargetVertex(2);
241     srcV000 = _sourceHypo->GetSourceVertex(1);
242     srcV100 = _sourceHypo->GetSourceVertex(2);
243   }
244   else
245   {
246     if ( !TAssocTool::FindSubShapeAssociation( tgtShell, tgtMesh, srcShell, srcMesh,
247                                                shape2ShapeMap) )
248       return error(COMPERR_BAD_SHAPE,"Topology of source and target shapes seems different" );
249
250     exp.Init( tgtShell, TopAbs_EDGE );
251     TopExp::Vertices( TopoDS::Edge( exp.Current() ), tgtV000, tgtV100 );
252
253     if ( !shape2ShapeMap.IsBound( tgtV000 ) || !shape2ShapeMap.IsBound( tgtV100 ))
254       return error(dfltErr(),"Association of subshapes failed" );
255     srcV000 = TopoDS::Vertex( shape2ShapeMap( tgtV000 ));
256     srcV100 = TopoDS::Vertex( shape2ShapeMap( tgtV100 ));
257     if ( !TAssocTool::IsSubShape( srcV000, srcShell ) ||
258          !TAssocTool::IsSubShape( srcV100, srcShell ))
259       return error(dfltErr(),"Incorrect association of subshapes" );
260   }
261
262   // Load 2 SMESH_Block's with src and tgt shells
263
264   SMESH_Block srcBlock, tgtBlock;
265   TopTools_IndexedMapOfOrientedShape scrShapes, tgtShapes;
266   if ( !tgtBlock.LoadBlockShapes( tgtShell, tgtV000, tgtV100, tgtShapes ))
267     return error(COMPERR_BAD_SHAPE, "Can't detect block subshapes. Not a block?");
268
269   if ( !srcBlock.LoadBlockShapes( srcShell, srcV000, srcV100, scrShapes ))
270     return error(COMPERR_BAD_SHAPE, "Can't detect block subshapes. Not a block?");
271
272   // Find matching nodes of src and tgt shells
273
274   TNodeNodeMap src2tgtNodeMap;
275   for ( int fId = SMESH_Block::ID_FirstF; fId < SMESH_Block::ID_Shell; ++fId )
276   {
277     // Corresponding subshapes
278     TopoDS_Face srcFace = TopoDS::Face( scrShapes( fId ));
279     TopoDS_Face tgtFace = TopoDS::Face( tgtShapes( fId ));
280     if ( _sourceHypo->HasVertexAssociation() ) { // associate face subshapes
281       shape2ShapeMap.Clear();
282       vector< int > edgeIdVec;
283       SMESH_Block::GetFaceEdgesIDs( fId, edgeIdVec );
284       for ( int i = 0; i < edgeIdVec.size(); ++i ) {
285         int eID = edgeIdVec[ i ];
286         shape2ShapeMap.Bind( tgtShapes( eID ), scrShapes( eID ));
287         if ( i < 2 ) {
288           vector< int > vertexIdVec;
289           SMESH_Block::GetEdgeVertexIDs( eID, vertexIdVec );
290           shape2ShapeMap.Bind( tgtShapes( vertexIdVec[0] ), scrShapes( vertexIdVec[0] ));
291           shape2ShapeMap.Bind( tgtShapes( vertexIdVec[1] ), scrShapes( vertexIdVec[1] ));
292         }
293       }
294     }
295     // Find matching nodes of tgt and src faces
296     TNodeNodeMap faceMatchingNodes;
297     if ( ! TAssocTool::FindMatchingNodesOnFaces( srcFace, srcMesh, tgtFace, tgtMesh, 
298                                                  shape2ShapeMap, faceMatchingNodes ))
299     return error(COMPERR_BAD_INPUT_MESH,SMESH_Comment("Mesh on faces #")
300                  << srcMeshDS->ShapeToIndex( srcFace ) << " and "
301                  << tgtMeshDS->ShapeToIndex( tgtFace ) << " seems different" );
302
303     // put found matching nodes of 2 faces to the global map
304     src2tgtNodeMap.insert( faceMatchingNodes.begin(), faceMatchingNodes.end() );
305   }
306
307   // ------------------
308   // Make mesh
309   // ------------------
310
311   SMDS_VolumeTool volTool;
312   SMESH_MesherHelper helper( *tgtMesh );
313   helper.IsQuadraticSubMesh( aShape );
314
315   SMESHDS_SubMesh* srcSMDS = srcSubMesh->GetSubMeshDS();
316   SMDS_ElemIteratorPtr volIt = srcSMDS->GetElements();
317   while ( volIt->more() ) // loop on source volumes
318   {
319     const SMDS_MeshElement* srcVol = volIt->next();
320     if ( !srcVol || srcVol->GetType() != SMDSAbs_Volume )
321         continue;
322     int nbNodes = srcVol->NbNodes();
323     SMDS_VolumeTool::VolumeType  volType = volTool.GetType( nbNodes );
324     if ( srcVol->IsQuadratic() )
325       nbNodes = volTool.NbCornerNodes( volType );
326
327     // Find or create a new tgt node for each node of a src volume
328
329     vector< const SMDS_MeshNode* > nodes( nbNodes );
330     for ( int i = 0; i < nbNodes; ++i )
331     {
332       const SMDS_MeshNode* srcNode = srcVol->GetNode( i );
333       const SMDS_MeshNode* tgtNode = 0;
334       TNodeNodeMap::iterator sN_tN = src2tgtNodeMap.find( srcNode );
335       if ( sN_tN != src2tgtNodeMap.end() ) // found
336       {
337         tgtNode = sN_tN->second;
338       }
339       else // Create a new tgt node
340       {
341         // compute normalized parameters of source node in srcBlock
342         gp_Pnt srcCoord = gpXYZ( srcNode );
343         gp_XYZ srcParam;
344         if ( !srcBlock.ComputeParameters( srcCoord, srcParam ))
345           return error(dfltErr(),SMESH_Comment("Can't compute normalized parameters ")
346                        << "for source node " << srcNode->GetID());
347         // compute coordinates of target node by srcParam
348         gp_XYZ tgtXYZ;
349         if ( !tgtBlock.ShellPoint( srcParam, tgtXYZ ))
350           return error(dfltErr(),"Can't compute coordinates by normalized parameters");
351         // add node
352         SMDS_MeshNode* newNode = tgtMeshDS->AddNode( tgtXYZ.X(), tgtXYZ.Y(), tgtXYZ.Z() );
353         tgtMeshDS->SetNodeInVolume( newNode, helper.GetSubShapeID() );
354         tgtNode = newNode;
355         src2tgtNodeMap.insert( make_pair( srcNode, tgtNode ));
356       }
357       nodes[ i ] = tgtNode;
358     }
359
360     // Create a new volume
361
362     SMDS_MeshVolume * tgtVol = 0;
363     int id = 0, force3d = false;
364     switch ( volType ) {
365     case SMDS_VolumeTool::TETRA     :
366     case SMDS_VolumeTool::QUAD_TETRA:
367       tgtVol = helper.AddVolume( nodes[0],
368                                  nodes[1],
369                                  nodes[2],
370                                  nodes[3], id, force3d); break;
371     case SMDS_VolumeTool::PYRAM     :
372     case SMDS_VolumeTool::QUAD_PYRAM:
373       tgtVol = helper.AddVolume( nodes[0],
374                                  nodes[1],
375                                  nodes[2],
376                                  nodes[3],
377                                  nodes[4], id, force3d); break;
378     case SMDS_VolumeTool::PENTA     :
379     case SMDS_VolumeTool::QUAD_PENTA:
380       tgtVol = helper.AddVolume( nodes[0],
381                                  nodes[1],
382                                  nodes[2],
383                                  nodes[3],
384                                  nodes[4],
385                                  nodes[5], id, force3d); break;
386     case SMDS_VolumeTool::HEXA      :
387     case SMDS_VolumeTool::QUAD_HEXA :
388       tgtVol = helper.AddVolume( nodes[0],
389                                  nodes[1],
390                                  nodes[2],
391                                  nodes[3],
392                                  nodes[4],
393                                  nodes[5],
394                                  nodes[6],
395                                  nodes[7], id, force3d); break;
396     default: // polyhedron
397       const SMDS_PolyhedralVolumeOfNodes * poly =
398         dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*>( srcVol );
399       if ( !poly )
400         RETURN_BAD_RESULT("Unexpected volume type");
401       tgtVol = tgtMeshDS->AddPolyhedralVolume( nodes, poly->GetQuanities() );
402     }
403     if ( tgtVol ) {
404       tgtMeshDS->SetMeshElementOnShape( tgtVol, helper.GetSubShapeID() );
405     }
406   } // loop on volumes of src shell
407
408   return true;
409 }
410
411 //=============================================================================
412 /*!
413  * \brief Sets a default event listener to submesh of the source shape
414   * \param subMesh - submesh where algo is set
415  *
416  * This method is called when a submesh gets HYP_OK algo_state.
417  * After being set, event listener is notified on each event of a submesh.
418  * Arranges that CLEAN event is translated from source submesh to
419  * the submesh
420  */
421 //=============================================================================
422
423 void StdMeshers_Projection_3D::SetEventListener(SMESH_subMesh* subMesh)
424 {
425   TAssocTool::SetEventListener( subMesh,
426                                 _sourceHypo->GetSource3DShape(),
427                                 _sourceHypo->GetSourceMesh() );
428 }
429