//---------------------------------------------------- void guiQuad::draw(float x, float y, float width, float height, int red, int green, int blue, int thickness){ getScaledQuadPoints(width, height); glPushMatrix(); glTranslatef(x, y, 0); ofNoFill(); ofSetColor(red, green, blue); glLineWidth(thickness); glBegin(GL_LINE_LOOP); for(int i = 0; i < 4; i++){ glVertex2f(srcScaled[i].x, srcScaled[i].y); } glEnd(); glLineWidth(1); ofSetRectMode(OF_RECTMODE_CENTER); for(int i = 0; i < 4; i++){ if(i == 0)ofSetColor(255, 0, 0); if(i == 1)ofSetColor(0, 255, 0); if(i == 2)ofSetColor(0, 0, 255); if(i == 3)ofSetColor(0, 255, 255); ofRect(srcScaled[i].x, srcScaled[i].y, 8, 8); } ofSetColor(255, 0, 0); ofCircle(anchorPointScaled.x, anchorPointScaled.y, 4); ofDrawBitmapString("+", anchorPointScaled.x - 4, anchorPointScaled.y + 4); ofSetRectMode(OF_RECTMODE_CORNER); ofFill(); glPopMatrix(); }
//---------------------------------------------------- void ofxCvCoordWarpingGui::draw(float passedX, float passedY, float scaleWidth, float scaleHeight, int red, int green, int blue, int thickness){ getScaledQuadPoints(scaleWidth, scaleHeight); ofPushMatrix(); ofPushStyle(); ofTranslate(passedX, passedY); ofNoFill(); ofSetColor(red, green, blue); ofSetLineWidth(thickness); ofBeginShape(); for(int i = 0; i < 4; i++){ ofVertex(srcScaled[i].x, srcScaled[i].y); } ofEndShape(true); ofFill(); glLineWidth(1); ofSetRectMode(OF_RECTMODE_CENTER); for(int i = 0; i < 4; i++){ if(i == 0)ofSetColor(255, 0, 0); if(i == 1)ofSetColor(0, 255, 0); if(i == 2)ofSetColor(255, 255, 0); if(i == 3)ofSetColor(0, 255, 255); ofRect(srcScaled[i].x, srcScaled[i].y, 8, 8); } ofSetRectMode(OF_RECTMODE_CORNER); ofFill(); ofSetColor(255,255,255,50); ofSetPolyMode(OF_POLY_WINDING_ODD); ofBeginShape(); ofVertex(0, 0); ofVertex(scaleWidth, 0); ofVertex(scaleWidth, scaleHeight); ofVertex(0, scaleHeight); ofVertex(0, 0); for(int i = 0; i < 4; i++){ ofVertex(srcScaled[i].x, srcScaled[i].y); } ofVertex(srcScaled[0].x+1, srcScaled[0].y+1); ofEndShape(); ofSetHexColor(0xffffff); ofPopStyle(); ofPopMatrix(); }
//----------------------------------------------------- bool guiQuad::selectPoint(float x, float y, float offsetX, float offsetY, float width, float height, float hitArea){ //make sure selected is -1 unless we really find a point selected = -1; if(width == 0 || height == 0 || x < offsetX || x > offsetX + width || y < offsetY || y > offsetY + height){ //then we are out of our possible quad area //so we ignore :) return false; } //lets get it in range x(0 - width) y(0 - height) float px = x - offsetX; float py = y - offsetY; //now get in 0-1 range px /= width; py /= height; hitArea /= width; //we want to store the smallest distance found //because in the case when two points are in the //hit area we want to pick the closet float storeDist = 9999999.0; for(int i = 0; i < 4; i++){ float dx = fabs(px - srcZeroToOne[i].x); float dy = fabs(py - srcZeroToOne[i].y); float dist = sqrt(dx*dx + dy*dy); if(dist > hitArea)continue; if(dist < storeDist){ selected = i; storeDist = dist; } } if(selected != -1){ getScaledQuadPoints(width, height); if ((srcScaled[selected].x - srcScaled[oppVertex(selected)].x) != 0) { diag_a = (srcScaled[selected].y - srcScaled[oppVertex(selected)].y) / (srcScaled[selected].x - srcScaled[oppVertex(selected)].x); diag_b = srcScaled[selected].y - (diag_a * srcScaled[selected].x); diag_v = false; } else { diag_v = true; } // side A if (srcScaled[0].x - srcScaled[1].x != 0) { sideA_m = (srcScaled[0].y - srcScaled[1].y) / (srcScaled[0].x - srcScaled[1].x); cout << "sideA_m = " << sideA_m << endl; sideA_b = srcScaled[0].y - (sideA_m * srcScaled[0].x); cout << "sideA_b = " << sideA_b << endl; sideA_v = -1; } else { sideA_m = 0; sideA_b = 0; sideA_v = srcScaled[0].x; cout << "sideA is vertical" << endl; cout << "sideA_v = " << sideA_v << endl; } // side B if (srcScaled[1].x - srcScaled[2].x != 0) { sideB_m = (srcScaled[1].y - srcScaled[2].y) / (srcScaled[1].x - srcScaled[2].x); cout << "sideB_m = " << sideB_m << endl; sideB_b = srcScaled[1].y - (sideB_m * srcScaled[1].x); cout << "sideB_b = " << sideB_b << endl; sideB_v = -1; } else { sideB_m = 0; sideB_b = 0; sideB_v = srcScaled[1].x; cout << "sideB is vertical" << endl; cout << "sideB_v = " << sideB_v << endl; } // side C if (srcScaled[2].x - srcScaled[3].x != 0) { sideC_m = (srcScaled[2].y - srcScaled[3].y) / (srcScaled[2].x - srcScaled[3].x); cout << "sideC_m = " << sideC_m << endl; sideC_b = srcScaled[2].y - (sideC_m * srcScaled[2].x); cout << "sideC_b = " << sideC_b << endl; sideC_v = -1; } else { sideC_m = 0; sideC_b = 0; sideC_v = srcScaled[2].x; cout << "sideC is vertical" << endl; cout << "sideC_v = " << sideC_v << endl; } // side D if (srcScaled[3].x - srcScaled[0].x != 0) { sideD_m = (srcScaled[3].y - srcScaled[0].y) / (srcScaled[3].x - srcScaled[0].x); cout << "srcScaled[3].x = " << srcScaled[3].x << endl; cout << "srcScaled[3].y = " << srcScaled[3].y << endl; cout << "srcScaled[0].x = " << srcScaled[0].x << endl; cout << "srcScaled[0].y = " << srcScaled[0].y << endl; cout << "sideD_m = " << sideD_m << endl; sideD_b = srcScaled[3].y - (sideD_m * srcScaled[3].x); cout << "sideD_b = " << sideD_b << endl; sideD_v = -1; } else { sideD_m = 0; sideD_b = 0; sideD_v = srcScaled[3].x; cout << "sideD is vertical" << endl; cout << "sideD_v = " << sideD_v << endl; } srcZeroToOne[selected].x = px; srcZeroToOne[selected].y = py; srcZeroToOne[selected].x = px; srcZeroToOne[selected].y = py; srcScaled[selected].x = px; srcScaled[selected].y = py; if(selected == 0)setCommonText("status: Quad - Top Left"); else if(selected == 1)setCommonText("status: Quad - Top Right"); else if(selected == 2)setCommonText("status: Quad - Bottom Right"); else if(selected == 3)setCommonText("status: Quad - Bottom Left"); return true; } return false; }
bool guiQuad::updatePoint(float x, float y, float offsetX, float offsetY, float width, float height, bool noDistorsion){ if(!noDistorsion) { updatePoint(x, y, offsetX, offsetY, width, height); } else { getScaledQuadPoints(width, height); //nothing to update if(selected == -1) return false; if(width == 0 || height == 0){ //dangerous so we ignore :) return false; } if( x < offsetX ) x = offsetX; if( x > offsetX + width ) x = offsetX + width; if( y < offsetY ) y = offsetY; if( y > offsetY + height) y = offsetY + height; //lets get it in range x(0 - width) y(0 - height) float px = x - offsetX; float py = y - offsetY; if (!diag_v){ py = diag_a*px + diag_b; if (py < 0) { py = 0; px = ((py - diag_b)/diag_a); } else if (py > height) { py = height; px = ((py - diag_b)/diag_a); } } else { px = selX; if (px < 0) { px = 0; py = diag_b; } else if (px > width) { px = width; py = diag_a * width + diag_b; } } cout << "px = " << px << endl; cout << "py = " << py << endl; if (selected == 0) { if (sideA_v == -1) { sideA_b = py - (sideA_m * px); cout << "sideA_b = " << sideA_b << endl; if (sideB_v == -1) { float temp_x; if (sideB_m < 0 || sideB_m > 1) { temp_x= (((sideA_m * temp_x) + sideA_b - sideB_b) / sideB_m) / width; } else { temp_x= (((sideB_m * temp_x) + sideB_b - sideA_b) / sideA_m) / width; } srcZeroToOne[1].x = temp_x; srcZeroToOne[1].y = ((sideA_m * (srcZeroToOne[1].x * width)) + sideA_b)/height; } else { srcZeroToOne[1].y = ((sideA_m * (srcZeroToOne[1].x * width)) + sideA_b) / height; } } else { srcZeroToOne[1].x = px/width; srcZeroToOne[1].y = ((sideB_m * px) + sideB_b) / height; } if (sideD_v == -1) { sideD_b = py - (sideD_m * px); cout << "sideD_b = " << sideD_b << endl; if (sideC_v == -1) { float temp_x; if (sideC_m < 0 || sideC_m > 1) { temp_x = (((sideD_m * temp_x) + sideD_b - sideC_b) / sideC_m) / width; } else { temp_x = (((sideC_m * temp_x) + sideC_b - sideD_b) / sideD_m) / width; } srcZeroToOne[3].x = temp_x; cout << "3x = " << temp_x * width << endl; srcZeroToOne[3].y = ((sideD_m * (srcZeroToOne[3].x * width)) + sideD_b)/height; cout << "3y = " << srcZeroToOne[3].y * height << endl; } else { srcZeroToOne[3].y = ((sideD_m * (srcZeroToOne[3].x * width)) + sideD_b) / height; } } else { srcZeroToOne[3].x = px/width; srcZeroToOne[3].y = ((sideC_m * px) + sideC_b) / height; } if (srcZeroToOne[1].x < 0) { srcZeroToOne[1].x = 0; } else if (srcZeroToOne[1].x > 1) { srcZeroToOne[1].x = 1; } if (srcZeroToOne[1].y < 0) { srcZeroToOne[1].y = 0; } else if (srcZeroToOne[1].y > 1) { srcZeroToOne[1].y = 1; } if (srcZeroToOne[3].x < 0) { srcZeroToOne[3].x = 0; } else if (srcZeroToOne[3].x > 1) { srcZeroToOne[3].x = 1; } if (srcZeroToOne[3].y < 0) { srcZeroToOne[3].y = 0; } else if (srcZeroToOne[3].y > 1) { srcZeroToOne[3].y = 1; } } //now get in 0-1 range px /= width;; py /= height; srcZeroToOne[selected].x = px; srcZeroToOne[selected].y = py; anchorPoint.x = (least(srcZeroToOne[1].x, srcZeroToOne[2].x) + greatest(srcZeroToOne[0].x, srcZeroToOne[3].x))/2; anchorPoint.y = (least(srcZeroToOne[2].y, srcZeroToOne[3].y) + greatest(srcZeroToOne[0].y, srcZeroToOne[1].y))/2; } return true; }