Example #1
0
void Plotter::refreshPixmap()
{
pixmap = QPixmap(size());
//pixmap.fill(this, 0, 0);
QPainter painter(&pixmap);
painter.initFrom(this);
drawGrid(&painter);
drawCurves(&painter);
update();
}
Example #2
0
/* This routine displays the control points */
static void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	
	if (isSelected) {
		drawPoints();
	}
	
	drawCurves(GL_RENDER);
	glFlush();
}
 //***************************************************************************************************
//The refreshPixmap() function redraws the plot onto the off-screen pixmap and updates the display.
//We resize the pixmap to have the same size as the widget and fill it with the widget's erase color.
//This color is the "dark" component of the palette, because of the call to setBackgroundRole() in the
//Plotter constructor. If the background is a non-solid brush, QPixmap::fill() needs to know the
//offset in the widget where the pixmap will end up to align the brush pattern correctly. Here, the
//pixmap corresponds to the entire widget, so we specify position (0, 0).
//Then we create a QPainter to draw on the pixmap. The initFrom() call sets the painter's pen,
//background, and font to the same ones as the Plotter widget. Next we call drawGrid() and
//drawCurves() to perform the drawing. At the end, we call update() to schedule a paint event for the
//whole widget. The pixmap is copied to the widget in the paintEvent() function (p. 123).
//***************************************************************************************************
void ScatterView::refreshPixmap()
{
	this->updateAxis();
	pixmap = QPixmap(size());
	pixmap.fill(QColor(220,220,220));
	QPainter painter(&pixmap);
	painter.initFrom(this);
	drawGrid(&painter);
	drawCurves(&painter);
	drawSelection(&painter);
}
Example #4
0
void Plotter::refreshPixmap()
{
    pixmap = QPixmap(size());
    pixmap.fill(this, 0, 0);
    //这句是关键!!
    //我们是在图片上进行画图,而不是Widget上。
    QPainter painter(&pixmap);
    painter.initFrom(this);
    drawGrid(&painter);
    drawCurves(&painter);
    update();
}
Example #5
0
/* This routine displays the control points */
static void display(void)
{
	int i;
	glClear(GL_COLOR_BUFFER_BIT);
	
	glBegin(GL_POINTS);
	for (i = 0; i < ncpts; i++)
		glVertex3fv(cpts[i]);
	glEnd();
	
	drawCurves();
	glFlush();
}
Example #6
0
File: plotter.cpp Project: Qmax/PT6
void Plotter::refreshPixmap()
{
    pixmap = QPixmap(size());
    pixmap.fill(this,0,0);
    QPainter painter(&pixmap);
    painter.initFrom(this);

    drawGrid(&painter);
    if(curveMap.count()>0 )
    {
        if(m_showData == true){
        painter.setRenderHint(QPainter::Antialiasing);
    }
    drawCurves(&painter);
    }
    update();
}
Example #7
0
// use the mouse call back to enter and leave the selection mode
void mouse(int button, int state, int x, int y)
{
	GLuint selectBuf[SIZE];
	GLint hits;
	GLint viewport[4];
	
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
		isSelected = true;
		glGetIntegerv (GL_VIEWPORT, viewport);
		
		glSelectBuffer (SIZE, selectBuf);
		glRenderMode(GL_SELECT);
		
		glInitNames(); // initialize the name stack
		glPushName(0); // with an unused name
		
		glMatrixMode (GL_PROJECTION);
		glPushMatrix ();
		glLoadIdentity ();
		/*  create 5x5 pixel picking region near cursor location	*/
		gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
					   5.0, 5.0, viewport);
