简体   繁体   中英

Triangulation of concave polygon using Triangle library

I'm using a C++ wrapper of the Triangle library and can't get rid of the triangles that are out of my polygon.

Here is what I want to get:

目标

And here is what I got with Triangle:

用Triangle三角剖分后的多边形

(I intentionally didn't fill triangles to show that there are some triangles out of polygon)

I tried to get rid of outer triangles using Ray casting algorithm (finding the center of triangles edges and looking if it is in polygon. If at least one is not there than don't paint this triangle) but it is very slow for my purpose (the polygon is updated every frame). Did I miss something? Which parameters should I pass to Triangle to triangulate concave polygon?

If it is not possible to do what I want with Triangle, could someone suggest me a fast method for painting polygon (my program uses Qt Quick Scene Graph and I'm restricted to paint only in triangles or convex polygons as OpenGL requires)? Currently I want to try tessellation method from GLU and drawing using stencil buffer. Also I plan to try this library .

There are at least two ways for drawing general concave polygons:

  1. Apply a triangulation algorithm. This is what you have been trying. There should be plenty of literature about the topic, and source code you may be able to use.

  2. Use the stencil buffer. This is illustrated in my answers to these older questions: Black out everything outside a polygon , How to force openGL to draw a non-convex filled polygon .

However, if you only need to draw the half-moon type shapes that you use in your example, and not arbitrary concave polygons, this all seems much more complicated than necessary. You can easily build a triangle strip to represent this shape.

Your current half moon is probably rendered with a vertex sequence that looks something like this, which defines the vertices in the order needed for a (concave) polygon:

0                                              9
  17                                        10
  1   16                                11   8
            15                    12
      2             14    13             7

            3                      6
                    4      5

You can render this shape directly with primitive type GL_TRIANGLE_STRIP by ordering the vertices differently, so that vertices from the bottom arc alternate with vertices from the top arc:

0                                             17
   2                                        16
  1    4                                14   15
             6                    12
      3              8    10             13

            5                      11
                    7      9

GLU tesselation solved the problem. It shows high performance and good triangulation quality and works well on my laptop as well as on Android tablet Nexus 7. You can find self-contained version of GLU libtess here .

You could try a hidden bitmap with a hit-test but it's working on the output only. I did this for contour plots: https://cntm.codeplex.com/ . Or you could try alpha shapes. It's defined as removing edges exceeding alpha.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM