我的2D插值C代码出了什么问题

时间:2011-03-12 23:07:11

标签: c opengl 2d interpolation

#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 200
typedef struct vertex{

    float x;                  //  x position of the point
    float y;                  //  y position of the point
    float r;                  //  red color component of the point
    float g;                  //  green color component of the point
    float b;                  //  blue color component of the point
    char isVisited;
}Vertex;

Vertex *borderLines,*interPolationLines;
int vertex_Count;// total vertex
int counter;//counts matched y coordinates 
FILE *f,*g;

void readTotalVertexCount(){

    if((f = fopen("vertex.txt","r"))==NULL){
        printf("File could not been read\n");
        return ;
    }
    fscanf(f,"%d",&vertex_Count);

    /*if((g = fopen("points.txt","w"))==NULL){

        return ;
    }*/
}

void readVertexCoordinatesFromFile(){

    Vertex v[vertex_Count];
    borderLines = (Vertex *)calloc(N*vertex_Count,sizeof(Vertex));
    interPolationLines = (Vertex *)calloc(N*N*(vertex_Count-1),sizeof(Vertex));

    int i = 0;int j;
    //read vertexes from file
    while(i<vertex_Count){
        fscanf(f,"%f",&(v[i].x));
        fscanf(f,"%f",&(v[i].y));
        fscanf(f,"%f",&(v[i].r));
        fscanf(f,"%f",&(v[i].g));
        fscanf(f,"%f",&(v[i].b));
        //printf("%f %f \n",v[i].x,v[i].y);  
        i++;
    }

    Vertex *borderLine,*temp;
    float k,landa;

    // draw border line actually I am doing 1D Interpolation with coordinates of my vertexes
    for (i = 0;i < vertex_Count;i++){
        int m = i+1;
        if(m==vertex_Count)
            m = 0;

        borderLine = borderLines + i*N;

        for(j = 0;j < N; j++){
            k = (float)j/(N - 1);
            temp = borderLine + j;
            landa = 1-k;
            //finding 1D interpolation coord. actually they are borders of my convex polygon 
            temp->x = v[i].x*landa + v[m].x*k;
            temp->y = v[i].y*landa + v[m].y*k;
            temp->r = v[i].r*landa + v[m].r*k;
            temp->g = v[i].g*landa + v[m].g*k;
            temp->b = v[i].b*landa + v[m].b*k;
            temp->isVisited = 'n'; // I didn't visit this point yet
            //fprintf(g,"%f %f %f %f %f\n",temp->x,temp->y,temp->r,temp->g,temp->b);
        }
    }
    /* here is actual place I am doing 2D Interpolation
       I am traversing along the border of the convex polygon and finding the points have the same y coordinates
       Between those two points have same y coord. I am doing 1D Interpolation*/
    int a;counter = 0;
    Vertex *searcherBorder,*wantedBorder,*interPolationLine;
    int start = N*(vertex_Count);   int finish = N*vertex_Count;

    for(i = 0;i< start ;i++){

        searcherBorder = i + borderLines;

        for(j = i - i%N + N +1; j< finish; j++){

            wantedBorder = j + borderLines;
            if((searcherBorder->y)==(wantedBorder->y) && searcherBorder->isVisited=='n' && wantedBorder->isVisited=='n'){
                //these points have been visited                                            
                searcherBorder->isVisited = 'y';
                wantedBorder->isVisited = 'y';

                interPolationLine = interPolationLines + counter*N;
                //counter variable counts the points have same y coordinates.
                counter++;
                //printf("%d %d %d\n",i,j,counter);
                //same as 1D ınterpolation     
                for(a= 0;a< N;a++){

                    k = (float)a/(N - 1);
                    temp = interPolationLine + a;
                    landa = 1-k;
                    temp->x = (wantedBorder->x)*landa + (searcherBorder->x)*k;
                    temp->y = (wantedBorder->y)*landa + (searcherBorder->y)*k;
                    temp->r = (wantedBorder->r)*landa + (searcherBorder->r)*k;
                    temp->g = (wantedBorder->g)*landa + (searcherBorder->g)*k;
                    /*if(temp->x==temp->y)
                        printf("%f  %f \n",wantedBorder->x,searcherBorder->x);*/
                    temp->b = (wantedBorder->b)*landa + (searcherBorder->b)*k;

                }
            }
        }
    }

    fclose(f);
}

void display(void){

    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);

    int i,j;
    Vertex *interPol,*temp;

    glBegin (GL_POINTS);

    for(i = 0;i< counter;i++){
        interPol = interPolationLines + i*N;
        for(j = 0;j< N;j++){
            temp = interPol + j;
            glColor3f((temp)->r,(temp)->g,(temp)->b);
            //fprintf(g,"%f %f \n",(temp)->x,(temp)->y);
            glVertex2f ((temp)->x,(temp)->y);
        }
    }
    //printf("%d\n",counter);
    fclose(g);
    glEnd ();
    glFlush();
}

void init(void){
    glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(900,500);
    glutInitWindowPosition(200,100);
    glutCreateWindow("2D InterPolation");
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);  
}

int main(int argc, char** argv)
{
    readTotalVertexCount();
    readVertexCoordinatesFromFile();
    glutInit(&argc,argv);
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

我正在实现一个凸多边形的二维插值,我的代码不关心concav.my代码适用于某些凸多边形,但是对于其他人失败。对于那些我的代码失败,它不会绘制多边形的中间。它只绘制上三角和下三角。从文件vertex.txt中读取顶点及其格式:x co,y co,红色,绿色,蓝色等信息,如下所示,对于我的代码下面的值失败。谢谢提前回复我会生气的。

7
0.9 0.4 1.0 0.0 1.0
0.8 0.2 1.0 0.0 1.0
0.5 0.1 1.0 0.0 0.0
0.3 0.3 0.0 0.0 1.0
0.3 0.35 0.0 0.0 1.0
0.4 0.4 0.0 1.0 0.0
0.6 0.5 1.0 1.0 1.0

1 个答案:

答案 0 :(得分:1)

如果没有完全调试您的程序,我会怀疑所说的行for(j = i - i%N + N +1; j< finish; j++){。我不知道你打算做什么,但它看起来很可疑。此外,我建议使用不同的算法:

  1. 围绕多边形追踪
  2. 标记跨越所需y值的任何边
  3. 除了角落的情况外,如果你找到两个点击,那么只有一个解决方案。
  4. 计算边缘与y值的交点
  5. 执行x插值
  6. 此外,简明扼要的问题比“我的计划为什么不起作用?”更好。原谅我,但感觉就像是一个家庭作业问题。

    注意:这应该是评论而不是答案吗?我是新来的......