comparison hilbert.cpp @ 43:b91a1859829a

used UIkit sliders
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 19 Apr 2013 18:50:04 +0100
parents a42903c61558
children
comparison
equal deleted inserted replaced
42:3d627dce8bf0 43:b91a1859829a
5 // Created by Robert Tubb on 04/04/2013. 5 // Created by Robert Tubb on 04/04/2013.
6 // 6 //
7 // 7 //
8 8
9 #include "hilbert.h" 9 #include "hilbert.h"
10 Hilbert hilbert; 10
11 11 // UTILS
12 //-------------------------------------------------------------------- 12 //--------------------------------------------------------------------
13 unsigned int rotl(unsigned int value, int shift, int digits) { 13 unsigned int rotl(unsigned int value, int shift, int digits) {
14 unsigned int ones = (1 << digits) - 1; 14 unsigned int ones = (1 << digits) - 1;
15 if(shift >= digits) 15 if(shift >= digits)
16 shift -= digits; 16 shift -= digits;
72 } 72 }
73 cout << "\n"; 73 cout << "\n";
74 74
75 } 75 }
76 //-------------------------------------------------------------------- 76 //--------------------------------------------------------------------
77 void Hilbert::init(int aN, int aP){ 77 //====================================================================
78 //--------------------------------------------------------------------
79
80 Hilbert::Hilbert(){
81 N = 7;
82 P = 5;
83 codeLength = (int)pow(2.0,P);
84
85
86 makeCode();
87 makeRotationRules();
88
89 }
90
91 //--------------------------------------------------------------------
92
93 Hilbert::Hilbert(int aN, int aP){
78 N = aN; 94 N = aN;
79 P = aP; 95 P = aP;
80 codeLength = (int)pow(2.0,P); 96 codeLength = (int)pow(2.0,P);
97
98
81 makeCode(); 99 makeCode();
82 makeRotationRules(); 100 makeRotationRules();
83 101
84 102 }
85 // whole process unit test(?) 103 //--------------------------------------------------------------------
86 104
87 unsigned long long inCoord = 982635920; 105 void Hilbert::changeCurve(int aN, int aP){
88 vector<int> checkParam; 106 N = aN;
89 checkParam = calculateParamsFromIndex(inCoord); 107 P = aP;
90 cout << "PARAMS: "; 108 codeLength = (int)pow(2.0,P);
91 for(int i=0; i<P;i++){ 109
92 cout << checkParam[i] << " "; 110
93 } 111 makeCode();
94 cout << '\n'; 112 makeRotationRules();
95 unsigned long long outCoord = calculateIndexFromParams(checkParam);
96 cout << "UNIT TEST COORD = " << outCoord << "\n";
97
98 113
99 } 114 }
100 //-------------------------------------------------------------- 115 //--------------------------------------------------------------
101 void Hilbert::makeRotationRules(){ 116 void Hilbert::makeRotationRules(){
102 117 theRotations.clear();
118 theEntryVertices.clear();
119
120 const int *rp;
121 const int *ep;
122 if(P == 5){
123 rp = rotations5;
124 ep = entryVertices5;
125 }else if(P == 4){
126 rp = rotations4;
127 ep = entryVertices4;
128 }else if(P == 3){
129 rp = rotations3;
130 ep = entryVertices3;
131 }else if(P == 2){
132 rp = rotations2;
133 ep = entryVertices2;
134 }else if(P == 1){ // will this even work?
135 rp = rotations1;
136 ep = entryVertices1;
137 }else{
138 cout << "ERROR: makeRotationRules: bad numb of MIDI params";
139 return;
140 }
141
142 for(int i=0; i < codeLength; i++){ // fill vector
143
144 theEntryVertices.push_back(ep[i]);
145 theRotations.push_back(rp[i]);
146
147 }
103 148
104 } 149 }
105 //-------------------------------------------------------------- 150 //--------------------------------------------------------------
106 void Hilbert::makeCode(){ 151 void Hilbert::makeCode(){
107 152
108 ////////////////////////////////~~~,,,,,,,,......... 153 ////////////////////////////////~~~,,,,,,,,.........
109 ////////// gray code generation 154 ////////// gray code generation
110 ////// 155 //////
111 /// 156 ///
112 // TODO 5bit specific 157 // choose the gray code sequence and rotation based on the numnber of dimensions P
113 // transition sequence.... what a palaver! only need to do once though.
114 158
115 // ALL SEQUENCES SHOULD END WITH 1 0 0 ... i.e. MSB changes 159 // ALL SEQUENCES SHOULD END WITH 1 0 0 ... i.e. MSB changes
116 160
117 // max MRL 5 digit 161 // max MRL 5 digit
118 int trans[] = {2,3,4,0,2,1,4,3,2,0,4,3,2,1,4,0,2,3,4,0,2,1,4,3,2,0,4,3,2,1,4,0}; 162
163 theGrayCode.clear();
164 theGrayCodeD.clear();
165
166 int trans5[] = {2,3,4,0,2,1,4,3,2,0,4,3,2,1,4,0,2,3,4,0,2,1,4,3,2,0,4,3,2,1,4,0};
119 167
120 // other ones 168 // other ones
121 int trans4[] = {1,2,0,3,0,2,1,2,3,0,3,2,1,3,1,0}; 169 int trans4[] = {1,2,0,3,0,2,1,2,3,0,3,2,1,3,1,0};
122 int trans3[] = {1,2,1,0,1,2,1,0}; 170 int trans3[] = {1,2,1,0,1,2,1,0};
123 int trans2[] = {1,0,1,0}; 171 int trans2[] = {1,0,1,0};
172 int trans1[] = {0,0};
124 // balanced 5 173 // balanced 5
125 int transB5[] = {1,2,3,4,5,1,5,2,3,5,2,4,2,3,4,1,4,3,2,3,1,5,3,4,1,5,2,5,3,4,1,3}; 174 int transB5[] = {1,2,3,4,5,1,5,2,3,5,2,4,2,3,4,1,4,3,2,3,1,5,3,4,1,5,2,5,3,4,1,3};
126 175
176 int *tp;
177
127 178
128 if(P == 5){ 179 if(P == 5){
129 180 tp = trans5;
130 }else{ 181 }else if(P == 4){
131 cout << "Only 5 digit supported now\n"; 182 tp = trans4;
132 return; 183 }else if(P == 3){
133 } 184 tp = trans3;
134 185 }else if(P == 2){
186 tp = trans2;
187 }else if(P == 1){ // will this even work?
188 tp = trans1;
189 }
190
135 191
136 int code[codeLength][P]; // start with normal array 192 int code[codeLength][P]; // start with normal array
137 for(int j=0; j<P; j++){ 193 for(int j=0; j<P; j++){
138 code[0][j] = 0; 194 code[0][j] = 0;
139 } 195 }
140 196
141 for(int i=0; i < codeLength-1; i++){ // don't need last 3 197 for(int i=0; i < codeLength-1; i++){ // don't need last 3
142 for(int j=0; j<P; j++){ 198 for(int j=0; j<P; j++){
143 if (j == abs(trans[i])){ 199 if (j == abs(tp[i])){
144 code[i+1][j] = !code[i][j]; 200 code[i+1][j] = !code[i][j];
145 }else{ 201 }else{
146 code[i+1][j] = code[i][j]; 202 code[i+1][j] = code[i][j];
147 } 203 }
148 } 204 }
215 271
216 for(int j=0;j<P;j++){ 272 for(int j=0;j<P;j++){
217 273
218 params[j] += ((vertex >> P-j-1) & 1)*(1<<blev); 274 params[j] += ((vertex >> P-j-1) & 1)*(1<<blev);
219 } 275 }
220 if(P==5){ 276 if(subindex > theEntryVertices.size()){
221 newe = entryVertices5[subindex]; 277 cout << "ERROR: subindex too large for theEntryVertices\n";
222 newd = rotations5[subindex]; 278 }
223 } 279 newe = theEntryVertices[subindex];
280 newd = theRotations[subindex];
224 // next rotations... 281 // next rotations...
225 int lse2 = rotr(newe, P-direction-1,P); 282 int lse2 = rotr(newe, P-direction-1,P);
226 entryPoint = entryPoint ^ lse2; 283 entryPoint = entryPoint ^ lse2;
227 direction = (direction + newd + 1) % P; 284 direction = (direction + newd + 1) % P;
228 //cout << "------------\n"; 285 //cout << "------------\n";
278 subindex = i; 335 subindex = i;
279 } 336 }
280 } 337 }
281 //cout << "INV subindex: " << subindex << "\n"; 338 //cout << "INV subindex: " << subindex << "\n";
282 // work out next rotations 339 // work out next rotations
283 newe = entryVertices5[subindex]; 340 if(subindex > theEntryVertices.size()){
284 newd = rotations5[subindex]; 341 cout << "ERROR: subindex too large for theEntryVertices\n";
342 }
343 newe = theEntryVertices[subindex];
344 newd = theRotations[subindex];
285 345
286 // next rotations... 346 // next rotations...
287 int lse2 = rotr(newe, P-direction-1,P); 347 int lse2 = rotr(newe, P-direction-1,P);
288 entryPoint = entryPoint ^ lse2; 348 entryPoint = entryPoint ^ lse2;
289 direction = (direction + newd + 1) % P; 349 direction = (direction + newd + 1) % P;