Jump to content

  • Log In with Google      Sign In   
  • Create Account


Triangulation OpenGl C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 bloupies   Members   -  Reputation: 112

Like
0Likes
Like

Posted 18 March 2014 - 11:01 AM

Hi everyone,
 
I have data corresponding to points of a surface of water and I trying to triangulate them. I thus obtained these functions from my teacher :
 

    typedef struct
    {
        float x, y;
    } Point2D;
    
    typedef struct
    {
        Point2D p1, p2, p3;
    } Triangle2D;
    
    Triangle2D* Terrain::Process( int nb_points, Point2D *points, int& nb_triangles )
    {
        /* allocate and initialize list of Vertices in polygon */
        Triangle2D *triangles = new Triangle2D[nb_points-2];
        nb_triangles = 0;
    
        int n = nb_points;
        if ( n < 3 ) return false;
    
        int *V = new int[n];
    
        /* we want a counter-clockwise polygon in V */
    
        if ( 0.0f < Area(nb_points, points) )
            for (int v=0; v<n; v++) V[v] = v;
        else
            for(int v=0; v<n; v++) V[v] = (n-1)-v;
    
        int nv = n;
    
        /*  remove nv-2 Vertices, creating 1 triangle every time */
        int count = 2*nv;   /* error detection */
    
        for(int m=0, v=nv-1; nv>2; )
        {
    
            /* if we loop, it is probably a non-simple polygon */
            if (0 >= (count--))
            {
                //** Triangulate: ERROR - probable bad polygon!
                // delete[] triangles;
                printf( "Degenerate polygon\n" );
                return triangles;
            }
    
            /* three consecutive vertices in current polygon, <u,v,w> */
            int u = v  ; if (nv <= u) u = 0;     /* previous */
            v = u+1; if (nv <= v) v = 0;     /* new v    */
            int w = v+1; if (nv <= w) w = 0;     /* next     */
    
            if ( Snip(nb_points, points,u,v,w,nv,V) )
            {
                int a,b,c,s,t;
    
                /* true names of the vertices */
                a = V[u]; b = V[v]; c = V[w];
    
                /* output Triangle */
                /*   result.push_back( points[a] );
          result.push_back( points[b] );
          result.push_back( points[c] );*/
    
                triangles[nb_triangles].p1 = points[a];
                triangles[nb_triangles].p2 = points[b];
                triangles[nb_triangles].p3 = points[c];
                nb_triangles++;
    
                m++;
    
                /* remove v from remaining polygon */
                for(s=v,t=v+1;t<nv;s++,t++) V[s] = V[t]; nv--;
    
                /* resest error detection counter */
                count = 2*nv;
            }
        }
    
        delete V;
    
        return triangles;
    }
    
    float Terrain::Area( int nb_points, Point2D *points )
    {
    
        int n = nb_points;
    
        float A=0.0f;
    
        for(int p=n-1,q=0; q<n; p=q++)
        {
            A+= points[p].x*points[q].y - points[q].x*points[p].y;
        }
        return A*0.5f;
    }
    
    
    bool Terrain::Snip( int nb_points, Point2D *points,int u,int v,int w,int n,int *V )
    {
        int p;
        float Ax, Ay, Bx, By, Cx, Cy, Px, Py;
    
        Ax = points[V[u]].x;
        Ay = points[V[u]].y;
    
        Bx = points[V[v]].x;
        By = points[V[v]].y;
    
        Cx = points[V[w]].x;
        Cy = points[V[w]].y;
    
        if ( EPSILON > (((Bx-Ax)*(Cy-Ay)) - ((By-Ay)*(Cx-Ax))) ) return false;
    
        for (p=0;p<n;p++)
        {
            if( (p == u) || (p == v) || (p == w) ) continue;
            Px = points[V[p]].x;
            Py = points[V[p]].y;
            if (InsideTriangle(Ax,Ay,Bx,By,Cx,Cy,Px,Py)) return false;
        }
    
        return true;
    }
    
    bool Terrain::InsideTriangle(float Ax, float Ay,
                                 float Bx, float By,
                                 float Cx, float Cy,
                                 float Px, float Py)
    
    {
        float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
        float cCROSSap, bCROSScp, aCROSSbp;
    
        ax = Cx - Bx;  ay = Cy - By;
        bx = Ax - Cx;  by = Ay - Cy;
        cx = Bx - Ax;  cy = By - Ay;
        apx= Px - Ax;  apy= Py - Ay;
        bpx= Px - Bx;  bpy= Py - By;
        cpx= Px - Cx;  cpy= Py - Cy;
    
        aCROSSbp = ax*bpy - ay*bpx;
        cCROSSap = cx*apy - cy*apx;
        bCROSScp = bx*cpy - by*cpx;
    
        return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
    }
 
