Salome HOME
Merge branch 'gn/evol_01'
[modules/homard.git] / src / FrontTrack / FrontTrack_Utils.cxx
1 // File      : FrontTrack_Utils.cxx
2 // Created   : Tue Apr 25 17:28:58 2017
3 // Author    : Edward AGAPOV (eap)
4
5 #include "FrontTrack_Utils.hxx"
6
7 #include <XAO_Xao.hxx>
8 #include <XAO_Group.hxx>
9
10 #include <fcntl.h>
11 #include <boost/filesystem.hpp>
12
13 namespace boofs = boost::filesystem;
14
15 //================================================================================
16 /*
17  * \brief Check if a file exists
18  */
19 //================================================================================
20
21 bool FT_Utils::fileExists( const std::string& path )
22 {
23   if ( path.empty() )
24     return false;
25
26   boost::system::error_code err;
27   bool res = boofs::exists( path, err );
28
29   return err ? false : res;
30 }
31
32 //================================================================================
33 /*!
34  * \brief Check if a file can be created/overwritten
35  */
36 //================================================================================
37
38 bool FT_Utils::canWrite( const std::string& path )
39 {
40   if ( path.empty() )
41     return false;
42
43   bool can = false;
44 #ifdef WIN32
45
46   HANDLE file = CreateFile( path.c_str(),           // name of the write
47                             GENERIC_WRITE,          // open for writing
48                             0,                      // do not share
49                             NULL,                   // default security
50                             OPEN_ALWAYS,            // CREATE NEW or OPEN EXISTING
51                             FILE_ATTRIBUTE_NORMAL,  // normal file
52                             NULL);                  // no attr. template
53   can = ( file != INVALID_HANDLE_VALUE );
54   CloseHandle( file );
55
56 #else
57
58   int file = ::open( path.c_str(),
59                      O_WRONLY | O_CREAT,
60                      S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ); // rw-r--r--
61   can = ( file >= 0 );
62
63 #endif
64
65   return can;
66 }
67
68 //================================================================================
69 /*!
70  * \brief Make a map of XAO groups
71  */
72 //================================================================================
73
74 FT_Utils::XaoGroups::XaoGroups( const XAO::Xao* theXao )
75 {
76   XAO::Xao* xao = const_cast< XAO::Xao* >( theXao );
77
78   for ( int iG = 0; iG < theXao->countGroups(); ++iG )
79   {
80     XAO::Group* group = xao->getGroup( iG );
81
82     if ( group->getDimension() == 1 )
83       
84       _xaoGroups[ 0 ].insert( std::make_pair( group->getName(), group ));
85
86     else if ( group->getDimension() == 2 )
87
88       _xaoGroups[ 1 ].insert( std::make_pair( group->getName(), group ));
89   }
90 }
91
92 //================================================================================
93 /*!
94  * \brief Return FT_Projector's by a group name
95  *  \param [in] groupName - the group name
96  *  \param [in] dim - the group dimension
97  *  \param [in] allProjectors - the projector of all shapes of \a dim dimension
98  *  \param [out] groupProjectors - projectors to shapes of the group
99  *  \return int - number of found shapes
100  */
101 //================================================================================
102
103 int FT_Utils::XaoGroups::getProjectors( const std::string&                   groupName,
104                                         const int                            dim,
105                                         const std::vector< FT_Projector > &  allProjectors,
106                                         std::vector< const FT_Projector* > & groupProjectors) const
107 {
108   // get namesake groups
109
110   const TGroupByNameMap* groupMap = 0;
111   if ( dim == 1 )
112     groupMap = &_xaoGroups[ 0 ];
113   else if ( dim == 2 )
114     groupMap = &_xaoGroups[ 1 ];
115   else
116     return 0;
117
118   TGroupByNameMap::const_iterator name2gr = groupMap->find( groupName );
119   if ( name2gr == groupMap->end() )
120     return 0;
121
122   std::vector< XAO::Group* > groups;
123   groups.push_back( name2gr->second );
124
125   for ( ++name2gr; name2gr != groupMap->end(); ++name2gr )
126   {
127     if ( name2gr->second->getName() == groupName )
128       groups.push_back( name2gr->second );
129     else
130       break;
131   }
132
133   // get projectors
134
135   int nbFound = 0;
136   for ( size_t i = 0; i < groups.size(); ++i )
137   {
138     // IDs in XAO correspond to indices of allProjectors
139     std::set<int>::iterator id = groups[i]->begin(), end = groups[i]->end();
140     for ( ; id != end; ++id, ++nbFound )
141       if ( *id < allProjectors.size() )
142         groupProjectors.push_back ( & allProjectors[ *id ]);
143   }
144
145   return nbFound;
146 }