summaryrefslogtreecommitdiff
path: root/offsetProject/src/ofApp.cpp
blob: ab02ea27c00e50836bcb05cf8bb857d338816264 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "ofApp.h"


//--------------------------------------------------------------
void ofApp::setup() {
	ofSetLogLevel(OF_LOG_WARNING);
	
	// enable depth->video image calibration
	kinect.setRegistration(true);
    
	kinect.init();
	//kinect.init(true); // shows infrared instead of RGB video image
	//kinect.init(false, false); // disable video image (faster fps)
	
	kinect.open();		// opens first available kinect
	//kinect.open(1);	// open a kinect by id, starting with 0 (sorted by serial # lexicographically))
	//kinect.open("A00362A08602047A");	// open a kinect using it's unique serial #
	
	// print the intrinsic IR sensor values
	if(kinect.isConnected()) {
		ofLogNotice() << "sensor-emitter dist: " << kinect.getSensorEmitterDistance() << "cm";
		ofLogNotice() << "sensor-camera dist:  " << kinect.getSensorCameraDistance() << "cm";
		ofLogNotice() << "zero plane pixel size: " << kinect.getZeroPlanePixelSize() << "mm";
		ofLogNotice() << "zero plane dist: " << kinect.getZeroPlaneDistance() << "mm";
	}
	
	
	colourImage.allocate(kinect.width, kinect.height);
	depthImage.allocate(kinect.width, kinect.height);
	
	farThreshold = 70;
	
	ofSetFrameRate(60);
	
	// zero the tilt on startup
	angle = 0;
	kinect.setCameraTiltAngle(angle);

	//start with the largest size square
	//get the depth, if below a certain size, subdivide and repeat

	//OR start at the smallest size, if adjacent squares are the same, work up

	//1 - create a pyramid of mip maps of depth
	//2 - work upwards

	//a find layout of largest tiles
	//b extend captured frame to this ratio
	//c mip map this down into prepared containers

	int tiles_w=ceil((float)kinect.width/(MAX_TILE_SIZE*2))*2;
	int tiles_h=ceil((float)kinect.height/(MAX_TILE_SIZE*2))*2;
	extend_w=(tiles_w)*MAX_TILE_SIZE;
	extend_h=(tiles_h)*MAX_TILE_SIZE;

	//get number of levels
	levels=0;
	for (int i=MIN_TILE_SIZE;i<=MAX_TILE_SIZE;i*=2) {
		levels++;
	}

	colourTiles.resize(levels);
	depthTiles.resize(levels);

	for (int i=MIN_TILE_SIZE,l=0;i<=MAX_TILE_SIZE;i*=2,l++) {
		colourTiles[l].allocate(extend_w/i,extend_h/i);
		depthTiles[l].allocate(extend_w/i,extend_h/i);
		cerr<<"level "<<l<<" mipmap: "<<colourTiles[l].getWidth()<<"x"<<colourTiles[l].getHeight()<<endl;
	}
	
}

//--------------------------------------------------------------
void ofApp::update() {
	
	ofBackground(100, 100, 100);
	
	kinect.update();
	
	// there is a new frame and we are connected
	if(kinect.isFrameNew()) {
		
		depthImage.setFromPixels(kinect.getDepthPixels(), kinect.width, kinect.height);
		colourImage.setFromPixels(kinect.getPixels(), kinect.width, kinect.height);
		
		depthImage.threshold(farThreshold);

		//threshold needs to be multiplied by the original
		depthImage2.setFromPixels(kinect.getDepthPixels(), kinect.width, kinect.height);
		depthImage*=depthImage2;

		depthImage.extend(extend_w,extend_h);
		colourImage.extend(extend_w,extend_h);
		
		// mark pixels and texture dirty
		depthImage.flagImageChanged();
		colourImage.flagImageChanged();

		ofxCvColorImage *prevCol=&colourImage;
		ofxCvGrayscaleImage *prevDepth=&depthImage;

		for (int i=0;i<colourTiles.size();i++){
			colourTiles[i].scaleIntoMe(*prevCol);
			depthTiles[i].scaleIntoMe(*prevDepth);
			prevCol=&colourTiles[i];
			prevDepth=&depthTiles[i];

			colourTiles[i].flagImageChanged();
			depthTiles[i].flagImageChanged();
		}
	}
}

