0024785: Visualization - Modifying z-layers concept to gain more control over OpenGl...
[occt.git] / src / Standard / Standard_Integer.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <stdlib.h>
16 #include <Standard_Integer.hxx>
17 #include <Standard_ConstructionError.hxx>
18 #include <Standard_RangeError.hxx>
19 #include <Standard_Stream.hxx>
20 #include <Standard_OStream.hxx>
21
22 // ------------------------------------------------------------------
23 // CharToInt : Converts a character in an integer value
24 // ------------------------------------------------------------------
25 const Handle_Standard_Type& Standard_Integer_Type_() 
26 {
27   static Handle_Standard_Type _aType = new 
28     Standard_Type("Standard_Integer",sizeof(Standard_Integer),0,NULL);
29   
30   return _aType;
31 }
32
33 Standard_Integer CharToInt(const Standard_Character me) 
34
35   if (!IsDigit(me)) {
36     Standard_ConstructionError::Raise();
37   }
38     
39   Standard_Character S[2];
40   S[0] = me;
41   S[1] = 0;
42   return atoi(S);
43 }
44
45 // ------------------------------------------------------------------
46 // CharToInt : Converts a string in an integer value
47 // ------------------------------------------------------------------
48
49 Standard_Integer CharToInt(const Standard_CString me) 
50
51   const Standard_Size Len = strlen(me);
52   for (Standard_Size I = 0; I < Len; I++)
53     if (!IsDigit(me[I])) {
54         Standard_ConstructionError::Raise();
55     }
56   return atoi(me);
57 }
58
59 // ------------------------------------------------------------------
60 // ShallowCopy : Copy of an integer
61 // ------------------------------------------------------------------
62
63 Standard_Integer ShallowCopy (const Standard_Integer me) 
64 { return me; }
65
66 // ------------------------------------------------------------------
67 // ShallowDump : Writes an integer value
68 // ------------------------------------------------------------------
69 Standard_EXPORT void ShallowDump (const Standard_Integer Value, Standard_OStream& s)
70 { s << Value << " Standard_Integer" << "\n"; }
71
72 // ------------------------------------------------------------------
73 // NextPrime : Compute the first prime number greater or equal than an integer
74 // ------------------------------------------------------------------
75
76 #define VALUESNBR 4
77
78 long NextPrime (const long me ) 
79 {
80
81  struct svalue {int signiaib ;
82                 int nbr ;} ;
83
84  struct svalue values[VALUESNBR] ;
85  long ia ;
86  long maxia ;
87  long ib[4] ;
88  int n[4] ;
89 // int signiaib[4] = { -1 , +1 , +1 , -1 } ;
90  int signiaib[4];
91  signiaib[0] = -1;
92  signiaib[1] = 1;
93  signiaib[2] = 1;
94  signiaib[3] = -1;
95  long remain ;
96
97  int nbvalues ;
98  int loop ;
99  int nindd ;
100  long minn ;
101  long maxvn ;
102  long premret = 0 ;
103
104    if (me < 0 || me >
105 #if defined (__alpha) || defined(DECOSF1)
106                       127149130704178201
107 #else
108                       2147483647
109 #endif
110                                ){
111       Standard_RangeError::
112        Raise("Try to apply NextPrime method with negative, null or too large value.");
113    }
114
115    if ( me <= 7 ) {
116      if ( me <= 1 )
117        return 1 ;
118      else if ( me <= 2 )
119        return 2 ;
120      else if ( me <= 3 )
121        return 3 ;
122      else if ( me <= 5 )
123        return 5 ;
124      else if ( me <= 7 )
125        return 7 ;
126    }
127
128    minn = ( me - 1 ) / 6 ;              // n minimum
129    while ( 6*minn+1 < me ) {
130         minn += 1 ;
131       }
132
133    maxia = long( sqrt((double ) me ) / 6 + 1 ) ;
134
135    maxvn = minn + VALUESNBR ;
136
137    nbvalues = 0 ;
138    for ( nindd = 0 ; nindd < VALUESNBR ; nindd++ ) {
139       if ( 6*(nindd+minn)-1 < me ) {
140         values[nindd].nbr = 1 ;
141         values[nindd].signiaib = -1 ;
142         nbvalues += 1 ;
143       }
144       else {
145         values[nindd].nbr = 0 ;
146         values[nindd].signiaib = 0 ;
147       }
148     }
149
150    for ( ia = 1 ; ia <= maxia ; ia++ ) {
151       if ( nbvalues == VALUESNBR*2 ) {
152         break ;
153       }
154       remain = -VALUESNBR ;
155       ib[0] = ( minn + ia - remain ) / (6*ia - 1) ;
156       n[0] = int ( 6*ia*ib[0] - ia - ib[0] - minn ) ;
157       ib[1] = ( minn - ia - remain ) / (6*ia - 1) ;
158       n[1] = int ( 6*ia*ib[1] + ia - ib[1] - minn ) ;
159       ib[2] = ( minn + ia - remain ) / (6*ia + 1) ;
160       n[2] = int ( 6*ia*ib[2] - ia + ib[2] - minn ) ;
161       ib[3] = ( minn - ia - remain ) / (6*ia + 1) ;
162       n[3] = int ( 6*ia*ib[3] + ia + ib[3] - minn ) ;
163       for ( loop = 0 ; loop < 4 ; loop++ ) {
164          if ( n[loop] >= 0 && n[loop] < VALUESNBR ) {
165            if ( ( values[n[loop]].nbr == 0 ) ||
166                 ( values[n[loop]].signiaib == signiaib[loop] ) ) {
167              values[n[loop]].signiaib = -signiaib[loop] ;
168              values[n[loop]].nbr += 1 ;
169              if ( values[n[loop]].nbr <= 2 )
170                nbvalues += 1 ;
171            }
172          }
173        }
174     }
175    for ( nindd = 0 ; nindd < VALUESNBR ; nindd++ ) {
176      if ( values[nindd].nbr == 0 ) {
177        if ( me <= 6*(nindd+minn)-1 ) {
178           premret = 6*(nindd+minn)-1 ;
179           break ;
180         }
181        else if ( me <= 6*(nindd+minn)+1 ) {
182          premret = 6*(nindd+minn)+1 ;
183          break ;
184        }
185      }
186      else if ( values[nindd].nbr == 1 ) {
187         if ( values[nindd].signiaib > 0 ) {
188           if ( me <= 6*(nindd+minn)-1 ) {
189             premret = 6*(nindd+minn)-1 ;
190             break ;
191           }
192         }
193         else {
194           if ( me <= 6*(nindd+minn)+1 ) {
195             premret = 6*(nindd+minn)+1 ;
196             break ;
197           }
198         }
199       }
200    }
201
202    if ( premret != 0 ) {
203      return premret ;
204    }
205
206  return NextPrime ( 6*(maxvn-1)+2) ;
207
208 }
209 static const Standard_Integer Primes[] = {
210     101,  1009,  2003,  3001,  4001,  5003,  6007,  7001,  8009,  9001,
211   10007,        12007,        14009,        16007,        18013, 
212   20011,               23003,               26003,               29009,
213                        33013,                      37003,              
214          41011,                             46021,                     
215          51001,                                    57037,              
216                                      65003, 
217   100019};  // catch the biggest
218
219 const Standard_Integer NbPrimes = 26; // does not include the biggest
220
221 Standard_Integer NextPrimeForMap(const Standard_Integer N)
222 {
223   Standard_Integer i;
224   for (i = 0; i < NbPrimes; i++) 
225     if (Primes[i] > N) break;
226   return Primes[i];
227 }