It works except that it takes place in this way: the file containing points has more than a million points, and I try to draw only certain triangles being situated in a specific zone. 
OK, except that it is possible that there are degenerated triangles, that is we do not have every everything points of a triangle. 
 
As a result, what it takes place, it is that after the triangulation of the specific triangles, I notice that it draws not the zone desired but what there is all around.
 
So I want to change the algorithm of triangulation but I do not find (either I did not enough search) of algo easily implémentables (which are not very different of my structures)
 
Would you have any suggestions to be proposed to me?
 
PS : The points of the mif file look like :

 
  976130.4 6449595.49999999
    976127.2 6449598.09999999
    976124.7 6449601.79999999
    976122.4 6449605.79999999
    976120.3 6449610.09999999
    976117.2 6449615.39999999
    976115.7 6449619.29999999
    976114.9 6449623.59999999
    976115.7 6449627.49999999
    976119.4 6449630.69999999
    976121.7 6449634.89999999
    976120.6 6449639.49999999

 

 

 
This is a pics to expose my problem : The real surface is in the middle http://img11.hostingpics.net/pics/469919Capturedcran20140318161524.png

Edited by bloupies, 18 March 2014 - 11:02 AM.


Sponsor:

#2 tobiasjs   Members   -  Reputation: 148

Like
0Likes
Like

Posted 18 March 2014 - 05:39 PM

I'll preface this by saying that robust triangulation is very difficult. Whatever you implement will almost certainly fail for some inputs.

 

FIST (http://www.cosy.sbg.ac.at/~held/projects/triang/triang.html) is an algorithm that is comparatively easy to implement. It relies on ear clipping, as does yours. The paper is good, and pretty easy to read.

 

I'm not completely sure what your problem is, from reading your description. I have comments along two lines:

 

1) Performance:

 

Given you're dealing with 1e6 points, you really should use some kind of spatial subdivision structure to accelerate your search for points inside the ear you intend to clip.

 

When you clip an ear of your polygon, having to move ~1e6 indices in V seems wasteful. A linked list might actually be an appropriate structure here (especially if you don't allocate nodes individually, but as a block). At the very least, start working backwards from the back of the array, rather than the front, so that you have less data to shift around.

 

Exploit locality of reference by not moving v when you successfully clip an ear.

 

2) Triangulation quality:

 

It seems like you're worried about producing large triangles that make it hard to render small regions of the total triangulation efficiently. If you think about a circle, there are basically two approaches to producing a triangulation. One will produce a fan, with long thin triangles. The other will produce lots of small triangles at the edge, and some large triangles at the centre. Any way you triangulate a circle will produce a result that is hard to render locally. In general I believe you won't be able to avoid this.

 

You could attempt to improve the quality of your triangulation by looking at adjacent triangles (that form a quad) and flipping the internal edge. You can treat this as an optimization problem - define a flip cost as being the differences in summed areas of the bounding boxes of the two triangles before and after flipping, and then greedily flip triangle pairs while you can decrease the cost. Other cost functions (such as considering how far the triangles deviate from equilateral) will also work.

 

I suspect that what you want to do is to add points to smooth out the triangulation. This is not easy to implement, but you can take a look at the Triangle library, by Jonathan Shewchuk (http://www.cs.cmu.edu/~quake/triangle.html) for an implementation of conforming delaunay triangulation (which is what you want). The Triangle code is free for non-commercial use, so this might be an option for you. 

 

Toby.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS