-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcameraControl.pde
162 lines (127 loc) · 7.74 KB
/
cameraControl.pde
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
/*
float roundToNearest( float unRounded, float nth ) {
//round up to the nearest nth
float rounded = round( unRounded / nth ) * nth;
return rounded;
}
void camSetup(){
//setup camera connection
String[] cams = Capture.list();
println( cams );
myCapture = new Capture(this, 640, 480, cams[8], 60);
//initialize the buffer for axonometric view
dest.loadPixels();
//initialize values for pixel distance comparison
for( int p = 0; p < pixelDists.length; p++ ){
pixelDists[p] = idealStep * 5;
}
}
void camUpdate(){
//draw the camera view
image(myCapture, width/2, height/1.6, 320, 240 );
//and indicate where centerframe is
stroke( 255, 0, 0, 50 );
fill( 255, 50 );
rect( width/2 + 160 - (camSample.width/2), height/1.6 + 120 - camSample.height/2, camSample.width, camSample.height );
//rect( width/2 + (myCapture.width/2) - (camSample.width/2), height/1.6 + myCapture.height/2 - camSample.height/2, camSample.width, camSample.height );
//get the position of the cam rounded up to the nearest physical pixel dimension
if( prevX != myCNC.xPos) {
currentX = myCNC.xPos;
roundedX = roundToNearest( currentX, idealStep );
prevX = currentX;
}
if( prevY != myCNC.yPos ){
currentY = myCNC.yPos;
roundedY = roundToNearest( currentY, idealStep );
prevY = currentY;
}
//draw the calculated axonometric view
image(dest, 5, height/1.6, 288, 288);
//and indicate where in the scan we are
rectMode(CENTER);
int pixelX = int( roundedX / idealStep );
int pixelY = int( roundedY / idealStep );
rect( map(pixelX + 5, 5, dest.width, 5, 293 ), map(pixelY + height/1.6, height/1.6, dest.height + height/1.6, height/1.6, height/1.6 + 288), camSample.width, camSample.height );
//get a sample from center of the cam
camSample = myCapture.get( myCapture.width/2 - (camSample.width/2), myCapture.height/2 - (camSample.height/2), camSample.width, camSample.height );
camSample.loadPixels();
if( myCNC.streaming ){
/*
float xOffset = pixelX - camSample.width/2;
float yOffset = pixelY - camSample.height/2;
//map each pixel from the camera sample to the correct part of the destination axonometric image
for( int p = 0; p < camSample.pixels.length; p++ ){
float destX = p % camSample.width + xOffset;
float destY = floor( p / camSample.height ) + yOffset;
if( destX >= 0 && destY >= 0 && destX <= dest.width && destY <= dest.height ){
int destIndex = int(destY) * dest.width + int(destX); //pixels[y*width+x]
dest.pixels[ destIndex ] = camSample.pixels[p];
println( "mapping " + hex(camSample.pixels[p]) + " from " + p + " (" + destX + "," + destY + ") to" + destIndex );
dest.updatePixels();
}
}*/
/*
##############################################
//get the x/y pixel coordinates in the dest img idealStep * camSample.height/2
int targetX = int( roundedX / idealStep );
int targetY = int( roundedY / idealStep );
println( "X " + myCNC.xPos + " " + roundedX + " " + targetX + " Y " + myCNC.yPos + " " + roundedY + " " + targetY );
dest.set( targetX - camSample.width/2, targetY - camSample.height/2, camSample );
##############################################
*/
/*
//##############################################
//look at each pixel in the camera sample
for( float h = 0; h < camSample.height; h++ ){
for( float w = 0; w < camSample.width; w++ ){
//get the actual position of each pixel in the camera sample
float realLocalX = currentX - float(camSample.width)/2.0 + w;
float realLocalY = currentY - float(camSample.height)/2.0 + h;
//get the closest rounded pixel position (as it would be in the destination image)
int roundedLocalX = int( roundedX - camSample.width/2 + w );
int roundedLocalY = int( roundedY - camSample.height/2 + h );
print( "(w" + w + ", h" + h + ") RLX " + roundedLocalX + " RLY " + roundedLocalY );
if( roundedLocalX >= 0 && roundedLocalY >= 0 ){
//get the distance between the actual & ideal pixel locations
float distance = dist( realLocalX, realLocalY, roundedLocalX, roundedLocalY );
//see if the distance is closer than what was previously recorded
int pixIndex = int( roundedLocalY * dest.width + roundedLocalX );
//if( distance < pixelDists[ pixIndex ] ){
//println( realLocalX + " " + realLocalY + " " + roundedLocalX + " " + roundedLocalY + " " + distance + " " + pixIndex );
//if it's closer, save that pixel to the dest buffer
dest.pixels[ pixIndex ] = camSample.pixels[ int(h) * camSample.width + int(w) ];
print( "mapping px " + (int(h) * camSample.width + int(w)) + " from camsample to " + pixIndex + " of dest " );
pixelDists[ pixIndex ] = distance;
dest.updatePixels();
//}
}
println("");
}
}
//##############################################
*/
/*
//##############################################
//get the x/y pixel coordinates in the dest img
int targetX = int( roundedX / idealStep );
int targetY = int( roundedY / idealStep );
println( "X " + myCNC.xPos + " " + roundedX + " " + targetX + " Y " + myCNC.yPos + " " + roundedY + " " + targetY );
dest.set( targetX, targetY, camSample );
//##############################################
*/
/* ##############################################
//compare the sample pixels to whats in the destination
for( int yRep = 0; yRep < 2; yRep++ ){
for( int xRep = 0; xRep < 2; xRep++ ){
int targetPix = targetY * dest.width + ( targetX + xRep % 2 );
//if( dest.pixels[ targetPix ] == 0 ){
// println( "setting dest " + ( targetX + xRep % 2 ) + "," + targetY + " to " + camSample.pixels[ yRep * camSample.width + xRep ] );
dest.pixels[ targetPix ] = myCapture.pixels[ myCapture.height/2 - 1 + yRep * myCapture.width + myCapture.width/2 - 1 + xRep ];
dest.updatePixels();
//}
}
targetY++;
}
############################################## */
// }
//}