1 // SMESH SMESH : implementaion of SMESH idl descriptions
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : StdMeshers_Projection_3D.cxx
26 // Created : Fri Oct 20 11:37:07 2006
27 // Author : Edward AGAPOV (eap)
30 #include "StdMeshers_Projection_3D.hxx"
31 #include "StdMeshers_ProjectionSource3D.hxx"
33 #include "StdMeshers_ProjectionUtils.hxx"
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"
49 #include "utilities.h"
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) // {\
55 // cout << msg << " ("<< p.X() << "; " <<p.Y() << "; " <<p.Z() << ") " <<endl;\
58 typedef StdMeshers_ProjectionUtils TAssocTool;
61 //=======================================================================
62 //function : StdMeshers_Projection_3D
64 //=======================================================================
66 StdMeshers_Projection_3D::StdMeshers_Projection_3D(int hypId, int studyId, SMESH_Gen* gen)
67 :SMESH_3D_Algo(hypId, studyId, gen)
69 _name = "Projection_3D";
70 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID); // 1 bit per shape type
72 _compatibleHypothesis.push_back("ProjectionSource3D");
76 //================================================================================
80 //================================================================================
82 StdMeshers_Projection_3D::~StdMeshers_Projection_3D()
85 //=======================================================================
86 //function : CheckHypothesis
88 //=======================================================================
90 bool StdMeshers_Projection_3D::CheckHypothesis(SMESH_Mesh& aMesh,
91 const TopoDS_Shape& aShape,
92 SMESH_Hypothesis::Hypothesis_Status& aStatus)
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 )
100 aStatus = HYP_BAD_GEOMETRY;
104 list <const SMESHDS_Hypothesis * >::const_iterator itl;
106 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
107 if ( hyps.size() == 0 )
109 aStatus = SMESH_Hypothesis::HYP_MISSING;
110 return false; // can't work with no hypothesis
113 if ( hyps.size() > 1 )
115 aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
119 const SMESHDS_Hypothesis *theHyp = hyps.front();
121 string hypName = theHyp->GetName();
123 aStatus = SMESH_Hypothesis::HYP_OK;
125 if (hypName == "ProjectionSource3D")
127 _sourceHypo = static_cast<const StdMeshers_ProjectionSource3D *>(theHyp);
128 // Check hypo parameters
130 SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
131 SMESH_Mesh* tgtMesh = & aMesh;
136 if ( _sourceHypo->HasVertexAssociation() )
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() ))
145 SCRUTE((edge.IsNull()));
146 SCRUTE((TAssocTool::IsSubShape( edge, srcMesh )));
147 SCRUTE((TAssocTool::IsSubShape( edge, _sourceHypo->GetSource3DShape() )));
148 aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
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 ))
159 SCRUTE((edge.IsNull()));
160 SCRUTE((TAssocTool::IsSubShape( edge, tgtMesh )));
161 SCRUTE((TAssocTool::IsSubShape( edge, aShape )));
162 aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
166 // check a source shape
167 if ( !TAssocTool::IsSubShape( _sourceHypo->GetSource3DShape(), srcMesh ) ||
168 ( srcMesh == tgtMesh && aShape == _sourceHypo->GetSource3DShape()))
170 SCRUTE((TAssocTool::IsSubShape( _sourceHypo->GetSource3DShape(), srcMesh)));
171 SCRUTE((srcMesh == tgtMesh));
172 SCRUTE((aShape == _sourceHypo->GetSource3DShape()));
173 aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
178 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
180 return ( aStatus == HYP_OK );
183 //=======================================================================
186 //=======================================================================
188 bool StdMeshers_Projection_3D::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
193 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
194 SMESH_Mesh * tgtMesh = & aMesh;
198 SMESHDS_Mesh * srcMeshDS = srcMesh->GetMeshDS();
199 SMESHDS_Mesh * tgtMeshDS = tgtMesh->GetMeshDS();
201 // get shell from shape3D
202 TopoDS_Shell srcShell, tgtShell;
203 TopExp_Explorer exp( _sourceHypo->GetSource3DShape(), TopAbs_SHELL );
205 for ( nbShell = 0; exp.More(); exp.Next(), ++nbShell )
206 srcShell = TopoDS::Shell( exp.Current() );
208 return error(COMPERR_BAD_SHAPE,
209 SMESH_Comment("Shape must have 1 shell but not") << nbShell);
211 exp.Init( aShape, TopAbs_SHELL );
212 for ( nbShell = 0; exp.More(); exp.Next(), ++nbShell )
213 tgtShell = TopoDS::Shell( exp.Current() );
215 return error(COMPERR_BAD_SHAPE,
216 SMESH_Comment("Shape must have 1 shell but not") << nbShell);
218 // Assure that mesh on a source shape is computed
220 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( _sourceHypo->GetSource3DShape() );
221 //SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( aShape );
223 if ( tgtMesh == srcMesh && !aShape.IsSame( _sourceHypo->GetSource3DShape() )) {
224 if ( !TAssocTool::MakeComputed( srcSubMesh ))
225 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
228 if ( !srcSubMesh->IsMeshComputed() )
229 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
232 // Find 2 pairs of corresponding vertices
234 TopoDS_Vertex tgtV000, tgtV100, srcV000, srcV100;
235 TAssocTool::TShapeShapeMap shape2ShapeMap;
237 if ( _sourceHypo->HasVertexAssociation() )
239 tgtV000 = _sourceHypo->GetTargetVertex(1);
240 tgtV100 = _sourceHypo->GetTargetVertex(2);
241 srcV000 = _sourceHypo->GetSourceVertex(1);
242 srcV100 = _sourceHypo->GetSourceVertex(2);
246 if ( !TAssocTool::FindSubShapeAssociation( tgtShell, tgtMesh, srcShell, srcMesh,
248 return error(COMPERR_BAD_SHAPE,"Topology of source and target shapes seems different" );
250 exp.Init( tgtShell, TopAbs_EDGE );
251 TopExp::Vertices( TopoDS::Edge( exp.Current() ), tgtV000, tgtV100 );
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" );
262 // Load 2 SMESH_Block's with src and tgt shells
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?");
269 if ( !srcBlock.LoadBlockShapes( srcShell, srcV000, srcV100, scrShapes ))
270 return error(COMPERR_BAD_SHAPE, "Can't detect block subshapes. Not a block?");
272 // Find matching nodes of src and tgt shells
274 TNodeNodeMap src2tgtNodeMap;
275 for ( int fId = SMESH_Block::ID_FirstF; fId < SMESH_Block::ID_Shell; ++fId )
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 ));
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] ));
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" );
303 // put found matching nodes of 2 faces to the global map
304 src2tgtNodeMap.insert( faceMatchingNodes.begin(), faceMatchingNodes.end() );
307 // ------------------
309 // ------------------
311 SMDS_VolumeTool volTool;
312 SMESH_MesherHelper helper( *tgtMesh );
313 helper.IsQuadraticSubMesh( aShape );
315 SMESHDS_SubMesh* srcSMDS = srcSubMesh->GetSubMeshDS();
316 SMDS_ElemIteratorPtr volIt = srcSMDS->GetElements();
317 while ( volIt->more() ) // loop on source volumes
319 const SMDS_MeshElement* srcVol = volIt->next();
320 if ( !srcVol || srcVol->GetType() != SMDSAbs_Volume )
322 int nbNodes = srcVol->NbNodes();
323 SMDS_VolumeTool::VolumeType volType = volTool.GetType( nbNodes );
324 if ( srcVol->IsQuadratic() )
325 nbNodes = volTool.NbCornerNodes( volType );
327 // Find or create a new tgt node for each node of a src volume
329 vector< const SMDS_MeshNode* > nodes( nbNodes );
330 for ( int i = 0; i < nbNodes; ++i )
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
337 tgtNode = sN_tN->second;
339 else // Create a new tgt node
341 // compute normalized parameters of source node in srcBlock
342 gp_Pnt srcCoord = gpXYZ( srcNode );
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
349 if ( !tgtBlock.ShellPoint( srcParam, tgtXYZ ))
350 return error(dfltErr(),"Can't compute coordinates by normalized parameters");
352 SMDS_MeshNode* newNode = tgtMeshDS->AddNode( tgtXYZ.X(), tgtXYZ.Y(), tgtXYZ.Z() );
353 tgtMeshDS->SetNodeInVolume( newNode, helper.GetSubShapeID() );
355 src2tgtNodeMap.insert( make_pair( srcNode, tgtNode ));
357 nodes[ i ] = tgtNode;
360 // Create a new volume
362 SMDS_MeshVolume * tgtVol = 0;
363 int id = 0, force3d = false;
365 case SMDS_VolumeTool::TETRA :
366 case SMDS_VolumeTool::QUAD_TETRA:
367 tgtVol = helper.AddVolume( nodes[0],
370 nodes[3], id, force3d); break;
371 case SMDS_VolumeTool::PYRAM :
372 case SMDS_VolumeTool::QUAD_PYRAM:
373 tgtVol = helper.AddVolume( nodes[0],
377 nodes[4], id, force3d); break;
378 case SMDS_VolumeTool::PENTA :
379 case SMDS_VolumeTool::QUAD_PENTA:
380 tgtVol = helper.AddVolume( nodes[0],
385 nodes[5], id, force3d); break;
386 case SMDS_VolumeTool::HEXA :
387 case SMDS_VolumeTool::QUAD_HEXA :
388 tgtVol = helper.AddVolume( nodes[0],
395 nodes[7], id, force3d); break;
396 default: // polyhedron
397 const SMDS_PolyhedralVolumeOfNodes * poly =
398 dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*>( srcVol );
400 RETURN_BAD_RESULT("Unexpected volume type");
401 tgtVol = tgtMeshDS->AddPolyhedralVolume( nodes, poly->GetQuanities() );
404 tgtMeshDS->SetMeshElementOnShape( tgtVol, helper.GetSubShapeID() );
406 } // loop on volumes of src shell
411 //=============================================================================
413 * \brief Sets a default event listener to submesh of the source shape
414 * \param subMesh - submesh where algo is set
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
421 //=============================================================================
423 void StdMeshers_Projection_3D::SetEventListener(SMESH_subMesh* subMesh)
425 TAssocTool::SetEventListener( subMesh,
426 _sourceHypo->GetSource3DShape(),
427 _sourceHypo->GetSourceMesh() );