//		glOrtho(-2.0, 2.0, -2.0, 2.0,0.0,5.0); // same as in the render mode
		glViewport(0, 0, width, height);
		drawCurves(GL_SELECT); // direct call of the drawing function
		
		glMatrixMode (GL_PROJECTION);
		glPopMatrix ();
		glFlush ();
		
		hits = glRenderMode (GL_RENDER); // returns the number of hits
		processHits (hits, selectBuf);
	}
	else
	{
		isSelected = false;
	}
	
	glutPostRedisplay();
}
Example #8
0
void Plotter::refreshPixmap()
{
        pixmap = QPixmap(size());
        pixmap.fill(this,0,0);
        QPainter painter(&pixmap);
        painter.initFrom(this);
        drawGrid(&painter);
        drawBand(&painter,0);
    if(curveMap.count()>=0 )
    {
        if(m_showData == true){
        	painter.setRenderHint(QPainter::Antialiasing);
        }
    }
    drawCurves(&painter);
    if(m_nLoopDrive==false){
        update();
    }
    else{
         setFocus(Qt::ActiveWindowFocusReason);
        //loopDrawCurves(&painter);
        repaint();
    }
}
Example #9
0
void display(void){

    int i;
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_POINTS);
    for (i = 0; i < ncpts; i++)
        glVertex3fv(BC[numberOfBezierCurve].cpts[i]);

    for (i = 0; i < ncpts_r; i++)
        glVertex3fv(RE[numberOfRectangle].cpts[i]);

    for (i = 0; i < ncpts_e; i++)
        glVertex3fv(EL[numberOfEllipse].cpts[i]);

    for (i = 0; i < ncpts_l; i++)
        glVertex3fv(L[numberOfLine].cpts[i]);
    glEnd();

    glFlush();


  if(value == 1){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].isFilled = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 0.0;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 2){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].isFilled = 1;
    RE[numberOfRectangle].color[0] = 0.0;
    RE[numberOfRectangle].color[1] = 1.0;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 3){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].isFilled = 1;
    RE[numberOfRectangle].color[0] = 0.0;
    RE[numberOfRectangle].color[1] = 0.0;
    RE[numberOfRectangle].color[2] = 1.0;
  }else if(value == 4){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].isFilled = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 1.0;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 5){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].isFilled = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 0.0;
    RE[numberOfRectangle].color[2] = 1.0;
  }else if(value == 6){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].isFilled = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 0.5;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 7){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].isFilled = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 1.0;
    RE[numberOfRectangle].color[2] = 1.0;
  }else if(value == 8){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].isFilled = 1;
    RE[numberOfRectangle].color[0] = 0.0;
    RE[numberOfRectangle].color[1] = 0.0;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 9){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 0.0;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 10){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].color[0] = 0.0;
    RE[numberOfRectangle].color[1] = 1.0;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 11){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].color[0] = 0.0;
    RE[numberOfRectangle].color[1] = 0.0;
    RE[numberOfRectangle].color[2] = 1.0;
  }else if(value == 12){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 1.0;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 13){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 0.0;
    RE[numberOfRectangle].color[2] = 1.0;
  }else if(value == 14){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 0.5;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 15){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].color[0] = 1.0;
    RE[numberOfRectangle].color[1] = 1.0;
    RE[numberOfRectangle].color[2] = 1.0;
  }else if(value == 16){
    option = 2;
    RE[numberOfRectangle].colorSeleted = 1;
    RE[numberOfRectangle].color[0] = 0.0;
    RE[numberOfRectangle].color[1] = 0.0;
    RE[numberOfRectangle].color[2] = 0.0;
  }else if(value == 17){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].isFilled = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 0.0;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 18){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].isFilled = 1;
    EL[numberOfEllipse].color[0] = 0.0;
    EL[numberOfEllipse].color[1] = 1.0;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 19){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].isFilled = 1;
    EL[numberOfEllipse].color[0] = 0.0;
    EL[numberOfEllipse].color[1] = 0.0;
    EL[numberOfEllipse].color[2] = 1.0;
  }else if(value == 20){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].isFilled = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 1.0;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 21){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].isFilled = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 0.0;
    EL[numberOfEllipse].color[2] = 1.0;
  }else if(value == 22){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].isFilled = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 0.5;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 23){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].isFilled = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 1.0;
    EL[numberOfEllipse].color[2] = 1.0;
  }else if(value == 24){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].isFilled = 1;
    EL[numberOfEllipse].color[0] = 0.0;
    EL[numberOfEllipse].color[1] = 0.0;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 25){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 0.0;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 26){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].color[0] = 0.0;
    EL[numberOfEllipse].color[1] = 1.0;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 27){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].color[0] = 0.0;
    EL[numberOfEllipse].color[1] = 0.0;
    EL[numberOfEllipse].color[2] = 1.0;
  }else if(value == 28){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 1.0;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 29){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 0.0;
    EL[numberOfEllipse].color[2] = 1.0;
  }else if(value == 30){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 0.5;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 31){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].color[0] = 1.0;
    EL[numberOfEllipse].color[1] = 1.0;
    EL[numberOfEllipse].color[2] = 1.0;
  }else if(value == 32){
    option = 4;
    EL[numberOfEllipse].colorSeleted = 1;
    EL[numberOfEllipse].color[0] = 0.0;
    EL[numberOfEllipse].color[1] = 0.0;
    EL[numberOfEllipse].color[2] = 0.0;
  }else if(value == 33){
      option = 3;
      L[numberOfLine].colorSeleted = 1;
      L[numberOfLine].color[0] = 1.0;
      L[numberOfLine].color[1] = 0.0;
      L[numberOfLine].color[2] = 0.0;
  }else if(value == 34){
      option = 3;
      L[numberOfLine].colorSeleted = 1;
      L[numberOfLine].color[0] = 0.0;
      L[numberOfLine].color[1] = 1.0;
      L[numberOfLine].color[2] = 0.0;
  }else if(value == 35){
      option = 3;
      L[numberOfLine].colorSeleted = 1;
      L[numberOfLine].color[0] = 0.0;
      L[numberOfLine].color[1] = 0.0;
      L[numberOfLine].color[2] = 1.0;
  }else if(value == 36){
      option = 3;
      L[numberOfLine].colorSeleted = 1;
      L[numberOfLine].color[0] = 1.0;
      L[numberOfLine].color[1] = 1.0;
      L[numberOfLine].color[2] = 0.0;
  }else if(value == 37){
      option = 3;
      L[numberOfLine].colorSeleted = 1;
      L[numberOfLine].color[0] = 1.0;
      L[numberOfLine].color[1] = 0.0;
      L[numberOfLine].color[2] = 1.0;
  }else if(value == 38){
      option = 3;
      L[numberOfLine].colorSeleted = 1;
      L[numberOfLine].color[0] = 1.0;
      L[numberOfLine].color[1] = 0.5;
      L[numberOfLine].color[2] = 0.0;
  }else if(value == 39){
      option = 3;
      L[numberOfLine].colorSeleted = 1;
      L[numberOfLine].color[0] = 1.0;
      L[numberOfLine].color[1] = 1.0;
      L[numberOfLine].color[2] = 1.0;

  }else if(value == 40){
      option = 3;
      L[numberOfLine].colorSeleted = 1;
      L[numberOfLine].color[0] = 0.0;
      L[numberOfLine].color[1] = 0.0;
      L[numberOfLine].color[2] = 0.0;
  }else if(value == 41){
	  option = 1;
	  BC[numberOfBezierCurve].colorSeleted = 1;
	  ncpts =0;
      BC[numberOfBezierCurve].color[0] = 1.0;
      BC[numberOfBezierCurve].color[1] = 0.0;
      BC[numberOfBezierCurve].color[2] = 0.0;
  }else if(value == 42){
      option = 1;
      BC[numberOfBezierCurve].colorSeleted = 1;
	  ncpts =0;
      BC[numberOfBezierCurve].color[0] = 0.0;
      BC[numberOfBezierCurve].color[1] = 1.0;
      BC[numberOfBezierCurve].color[2] = 0.0;
  }else if(value == 43){
	  option = 1;
	  BC[numberOfBezierCurve].colorSeleted = 1;
	  ncpts =0;
      BC[numberOfBezierCurve].color[0] = 0.0;
      BC[numberOfBezierCurve].color[1] = 0.0;
      BC[numberOfBezierCurve].color[2] = 1.0;
  }else if(value == 44){
	  option = 1;
	  BC[numberOfBezierCurve].colorSeleted = 1;
	  ncpts =0;
      BC[numberOfBezierCurve].color[0] = 1.0;
      BC[numberOfBezierCurve].color[1] = 1.0;
      BC[numberOfBezierCurve].color[2] = 0.0;
  }else if(value == 45){
	  option = 1;
	  BC[numberOfBezierCurve].colorSeleted = 1;
	  ncpts =0;
      BC[numberOfBezierCurve].color[0] = 1.0;
      BC[numberOfBezierCurve].color[1] = 0.0;
      BC[numberOfBezierCurve].color[2] = 1.0;
  }else if(value == 46){
	  option = 1;
	  BC[numberOfBezierCurve].colorSeleted = 1;
	  ncpts =0;
      BC[numberOfBezierCurve].color[0] = 1.0;
      BC[numberOfBezierCurve].color[1] = 0.5;
      BC[numberOfBezierCurve].color[2] = 0.0;
  }else if(value == 47){
	  option = 1;
	  BC[numberOfBezierCurve].colorSeleted = 1;
	  ncpts =0;
      BC[numberOfBezierCurve].color[0] = 1.0;
      BC[numberOfBezierCurve].color[1] = 1.0;
      BC[numberOfBezierCurve].color[2] = 1.0;
  }else if(value == 48){
	  option = 1;
	  BC[numberOfBezierCurve].colorSeleted = 1;
	  ncpts =0;
      BC[numberOfBezierCurve].color[0] = 0.0;
      BC[numberOfBezierCurve].color[1] = 0.0;
      BC[numberOfBezierCurve].color[2] = 0.0;
  }
  glFlush();
  value = 0;
  drawCurves();
  drawRectangle();
  drawLine();
  drawEllipse();
}
Example #10
0
void kangxueCalcVectorField(IplImage* sourceImage,IplImage* screen, std::vector<std::vector<CvPoint> > curves) {
    cvSetMouseCallback( "curveDetect", mouseCallback ) ;
    // calculate edgels and display it on the source image
    IplImage *edgelsImage = cvCreateImage(cvGetSize(sourceImage),IPL_DEPTH_8U, 1 );
    getEdgels( sourceImage,  edgelsImage ) ;
    // get gradient
    IplImage *gradImage = cvCreateImage(cvGetSize(sourceImage),IPL_DEPTH_8U, 3 );
    getGradImage( sourceImage, gradImage, gradmap );
    Edges dstSnakeCurves ;
    Edges dstVectorCurves ;
    int thresh_t100 = 100;
    cvCreateTrackbar("threshold", "curveDetect",&thresh_t100, 400  ) ;
    Edges edges_bkup ;

    //screen = cvCloneImage( sourceImage ) ;        //已经注释掉了,需要在外部进行cpoy,防止指针出错
    vecfield.compute( curves, cvSize( sourceImage->width,sourceImage->height), 400 ) ;
    //drawCurves( screen , curves, cvScalar(255,0,255),2) ;
    drawVectors( screen, vecfield ) ;
    return;

    while(1){
        screen = cvCloneImage( sourceImage ) ;
        if( displayEdge )
            cvCvtColor(edgelsImage, screen, CV_GRAY2RGB) ;
        if( displayCurve )
            drawCurves( screen , curves, cvScalar(255,0,255),2) ;
        if( 0 && curves.size() && curves[0].size() > 10 ){
            Edges guideCurve, spline ;
            guideCurve.elems = curves ;
            getUniformCubicBSpline(guideCurve, spline) ;
            drawCurves( screen , spline.elems, cvScalar(0,255,0),2) ;
        }
        if( AppState != _draw_curves && displayVector)
            drawVectors( screen, vecfield ) ;
        if( AppState == _got_result )
            if( displayVecOrSnake )
                drawCurves(screen, dstSnakeCurves.elems ,  cvScalar(0,255,255),2) ;
            else
                drawCurves(screen, dstVectorCurves.elems ,  cvScalar(0,255,0),2) ;
        if( AppState >= _edges_linkeded ){
            edges = edges_bkup ;
            filterLinkedEdge(edges, vecfield, gradmap, curves, thresh_t100 * 0.01 ) ;
        }
        if( displayEdges )
            drawCurves_randomColor( screen, edges.elems, 4 ) ;
        cvShowImage("curveDetect", screen );
        unsigned char key = cvWaitKey(50) ;
        if( key == 99 ) break; // press c to exit
        if( key == 114 ){ // press r to reset
            curves.clear() ;
            AppState = _draw_curves ;
            getEdgels( sourceImage,  edgelsImage ) ;
            edges.elems.clear();
            dstVectorCurves.elems.clear();
            dstSnakeCurves.elems.clear();
            ctrpoints.elems.clear();
        }
        if( key == 115 ){ // press s
            if( AppState == _draw_curves ){
                AppState = _show_vectors ;
                 // calculate vector field
                std::cout<<"begin calculate the vector field" << std::endl;
                vecfield.compute( curves, cvSize( sourceImage->width,sourceImage->height), 400 ) ;
                std::cout<<"vector field calculated" << std::endl;
            }
        }
    }
}
Example #11
0
int main2(int argc, char **argv) {
    std::cout<<"  LBFGSERR_MAXIMUMLINESEARCH == "<<   LBFGSERR_MAXIMUMLINESEARCH << std::endl;
	std::string sourceImageName ;
    sourceImageName = std::string("/home/netbeen/桌面/周叔项目/imgSalCurDetector/test.jpg") ;
    sourceImage = cvLoadImage(sourceImageName.c_str(), CV_LOAD_IMAGE_COLOR) ;
	cvNamedWindow( "curveDetect") ;
	cvSetMouseCallback( "curveDetect", mouseCallback ) ;
	// calculate edgels and display it on the source image
	IplImage *edgelsImage = cvCreateImage(cvGetSize(sourceImage),IPL_DEPTH_8U, 1 );
	getEdgels( sourceImage,  edgelsImage ) ;
	// get gradient
	IplImage *gradImage = cvCreateImage(cvGetSize(sourceImage),IPL_DEPTH_8U, 3 );
	getGradImage( sourceImage, gradImage, gradmap );
	Edges dstSnakeCurves ;
	Edges dstVectorCurves ;
	int thresh_t100 = 100;
	cvCreateTrackbar("threshold", "curveDetect",&thresh_t100, 400  ) ;
	Edges edges_bkup ;
	while(1){
		IplImage *screen = cvCloneImage( sourceImage ) ;
		if( !displaySourceimage ){
            std::cout << "NO, 我删掉了一行" << std::endl;
            //cvCopyImage(gradImage,screen);
            //////////////////////////////////////////////////////////////////////我删掉了一行//////////////////////////////////////
		}
		if( displayEdge )
			cvCvtColor(edgelsImage, screen, CV_GRAY2RGB) ;
		if( displayCurve )
			drawCurves( screen , curves, cvScalar(255,0,255),2) ;
		if( 0 && curves.size() && curves[0].size() > 10 ){
			Edges guideCurve, spline ;
			guideCurve.elems = curves ;
			getUniformCubicBSpline(guideCurve, spline) ;
			drawCurves( screen , spline.elems, cvScalar(0,255,0),2) ;
		}
		if( AppState != _draw_curves && displayVector)
			drawVectors( screen, vecfield ) ;
        if( AppState == _got_result )
			if( displayVecOrSnake )
				drawCurves(screen, dstSnakeCurves.elems ,  cvScalar(0,255,255),2) ;
			else
				drawCurves(screen, dstVectorCurves.elems ,  cvScalar(0,255,0),2) ;
		if( AppState >= _edges_linkeded ){
			edges = edges_bkup ;
			filterLinkedEdge(edges, vecfield, gradmap, curves, thresh_t100 * 0.01 ) ;
		}
		if( displayEdges )
			drawCurves_randomColor( screen, edges.elems, 4 ) ;
		cvShowImage("curveDetect", screen );
		unsigned char key = cvWaitKey(50) ;
		if( key !=255)
			std::cout<<(unsigned)key ;
		if( key == 99 ) break; // press c to exit
		if( key == 27 )  {  // press ESC to delete the last curve drawn by user
			if( AppState == _draw_curves && curves.size()!=0)
				curves.erase( curves.end()-1) ;
		}
		if( key == 114 ){ // press r to reset
			curves.clear() ;
			AppState = _draw_curves ;
			getEdgels( sourceImage,  edgelsImage ) ;
			edges.elems.clear();
			dstVectorCurves.elems.clear();
			dstSnakeCurves.elems.clear();
			ctrpoints.elems.clear();
		}
        if( key == 9 ) // press TAB to switch between displaying sourceImage and edges
			displayEdge = !displayEdge ;
        if( key == 113 )  //press q
			displayVector = !displayVector ;
        if( key == 101 )  //press e
			displaySourceimage = !displaySourceimage ;
        if( key == 97 )  //press a
			displayVecOrSnake = !displayVecOrSnake ;
        if( key == 122 )  //press z
			displayEdges = !displayEdges ;
        if( key == 119 ) // press TAB to switch between displaying sourceImage and edges
			displayCurve = !displayCurve ;
		if( key == 115 ){ // press s
			if( AppState == _draw_curves ){ 
				AppState = _show_vectors ;
				 // calculate vector field
				std::cout<<"begin calculate the vector field" << std::endl;
                vecfield.compute( curves, cvSize( sourceImage->width,sourceImage->height), 400 ) ;
				std::cout<<"vector field calculated" << std::endl;
                std::cout << "验证,是main2"  << std::endl;
                //cvZero( bandMsk ) ;
                //drawCurves( bandMsk, curves, cvScalar(1, 1,1), 80 ) ;
                //cvMul(bandMsk, edgelsImage, edgelsImage ) ;
            }
            /*else if( AppState == _show_vectors ){
				AppState = _edges_linkeded ;
				linkEdge(edgelsImage, edges, 20) ;
				edges_bkup = edges ;
            }
            else if( AppState == _edges_linkeded ){
				AppState = _edges_filtered ;
				filterLinkedEdge( edges,  vecfield, gradmap, curves, thresh_t100 * 0.01 ) ;
            }
            else if( AppState == _edges_filtered ){
				AppState = _new_field ;
                std::vector<std::vector<CvPoint> > allCurves = edges.elems;
                for( size_t i=0; i<curves.size(); ++i )
					allCurves.push_back(curves[i] ) ;
				// smooth edges
				std::cout<<"begin calculate the new vector field" << std::endl;
				vecfield.compute( edges.elems, cvSize( sourceImage->width,sourceImage->height), 400 ) ;
				std::cout<<"new vector field calculated" << std::endl;
            }
            else if( AppState == _new_field ){
				AppState = _got_result ;
				// calculate ctrpoints
				ctrpoints.elems = curves ;
				CvPoint prepoint = ctrpoints.elems[0][0] ;
                for( size_t j=1; j<ctrpoints.elems[0].size()-1; ++j ){
					if( sqrt( (double)((prepoint.x - ctrpoints.elems[0][j].x) *(prepoint.x - ctrpoints.elems[0][j].x) 
						+ (prepoint.y - ctrpoints.elems[0][j].y) *(prepoint.y - ctrpoints.elems[0][j].y)) ) > 20  ){
							prepoint = ctrpoints.elems[0][j] ;
					}
					else{
						ctrpoints.elems[0].erase( ctrpoints.elems[0].begin() + j ) ;
						j-- ;
					}
                }
                #define _lbfgs 0
				if( _lbfgs ){
					// Snakes
					extern bool consider_vecfield ;
					consider_vecfield = false ;
					std::cout<<"begin optimization: consider_vecfield = false"<<std::endl;
					int ret = 0;
					lbfgsfloatval_t fx;
					lbfgsfloatval_t *x = lbfgs_malloc( ctrpoints.elems[0].size() * 2 );
					lbfgs_parameter_t param;
					if (x == NULL) {
						printf("ERROR: Failed to allocate a memory block for variables.\n");
						return 1;
					}
                    // Initialize the variables.
                    for (size_t i = 0;i < ctrpoints.elems[0].size() * 2 ; i++)
						x[i] = 0;
					lbfgs_parameter_init(&param);
					param.max_linesearch = 100 ;
					ret = lbfgs(ctrpoints.elems[0].size() * 2, x, &fx, evaluate, progress, NULL, &param);
					printf("L-BFGS optimization terminated with status code = %d\n", ret);
					printf("  fx = %f, x[0] = %f, x[1] = %f\n", fx, x[0], x[1]);
					// apply the optimization result to ctrpoints
					Edges dstctrpoints = ctrpoints ;
                    for( size_t i=0; i<dstctrpoints.elems[0].size(); ++i ){
						dstctrpoints.elems[0][i].x += x[ i ] ;
						dstctrpoints.elems[0][i].y += x[ i + dstctrpoints.elems[0].size() ] ;
					}
					curve_interplation(dstctrpoints, dstSnakeCurves ) ;
					// field guided snakes
					consider_vecfield = true ;
					std::cout<<"begin optimization: consider_vecfield = true"<<std::endl;
                    for (size_t i = 0;i < ctrpoints.elems[0].size() * 2 ; i++)
						x[i] = 0;
					lbfgs_parameter_init(&param);
					param.max_linesearch = 100 ;
					ret = lbfgs(ctrpoints.elems[0].size() * 2, x, &fx, evaluate, progress, NULL, &param);
					printf("L-BFGS optimization terminated with status code = %d\n", ret);
					printf("  fx = %f, x[0] = %f, x[1] = %f\n", fx, x[0], x[1]);
					// apply the optimization result to ctrpoints
					dstctrpoints = ctrpoints ;
                    for( size_t i=0; i<dstctrpoints.elems[0].size(); ++i ){
						dstctrpoints.elems[0][i].x += x[ i ] ;
						dstctrpoints.elems[0][i].y += x[ i + dstctrpoints.elems[0].size() ] ;
					}
					curve_interplation(dstctrpoints, dstVectorCurves ) ;
					lbfgs_free(x);
				}
				else{
					// Snakes
					extern bool consider_vecfield ;
					consider_vecfield = false ;
					std::cout<<"begin optimization: consider_vecfield = false"<<std::endl;
					splab::BFGS<double, EnergyFunction> solver;
					splab::Vector<double> x0;
					x0.resize(ctrpoints.elems[0].size() * 2) ;
					for( int i=0; i<x0.size(); ++i )
						x0[i] = 0 ;
					EnergyFunction ef ;
					solver.optimize(ef, x0, 1e-10, 1000 ) ;
					x0 = solver.getOptValue() ;
					std::cout<<"func["<<x0<<"] = "<<solver.getFuncMin()<<std::endl;
					std::vector<double> X;
					for( int i=0; i<x0.size(); ++i )
						X.push_back(x0[i]);
					// apply the optimization result to ctrpoints
					Edges dstctrpoints = ctrpoints ;
                    for( size_t i=0; i<dstctrpoints.elems[0].size(); ++i ){
						dstctrpoints.elems[0][i].x += X[ i ] ;
						dstctrpoints.elems[0][i].y += X[ i + dstctrpoints.elems[0].size() ] ;
					}
					getUniformCubicBSpline(dstctrpoints, dstSnakeCurves ) ;
					// field guided Snakes
					consider_vecfield = true ;
					std::cout<<"begin optimization: consider_vecfield = true"<<std::endl;
					for( int i=0; i<x0.size(); ++i )
						x0[i] = 0 ;
					solver.optimize(ef, x0, 1e-10, 1000 ) ;
					x0 = solver.getOptValue() ;
					std::cout<<"func["<<x0<<"] = "<<solver.getFuncMin()<<std::endl;
					// apply the optimization result to ctrpoints
                    for( size_t i=0; i<ctrpoints.elems[0].size(); ++i ){
						ctrpoints.elems[0][i].x += x0[ i ] ;
						ctrpoints.elems[0][i].y += x0[ i + ctrpoints.elems[0].size() ] ;
					}
					getUniformCubicBSpline(ctrpoints, dstVectorCurves) ;
				}
            }*/
		}
	}
	return 0;
}