Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEM / MEDMEM_ModulusArray.hxx
1 // Copyright (C) 2007-2012  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
23 #ifndef __MEDMODULUSARRAY_H__
24 #define __MEDMODULUSARRAY_H__
25
26 #include "MEDMEM.hxx"
27
28 #include "MEDMEM_Utilities.hxx"
29 #include "MEDMEM_define.hxx"
30
31
32 /*
33   This class is use to set cyclic (modulus length) array.
34   (array[length+1]==array[1])
35
36   We use it in particular to compare faces nodes lists to know if
37   they are the same (positive or negative cycle) !
38 */
39
40 namespace MEDMEM {
41 class MEDMEM_EXPORT MEDMODULUSARRAY {
42 private:
43   // nb vertex nodes; only vertex nodes are in cycle
44   int   _length ;
45   // total nb nodes; not vertex nodes exists in 2-nd order elements,
46   // only presence of not vertex nodes is checked by compare()
47   int   _length2;
48   const int * _array ;
49
50   inline bool compareNotVertexNodes(const MEDMODULUSARRAY &modulusArray) const;
51
52 public:
53   MEDMODULUSARRAY(int length, const int * array) :
54     _length(length), _length2(length), _array(array) {}
55
56   MEDMODULUSARRAY(int vertexLength, int totalLength, const int * array):
57     _length(vertexLength), _length2( totalLength ), _array(array) {}
58
59   ~MEDMODULUSARRAY() {}
60
61   inline const int operator[](const int &i) const ;
62
63   inline int compare(const MEDMODULUSARRAY &modulusArray) const;
64
65   const int  *getArray(int& length) const { length=_length; return _array; }
66 };
67
68
69 inline const int MEDMODULUSARRAY::operator[](const int &i) const
70 {
71   int position = i%_length ;
72   //int position = i%_length2 ;
73   if (position < 0)
74     position+=_length ;
75   //position += _length2 ;
76   return _array[position] ;
77 }
78
79 inline int MEDMODULUSARRAY::compare(const MEDMODULUSARRAY &modulusArray) const
80 {
81   int ret = 0 ;
82
83   if (modulusArray._length  != _length ||
84       modulusArray._length2 != _length2 )
85     return ret ;
86
87   if (_length==1)
88     {
89       if (_array[0]==modulusArray[0])
90         return 1;
91       else 
92         return 0;
93     }
94
95   if (_length==2) {
96     if ((_array[0]==modulusArray[0])&(_array[1]==modulusArray[1]))
97       ret = 1;
98     else if ((_array[0]==modulusArray[1])&(_array[1]==modulusArray[0]))
99       ret = -1;
100     else
101       return 0;
102     if ( !compareNotVertexNodes( modulusArray ) )
103       ret = 0;
104     return ret;
105   }
106
107   //search if there is one start point in common in two array
108   for(int i=0;i<_length;i++)
109     if ( _array[0] == modulusArray[i] ) {
110       // we search if cycle is the same
111       if (_array[1]==modulusArray[i+1]){ // positive order
112         ret=1;
113         for(int j=2;j<_length;j++)
114           if (_array[j]!=modulusArray[i+j]) {
115             ret = 0 ;
116             break ;
117           }
118       } else if(_array[1]==modulusArray[i-1]) { //negative order
119         ret=-1;
120         for(int j=2;j<_length;j++)
121           if (_array[j]!=modulusArray[i-j]) {
122             ret = 0 ;
123             break ;
124           }
125       }
126       if (ret!=0) {// we have found it !
127         if ( !compareNotVertexNodes( modulusArray ) )
128           ret = 0;
129         break ;
130       }
131       // else we continue if there is another start point i
132     }
133     return ret ;
134 }
135
136 /*!
137  * \brief Check presence of the same not vertex nodes
138   * \retval bool - comparison result
139  */
140 inline bool MEDMODULUSARRAY::compareNotVertexNodes(const MEDMODULUSARRAY &modulusArray) const
141 {
142   if ( _length2 > _length ) {
143     for ( int i = _length; i < _length2; ++i ) {
144       bool found = false;
145       for ( int j = _length; ( j < _length2 && !found ); ++j )
146         found = ( _array[ i ] == modulusArray._array[ j ] );
147       if ( !found )
148         return false;
149     }
150   }
151   return true;
152 }
153
154 }
155
156 # endif         /* # ifndef __MEDMODULUSARRAY_H__ */
157