void ofApp::checktile(int level,int x,int y,int size){
	int levels_factor=255/levels;
	if (depthTiles[level].getPixels()[y*((int)depthTiles[level].getWidth())+x]>level*levels_factor&&level>0){
		for (int i=0;i<2;i++){
			for (int j=0;j<2;j++){
				checktile(level-1,x*2+i,y*2+j,size/2);
			}
		}
	}
	else {
		ofSetColor(colourTiles[level].getPixels()[(y*((int)colourTiles[level].getWidth())+x)*3],
			colourTiles[level].getPixels()[(y*((int)colourTiles[level].getWidth())+x)*3+1],
			colourTiles[level].getPixels()[(y*((int)colourTiles[level].getWidth())+x)*3+2]);
		ofRect(x*size,y*size,size,size);
	}
}

//--------------------------------------------------------------
void ofApp::draw() {

	ofSetColor(255, 255, 255);

	//recursively draw pixels

	int pixelsize=ofGetWidth()/colourTiles[levels-1].getWidth();
	//int yoffset=

	for (int i=0;i<colourTiles[levels-1].getWidth();i++){
		for (int j=0;j<colourTiles[levels-1].getHeight();j++){
			checktile(levels-1,i,j,pixelsize);
		}
	}
	
	/*

	depthImage.draw(10, 10, 640, 480);
	colourImage.draw(10, 500, 640, 480);

	int xoffs=660;
	for (int i=0;i<depthTiles.size();i++){
		depthTiles[i].draw(xoffs,10,depthTiles[i].getWidth(),depthTiles[i].getHeight());
		colourTiles[i].draw(xoffs,500,colourTiles[i].getWidth(),colourTiles[i].getHeight());
		xoffs+=(colourTiles[i].getWidth()+10);
	}

	*/


	/*
	
	// draw from the live kinect
	kinect.drawDepth(10, 10, 400, 300);
	kinect.draw(420, 10, 400, 300);
	
	depthImage.draw(10, 320, 400, 300);
		
	
	// draw instructions
	ofSetColor(255, 255, 255);
	stringstream reportStream;
    
	reportStream << "press p to switch between images and point cloud, rotate the point cloud with the mouse" << endl
	<< "set far threshold " << farThreshold 
	<< ", fps: " << ofGetFrameRate() << endl
	<< "press c to close the connection and o to open it again, connection is: " << kinect.isConnected() << endl;

    if(kinect.hasCamTiltControl()) {
    	reportStream << "press UP and DOWN to change the tilt angle: " << angle << " degrees" << endl
        << "press 1-5 & 0 to change the led mode" << endl;
    }
    
	ofDrawBitmapString(reportStream.str(), 20, 652);

	*/
    
}



//--------------------------------------------------------------
void ofApp::exit() {
	kinect.setCameraTiltAngle(0); // zero the tilt on exit
	kinect.close();
	
}

//--------------------------------------------------------------
void ofApp::keyPressed (int key) {
	switch (key) {
			
		case '>':
		case '.':
			farThreshold ++;
			if (farThreshold > 255) farThreshold = 255;
			break;
			
		case '<':
		case ',':
			farThreshold --;
			if (farThreshold < 0) farThreshold = 0;
			break;
			
			
		case 'w':
			kinect.enableDepthNearValueWhite(!kinect.isDepthNearValueWhite());
			break;
			
		case 'o':
			kinect.setCameraTiltAngle(angle); // go back to prev tilt
			kinect.open();
			break;
			
		case 'c':
			kinect.setCameraTiltAngle(0); // zero the tilt
			kinect.close();
			break;
			
		case '1':
			kinect.setLed(ofxKinect::LED_GREEN);
			break;
			
		case '2':
			kinect.setLed(ofxKinect::LED_YELLOW);
			break;
			
		case '3':
			kinect.setLed(ofxKinect::LED_RED);
			break;
			
		case '4':
			kinect.setLed(ofxKinect::LED_BLINK_GREEN);
			break;
			
		case '5':
			kinect.setLed(ofxKinect::LED_BLINK_YELLOW_RED);
			break;
			
		case '0':
			kinect.setLed(ofxKinect::LED_OFF);
			break;
			
		case OF_KEY_UP:
			angle++;
			if(angle>30) angle=30;
			kinect.setCameraTiltAngle(angle);
			break;
			
		case OF_KEY_DOWN:
			angle--;
			if(angle<-30) angle=-30;
			kinect.setCameraTiltAngle(angle);
			break;
	}
}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button)
{}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button)
{}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button)
{}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h)
{}