rt300@4
|
1 //
|
rt300@4
|
2 // grid.cpp
|
rt300@4
|
3 // oscSenderExample
|
rt300@4
|
4 //
|
rt300@4
|
5 // Created by Robert Tubb on 03/10/2012.
|
rt300@4
|
6 //
|
rt300@37
|
7 // This is the grid view, i.e. the viewable representation of hilbert surface
|
rt300@4
|
8
|
rt300@4
|
9 #include "grid.h"
|
rt300@4
|
10
|
rt300@4
|
11
|
rt300@4
|
12 #include <sstream>
|
rt300@4
|
13
|
rt300@4
|
14 extern PresetManager presetManager;
|
rt300@4
|
15 extern EventLogger eventLogger;
|
rt300@34
|
16 extern Hilbert hilbert;
|
rt300@4
|
17 //--------------------------------------------------------------
|
rt300@35
|
18 Grid::Grid(): maxValue(pow(32.0,7.0)-1), minValue(30), paramsPerDim(5), paramBitDepth(7){
|
rt300@4
|
19
|
rt300@37
|
20 interpLevel = 4;
|
rt300@4
|
21 }
|
rt300@4
|
22 //--------------------------------------------------------------
|
rt300@4
|
23 //--------------------------------------------------------------
|
rt300@4
|
24 Grid::~Grid(){
|
rt300@4
|
25
|
rt300@4
|
26 }
|
rt300@4
|
27 void Grid::init(){
|
rt300@34
|
28
|
rt300@38
|
29 interpolateMode = NO_INTERPOLATION;
|
rt300@37
|
30
|
rt300@4
|
31 maxZoom = false;
|
rt300@4
|
32 minZoom = false;
|
rt300@4
|
33
|
rt300@4
|
34 pixSize.setCoord(ofGetWidth(), ofGetHeight());
|
rt300@4
|
35
|
rt300@35
|
36 //set scale and position to random
|
rt300@27
|
37 scale = 15500.0;
|
rt300@35
|
38
|
rt300@37
|
39 smallestGridSpacing = 3; //pixels
|
rt300@37
|
40
|
rt300@8
|
41 snapDist = TwoVector(9,9);
|
rt300@5
|
42 snapped = false;
|
rt300@4
|
43
|
rt300@4
|
44 size.setCoord(pixSize.x*scale, pixSize.y*scale);
|
rt300@4
|
45 centre.setCoord(maxValue/2 , maxValue/2);
|
rt300@4
|
46 topLeft.setCoord(centre.x - size.x/2, centre.y - size.y/2);
|
rt300@34
|
47
|
rt300@34
|
48 hilbert.init(paramBitDepth, paramsPerDim);
|
rt300@4
|
49
|
rt300@4
|
50 // set a starting param value, find coord that does it.
|
rt300@4
|
51 vector<int> params(paramsPerDim*2);
|
rt300@4
|
52 for(int i = 0;i<2*paramsPerDim;i++){
|
rt300@4
|
53 midiCC[i] = 12;
|
rt300@4
|
54 params[i] = 12;
|
rt300@4
|
55 }
|
rt300@35
|
56
|
rt300@35
|
57
|
rt300@4
|
58 TwoVector coord = calculateCoordFromParams(params);
|
rt300@4
|
59 setCoord(coord);
|
rt300@4
|
60
|
rt300@4
|
61 viewWasChanged();
|
rt300@37
|
62 calculateInterpolateLevel();
|
rt300@4
|
63 }
|
rt300@4
|
64
|
rt300@4
|
65 template <typename T> int sgn(T val) {
|
rt300@4
|
66 return (T(0) < val) - (val < T(0));
|
rt300@4
|
67 }
|
rt300@37
|
68 //--------------------------------------------------------------
|
rt300@38
|
69 double Grid::calculateInterpolateLevel(){
|
rt300@37
|
70 if(interpolateMode == INTERPOLATE_GRID){
|
rt300@38
|
71
|
rt300@38
|
72 // log_32 of scale times the pixel size we want to be the "integral" interp
|
rt300@38
|
73 int N = 1 << paramsPerDim;
|
rt300@38
|
74 interpLevel = log(scale*N*smallestGridSpacing)/log(N);
|
rt300@38
|
75
|
rt300@38
|
76 return interpLevel;
|
rt300@37
|
77 }else{
|
rt300@37
|
78 interpLevel = 0;
|
rt300@37
|
79 return 0; // ???
|
rt300@37
|
80 }
|
rt300@37
|
81 }
|
rt300@37
|
82 //--------------------------------------------------------------
|
rt300@38
|
83 void Grid::setInterpolation(interpolateModeType mode){
|
rt300@38
|
84 interpolateMode = mode;
|
rt300@38
|
85 viewWasChanged();
|
rt300@38
|
86 calculateInterpolateLevel();
|
rt300@38
|
87 eventLogger.clearTrail();
|
rt300@38
|
88 }
|
rt300@38
|
89 //--------------------------------------------------------------
|
rt300@37
|
90 void Grid::draw(){
|
rt300@37
|
91 if(interpolateMode == NO_INTERPOLATION){
|
rt300@37
|
92 drawGridLines();
|
rt300@37
|
93 drawPresets();
|
rt300@37
|
94 }else if(interpolateMode == INTERPOLATE_GRID){
|
rt300@37
|
95 // calculate according to smallest gridlines
|
rt300@37
|
96 drawGridLines();
|
rt300@37
|
97
|
rt300@37
|
98 }else if(interpolateMode == INTERPOLATE_PRESET){
|
rt300@37
|
99 drawPresets();
|
rt300@37
|
100 return; // ???
|
rt300@37
|
101 }
|
rt300@37
|
102
|
rt300@37
|
103
|
rt300@37
|
104
|
rt300@37
|
105 // draw centre cross hairs
|
rt300@37
|
106 drawCrossHairs();
|
rt300@37
|
107
|
rt300@37
|
108
|
rt300@37
|
109 // draw the undo history trail, given viewing area
|
rt300@37
|
110 eventLogger.drawTrail(topLeft, topLeft + size);
|
rt300@37
|
111 }
|
rt300@4
|
112 //--------------------------------------------------------------
|
rt300@37
|
113 void Grid::drawGridLines(){ // draw lines
|
rt300@4
|
114 // TODO too friggin long
|
rt300@4
|
115
|
rt300@16
|
116 pixSize.setCoord(ofGetWidth(), ofGetHeight()); // incase of rotation?
|
rt300@37
|
117
|
rt300@4
|
118 int power = 0;
|
rt300@4
|
119
|
rt300@4
|
120 // these get big!
|
rt300@4
|
121 double divsr;
|
rt300@4
|
122 double yl,xl;
|
rt300@4
|
123 double gridSize = 0;
|
rt300@4
|
124
|
rt300@4
|
125 double firstLineXPos, lastLineXPos,firstLineYPos, lastLineYPos, remleft, xoffset, yoffset = 0.0;
|
rt300@4
|
126 double xstart, xfinish, ystart,yfinish;
|
rt300@4
|
127 // these don't
|
rt300@4
|
128 int xpos, ypos = 0;
|
rt300@4
|
129
|
rt300@4
|
130 float alpha;
|
rt300@4
|
131 bool done = false;
|
rt300@37
|
132 float markpow = 1 << paramsPerDim; // power that denotes next grid markings e.g. 32
|
rt300@4
|
133 int lineWidth = 0;
|
rt300@4
|
134 int colCycle = 0;
|
rt300@4
|
135
|
rt300@4
|
136
|
rt300@37
|
137 // loop thru powers of (base?) smallest to biggest
|
rt300@37
|
138 // to determine which should be shown as grids
|
rt300@4
|
139 while (!done){
|
rt300@4
|
140 gridSize = pow(markpow,power);
|
rt300@4
|
141
|
rt300@37
|
142 colCycle = power % paramBitDepth;
|
rt300@4
|
143
|
rt300@4
|
144 divsr = size.x / gridSize;
|
rt300@4
|
145
|
rt300@4
|
146 // if (divisor i.e. number of lines is less than 1000
|
rt300@37
|
147 if( ofGetWidth()/divsr >= smallestGridSpacing){
|
rt300@4
|
148
|
rt300@4
|
149 // calculate transparency
|
rt300@37
|
150 float visCont = log10(divsr*2); // 0 if only 1 line, 3 if 1000 lines
|
rt300@4
|
151 alpha = 90*(3 - visCont);
|
rt300@4
|
152 ofSetLineWidth(lineWidth);
|
rt300@4
|
153
|
rt300@4
|
154 // cycle colors for different scales
|
rt300@4
|
155 if(colCycle == 0){
|
rt300@4
|
156 ofSetColor(255,255,255,alpha);
|
rt300@4
|
157 }else if(colCycle == 1){
|
rt300@4
|
158 ofSetColor(255,0,0,alpha);
|
rt300@4
|
159 }else if(colCycle == 2){
|
rt300@4
|
160 ofSetColor(0,0,255,alpha);
|
rt300@4
|
161 }else if(colCycle == 3){
|
rt300@4
|
162 ofSetColor(0,255,0,alpha);
|
rt300@4
|
163 }else if(colCycle == 4){
|
rt300@4
|
164 ofSetColor(255,0,255,alpha);
|
rt300@4
|
165 }else if(colCycle == 5){
|
rt300@4
|
166 ofSetColor(0,255,255,alpha);
|
rt300@4
|
167 }else if(colCycle == 6){
|
rt300@4
|
168 ofSetColor(255,255,0,alpha);
|
rt300@4
|
169 }else if(colCycle == 7){
|
rt300@4
|
170 ofSetColor(255,255,255,alpha);
|
rt300@4
|
171 }else{
|
rt300@4
|
172 cout << "err colour messed up\n";
|
rt300@4
|
173 }
|
rt300@4
|
174
|
rt300@4
|
175
|
rt300@4
|
176 // draw level numbers associated with each color. This would not be true if markpow wasnt 2^d
|
rt300@4
|
177 ////////-------/////////
|
rt300@4
|
178 /*
|
rt300@4
|
179 temp << "THIS LEVEL = " << (int)pow(2.0,power) << " ";
|
rt300@4
|
180 string s = temp.str();
|
rt300@4
|
181 ofDrawBitmapString( s, 10, line );
|
rt300@4
|
182 line+=30;
|
rt300@4
|
183 temp.str("");
|
rt300@4
|
184 *////////-------/////////
|
rt300@4
|
185
|
rt300@4
|
186 // draw presets at this level
|
rt300@4
|
187 // fill in smallest squares if they contain a preset
|
rt300@4
|
188 // how to do this efficiently?
|
rt300@4
|
189
|
rt300@4
|
190
|
rt300@4
|
191 if(topLeft.x < 0.0){
|
rt300@4
|
192 firstLineXPos = 0.0;
|
rt300@4
|
193 xoffset = firstLineXPos - topLeft.x;
|
rt300@4
|
194 xstart = xoffset/scale;
|
rt300@4
|
195 }else{
|
rt300@4
|
196 firstLineXPos = topLeft.x;
|
rt300@4
|
197 // kinda float version of % operator
|
rt300@4
|
198 remleft = ceil(firstLineXPos/gridSize);
|
rt300@4
|
199 xoffset = (remleft * gridSize) - topLeft.x;
|
rt300@4
|
200 xstart = 0;
|
rt300@4
|
201 }
|
rt300@4
|
202 if(topLeft.x + size.x > maxValue){
|
rt300@4
|
203 lastLineXPos = maxValue+1 - topLeft.x;
|
rt300@4
|
204
|
rt300@4
|
205
|
rt300@4
|
206 }else{
|
rt300@4
|
207 lastLineXPos = size.x;
|
rt300@4
|
208
|
rt300@4
|
209 }
|
rt300@4
|
210 xfinish = lastLineXPos/scale;
|
rt300@4
|
211
|
rt300@4
|
212 //////////------------------------
|
rt300@4
|
213 // same for y
|
rt300@4
|
214
|
rt300@4
|
215 if(topLeft.y < 0.0){
|
rt300@4
|
216 firstLineYPos = 0.0;
|
rt300@4
|
217 yoffset = firstLineYPos - topLeft.y;
|
rt300@4
|
218 ystart = yoffset/scale;
|
rt300@4
|
219 }else{
|
rt300@4
|
220 firstLineYPos = topLeft.y;
|
rt300@4
|
221 // kinda float version of % operator
|
rt300@4
|
222 remleft = ceil(firstLineYPos/gridSize);
|
rt300@4
|
223 yoffset = (remleft * gridSize) - topLeft.y;
|
rt300@4
|
224 ystart = 0;
|
rt300@4
|
225 }
|
rt300@4
|
226 if(topLeft.y + size.y > maxValue){
|
rt300@4
|
227 lastLineYPos = maxValue+1 - topLeft.y;
|
rt300@4
|
228
|
rt300@4
|
229 }else{
|
rt300@4
|
230 lastLineYPos = size.y;
|
rt300@4
|
231
|
rt300@4
|
232 }
|
rt300@4
|
233 yfinish = lastLineYPos/scale;
|
rt300@4
|
234 // -------------------------------------------
|
rt300@4
|
235 // now draw
|
rt300@4
|
236 for(xl = xoffset; xl <= (lastLineXPos); xl+= gridSize){
|
rt300@4
|
237
|
rt300@4
|
238 xpos = xl/scale;
|
rt300@4
|
239 ofLine(xpos, ystart, xpos, yfinish);
|
rt300@4
|
240
|
rt300@4
|
241 }
|
rt300@4
|
242
|
rt300@4
|
243 for(yl = yoffset; yl <= (lastLineYPos); yl+= gridSize){
|
rt300@4
|
244
|
rt300@4
|
245 ypos = yl/scale;
|
rt300@4
|
246 ofLine(xstart, ypos, xfinish, ypos);
|
rt300@4
|
247 }
|
rt300@4
|
248
|
rt300@4
|
249
|
rt300@37
|
250 }
|
rt300@37
|
251 /*else if (divsr < 1){
|
rt300@37
|
252 // maximum only one line. worth checking so that bigger lines still show up?
|
rt300@4
|
253 done = true;
|
rt300@4
|
254 }
|
rt300@37
|
255 */
|
rt300@4
|
256 power++;
|
rt300@37
|
257 if(power > paramBitDepth)
|
rt300@37
|
258 done = true;
|
rt300@37
|
259
|
rt300@4
|
260 }
|
rt300@4
|
261 //cout << "draw done" << "\n";
|
rt300@4
|
262 //displayInfo();
|
rt300@4
|
263
|
rt300@33
|
264
|
rt300@33
|
265
|
rt300@4
|
266 ////////-------////////
|
rt300@4
|
267 /*
|
rt300@4
|
268 ostringstream temp;
|
rt300@4
|
269 temp << "Centre x = " << centre.x << "\n y = " << centre.y << " ";
|
rt300@4
|
270 string s = temp.str();
|
rt300@4
|
271 ofDrawBitmapString( s, pixSize.x/2+10, pixSize.y/2+10 );
|
rt300@4
|
272 */
|
rt300@4
|
273 }
|
rt300@5
|
274 //-----------------------------------------------------------------------
|
rt300@5
|
275 void Grid::drawCrossHairs(){
|
rt300@5
|
276 // snapping
|
rt300@5
|
277 TwoVector pos;
|
rt300@5
|
278 if(snapped){
|
rt300@5
|
279 pos = coordToPixel(snapCentre);
|
rt300@5
|
280 }else{
|
rt300@5
|
281 pos = coordToPixel(centre);
|
rt300@5
|
282
|
rt300@5
|
283 }
|
rt300@5
|
284 ofSetColor(255, 0, 0);
|
rt300@5
|
285 ofNoFill();
|
rt300@5
|
286 ofLine(pos.x-20, pos.y, pos.x+20, pos.y);
|
rt300@5
|
287 ofLine(pos.x, pos.y-20, pos.x, pos.y+20);
|
rt300@5
|
288 ofEllipse(pos.x, pos.y, 20, 20);
|
rt300@5
|
289 ofFill();
|
rt300@5
|
290 }
|
rt300@5
|
291 //-----------------------------------------------------------------------
|
rt300@4
|
292 TwoVector Grid::coordToPixel(TwoVector coord){
|
rt300@4
|
293 TwoVector pix;
|
rt300@4
|
294 pix.x = (coord.x - topLeft.x)/scale;
|
rt300@4
|
295 pix.y = (coord.y - topLeft.y)/scale;
|
rt300@4
|
296 return pix;
|
rt300@4
|
297
|
rt300@4
|
298 }
|
rt300@4
|
299
|
rt300@4
|
300 //-----------------------------------------------------------------------
|
rt300@4
|
301 void Grid::drawPresets(){
|
rt300@5
|
302 presetManager.drawPresetsInRange(topLeft, topLeft + size);
|
rt300@7
|
303 // draw snapped preset info
|
rt300@7
|
304 if(snapped && closestPreset != NULL){
|
rt300@8
|
305 ofDrawBitmapString( closestPreset->displayTextDescription(), pixSize.x/2+10, pixSize.y/2+10 );
|
rt300@7
|
306 }
|
rt300@33
|
307
|
rt300@4
|
308 }
|
rt300@4
|
309
|
rt300@4
|
310 //-----------------------------------------------------------------------
|
rt300@4
|
311 void Grid::displayInfo(){
|
rt300@4
|
312
|
rt300@4
|
313 // display some "useful information"
|
rt300@4
|
314
|
rt300@4
|
315 ofSetColor(255,255,255,255);
|
rt300@4
|
316
|
rt300@4
|
317 ostringstream temp;
|
rt300@4
|
318 int line = 10; // text output pos
|
rt300@4
|
319
|
rt300@4
|
320
|
rt300@4
|
321 ////////-------/////////
|
rt300@4
|
322 temp << "scale = " << scale << " ";
|
rt300@4
|
323 string s = temp.str();
|
rt300@4
|
324 ofDrawBitmapString( s, 10, line );
|
rt300@4
|
325 line+=30;
|
rt300@4
|
326 temp.str("");
|
rt300@4
|
327 ////////-------/////////
|
rt300@4
|
328 temp << "Top Left = " << topLeft.x << "," << topLeft.y << " ";
|
rt300@4
|
329 s = temp.str();
|
rt300@4
|
330 ofDrawBitmapString( s, 10, line );
|
rt300@4
|
331 line+=60;
|
rt300@4
|
332 temp.str("");
|
rt300@4
|
333
|
rt300@4
|
334 ////////-------/////////
|
rt300@4
|
335 temp << "View Size = " << size.x << "," << size.y << " ";
|
rt300@4
|
336 s = temp.str();
|
rt300@4
|
337 ofDrawBitmapString( s, 10, line );
|
rt300@4
|
338 line+=60;
|
rt300@4
|
339 temp.str("");
|
rt300@4
|
340
|
rt300@4
|
341 ////////-------/////////
|
rt300@4
|
342
|
rt300@4
|
343 for(int i=0;i<10;i++){ // TODO 5bit specific
|
rt300@4
|
344 temp << midiCC[i] << " ";
|
rt300@4
|
345 s = temp.str();
|
rt300@4
|
346 ofDrawBitmapString( s, 10, line );
|
rt300@4
|
347 line+=20;
|
rt300@4
|
348 temp.str("");
|
rt300@4
|
349 }
|
rt300@4
|
350 }
|
rt300@4
|
351 //--------------------------------------------------------------
|
rt300@4
|
352 void Grid::update(){ // ?
|
rt300@4
|
353
|
rt300@4
|
354
|
rt300@4
|
355 }
|
rt300@5
|
356
|
rt300@4
|
357 //--------------------------------------------------------------
|
rt300@33
|
358
|
rt300@4
|
359 void Grid::move(TwoVector moveP){
|
rt300@4
|
360 // numspacing, pixelspacing stay the same
|
rt300@4
|
361
|
rt300@4
|
362 // convert pixels to surf units
|
rt300@4
|
363 TwoVector moveS;
|
rt300@4
|
364 moveS = moveP * scale;
|
rt300@4
|
365
|
rt300@4
|
366 topLeft = topLeft - moveS; // - because moving to the right means taking away from offset
|
rt300@4
|
367 centre = centre - moveS;
|
rt300@4
|
368
|
rt300@4
|
369 viewWasChanged();
|
rt300@5
|
370 eventLogger.logEvent(SCROLL, centre, scale);
|
rt300@4
|
371
|
rt300@4
|
372 }
|
rt300@4
|
373 //--------------------------------------------------------------
|
rt300@4
|
374 void Grid::zoom(float factor){
|
rt300@5
|
375 if(snapped)centre = (centre + snapCentre)*0.5; // clunky
|
rt300@5
|
376
|
rt300@4
|
377 if(maxZoom && factor > 1.0){
|
rt300@4
|
378 return;
|
rt300@4
|
379
|
rt300@4
|
380 }
|
rt300@4
|
381 if(factor < 1.0){
|
rt300@4
|
382 maxZoom = false;
|
rt300@4
|
383 }
|
rt300@4
|
384 if(minZoom && factor < 1.0){
|
rt300@4
|
385 return;
|
rt300@4
|
386
|
rt300@4
|
387 }
|
rt300@4
|
388 if(factor > 1.0){
|
rt300@4
|
389 minZoom = false;
|
rt300@4
|
390 }
|
rt300@4
|
391 scale = scale*factor; // simple eh?
|
rt300@4
|
392
|
rt300@4
|
393 // update view size using centre
|
rt300@4
|
394 // and scale...
|
rt300@4
|
395 size.x = size.x*factor; // zooming in, size gets SMALLER (view less)
|
rt300@4
|
396 size.y = size.y*factor;
|
rt300@4
|
397
|
rt300@4
|
398
|
rt300@4
|
399
|
rt300@4
|
400 viewWasChanged();
|
rt300@37
|
401 calculateInterpolateLevel();
|
rt300@5
|
402 eventLogger.logEvent(ZOOM, centre, scale);
|
rt300@4
|
403
|
rt300@5
|
404 }
|
rt300@5
|
405 //--------------------------------------------------------------
|
rt300@20
|
406 void Grid::shiftCentreToSnapped(){
|
rt300@20
|
407 // TODO just in case we're freezing something
|
rt300@20
|
408 // snapping actually change centre
|
rt300@20
|
409 centre = snapCentre;
|
rt300@20
|
410
|
rt300@20
|
411 }
|
rt300@20
|
412 //--------------------------------------------------------------
|
rt300@5
|
413 void Grid::snapCheck(){
|
rt300@38
|
414
|
rt300@38
|
415 if(interpolateMode == INTERPOLATE_GRID){
|
rt300@38
|
416 snapped = false;
|
rt300@38
|
417 closestPreset = NULL;
|
rt300@38
|
418 snapCentre = centre;
|
rt300@38
|
419 return;
|
rt300@38
|
420 } // no presets visible
|
rt300@38
|
421
|
rt300@5
|
422 // check environs for presets.
|
rt300@5
|
423 vector<Preset *> closePresets = presetManager.getPresetsInRange(centre - snapDist*scale, centre + snapDist*scale);
|
rt300@5
|
424 if(closePresets.size() > 0){
|
rt300@5
|
425 snapped = true;
|
rt300@5
|
426 // find closest
|
rt300@5
|
427 double dist, mindist = maxValue;
|
rt300@5
|
428 closestPreset = closePresets[0];
|
rt300@5
|
429
|
rt300@5
|
430 for(vector<Preset *>::iterator piter = closePresets.begin(); piter < closePresets.end(); piter++){
|
rt300@5
|
431 dist = (*piter)->coordinates.distanceTo(centre);
|
rt300@5
|
432
|
rt300@5
|
433 if(dist < mindist){
|
rt300@5
|
434 mindist = dist;
|
rt300@5
|
435 closestPreset = *piter;
|
rt300@5
|
436 }
|
rt300@5
|
437 }
|
rt300@5
|
438 snapCentre = closestPreset->coordinates;
|
rt300@22
|
439 eventLogger.logEvent(SNAPPED_TO_PRESET, getCoord(),closestPreset->creationTime );
|
rt300@35
|
440 //cout << "SNAPPED CHECK\n";
|
rt300@5
|
441 }else{
|
rt300@5
|
442 snapped = false;
|
rt300@7
|
443 closestPreset = NULL;
|
rt300@5
|
444 snapCentre = centre;
|
rt300@5
|
445 }
|
rt300@5
|
446
|
rt300@4
|
447
|
rt300@4
|
448 }
|
rt300@4
|
449 //--------------------------------------------------------------
|
rt300@5
|
450 void Grid::setMaxZoom(){ // got to smallest (white)
|
rt300@5
|
451 if(snapped)centre = snapCentre;
|
rt300@5
|
452 size.x = maxValue*2.0;
|
rt300@5
|
453 scale = size.x/pixSize.x;
|
rt300@5
|
454 size.y = scale * pixSize.y;
|
rt300@7
|
455 maxZoom = true;
|
rt300@7
|
456 minZoom = false;
|
rt300@5
|
457 viewWasChanged();
|
rt300@5
|
458 }
|
rt300@5
|
459 //--------------------------------------------------------------
|
rt300@5
|
460 void Grid::setMinZoom(){ // go to entire space (orange)
|
rt300@5
|
461 if(snapped)centre = snapCentre;
|
rt300@5
|
462 size.x = minValue*2.0;
|
rt300@5
|
463 scale = size.x/pixSize.x;
|
rt300@5
|
464 size.y = scale * pixSize.y;
|
rt300@7
|
465 minZoom = true;
|
rt300@7
|
466 maxZoom = false;
|
rt300@5
|
467 viewWasChanged();
|
rt300@4
|
468 }
|
rt300@4
|
469 //--------------------------------------------------------------
|
rt300@4
|
470 void Grid::viewWasChanged(){
|
rt300@5
|
471 snapCheck();
|
rt300@4
|
472 checkLimits();
|
rt300@5
|
473 // calculate new params?
|
rt300@5
|
474 vector<int> params;
|
rt300@5
|
475 if(snapped){
|
rt300@5
|
476 params = calculateParamsFromCoord(snapCentre);
|
rt300@5
|
477 }else{
|
rt300@37
|
478 if(interpolateMode == NO_INTERPOLATION){
|
rt300@37
|
479 params = calculateParamsFromCoord(centre);
|
rt300@37
|
480 }else if(interpolateMode == INTERPOLATE_GRID){
|
rt300@37
|
481 params = calculateInterpolatedParamsFromCoord(centre);
|
rt300@37
|
482 }
|
rt300@5
|
483 }
|
rt300@4
|
484 for(int i = 0;i<2*paramsPerDim;i++){
|
rt300@4
|
485 midiCC[i] = params[i];
|
rt300@4
|
486 }
|
rt300@4
|
487
|
rt300@4
|
488 }
|
rt300@4
|
489
|
rt300@4
|
490 //--------------------------------------------------------------
|
rt300@4
|
491 void Grid::checkLimits(){
|
rt300@4
|
492 // check for maximum zoom level
|
rt300@5
|
493
|
rt300@4
|
494
|
rt300@4
|
495 if(size.x > maxValue*2.0){
|
rt300@4
|
496 cout << "maxZoom\n";
|
rt300@4
|
497 maxZoom = true;
|
rt300@4
|
498 size.x = maxValue*2.0;
|
rt300@4
|
499 // need to also set y size back to
|
rt300@4
|
500 }
|
rt300@4
|
501 if(size.y > maxValue*2.0){
|
rt300@4
|
502 cout << "maxZoom\n";
|
rt300@4
|
503 maxZoom = true;
|
rt300@4
|
504 size.y = maxValue*2.0;
|
rt300@4
|
505 }
|
rt300@4
|
506
|
rt300@4
|
507 if(size.x < minValue){
|
rt300@4
|
508 cout << "min Zoom\n";
|
rt300@4
|
509 minZoom = true;
|
rt300@4
|
510 size.x = minValue;
|
rt300@4
|
511 // need to also set y size back to
|
rt300@4
|
512 }
|
rt300@4
|
513 if(size.y < minValue){
|
rt300@4
|
514 minZoom = true;
|
rt300@4
|
515 cout << "min Zoom\n";
|
rt300@4
|
516 size.y = minValue;
|
rt300@4
|
517 }
|
rt300@4
|
518
|
rt300@4
|
519 scale = size.x/pixSize.x;
|
rt300@4
|
520 size.y = scale * pixSize.y;
|
rt300@4
|
521
|
rt300@4
|
522 topLeft.x = centre.x - size.x/2;
|
rt300@4
|
523 topLeft.y = centre.y - size.y/2;
|
rt300@4
|
524 // check for negatives
|
rt300@4
|
525
|
rt300@4
|
526 // allow centre to be at limits
|
rt300@4
|
527 if((topLeft.x + size.x*0.5) < 0.0){
|
rt300@4
|
528 cout << "left Wall\n";
|
rt300@4
|
529 topLeft.x = 0.0 - size.x*0.5;
|
rt300@4
|
530 centre.x = 0.0;
|
rt300@4
|
531 }
|
rt300@4
|
532
|
rt300@4
|
533 if(topLeft.y + size.y*0.5 < 0.0) {
|
rt300@4
|
534 cout << "top Wall\n";
|
rt300@4
|
535 topLeft.y = 0.0 - size.y*0.5;
|
rt300@4
|
536 centre.y = 0.0;
|
rt300@4
|
537 }
|
rt300@4
|
538
|
rt300@4
|
539 // does topleft refer to lines or view?
|
rt300@4
|
540
|
rt300@4
|
541 // check max
|
rt300@4
|
542 if(topLeft.x + size.x/2 > maxValue){
|
rt300@4
|
543 cout << "right Wall\n";
|
rt300@4
|
544 topLeft.x = maxValue - size.x/2;
|
rt300@4
|
545 centre.x = maxValue;
|
rt300@4
|
546 }
|
rt300@4
|
547 if(topLeft.y + size.y/2 > maxValue) {
|
rt300@4
|
548 cout << "bottom Wall\n";
|
rt300@4
|
549 topLeft.y = maxValue - size.y/2;
|
rt300@4
|
550 centre.y = maxValue;
|
rt300@4
|
551 }
|
rt300@4
|
552
|
rt300@4
|
553 }
|
rt300@4
|
554 //--------------------------------------------------------------
|
rt300@4
|
555 void Grid::checkConsistencies(){
|
rt300@4
|
556 // debug function to check all the parameters are consistent maybe
|
rt300@4
|
557
|
rt300@4
|
558 }
|
rt300@4
|
559 //--------------------------------------------------------------
|
rt300@4
|
560 void Grid::setCoord(TwoVector coord){
|
rt300@4
|
561
|
rt300@4
|
562 centre = coord;
|
rt300@5
|
563
|
rt300@4
|
564 viewWasChanged();
|
rt300@4
|
565 }
|
rt300@4
|
566 //--------------------------------------------------------------
|
rt300@4
|
567 TwoVector Grid::getCoord(){
|
rt300@5
|
568 // return read point crosshairs
|
rt300@5
|
569 if(snapped){
|
rt300@5
|
570 return snapCentre;
|
rt300@5
|
571 }
|
rt300@4
|
572 return centre;
|
rt300@4
|
573 }
|
rt300@4
|
574 //--------------------------------------------------------------
|
rt300@4
|
575 vector<int> Grid::getParams(){
|
rt300@4
|
576 // return a vector with midiCCs in
|
rt300@4
|
577 // should we store params somewhere and use this as a low computation get ?
|
rt300@4
|
578 vector<int> params(2*paramsPerDim,0);
|
rt300@4
|
579 //
|
rt300@4
|
580 for(int i = 0;i<2*paramsPerDim;i++){
|
rt300@4
|
581 params[i] = midiCC[i];
|
rt300@4
|
582 }
|
rt300@4
|
583 return params;
|
rt300@4
|
584 }
|
rt300@4
|
585 //--------------------------------------------------------------
|
rt300@4
|
586 void Grid::setParams(vector<int> params){
|
rt300@4
|
587 // input midiCCs, and go to the appropriate coordinate
|
rt300@4
|
588
|
rt300@4
|
589 for(int i = 0;i<2*paramsPerDim;i++){
|
rt300@4
|
590 midiCC[i] = 64;
|
rt300@4
|
591 }
|
rt300@4
|
592 TwoVector coord = calculateCoordFromParams(params);
|
rt300@4
|
593 setCoord(coord);
|
rt300@4
|
594
|
rt300@20
|
595 // snapping?
|
rt300@4
|
596 viewWasChanged();
|
rt300@4
|
597
|
rt300@4
|
598 }
|
rt300@4
|
599 //--------------------------------------------------------------
|
rt300@5
|
600 //--------------------------------------------------------------
|
rt300@5
|
601 //--------------------------------------------------------------
|
rt300@5
|
602 //--------------------------------------------------------------
|
rt300@5
|
603 //--------------------------------------------------------------
|
rt300@5
|
604 #pragma mark const utils
|
rt300@5
|
605
|
rt300@37
|
606 TwoVector Grid::calculateCoordFromParams(vector<int> params){
|
rt300@4
|
607
|
rt300@4
|
608 vector<int> ccValueX(paramsPerDim);
|
rt300@4
|
609 vector<int> ccValueY(paramsPerDim);
|
rt300@4
|
610 for(int i=0;i<paramsPerDim;i++){
|
rt300@4
|
611 ccValueX[i] = params[i];
|
rt300@4
|
612 ccValueY[i] = params[i+paramsPerDim];
|
rt300@4
|
613 }
|
rt300@4
|
614
|
rt300@4
|
615 TwoVector result;
|
rt300@35
|
616 result.x = hilbert.calculateIndexFromParams(ccValueX);
|
rt300@35
|
617 result.y = hilbert.calculateIndexFromParams(ccValueY);
|
rt300@4
|
618 return result;
|
rt300@4
|
619 }
|
rt300@4
|
620
|
rt300@4
|
621 //--------------------------------------------------------------
|
rt300@37
|
622 vector<int> Grid::calculateParamsFromCoord(TwoVector coord){
|
rt300@35
|
623
|
rt300@4
|
624 if (coord.x > maxValue || coord.y > maxValue){
|
rt300@4
|
625 cout << "calculateParams Error: centre double value is too large" << "\n";
|
rt300@4
|
626 vector<int> empty;
|
rt300@4
|
627 return empty;
|
rt300@4
|
628 }
|
rt300@35
|
629 if (coord.x < 0 || coord.y < 0){
|
rt300@35
|
630 cout << "calculateParams Error: centre double value is negative" << "\n";
|
rt300@35
|
631 vector<int> empty;
|
rt300@35
|
632 return empty;
|
rt300@4
|
633 }
|
rt300@4
|
634
|
rt300@35
|
635 // X
|
rt300@35
|
636 vector<int> resultX;
|
rt300@35
|
637 resultX = hilbert.calculateParamsFromIndex(coord.x);
|
rt300@35
|
638 // Y
|
rt300@4
|
639 vector<int> resultY;
|
rt300@35
|
640 resultY = hilbert.calculateParamsFromIndex(coord.y);
|
rt300@4
|
641
|
rt300@4
|
642 // concatenate
|
rt300@35
|
643 vector<int> result = resultX;
|
rt300@35
|
644
|
rt300@4
|
645 result.insert( result.end(), resultY.begin(), resultY.end() );
|
rt300@4
|
646 return result;
|
rt300@4
|
647 }
|
rt300@37
|
648 //--------------------------------------------------------------
|
rt300@37
|
649 vector<int> Grid::calculateInterpolatedParamsFromCoord(TwoVector coord){
|
rt300@4
|
650
|
rt300@37
|
651 vector<int> result;
|
rt300@37
|
652
|
rt300@38
|
653 int interpLevelBigGrid = ceil(interpLevel);
|
rt300@38
|
654 int interpLevelSmallGrid = floor(interpLevel);
|
rt300@38
|
655
|
rt300@38
|
656 if(interpLevelBigGrid >= paramBitDepth){
|
rt300@38
|
657 // maximum is bit-1 so don't do interp at all
|
rt300@38
|
658 vector<int> pSmallGrid = interpHilbert(interpLevelSmallGrid, coord);
|
rt300@38
|
659 return pSmallGrid;
|
rt300@38
|
660 }
|
rt300@38
|
661 double interpFrac = interpLevel - interpLevelSmallGrid;
|
rt300@38
|
662
|
rt300@38
|
663 vector<int> pBigGrid = interpHilbert(interpLevelBigGrid, coord);
|
rt300@38
|
664 vector<int> pSmallGrid = interpHilbert(interpLevelSmallGrid, coord);
|
rt300@38
|
665 // now we need to interpolate twixt these two aswell!
|
rt300@38
|
666
|
rt300@38
|
667 result = interpVector(pBigGrid, pSmallGrid, interpFrac);
|
rt300@38
|
668
|
rt300@38
|
669 return result;
|
rt300@38
|
670 }
|
rt300@38
|
671 //--------------------------------------------------------------
|
rt300@38
|
672 vector<int> Grid::interpHilbert(int bitlevel, TwoVector coord){
|
rt300@38
|
673 vector<int> result;
|
rt300@37
|
674 long long x = coord.x;
|
rt300@37
|
675 long long y = coord.y;
|
rt300@38
|
676
|
rt300@38
|
677 int roundDigits = (paramsPerDim*bitlevel);
|
rt300@37
|
678 // knock off last digits
|
rt300@37
|
679 x = x >> roundDigits;
|
rt300@37
|
680 x = x << roundDigits;
|
rt300@37
|
681 float frac = (coord.x - x)/(1 << roundDigits);
|
rt300@37
|
682 long long xlower = x;
|
rt300@37
|
683 long long xupper = x + (1 << roundDigits);
|
rt300@38
|
684
|
rt300@37
|
685 vector<int> pupper = hilbert.calculateParamsFromIndex(xupper);
|
rt300@37
|
686 vector<int> plower = hilbert.calculateParamsFromIndex(xlower);
|
rt300@37
|
687
|
rt300@37
|
688 result = interpVector(pupper,plower,frac);
|
rt300@38
|
689 // now Y
|
rt300@37
|
690 y = y >> roundDigits;
|
rt300@37
|
691 y = y << roundDigits;
|
rt300@37
|
692 frac = (coord.y - y)/(1 << roundDigits);
|
rt300@37
|
693 long long ylower = y;
|
rt300@37
|
694 long long yupper = y + (1 << roundDigits);
|
rt300@37
|
695
|
rt300@37
|
696 pupper = hilbert.calculateParamsFromIndex(yupper);
|
rt300@37
|
697 plower = hilbert.calculateParamsFromIndex(ylower);
|
rt300@37
|
698
|
rt300@37
|
699 vector<int> resultY = interpVector(pupper,plower,frac);
|
rt300@38
|
700 // stickem together
|
rt300@37
|
701 result.insert( result.end(), resultY.begin(), resultY.end() );
|
rt300@38
|
702
|
rt300@37
|
703 return result;
|
rt300@37
|
704 }
|
rt300@37
|
705 //--------------------------------------------------------------
|
rt300@37
|
706 vector<int> Grid::interpVector(vector<int> upper, vector<int> lower, float frac){
|
rt300@37
|
707 vector<int> result;
|
rt300@37
|
708 if(upper.size() != lower.size()){
|
rt300@37
|
709 cout << "Error: interpVector takes vectors of same length" << "\n";
|
rt300@37
|
710 return result;
|
rt300@37
|
711 }
|
rt300@37
|
712 if(frac > 1){
|
rt300@37
|
713 cout << "Error: bad fraction for vector interpolation" << "\n";
|
rt300@37
|
714 return result;
|
rt300@37
|
715 }
|
rt300@37
|
716 int N = upper.size();
|
rt300@37
|
717 for(int i=0; i<N; i++){
|
rt300@37
|
718 result.push_back(frac*upper[i] + (1-frac)*lower[i]);
|
rt300@37
|
719 }
|
rt300@37
|
720 return result;
|
rt300@37
|
721 }
|
rt300@4
|
722 //--------------------------------------------------------------
|
rt300@4
|
723 vector<int> Grid::walkDiff(vector<bool> left, vector<bool> right){
|
rt300@4
|
724 // horrible
|
rt300@4
|
725 vector<int> result(2,0);
|
rt300@4
|
726
|
rt300@4
|
727 int size = left.size();
|
rt300@4
|
728 for(int i = 0; i < size; i++){
|
rt300@4
|
729 if(left[i] && !right[i]){ // 1 - 0
|
rt300@4
|
730 // dim
|
rt300@4
|
731 result[0] = i;
|
rt300@4
|
732 result[1] = 1;
|
rt300@4
|
733 }else if(!left[i] && right[i]){ // 0 - 1
|
rt300@4
|
734 result[0] = i;
|
rt300@4
|
735 result[1] = -1;
|
rt300@4
|
736 }else{ // equal do nothing
|
rt300@4
|
737
|
rt300@4
|
738
|
rt300@4
|
739 }
|
rt300@4
|
740 }
|
rt300@4
|
741
|
rt300@4
|
742 return result;
|
rt300@4
|
743 }
|
rt300@4
|
744 //--------------------------------------------------------------
|
rt300@4
|
745 //--------------------------------------------------------------
|
rt300@38
|
746 //--------------------------------------------------------------
|
rt300@38
|
747
|
rt300@38
|
748
|
rt300@38
|
749 //--------------------------------------------------------------
|
rt300@38
|
750 //--------------------------------------------------------------
|
rt300@38
|
751 //--------------------------------------------------------------
|
rt300@38
|
752 //--------------------------------------------------------------
|
rt300@38
|
753 //--------------------------------------------------------------
|
rt300@38
|
754 #pragma mark The old algorithm
|
rt300@38
|
755
|
rt300@38
|
756 TwoVector Grid::calculateCoordFromParamsOld(vector<int> params) const{
|
rt300@38
|
757
|
rt300@38
|
758 vector<int> ccValueX(paramsPerDim);
|
rt300@38
|
759 vector<int> ccValueY(paramsPerDim);
|
rt300@38
|
760 for(int i=0;i<paramsPerDim;i++){
|
rt300@38
|
761 ccValueX[i] = params[i];
|
rt300@38
|
762 ccValueY[i] = params[i+paramsPerDim];
|
rt300@38
|
763 }
|
rt300@38
|
764 vector<vector <bool> > codesX = midiToGray(ccValueX);
|
rt300@38
|
765 vector<vector <bool> > codesY = midiToGray(ccValueY);
|
rt300@38
|
766
|
rt300@38
|
767 vector<int> base32X = codesToBase32(codesX);
|
rt300@38
|
768 vector<int> base32Y = codesToBase32(codesY);
|
rt300@38
|
769
|
rt300@38
|
770 TwoVector result;
|
rt300@38
|
771 result.x = base32toCoord(base32X);
|
rt300@38
|
772 result.y = base32toCoord(base32Y);
|
rt300@38
|
773 return result;
|
rt300@38
|
774 }
|
rt300@38
|
775
|
rt300@38
|
776 //--------------------------------------------------------------
|
rt300@38
|
777 vector<int> Grid::calculateParamsFromCoordOld(TwoVector coord) const{
|
rt300@38
|
778 // some arrays in reverse order of power from normal numbers! 1,2,4,
|
rt300@38
|
779
|
rt300@38
|
780 // centre to base 32
|
rt300@38
|
781 if (coord.x > maxValue || coord.y > maxValue){
|
rt300@38
|
782 cout << "calculateParams Error: centre double value is too large" << "\n";
|
rt300@38
|
783 vector<int> empty;
|
rt300@38
|
784 return empty;
|
rt300@38
|
785 }
|
rt300@38
|
786
|
rt300@38
|
787 //--------------------------
|
rt300@38
|
788 // X
|
rt300@38
|
789 vector<int> base32x = coordTobase32(coord.x); // 7 numbers from 0 to 31
|
rt300@38
|
790 vector<vector <bool> > grayCodesX;
|
rt300@38
|
791
|
rt300@38
|
792 int size = base32x.size();
|
rt300@38
|
793 for(int i=0;i<size;i++){
|
rt300@38
|
794 grayCodesX.push_back(intToGray(base32x[i]));
|
rt300@38
|
795 }
|
rt300@38
|
796
|
rt300@38
|
797 vector<int> result;
|
rt300@38
|
798 result = grayToMidi(grayCodesX);
|
rt300@38
|
799 //--------------------------
|
rt300@38
|
800 // AND FOR Y
|
rt300@38
|
801 vector<int> base32y = coordTobase32(coord.y);
|
rt300@38
|
802 vector<vector <bool> > grayCodesY;
|
rt300@38
|
803
|
rt300@38
|
804 size = base32y.size();
|
rt300@38
|
805 for(int i=0;i<size;i++){
|
rt300@38
|
806 grayCodesY.push_back(intToGray(base32y[i]));
|
rt300@38
|
807 }
|
rt300@38
|
808
|
rt300@38
|
809 vector<int> resultY;
|
rt300@38
|
810 resultY = grayToMidi(grayCodesY);
|
rt300@38
|
811
|
rt300@38
|
812 // concatenate
|
rt300@38
|
813 result.insert( result.end(), resultY.begin(), resultY.end() );
|
rt300@38
|
814 return result;
|
rt300@38
|
815 }
|
rt300@38
|
816 //-------------------------------------------------------------------
|
rt300@38
|
817
|
rt300@38
|
818 // for 1 dimension!!! i.e. call this twice
|
rt300@38
|
819 vector<int> Grid::grayToMidi(vector<vector <bool> > grayCodes) const{
|
rt300@38
|
820
|
rt300@38
|
821 // gray to midi CC values
|
rt300@38
|
822 // loop thru the scales to build up a CC number per dimension
|
rt300@38
|
823 int midiCCresult[paramsPerDim]; // TODO dims specific
|
rt300@38
|
824 for(int i=0;i<paramsPerDim;i++){
|
rt300@38
|
825 midiCCresult[i] = 0;
|
rt300@38
|
826 }
|
rt300@38
|
827
|
rt300@38
|
828 int bin = 1;
|
rt300@38
|
829 bin = bin << grayCodes.size()-1;
|
rt300@38
|
830
|
rt300@38
|
831 int midP = 0;
|
rt300@38
|
832
|
rt300@38
|
833 vector<vector <bool> >::iterator piter = grayCodes.begin();
|
rt300@38
|
834 for(;piter < grayCodes.end();piter++){ // each lesser power of 2
|
rt300@38
|
835 midP = 0; // reset
|
rt300@38
|
836 vector<bool>::iterator diter = (*piter).begin();
|
rt300@38
|
837 for(; diter <= (*piter).end();diter++){ // each one is different dimension
|
rt300@38
|
838 int ig = int(*diter); // convert binary to int
|
rt300@38
|
839 //cout << "ig: " << ig;
|
rt300@38
|
840 midiCCresult[midP] += ig*bin; // mult by power of two
|
rt300@38
|
841 midP++;
|
rt300@38
|
842 }
|
rt300@38
|
843 bin = bin >> 1; // next power of 2 down
|
rt300@38
|
844
|
rt300@38
|
845 }
|
rt300@38
|
846
|
rt300@38
|
847 // put in vector
|
rt300@38
|
848 vector<int> result;
|
rt300@38
|
849 for(int i=0;i<paramsPerDim;i++){
|
rt300@38
|
850 result.push_back(midiCCresult[i]);
|
rt300@38
|
851 }
|
rt300@38
|
852
|
rt300@38
|
853 return result;
|
rt300@38
|
854
|
rt300@38
|
855
|
rt300@38
|
856 }
|
rt300@38
|
857
|
rt300@38
|
858
|
rt300@38
|
859 //--------------------------------------------------------------
|
rt300@38
|
860
|
rt300@38
|
861 vector<bool> Grid::intToGray(int num, int dimToTravel) const{
|
rt300@38
|
862
|
rt300@38
|
863 // dimToTravel - this is the dimension that we want the walk to traverse, i.e. the last non zero digit of the code
|
rt300@38
|
864 // so swap it for 3 to produce correct orientation
|
rt300@38
|
865
|
rt300@38
|
866 // just use look up table... until it gets huuuge.
|
rt300@38
|
867 vector<bool> grayCode = vcode[num];
|
rt300@38
|
868 grayCode[3] = vcode[num][dimToTravel];
|
rt300@38
|
869 grayCode[dimToTravel] = vcode[num][3];
|
rt300@38
|
870 return grayCode;
|
rt300@38
|
871 }
|
rt300@38
|
872 //--------------------------------------------------------------
|
rt300@38
|
873 /*
|
rt300@38
|
874 breaks down float into a base 32 number (2^D)
|
rt300@38
|
875 each of these is converted to gray code
|
rt300@38
|
876 this is then converted to 10 parameters, where each 32-digit becomes separate power of 2
|
rt300@38
|
877 so "zoom" grid 32-patches are converted to 2-scales. 1,2,4,8,16,32,64,(128?) biggest number is 32^7
|
rt300@38
|
878 */
|
rt300@38
|
879 vector<int> Grid::coordTobase32(double value) const{
|
rt300@38
|
880 //double base = 32.0;
|
rt300@38
|
881 if(value < 0.0){
|
rt300@38
|
882 cout << "coordTobase32 error: input value is negative\n";
|
rt300@38
|
883 value = 0.0;
|
rt300@38
|
884 }else if(value > maxValue){
|
rt300@38
|
885 cout << "coordTobase32 error: input value too big!\n";
|
rt300@38
|
886 }
|
rt300@38
|
887 double rem, divdr = 0.0;
|
rt300@38
|
888 int digit32;
|
rt300@38
|
889
|
rt300@38
|
890 // what power of 32 to start at?
|
rt300@38
|
891 int maxpow = 7; // midi cc specific
|
rt300@38
|
892 vector<int> result;
|
rt300@38
|
893
|
rt300@38
|
894 rem = value;
|
rt300@38
|
895 for(;maxpow >=0;maxpow--){
|
rt300@38
|
896 // repeatedly get digit and remainder. This is exactly what we're doing in draw !?... could put all this in one place?
|
rt300@38
|
897 divdr = pow((double)codeLength,(double)maxpow);
|
rt300@38
|
898 digit32 = floor(rem/divdr);
|
rt300@38
|
899 rem = rem - digit32*divdr;
|
rt300@38
|
900 result.push_back(digit32); // array, biggest index is smallest power
|
rt300@38
|
901 }
|
rt300@38
|
902 // at this point rem should be fractional... use for interp?
|
rt300@38
|
903
|
rt300@38
|
904 return result;
|
rt300@38
|
905 }
|
rt300@38
|
906 //--------------------------------------------------------------
|
rt300@38
|
907
|
rt300@38
|
908 vector<int> Grid::codesToBase32(vector<vector<bool> > inCodes) const{
|
rt300@38
|
909 vector<int> result;
|
rt300@38
|
910 for(int i=0;i<paramBitDepth;i++){
|
rt300@38
|
911 result.push_back(grayToInt(inCodes[i]));
|
rt300@38
|
912 }
|
rt300@38
|
913 return result;
|
rt300@38
|
914 }
|
rt300@38
|
915 //--------------------------------------------------------------
|
rt300@38
|
916
|
rt300@38
|
917 int Grid::grayToInt(vector<bool> incode) const{
|
rt300@38
|
918 // look for match in table
|
rt300@38
|
919
|
rt300@38
|
920 int s = vcode.size();
|
rt300@38
|
921
|
rt300@38
|
922 for(int i=0; i<s;i++){ // fill vector
|
rt300@38
|
923 if(vcode[i] == incode){
|
rt300@38
|
924 return i;
|
rt300@38
|
925 }
|
rt300@38
|
926
|
rt300@38
|
927 }
|
rt300@38
|
928 cout << "grayToInt error: no matching code found!";
|
rt300@38
|
929 return -1;
|
rt300@38
|
930 }
|
rt300@38
|
931 //--------------------------------------------------------------
|
rt300@38
|
932 double Grid::base32toCoord(vector<int> base32Digs) const{
|
rt300@38
|
933 // build up the big float from a base 32 number
|
rt300@38
|
934 double result = 0.0;
|
rt300@38
|
935
|
rt300@38
|
936 // what power of 32 to start at?
|
rt300@38
|
937 int mpow = base32Digs.size() - 1; // should be 7...
|
rt300@38
|
938
|
rt300@38
|
939 for(int p=0;p<=mpow;p++){ // biggest index is smallest power
|
rt300@38
|
940 result += ((double)base32Digs[p]) * pow(32.0, (double)mpow-p);
|
rt300@38
|
941
|
rt300@38
|
942 }
|
rt300@38
|
943 return result;
|
rt300@38
|
944
|
rt300@38
|
945 }
|
rt300@38
|
946 //--------------------------------------------------------------
|
rt300@38
|
947 // for 1 dimension - takes in 5 cc params and outputs 7 codes
|
rt300@38
|
948 vector<vector <bool> > Grid::midiToGray(vector<int> ccValue) const{
|
rt300@38
|
949 int pow2 = 1 << (paramBitDepth-1);
|
rt300@38
|
950
|
rt300@38
|
951 vector<int> aCode(paramsPerDim);
|
rt300@38
|
952 vector<vector<int> > theXCodes(paramBitDepth,aCode);
|
rt300@38
|
953
|
rt300@38
|
954 // build up binary gray code representations from the bits of the midi ccs
|
rt300@38
|
955
|
rt300@38
|
956 vector<vector<bool> > theCodes(paramBitDepth, vector<bool>(paramsPerDim));
|
rt300@38
|
957
|
rt300@38
|
958 // x
|
rt300@38
|
959 for(int p=0;p<paramBitDepth;p++){
|
rt300@38
|
960
|
rt300@38
|
961 for(int i=0;i<paramsPerDim;i++){
|
rt300@38
|
962
|
rt300@38
|
963 bool bit = (pow2 == (ccValue[i] & pow2));
|
rt300@38
|
964 theCodes[p][i] = bit;
|
rt300@38
|
965 }
|
rt300@38
|
966 pow2 = pow2 >> 1;
|
rt300@38
|
967
|
rt300@38
|
968 }
|
rt300@38
|
969 return theCodes;
|
rt300@38
|
970 }
|
rt300@38
|
971
|
rt300@38
|
972 //--------------------------------------------------------------
|
rt300@38
|
973 void Grid::makeCode(){
|
rt300@38
|
974
|
rt300@38
|
975 ////////////////////////////////~~~,,,,,,,,.........
|
rt300@38
|
976 ////////// gray code generation
|
rt300@38
|
977 //////
|
rt300@38
|
978 ///
|
rt300@38
|
979 // TODO 5bit specific
|
rt300@38
|
980 // transition sequence.... what a palaver! only need to do once though.
|
rt300@38
|
981
|
rt300@38
|
982 // max MRL
|
rt300@38
|
983 int trans[] = {0,1,2,3,0,4,2,1,0,3,2,1,0,4,2,3,0,1,2,3,0,4,2,1,0,3,2,1,0,4,2,3};
|
rt300@38
|
984 // balanced
|
rt300@38
|
985 int transB[] = {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};
|
rt300@38
|
986 for(int i = 0; i<codeLength; i++){
|
rt300@38
|
987 transB[i] = transB[i] - 1;
|
rt300@38
|
988 }
|
rt300@38
|
989
|
rt300@38
|
990 int code[codeLength][paramsPerDim]; // start with normal array
|
rt300@38
|
991
|
rt300@38
|
992
|
rt300@38
|
993
|
rt300@38
|
994 for(int j=0; j<paramsPerDim; j++){
|
rt300@38
|
995 code[0][j] = 0;
|
rt300@38
|
996 }
|
rt300@38
|
997
|
rt300@38
|
998 for(int i=0; i < codeLength-1; i++){ // don't need last 3
|
rt300@38
|
999 transSeq.push_back(trans[i]); // member vector
|
rt300@38
|
1000 for(int j=0; j<paramsPerDim; j++){
|
rt300@38
|
1001
|
rt300@38
|
1002 if (j == abs(trans[i])){
|
rt300@38
|
1003 code[i+1][j] = !code[i][j];
|
rt300@38
|
1004 }else{
|
rt300@38
|
1005 code[i+1][j] = code[i][j];
|
rt300@38
|
1006 }
|
rt300@38
|
1007 }
|
rt300@38
|
1008
|
rt300@38
|
1009 }
|
rt300@38
|
1010
|
rt300@38
|
1011 for(int i=0; i < codeLength; i++){ // fill vector
|
rt300@38
|
1012
|
rt300@38
|
1013 vcode.push_back(vector<bool>());
|
rt300@38
|
1014
|
rt300@38
|
1015 for(int j=0; j<paramsPerDim; j++){
|
rt300@38
|
1016 vcode[i].push_back(code[i][j]);
|
rt300@38
|
1017 }
|
rt300@38
|
1018 }
|
rt300@38
|
1019 /*
|
rt300@38
|
1020 // now try with iterators to print...
|
rt300@38
|
1021 vector< vector<bool> >::iterator cit;
|
rt300@38
|
1022 //vector<bool>::iterator bit;
|
rt300@38
|
1023 vector<bool>::iterator bit;
|
rt300@38
|
1024 int i = 0;
|
rt300@38
|
1025 for(cit=vcode.begin(); cit!=vcode.end() ; cit++){ // fill vector
|
rt300@38
|
1026 i++;
|
rt300@38
|
1027 cout << i << " = ";
|
rt300@38
|
1028 bit = (*cit).begin();
|
rt300@38
|
1029 for(bit=(*cit).begin(); bit!=(*cit).end() ; bit++){
|
rt300@38
|
1030
|
rt300@38
|
1031 cout << *bit;
|
rt300@38
|
1032 }
|
rt300@38
|
1033 cout << "\n";
|
rt300@38
|
1034
|
rt300@38
|
1035 }
|
rt300@38
|
1036
|
rt300@38
|
1037 // codeToInt unit test
|
rt300@38
|
1038 vector<bool> aCode = vcode[12];
|
rt300@38
|
1039 int result = grayToInt(aCode);
|
rt300@38
|
1040 cout << "GRAY TO INT : " << result << "\n";
|
rt300@38
|
1041
|
rt300@38
|
1042 cout << "-------------------------------\n";
|
rt300@38
|
1043
|
rt300@38
|
1044 // base32toFloat unit test
|
rt300@38
|
1045
|
rt300@38
|
1046 vector<int> test32 = coordTobase32(869437.0);
|
rt300@38
|
1047 double cresult = base32toCoord(test32);
|
rt300@38
|
1048 cout << "base32toCoord : " << cresult << "\n";
|
rt300@38
|
1049
|
rt300@38
|
1050 cout << "-------------------------------\n";
|
rt300@38
|
1051
|
rt300@38
|
1052
|
rt300@38
|
1053
|
rt300@38
|
1054 // midiToGray unit test
|
rt300@38
|
1055
|
rt300@38
|
1056 vector<vector<bool> > incodes(paramBitDepth, vector<bool>(paramsPerDim));
|
rt300@38
|
1057 for(int i=0;i<7;i++){
|
rt300@38
|
1058 incodes[i] = vcode[i+5];
|
rt300@38
|
1059 }
|
rt300@38
|
1060
|
rt300@38
|
1061 vector<int> midiTest;
|
rt300@38
|
1062 midiTest = grayToMidi(incodes);
|
rt300@38
|
1063 vector<vector<bool> > outcodes = midiToGray(midiTest);
|
rt300@38
|
1064
|
rt300@38
|
1065 vector< vector<bool> >::iterator cit1;
|
rt300@38
|
1066 //vector<bool>::iterator bit;
|
rt300@38
|
1067 vector<bool>::iterator bit1;
|
rt300@38
|
1068 cout << "midiToGray Unit test\n";
|
rt300@38
|
1069 for(int i=0;i<paramBitDepth;i++){ // fill vector
|
rt300@38
|
1070
|
rt300@38
|
1071 for(int j=0;j<paramsPerDim;j++){
|
rt300@38
|
1072
|
rt300@38
|
1073 cout << (incodes[i][j] == outcodes[i][j]) << ",";
|
rt300@38
|
1074 }
|
rt300@38
|
1075 cout << "\n";
|
rt300@38
|
1076
|
rt300@38
|
1077 }
|
rt300@38
|
1078 cout << "-------------------------------\n";
|
rt300@38
|
1079
|
rt300@38
|
1080
|
rt300@38
|
1081 // whole process unit test(?)
|
rt300@38
|
1082
|
rt300@38
|
1083 TwoVector inCoord(888999777,777666555);
|
rt300@38
|
1084 vector<int> checkParam;
|
rt300@38
|
1085 checkParam = calculateParamsFromCoord(inCoord);
|
rt300@38
|
1086 for(int i=0; i<2*paramsPerDim;i++){
|
rt300@38
|
1087 cout << checkParam[i] << "_";
|
rt300@38
|
1088 }
|
rt300@38
|
1089 TwoVector outCoord = calculateCoordFromParams(checkParam);
|
rt300@38
|
1090 cout << "Unit TEst coord = " << outCoord.x << "," << outCoord.y << "\n";
|
rt300@38
|
1091 */
|
rt300@38
|
1092
|
rt300@38
|
1093 }
|
rt300@38
|
1094 //--------------------------------------------------------------
|
rt300@38
|
1095 //--------------------------------------------------------------
|
rt300@38
|
1096 //--------------------------------------------------------------
|