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