CvRect unionOfCvRects(const CvRect& a, const CvRect&b) { int xmin = MT_MIN(a.x, b.x); int xmax = MT_MAX(a.x+a.width, b.x+b.width); int ymin = MT_MIN(a.y, b.y); int ymax = MT_MAX(a.y+a.height, b.y+b.height); return cvRect(xmin, ymin, xmax-xmin, ymax-ymin); }
CGPoint CGLineIntersectsRectAtPoint(CGRect rect, CGLine line) { CGLine top = CGLineMake( CGPointMake( CGRectGetMinX(rect), CGRectGetMinY(rect) ), CGPointMake( CGRectGetMaxX(rect), CGRectGetMinY(rect) ) ); CGLine right = CGLineMake( CGPointMake( CGRectGetMaxX(rect), CGRectGetMinY(rect) ), CGPointMake( CGRectGetMaxX(rect), CGRectGetMaxY(rect) ) ); CGLine bottom = CGLineMake( CGPointMake( CGRectGetMinX(rect), CGRectGetMaxY(rect) ), CGPointMake( CGRectGetMaxX(rect), CGRectGetMaxY(rect) ) ); CGLine left = CGLineMake( CGPointMake( CGRectGetMinX(rect), CGRectGetMinY(rect) ), CGPointMake( CGRectGetMinX(rect), CGRectGetMaxY(rect) ) ); // ensure the line extends beyond outside the rectangle CGFloat topLeftToBottomRight = CGPointDistance(CGRectTopLeftPoint(rect), CGRectBottomRightPoint(rect)); CGFloat bottomLeftToTopRight = CGPointDistance(CGRectBottomLeftPoint(rect), CGRectTopRightPoint(rect)); CGFloat maxDimension = MT_MAX(topLeftToBottomRight, bottomLeftToTopRight); CGFloat scaleFactor = maxDimension / MT_MIN(CGLineLength(line), maxDimension); CGLine extendedLine = CGLineScale(line, scaleFactor + 3); CGPoint points[4] = { CGLinesIntersectAtPoint(top, extendedLine), CGLinesIntersectAtPoint(right, extendedLine), CGLinesIntersectAtPoint(bottom, extendedLine), CGLinesIntersectAtPoint(left, extendedLine) }; for (int i = 0; i < 4; i++) { CGPoint p = points[i]; if (!CGPointEqualToPoint(p, NULL_POINT)) { return p; } } return NULL_POINT; }
/* Drawing function - gets called by the GUI * All of the drawing is done with OpenGL */ void DanceTracker::doGLDrawing(int flags) { /* MT_R3 is a 3-vector class, used here for convenience */ MT_R3 blobcenter; /* m_bShowBlobs is a boolean in the "Drawing Options" data group. If the user selects "Drawing Options" from the "Tracking" menu, a dialog will pop up with a check box labeled "Show blob arrows", and its state will be linked (via a pointer) to the value of this variable */ if(m_bShowBlobs) { for (unsigned int i = 0; i < MT_MIN(m_vdBlobs_X.size(), m_vBlobs.size()); i++) { /* note that we have to subtract y from the frame_height to account for the window coordinates originating from the bottom left but the image coordinates originating from the top left */ blobcenter.setx(m_vdBlobs_X[i]); blobcenter.sety(m_iFrameHeight - m_vdBlobs_Y[i]); blobcenter.setz( 0 ); double M, m; M = MT_MAX(m_vBlobs[i].m_dMajorAxis, 0.01); m = MT_MAX(m_vBlobs[i].m_dMinorAxis, 0.01); MT_DrawEllipse(blobcenter, M, m, m_vBlobs[i].m_dOrientation, MT_Primaries[i % MT_NPrimaries]); /* draws an arrow using OpenGL */ /* MT_DrawArrow(blobcenter, // center of the base of the arrow * 20.0, // arrow length (pixels) * MT_DEG2RAD*m_vdBlobs_Orientation[i], // arrow angle * MT_Primaries[i % MT_NPrimaries], // color * 1.0 // fixes the arrow width * ); */ } } /* essentially the same as above, but with the tracked positions instead of the blob positions */ char s[50]; if(m_bShowTracking) { for (unsigned int i = 0; i < m_vdTracked_X.size(); i++) { blobcenter.setx(m_vdTracked_X[i]); blobcenter.sety(m_iFrameHeight - m_vdTracked_Y[i]); blobcenter.setz( 0 ); sprintf(s, "%d", i); printw(blobcenter.getx(), blobcenter.gety(), s); double th = atan2(-m_vdTracked_Vy[i], m_vdTracked_Vx[i]); MT_DrawArrow(blobcenter, 10.0, th, MT_Primaries[i % MT_NPrimaries], 0.5 ); } } double x1, y1, x2, y2; if(m_bShowInitialBlobs) { glLineStipple(1, 0x00FF); glEnable(GL_LINE_STIPPLE); for(unsigned int i = 0; i < m_vInitBlobs.size(); i++) { blobcenter.setx(m_vInitBlobs[i].COMx); blobcenter.sety(m_iFrameHeight - m_vInitBlobs[i].COMy); blobcenter.setz( 0 ); double M, m; M = MT_MAX(m_vInitBlobs[i].major_axis, 0.01); m = MT_MAX(m_vInitBlobs[i].minor_axis, 0.01); MT_DrawEllipse(blobcenter, M, m, m_vInitBlobs[i].orientation, MT_Green); } glDisable(GL_LINE_STIPPLE); } if(m_bShowAssociations) { glLineStipple(1, 0x00FF); glEnable(GL_LINE_STIPPLE); if(m_vInitBlobs.size() > 0 && m_viAssignments.size() > 0) { unsigned int max_label = MT_BiCC::getNumberOfLabels(m_viAssignments); for(unsigned int c = 0; c < max_label; c++) { for(unsigned int i = 0; i < m_iAssignmentRows; i++) { x1 = m_vdBlobs_X[i]; y1 = m_iFrameHeight - m_vdBlobs_Y[i]; for(unsigned int j = 0; j < m_iAssignmentCols; j++) { if(m_viAssignments[i] == c && m_viAssignments[m_iAssignmentRows + j] == c) { x2 = m_vInitBlobs[j].COMx; y2 = m_iFrameHeight - m_vInitBlobs[j].COMy; MT_DrawLineFromTo(x1, y1, x2, y2, MT_Red); } } } } } glDisable(GL_LINE_STIPPLE); } // MT_DrawRectangle(m_SearchArea.x, m_iFrameHeight - m_SearchArea.y - m_SearchArea.height, m_SearchArea.width, m_SearchArea.height); }