Salome HOME
Merge from V6_main 01/04/2013
[modules/smesh.git] / src / StdMeshers / StdMeshers_Projection_1D2D.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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 // File      : StdMeshers_Projection_1D2D.cxx
23 // Module    : SMESH
24 // Author    : Edward AGAPOV (eap)
25 //
26 #include "StdMeshers_Projection_1D2D.hxx"
27
28 #include "SMESH_Gen.hxx"
29 #include "SMESH_MesherHelper.hxx"
30 #include "SMESH_subMesh.hxx"
31 #include "SMESH_subMeshEventListener.hxx"
32 #include "StdMeshers_FaceSide.hxx"
33 #include "StdMeshers_ProjectionSource2D.hxx"
34 #include "StdMeshers_ProjectionUtils.hxx"
35
36 #include <TopoDS.hxx>
37 #include <TopExp_Explorer.hxx>
38
39 using namespace std;
40
41 namespace
42 {
43   // --------------------------------------------------------------------------------
44   /*!
45    * \brief an event listener updating submehses of EDGEs according to
46    *        events on the target FACE submesh
47    */
48   struct EventProparatorToEdges : public SMESH_subMeshEventListener
49   {
50     EventProparatorToEdges(): SMESH_subMeshEventListener(/*isDeletable=*/false,
51                                                          "Projection_1D2D::EventProparatorToEdges")
52     {}
53     static EventProparatorToEdges* Instance() { static EventProparatorToEdges E; return &E; }
54
55     static void Set(SMESH_subMesh* faceSubMesh)
56     {
57       SMESH_subMeshEventListenerData* edgeSubMeshes =
58         new SMESH_subMeshEventListenerData(/*isDeletable=*/true);
59       SMESH_Mesh* mesh = faceSubMesh->GetFather();
60       TopExp_Explorer eExp( faceSubMesh->GetSubShape(), TopAbs_EDGE );
61       for ( ; eExp.More(); eExp.Next() )
62         edgeSubMeshes->mySubMeshes.push_back( mesh->GetSubMesh( eExp.Current() ));
63
64       // set a listener
65       faceSubMesh->SetEventListener( Instance(), edgeSubMeshes, faceSubMesh );
66     }
67   };
68   // --------------------------------------------------------------------------------
69   /*!
70    * \brief Structure used to temporary remove EventProparatorToEdges from faceSubMesh
71    *  in order to prevent propagation of CLEAN event from FACE to EDGEs during 
72    *  StdMeshers_Projection_1D2D::Compute(). The CLEAN event is emmited by Pattern mapper
73    *  and causes removal of faces generated on adjacent FACEs.
74    */
75   struct UnsetterOfEventProparatorToEdges
76   {
77     SMESH_subMesh* _faceSubMesh;
78     UnsetterOfEventProparatorToEdges( SMESH_subMesh* faceSubMesh ):_faceSubMesh(faceSubMesh)
79     {
80       faceSubMesh->DeleteEventListener( EventProparatorToEdges::Instance() );
81     }
82     ~UnsetterOfEventProparatorToEdges()
83     {
84       EventProparatorToEdges::Set(_faceSubMesh);
85     }
86   };
87 }
88
89 //=======================================================================
90 //function : StdMeshers_Projection_1D2D
91 //purpose  : 
92 //=======================================================================
93
94 StdMeshers_Projection_1D2D::StdMeshers_Projection_1D2D(int hypId, int studyId, SMESH_Gen* gen)
95   :StdMeshers_Projection_2D(hypId, studyId, gen)
96 {
97   _name = "Projection_1D2D";
98   _requireDiscreteBoundary = false;
99   _supportSubmeshes = true;
100 }
101
102 //=======================================================================
103 //function : Compute
104 //purpose  : 
105 //=======================================================================
106
107 bool StdMeshers_Projection_1D2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
108 {
109   UnsetterOfEventProparatorToEdges eventBarrier( theMesh.GetSubMesh( theShape ));
110
111   // 1) Project faces
112
113   if ( !StdMeshers_Projection_2D::Compute(theMesh, theShape))
114     return false;
115
116   // 2) Create segments
117
118   SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
119
120   SMESHDS_SubMesh * faceSubMesh = meshDS->MeshElements( theShape );
121   if ( !faceSubMesh || faceSubMesh->NbElements() == 0 ) return false;
122   _quadraticMesh = faceSubMesh->GetElements()->next()->IsQuadratic();
123
124   SMESH_MesherHelper helper( theMesh );
125   helper.SetSubShape( theShape );
126
127   TopoDS_Face F = TopoDS::Face( theShape );
128   TError err;
129   TSideVector wires = StdMeshers_FaceSide::GetFaceWires( F, theMesh,
130                                                          /*ignoreMediumNodes=*/false, err);
131   if ( err && !err->IsOK() )
132     return error( err );
133
134   for ( size_t iWire = 0; iWire < wires.size(); ++iWire )
135   {
136     vector<const SMDS_MeshNode*> nodes = wires[ iWire ]->GetOrderedNodes();
137     if ( nodes.empty() )
138       return error("Wrong nodes on a wire");
139
140     // check that all nodes are shared by faces generated on F
141     for ( size_t i = 0; i < nodes.size(); ++i )
142     {
143       SMDS_ElemIteratorPtr fIt = nodes[i]->GetInverseElementIterator(SMDSAbs_Face);
144       bool faceFound = false;
145       while ( !faceFound && fIt->more() )
146         faceFound = ( helper.GetSubShapeID() == fIt->next()->getshapeId() );
147       if ( !faceFound )
148         return error("The existing 1D mesh mismatches the generated 2D mesh");
149     }
150
151     const bool checkExisting = ( wires[ iWire ]->NbSegments() || helper.HasSeam() );
152
153     if ( _quadraticMesh )
154     {
155       for ( size_t i = 2; i < nodes.size(); i += 2 )
156       {
157         if ( checkExisting && meshDS->FindEdge( nodes[i-2], nodes[i], nodes[i-1]))
158           continue;
159         SMDS_MeshElement* e = meshDS->AddEdge( nodes[i-2], nodes[i], nodes[i-1] );
160         meshDS->SetMeshElementOnShape( e, nodes[i-1]->getshapeId() );
161       }
162     }
163     else
164     {
165       int edgeID = meshDS->ShapeToIndex( wires[ iWire ]->Edge(0) );
166       for ( size_t i = 1; i < nodes.size(); ++i )
167       {
168         if ( checkExisting && meshDS->FindEdge( nodes[i-1], nodes[i]))
169           continue;
170         SMDS_MeshElement* e = meshDS->AddEdge( nodes[i-1], nodes[i] );
171         if ( nodes[i-1]->getshapeId() != edgeID &&
172              nodes[i  ]->getshapeId() != edgeID )
173         {
174           edgeID = helper.GetMediumPos( nodes[i-1], nodes[i] ).first;
175           if ( edgeID < 1 ) edgeID = helper.GetSubShapeID();
176         }
177         meshDS->SetMeshElementOnShape( e, edgeID );
178       }
179     }
180   }
181
182   return true;
183 }
184
185 //=======================================================================
186 //function : Evaluate
187 //purpose  : 
188 //=======================================================================
189
190 bool StdMeshers_Projection_1D2D::Evaluate(SMESH_Mesh&         theMesh,
191                                           const TopoDS_Shape& theShape,
192                                           MapShapeNbElems&    aResMap)
193 {
194   if ( !StdMeshers_Projection_2D::Evaluate(theMesh,theShape,aResMap))
195     return false;
196
197   TopoDS_Shape srcFace = _sourceHypo->GetSourceFace();
198   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
199   if ( !srcMesh ) srcMesh = & theMesh;
200   SMESH_subMesh* srcFaceSM = srcMesh->GetSubMesh( srcFace );
201
202   namespace SPU = StdMeshers_ProjectionUtils;
203   SPU::TShapeShapeMap shape2ShapeMap;
204   SPU::InitVertexAssociation( _sourceHypo, shape2ShapeMap );
205   if ( !SPU::FindSubShapeAssociation( theShape, &theMesh, srcFace, srcMesh, shape2ShapeMap))
206     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
207
208   MapShapeNbElems srcResMap;
209   if ( !srcFaceSM->IsMeshComputed() )
210     _gen->Evaluate( *srcMesh, srcFace, srcResMap);
211
212   SMESH_subMeshIteratorPtr smIt = srcFaceSM->getDependsOnIterator(/*includeSelf=*/false,
213                                                                   /*complexShapeFirst=*/true);
214   while ( smIt->more() )
215   {
216     SMESH_subMesh* srcSM = smIt->next();
217     TopAbs_ShapeEnum shapeType = srcSM->GetSubShape().ShapeType();
218     if ( shapeType == TopAbs_EDGE )
219     {
220       std::vector<int> aVec;
221       SMESHDS_SubMesh* srcSubMeshDS = srcSM->GetSubMeshDS();
222       if ( srcSubMeshDS && srcSubMeshDS->NbElements() )
223       {
224         aVec.resize(SMDSEntity_Last, 0);
225         SMDS_ElemIteratorPtr eIt = srcSubMeshDS->GetElements();
226         _quadraticMesh = ( eIt->more() && eIt->next()->IsQuadratic() );
227
228         aVec[SMDSEntity_Node] = srcSubMeshDS->NbNodes();
229         aVec[_quadraticMesh ? SMDSEntity_Quad_Edge : SMDSEntity_Edge] = srcSubMeshDS->NbElements();
230       }
231       else
232       {
233         if ( srcResMap.empty() )
234           if ( !_gen->Evaluate( *srcMesh, srcSM->GetSubShape(), srcResMap ))
235             return error(COMPERR_BAD_INPUT_MESH,"Source mesh not evaluatable");
236         aVec = srcResMap[ srcSM ];
237         if ( aVec.empty() )
238           return error(COMPERR_BAD_INPUT_MESH,"Source mesh is wrongly evaluated");
239       }
240       TopoDS_Shape tgtEdge = shape2ShapeMap( srcSM->GetSubShape(), /*isSrc=*/true  );
241       SMESH_subMesh* tgtSM = theMesh.GetSubMesh( tgtEdge );
242       aResMap.insert(std::make_pair(tgtSM,aVec));
243     }
244     if ( shapeType == TopAbs_VERTEX ) break;
245   }
246
247   return true;
248 }
249
250 //=======================================================================
251 //function : SetEventListener
252 //purpose  : Sets a default event listener to submesh of the source face.
253 //           faceSubMesh - submesh where algo is set
254 // After being set, event listener is notified on each event of a submesh.
255 // This method is called when a submesh gets HYP_OK algo_state.
256 // Arranges that CLEAN event is translated from source submesh to
257 // the faceSubMesh submesh.
258 //=======================================================================
259
260 void StdMeshers_Projection_1D2D::SetEventListener(SMESH_subMesh* faceSubMesh)
261 {
262   // set a listener of events on a source submesh
263   StdMeshers_Projection_2D::SetEventListener(faceSubMesh);
264
265   // set a listener to the target FACE submesh in order to update submehses
266   // of EDGEs according to events on the target FACE submesh
267   EventProparatorToEdges::Set( faceSubMesh );
268 }
269