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