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"
52 #include <TopExp_Explorer.hxx>
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) // {\
61 // cout << msg << " ("<< p.X() << "; " <<p.Y() << "; " <<p.Z() << ") " <<endl;\
64 typedef StdMeshers_ProjectionUtils TAssocTool;
67 //=======================================================================
68 //function : StdMeshers_Projection_3D
70 //=======================================================================
72 StdMeshers_Projection_3D::StdMeshers_Projection_3D(int hypId, int studyId, SMESH_Gen* gen)
73 :SMESH_3D_Algo(hypId, studyId, gen)
75 _name = "Projection_3D";
76 _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID); // 1 bit per shape type
78 _compatibleHypothesis.push_back("ProjectionSource3D");
82 //================================================================================
86 //================================================================================
88 StdMeshers_Projection_3D::~StdMeshers_Projection_3D()
91 //=======================================================================
92 //function : CheckHypothesis
94 //=======================================================================
96 bool StdMeshers_Projection_3D::CheckHypothesis(SMESH_Mesh& aMesh,
97 const TopoDS_Shape& aShape,
98 SMESH_Hypothesis::Hypothesis_Status& aStatus)
100 // check aShape that must be a 6 faces block
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 )
107 aStatus = HYP_BAD_GEOMETRY;
111 list <const SMESHDS_Hypothesis * >::const_iterator itl;
113 const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
114 if ( hyps.size() == 0 )
116 aStatus = SMESH_Hypothesis::HYP_MISSING;
117 return false; // can't work with no hypothesis
120 if ( hyps.size() > 1 )
122 aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
126 const SMESHDS_Hypothesis *theHyp = hyps.front();
128 string hypName = theHyp->GetName();
130 aStatus = SMESH_Hypothesis::HYP_OK;
132 if (hypName == "ProjectionSource3D")
134 _sourceHypo = static_cast<const StdMeshers_ProjectionSource3D *>(theHyp);
135 // Check hypo parameters
137 SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
138 SMESH_Mesh* tgtMesh = & aMesh;
143 if ( _sourceHypo->HasVertexAssociation() )
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() ))
152 SCRUTE((edge.IsNull()));
153 SCRUTE((TAssocTool::IsSubShape( edge, srcMesh )));
154 SCRUTE((TAssocTool::IsSubShape( edge, _sourceHypo->GetSource3DShape() )));
155 aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
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 ))
166 SCRUTE((edge.IsNull()));
167 SCRUTE((TAssocTool::IsSubShape( edge, tgtMesh )));
168 SCRUTE((TAssocTool::IsSubShape( edge, aShape )));
169 aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
173 // check a source shape
174 if ( !TAssocTool::IsSubShape( _sourceHypo->GetSource3DShape(), srcMesh ) ||
175 ( srcMesh == tgtMesh && aShape == _sourceHypo->GetSource3DShape()))
177 SCRUTE((TAssocTool::IsSubShape( _sourceHypo->GetSource3DShape(), srcMesh)));
178 SCRUTE((srcMesh == tgtMesh));
179 SCRUTE((aShape == _sourceHypo->GetSource3DShape()));
180 aStatus = SMESH_Hypothesis::HYP_BAD_PARAMETER;
185 aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
187 return ( aStatus == HYP_OK );
190 //=======================================================================
193 //=======================================================================
195 bool StdMeshers_Projection_3D::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
200 SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
201 SMESH_Mesh * tgtMesh = & aMesh;
205 SMESHDS_Mesh * srcMeshDS = srcMesh->GetMeshDS();
206 SMESHDS_Mesh * tgtMeshDS = tgtMesh->GetMeshDS();
208 // get shell from shape3D
209 TopoDS_Shell srcShell, tgtShell;
210 TopExp_Explorer exp( _sourceHypo->GetSource3DShape(), TopAbs_SHELL );
212 for ( nbShell = 0; exp.More(); exp.Next(), ++nbShell )
213 srcShell = TopoDS::Shell( exp.Current() );
215 return error(COMPERR_BAD_SHAPE,
216 SMESH_Comment("Source shape must have 1 shell but not ") << nbShell);
218 exp.Init( aShape, TopAbs_SHELL );
219 for ( nbShell = 0; exp.More(); exp.Next(), ++nbShell )
220 tgtShell = TopoDS::Shell( exp.Current() );
222 return error(COMPERR_BAD_SHAPE,
223 SMESH_Comment("Target shape must have 1 shell but not ") << nbShell);
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");
235 // Assure that mesh on a source shape is computed
237 SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( _sourceHypo->GetSource3DShape() );
238 //SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( aShape );
240 if ( tgtMesh == srcMesh && !aShape.IsSame( _sourceHypo->GetSource3DShape() )) {
241 if ( !TAssocTool::MakeComputed( srcSubMesh ))
242 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
245 if ( !srcSubMesh->IsMeshComputed() )
246 return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
249 // Find 2 pairs of corresponding vertices
251 TopoDS_Vertex tgtV000, tgtV100, srcV000, srcV100;
252 TAssocTool::TShapeShapeMap shape2ShapeMap;
254 if ( _sourceHypo->HasVertexAssociation() )
256 tgtV000 = _sourceHypo->GetTargetVertex(1);
257 tgtV100 = _sourceHypo->GetTargetVertex(2);
258 srcV000 = _sourceHypo->GetSourceVertex(1);
259 srcV100 = _sourceHypo->GetSourceVertex(2);
263 if ( !TAssocTool::FindSubShapeAssociation( tgtShell, tgtMesh, srcShell, srcMesh,
265 return error(COMPERR_BAD_SHAPE,"Topology of source and target shapes seems different" );
267 exp.Init( tgtShell, TopAbs_EDGE );
268 TopExp::Vertices( TopoDS::Edge( exp.Current() ), tgtV000, tgtV100 );
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" );
279 // Load 2 SMESH_Block's with src and tgt shells
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?");
286 if ( !srcBlock.LoadBlockShapes( srcShell, srcV000, srcV100, scrShapes ))
287 return error(COMPERR_BAD_SHAPE, "Can't detect block subshapes. Not a block?");
289 // Find matching nodes of src and tgt shells
291 TNodeNodeMap src2tgtNodeMap;
292 for ( int fId = SMESH_Block::ID_FirstF; fId < SMESH_Block::ID_Shell; ++fId )
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 ));
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] ));
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" );
320 // put found matching nodes of 2 faces to the global map
321 src2tgtNodeMap.insert( faceMatchingNodes.begin(), faceMatchingNodes.end() );
324 // ------------------
326 // ------------------
328 SMDS_VolumeTool volTool;
329 SMESH_MesherHelper helper( *tgtMesh );
330 helper.IsQuadraticSubMesh( aShape );
332 SMESHDS_SubMesh* srcSMDS = srcSubMesh->GetSubMeshDS();
333 SMDS_ElemIteratorPtr volIt = srcSMDS->GetElements();
334 while ( volIt->more() ) // loop on source volumes
336 const SMDS_MeshElement* srcVol = volIt->next();
337 if ( !srcVol || srcVol->GetType() != SMDSAbs_Volume )
339 int nbNodes = srcVol->NbNodes();
340 SMDS_VolumeTool::VolumeType volType = volTool.GetType( nbNodes );
341 if ( srcVol->IsQuadratic() )
342 nbNodes = volTool.NbCornerNodes( volType );
344 // Find or create a new tgt node for each node of a src volume
346 vector< const SMDS_MeshNode* > nodes( nbNodes );
347 for ( int i = 0; i < nbNodes; ++i )
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
354 tgtNode = sN_tN->second;
356 else // Create a new tgt node
358 // compute normalized parameters of source node in srcBlock
359 gp_Pnt srcCoord = gpXYZ( srcNode );
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
366 if ( !tgtBlock.ShellPoint( srcParam, tgtXYZ ))
367 return error("Can't compute coordinates by normalized parameters");
369 SMDS_MeshNode* newNode = tgtMeshDS->AddNode( tgtXYZ.X(), tgtXYZ.Y(), tgtXYZ.Z() );
370 tgtMeshDS->SetNodeInVolume( newNode, helper.GetSubShapeID() );
372 src2tgtNodeMap.insert( make_pair( srcNode, tgtNode ));
374 nodes[ i ] = tgtNode;
377 // Create a new volume
379 SMDS_MeshVolume * tgtVol = 0;
380 int id = 0, force3d = false;
382 case SMDS_VolumeTool::TETRA :
383 case SMDS_VolumeTool::QUAD_TETRA:
384 tgtVol = helper.AddVolume( nodes[0],
387 nodes[3], id, force3d); break;
388 case SMDS_VolumeTool::PYRAM :
389 case SMDS_VolumeTool::QUAD_PYRAM:
390 tgtVol = helper.AddVolume( nodes[0],
394 nodes[4], id, force3d); break;
395 case SMDS_VolumeTool::PENTA :
396 case SMDS_VolumeTool::QUAD_PENTA:
397 tgtVol = helper.AddVolume( nodes[0],
402 nodes[5], id, force3d); break;
403 case SMDS_VolumeTool::HEXA :
404 case SMDS_VolumeTool::QUAD_HEXA :
405 tgtVol = helper.AddVolume( nodes[0],
412 nodes[7], id, force3d); break;
413 default: // polyhedron
414 const SMDS_PolyhedralVolumeOfNodes * poly =
415 dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*>( srcVol );
417 RETURN_BAD_RESULT("Unexpected volume type");
418 tgtVol = tgtMeshDS->AddPolyhedralVolume( nodes, poly->GetQuanities() );
421 tgtMeshDS->SetMeshElementOnShape( tgtVol, helper.GetSubShapeID() );
423 } // loop on volumes of src shell
428 //=============================================================================
430 * \brief Sets a default event listener to submesh of the source shape
431 * \param subMesh - submesh where algo is set
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
438 //=============================================================================
440 void StdMeshers_Projection_3D::SetEventListener(SMESH_subMesh* subMesh)
442 TAssocTool::SetEventListener( subMesh,
443 _sourceHypo->GetSource3DShape(),
444 _sourceHypo->GetSourceMesh() );