1 // File : FrontTrack_NodesOnGeom.cxx
2 // Created : Tue Apr 25 20:48:23 2017
3 // Author : Edward AGAPOV (eap)
5 #include "FrontTrack_NodesOnGeom.hxx"
6 #include "FrontTrack_Utils.hxx"
8 #include <MEDCouplingMemArray.hxx>
18 * \brief Close a file at destruction
24 FileCloser( FILE * file ): _file( file ) {}
25 ~FileCloser() { if ( _file ) ::fclose( _file ); }
29 //================================================================================
31 * \brief Read node ids from a file and find shapes for projection
32 * \param [in] theNodeFile - a name of file holding IDs of nodes that
33 * will be moved onto geometry
34 * \param [in] theXaoGroups - a tool returning FT_Projector's by XAO group name
35 * \param [inout] theNodeCoords - array of node coordinates
36 * \param [in] theAllProjectorsByDim - all projectors of 2 dimensions, ordered so that
37 * a vector index corresponds to a XAO sub-shape ID
39 //================================================================================
41 void FT_NodesOnGeom::read( const std::string& theNodeFile,
42 const FT_Utils::XaoGroups& theXaoGroups,
43 MEDCoupling::DataArrayDouble* theNodeCoords,
44 std::vector< FT_Projector > * theAllProjectorsByDim )
46 _nodeCoords = theNodeCoords;
48 FILE * file = ::fopen( theNodeFile.c_str(), "r" );
50 throw std::invalid_argument( "Can't open an input node file: " + theNodeFile );
52 FileCloser fileCloser( file );
54 // -------------------------------------
55 // get shape dimension by the file name
56 // -------------------------------------
58 // hope the file name is something like "fr2D.**"
59 int dimPos = theNodeFile.size() - 5;
60 if ( theNodeFile[ dimPos ] == '2' )
62 else if ( theNodeFile[ dimPos ] == '1' )
65 throw std::invalid_argument( "Can't define dimension by node file name " + theNodeFile );
67 // -------------------------------------
68 // read geom group names; several lines
69 // -------------------------------------
71 std::vector< std::string > geomNames;
73 const int maxLineLen = 256;
74 char line[ maxLineLen ];
76 long int pos = ::ftell( file );
77 while ( ::fgets( line, maxLineLen, file )) // read a line
80 return; // no nodes in the file
82 // check if the line describes node ids in format 3I10 (e.g. " 120 1 43\n")
83 size_t lineLen = strlen( line );
85 ::isdigit( line[9] ) &&
87 ::isdigit( line[19] ) &&
89 ::isdigit( line[29] ) &&
90 ::isspace( line[30] ))
93 geomNames.push_back( line + 1 ); // skip the 1st white space
95 pos = ::ftell( file ); // remember the position to return if the next line holds node ids
98 ::fseek( file, pos, SEEK_SET ); // return to the 1st line holding nodes ids
105 FT_NodeToMove nodeIds;
106 std::vector< int > ids;
108 const int nbNodes = theNodeCoords->getNumberOfTuples(); // to check validity of node IDs
110 while ( ::fgets( line, maxLineLen, file )) // read a line
112 // find node ids in the line
114 char *beg = line, *end = 0;
118 while (( id = ::strtol( beg, &end, 10 )) &&
123 throw std::invalid_argument( "Too large node ID: " + FT_Utils::toStr( id ));
127 if ( ids.size() >= 3 )
129 std::vector< int >::iterator i = ids.begin();
130 nodeIds._nodeToMove = *i;
131 nodeIds._neighborNodes.assign( ++i, ids.end() );
133 _nodes.push_back( nodeIds );
140 // -----------------------------------------------------------------
141 // try to find FT_Projector's to boundary sub-shapes by group names
142 // -----------------------------------------------------------------
144 _allProjectors = & theAllProjectorsByDim[ _shapeDim - 1 ];
146 _projectors.reserve( geomNames.size() );
147 std::vector< const FT_Projector* > projectors;
149 for ( size_t i = 0; i < geomNames.size(); ++i )
151 std::string & groupName = geomNames[i];
153 // remove trailing white spaces
154 for ( int iC = groupName.size() - 1; iC >= 0; --iC )
156 if ( ::isspace( groupName[iC] ) )
157 groupName.resize( iC );
161 if ( groupName.empty() )
164 _groupNames.push_back( groupName ); // keep _groupNames for easier debug :)
166 // get projectors by group name
167 theXaoGroups.getProjectors( groupName, _shapeDim,
168 theAllProjectorsByDim[ _shapeDim-1 ], projectors );
171 // ------------------------------
172 // check the found FT_Projector's
173 // ------------------------------
175 if ( projectors.size() == 1 )
177 _projectors.push_back( *projectors[ 0 ]);
182 for ( size_t i = 0; i < _nodes.size(); ++i )
183 nodesBox.Add( getPoint( _nodes[i]._nodeToMove ));
185 if ( projectors.size() > 1 )
187 // more than one boundary shape;
188 // try to filter off unnecessary projectors using a bounding box of nodes
189 for ( size_t i = 0; i < projectors.size(); ++i )
190 if ( !nodesBox.IsOut( projectors[ i ]->getBoundingBox() ))
191 _projectors.push_back( *projectors[ i ]);
194 if ( _projectors.empty() )
196 // select projectors using a bounding box of nodes
197 std::vector< FT_Projector > & allProjectors = *_allProjectors;
198 for ( size_t i = 0; i < allProjectors.size(); ++i )
199 if ( !nodesBox.IsOut( allProjectors[ i ].getBoundingBox() ))
200 _projectors.push_back( allProjectors[ i ]);
202 if ( _projectors.empty() && !_nodes.empty() )
203 throw std::runtime_error("No boundary shape found for nodes in file " + theNodeFile );
207 // prepare for projection - create real projectors
208 for ( size_t i = 0; i < _projectors.size(); ++i )
209 _projectors[ i ].prepareForProjection();
213 //================================================================================
215 * \brief Project nodes to the shapes and move them to new positions
217 //================================================================================
219 void FT_NodesOnGeom::projectAndMove()
223 // check if all the shapes are planar
224 bool isAllPlanar = true;
225 for ( size_t i = 0; i < _projectors.size() && isAllPlanar; ++i )
226 isAllPlanar = _projectors[i].isPlanarBoundary();
230 // set nodes in the order suitable for optimal projection
233 // project and move nodes
235 std::vector< FT_NodeToMove* > notProjectedNodes;
236 size_t iP, iProjector;
239 if ( _projectors.size() > 1 )
241 // the nodes are to be projected onto several boundary shapes;
242 // in addition to the projecting, classification on a shape is necessary
243 // in order to find out on which of the shapes a node is to be projected
246 for ( size_t i = 0; i < _nodesOrder.size(); ++i )
248 FT_NodeToMove& nn = _nodes[ _nodesOrder[ i ]];
249 gp_Pnt xyz = getPoint( nn._nodeToMove );
250 gp_Pnt xyz1 = getPoint( nn._neighborNodes[0] );
251 gp_Pnt xyz2 = getPoint( nn._neighborNodes[1] );
252 double maxDist2 = xyz1.SquareDistance( xyz2 ) / 4.;
253 if ( _projectors[ iProjector ].projectAndClassify( xyz, maxDist2, newXyz,
254 nn._params, nn._nearParams ))
256 moveNode( nn._nodeToMove, newXyz );
258 else // a node is not on iProjector-th shape, find the shape it is on
260 for ( iP = 1; iP < _projectors.size(); ++iP ) // check _projectors other than iProjector
262 iProjector = ( iProjector + 1 ) % _projectors.size();
263 if ( _projectors[ iProjector ].projectAndClassify( xyz, maxDist2, newXyz,
264 nn._params, nn._nearParams ))
266 moveNode( nn._nodeToMove, newXyz );
270 if ( iP == _projectors.size() )
272 notProjectedNodes.push_back( &nn );
275 std::cerr << "Warning: no shape found for node " << nn._nodeToMove << std::endl;
276 if ( !_groupNames.empty() )
277 std::cerr << "Warning: group -- " << _groupNames[0] << std::endl;
285 for ( size_t i = 0; i < _nodesOrder.size(); ++i )
287 FT_NodeToMove& nn = _nodes[ _nodesOrder[ i ]];
288 gp_Pnt xyz = getPoint( nn._nodeToMove );
289 gp_Pnt xyz1 = getPoint( nn._neighborNodes[0] );
290 gp_Pnt xyz2 = getPoint( nn._neighborNodes[1] );
291 double maxDist2 = xyz1.SquareDistance( xyz2 ) / 4.;
292 if ( _projectors[ 0 ].project( xyz, maxDist2, newXyz,
293 nn._params, nn._nearParams ))
294 moveNode( nn._nodeToMove, newXyz );
296 notProjectedNodes.push_back( &nn );
301 if ( !notProjectedNodes.empty() )
303 // project nodes that are not projected by any of _projectors;
304 // a proper projector is selected by evaluation of a distance between neighbor nodes
307 std::vector< FT_Projector > & projectors = *_allProjectors;
310 for ( size_t i = 0; i < notProjectedNodes.size(); ++i )
312 FT_NodeToMove& nn = *notProjectedNodes[ i ];
313 gp_Pnt xyz = getPoint( nn._nodeToMove );
314 gp_Pnt xyz1 = getPoint( nn._neighborNodes[0] );
315 gp_Pnt xyz2 = getPoint( nn._neighborNodes[1] );
316 double maxDist2 = xyz1.SquareDistance( xyz2 ) / 4.;
317 double tol2 = 1e-6 * maxDist2;
320 for ( iP = 0; iP < projectors.size(); ++iP )
322 projectors[ iProjector ].prepareForProjection();
324 if (( ok = projectors[ iProjector ].isOnShape( xyz1, tol2, nn._params, nn._nearParams )) &&
325 ( ok = projectors[ iProjector ].isOnShape( xyz2, tol2, nn._params, nn._params )))
327 if ( nn._neighborNodes.size() == 4 )
329 gp_Pnt xyz1 = getPoint( nn._neighborNodes[2] );
330 gp_Pnt xyz2 = getPoint( nn._neighborNodes[3] );
331 if (( ok = projectors[ iProjector ].isOnShape( xyz1, tol2, nn._params, nn._params )))
332 ok = projectors[ iProjector ].isOnShape( xyz2, tol2, nn._params, nn._params );
336 if ( ok && projectors[iProjector].project( xyz, maxDist2, newXyz, nn._params, nn._params ))
338 moveNode( nn._nodeToMove, newXyz );
341 iProjector = ( iProjector + 1 ) % projectors.size();
343 if ( iP == projectors.size() )
347 std::cerr << "Error: not projected node " << nn._nodeToMove << std::endl;
353 //================================================================================
355 * \brief Put nodes in the order for optimal projection and set FT_NodeToMove::_nearParams
356 * to point to a FT_NodeToMove::_params of a node that will be projected earlier
358 //================================================================================
360 void FT_NodesOnGeom::putNodesInOrder()
362 if ( !_nodesOrder.empty() )
365 // check if any of projectors can use parameters of a previously projected node on a shape
366 // to speed up projection
368 bool isPrevSolutionUsed = false;
369 for ( size_t i = 0; i < _projectors.size() && !isPrevSolutionUsed; ++i )
370 isPrevSolutionUsed = _projectors[i].canUsePrevSolution();
372 if ( !isPrevSolutionUsed )
374 _nodesOrder.resize( _nodes.size() );
375 for ( size_t i = 0; i < _nodesOrder.size(); ++i )
376 _nodesOrder[ i ] = i;
380 // make a map to find a neighbor projected node
382 // map of { FT_NodeToMove::_neighborNodes[i] } to { FT_NodeToMove* };
383 // here we call FT_NodeToMove a 'link' as this data links a _neighborNodes[i] node to other nodes
384 typedef NCollection_DataMap< int, std::vector< FT_NodeToMove* > > TNodeIDToLinksMap;
385 TNodeIDToLinksMap neigborsMap;
387 int mapSize = ( _shapeDim == 1 ) ? _nodes.size() + 1 : _nodes.size() * 3;
389 neigborsMap.ReSize( mapSize );
391 std::vector< FT_NodeToMove* > linkVec, *linkVecPtr;
392 const int maxNbLinks = ( _shapeDim == 1 ) ? 2 : 6; // usual nb of links
394 for ( size_t i = 0; i < _nodes.size(); ++i )
396 FT_NodeToMove& nn = _nodes[i];
397 for ( size_t iN = 0; iN < nn._neighborNodes.size(); ++iN )
399 if ( !( linkVecPtr = neigborsMap.ChangeSeek( nn._neighborNodes[ iN ] )))
401 linkVecPtr = neigborsMap.Bound( nn._neighborNodes[ iN ], linkVec );
402 linkVecPtr->reserve( maxNbLinks );
404 linkVecPtr->push_back( & nn );
408 // fill in _nodesOrder
410 _nodesOrder.reserve( _nodes.size() );
412 std::list< FT_NodeToMove* > queue;
413 queue.push_back( &_nodes[0] );
414 _nodes[0]._nearParams = _nodes[0]._params; // to avoid re-adding to the queue
416 while ( !queue.empty() )
418 FT_NodeToMove* nn = queue.front();
421 _nodesOrder.push_back( nn - & _nodes[0] );
423 // add neighbors to the queue and set their _nearParams = nn->_params
424 for ( size_t iN = 0; iN < nn->_neighborNodes.size(); ++iN )
426 std::vector< FT_NodeToMove* >& linkVec = neigborsMap( nn->_neighborNodes[ iN ]);
427 for ( size_t iL = 0; iL < linkVec.size(); ++iL )
429 FT_NodeToMove* nnn = linkVec[ iL ];
430 if ( nnn != nn && nnn->_nearParams == 0 )
432 nnn->_nearParams = nn->_params;
433 queue.push_back( nnn );
438 _nodes[0]._nearParams = 0; // reset
441 //================================================================================
443 * \brief Get node coordinates. Node IDs count from a unit
445 //================================================================================
447 gp_Pnt FT_NodesOnGeom::getPoint( const int nodeID )
449 const size_t dim = _nodeCoords->getNumberOfComponents();
450 const double * xyz = _nodeCoords->getConstPointer() + ( dim * ( nodeID - 1 ));
451 return gp_Pnt( xyz[0], xyz[1], dim == 2 ? 0 : xyz[2] );
454 //================================================================================
456 * \brief change node coordinates
458 //================================================================================
460 void FT_NodesOnGeom::moveNode( const int nodeID, const gp_Pnt& newXyz )
462 const size_t dim = _nodeCoords->getNumberOfComponents();
463 double z, *xyz = _nodeCoords->getPointer() + ( dim * ( nodeID - 1 ));
464 newXyz.Coord( xyz[0], xyz[1], dim == 2 ? z : xyz[2] );