Click here to Skip to main content
13,629,890 members
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 6 Feb 2002
Licenced CPOL

Tesselation of Mono Connected Non Convex Polygon

, 6 Feb 2002
Rate this:
Please Sign up or sign in to vote.
Tesselation of mono connected non convex polygon

Sample Image - PolyTry.jpg


A simple alternative to OpenGL polygon tesselation callback. You can focus on file triangle.h that contain the class to evaluate convexity of polygon and tesselate the polygon. No extra points are inserted. You can work only with planar polygon also in 3D.

Every suggestion in order to speed up the class and create better tesselation ('better' triangles) is appreciated.


  • 6th February, 2002: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Peppino Sbargzeguti
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalcut object in slicing Pin
joy_nim24-Feb-06 6:01
memberjoy_nim24-Feb-06 6:01 
GeneralPossible error and fix Pin
GregDude21-Jul-05 15:34
memberGregDude21-Jul-05 15:34 
I found this routine sometimes produces incorrect tesselation. When certain points in the polygon winding are colinear, it can skip over them and produce triangles outside of the original polygon.

My fix simply classifies points on the edge of the test triangle as 'inside' instead of 'outside'. This appears to be a stable and beneficial fix so far.

Example Input:
<br />
poly normal (0.000f,-0.500f,0.866f)<br />
poly points<br />
(0.700f,6.500f,4.071f)  // 0<br />
(0.700f,5.500f,3.494f)  // 1 This point gets mis-classified with 2,8,9 triangle<br />
(-1.700f,5.500f,3.494f) // 2<br />
(-1.700f,6.500f,4.071f) // 3<br />
(-2.500f,6.500f,4.071f) // 4<br />
(-7.300f,1.700f,1.300f) // 5<br />
(11.300f,1.700f,1.300f) // 6<br />
(7.500f,5.500f,3.494f)  // 7<br />
(4.300f,5.500f,3.494f)  // 8<br />
(4.300f,6.500f,4.071f)  // 9<br />

Old Code in function IsPointInside():
<br />
pmq2.Vector(m_e1,ntmp);  if( (B0=m_N.Dot(ntmp)) <= 0.0 ) return false;<br />
m_e0.Vector(pmq2,ntmp);  if( (B1=m_N.Dot(ntmp)) <= 0.0 ) return false;<br />
return ( (m_A-B0-B1) > 0.0 ? true : false );<br />

New Code:
<br />
pmq2.Vector(m_e1,ntmp);  if( (B0=m_N.Dot(ntmp)) < 0.0 ) return false;<br />
m_e0.Vector(pmq2,ntmp);  if( (B1=m_N.Dot(ntmp)) < 0.0 ) return false;<br />
return ( (m_A-B0-B1) < 0.0 ? false : true);<br />

Helpful explaination of internal variables:
<br />
//...........^.....<br />
//.........I/......<br />
//.N......./.......<br />
//.^...e0./........<br />
//.|...../__-->p...<br />
//.|...K.\.........<br />
//........\...A....<br />
//......e1.\.......<br />
//..........\......<br />
//...........v.....<br />
//..........J......<br />
//(I replaced spaces with dots as they were not preserved in post?)<br />
//<br />
// I,J,K (points of current test triangle, indices or indirect indices)<br />
// e0,e1 (edges from IK, JK)<br />
// A (Area of current triangle, is actually area*2)<br />
// N (Normal of current triangle, not unit length)<br />
//<br />
// p Test point if in current triangle.<br />
//<br />


Further testing shows that neither the old or new code correctly and consistently handles coincident and colinear points. (Eg. An example of a coincident point, is a Figure '8' polygons that does not actually cross over, just converges in the middle.)

Example Input:
<br />
// Eg. Coincident point, figure 8 style<br />
poly normal (0.000f,-0.500f,0.866f)<br />
poly points<br />
(1.700f,6.300f,2.801f)<br />
(4.300f,3.700f,1.300f)<br />
(5.700f,3.700f,1.300f)<br />
(7.000f,5.000f,2.051f)<br />
(8.300f,3.700f,1.300f)<br />
(9.300f,3.700f,1.300f)<br />
(8.000f,5.000f,2.051f)<br />
(7.000f,5.000f,2.051f)<br />
(1.700f,10.300f,5.111f)<br />

This should help more with coincident points:
Old code in IsAnyPointInside():
<br />
if( ( ip < i || ip > k ) &&<br />
  IsPointInside(points[m_nIndex[ip]],points[ik]) )<br />
{<br />
  return true;<br />
}<br />

New code:
<br />
if( ip < i || ip > k )<br />
{<br />
  // NOTE: This is Vector3 exact equality test<br />
  if(    points[m_nIndex[ip]].Equal(points[m_nIndex[i]]) <br />
      || points[m_nIndex[ip]].Equal(points[m_nIndex[j]])<br />
      || points[m_nIndex[ip]].Equal(points[m_nIndex[k]])<br />
    )<br />
  {<br />
    continue; // Identical points are not inside, they do not hurt turns<br />
  }<br />
  if( IsPointInside(points[m_nIndex[ip]],points[ik]) )<br />
  {<br />
    return true;<br />
  }<br />
}<br />

If topus's suggested change is valid, it should read
<br />
//<br />
// j is alligned from i to k ?<br />
//<br />
if( ((-FLT_EPSILON) < m_A && m_A < FLT_EPSILON) || // Area OR Poor Normal<br />
((-FLT_EPSILON) < m_N[0] && m_N[0] < FLT_EPSILON &&<br />
(-FLT_EPSILON) < m_N[1] && m_N[1] < FLT_EPSILON &&<br />
(-FLT_EPSILON) < m_N[2] && m_N[2] < FLT_EPSILON ))<br />
return degenerate;<br />
// NOTE: This code is more efficient on PC with eg. 'fabsf(m_A) <= FLT_EPSILON' to reduce float compares.<br />

Also note that you can help the algorithm by removing 'junk' before processing. Eg. project 3D points onto plane (of which normal is used). Remove degeneracies like coincident and colinear points. Snapping and merging points may help, but could lead to bad-snaps that cause self intersections.

I have found another case which I believe shows a flaw in the algorithm. As 'ears' are clipped off the polygon, the remaining polygon can become self intersecting and fail 'point in poly' test, to produce triangles outside the original shape.

Example input:
<br />
poly normal(0,0,1)<br />
(10.52881622f,-1.25890017f,0.00000000f)<br />
(10.52881241f,0.24110639f,0.00000000f)<br />
(-1.47119045f,0.24110317f,0.00000000f)<br />
(-1.47119141f,-1.25889719f,0.00000000f)<br />
(4.52881575f,-1.25890088f,0.00000000f)<br />
(4.52881479f,-3.25889254f,0.00000000f)<br />
(6.08583260f,-3.25889111f,0.00000000f)<br />
(6.08583069f,-1.25889945f,0.00000000f)<br />

The fix:
in function Triangulate()
in 'case convex :'
in else, after 'RemoveVertex'
<br />
// Advance to preserve poly integrity<br />
i = j;<br />
j = k;<br />
k++;<br />

This allows the point AFTER the removed triangle point to be the start of the next test triangle. This helps with near colinear points that fan out from an otherwise stationary start point.

There are still issues with the code. The bottom line is that the original algorithm does not handle simple non-covex polys with colinear, but non intersecting segments. The overall algorithm does not handle polygons that intersect in anyway, including at single points, though it often produces a correct or near correct result.

Please comment on this.
GeneralRobust tesselation Pin
Skavenger1-Jul-05 22:04
memberSkavenger1-Jul-05 22:04 
GeneralRe: Robust tesselation [modified] Pin
Liu Guoping9-Aug-06 15:51
memberLiu Guoping9-Aug-06 15:51 
AnswerRe: Robust tesselation Pin
Skavenger310-Aug-06 1:42
memberSkavenger310-Aug-06 1:42 
GeneralProblem in CPolyTri::ComputeNormal Pin
Torsten B8-Jun-04 1:43
memberTorsten B8-Jun-04 1:43 
GeneralRe: Problem in CPolyTri::ComputeNormal Pin
Sebastien Maraux13-Dec-04 3:52
memberSebastien Maraux13-Dec-04 3:52 
GeneralRe: Problem in CPolyTri::ComputeNormal Pin
Sébastien Maraux13-Dec-04 23:45
memberSébastien Maraux13-Dec-04 23:45 
GeneralGood Article Pin
mchapman123422-May-04 11:50
membermchapman123422-May-04 11:50 
GeneralFound an index range error in code Pin
TruePyroman23-Feb-03 14:38
memberTruePyroman23-Feb-03 14:38 
GeneralRe: Found an index range error in code Pin
topus10-Apr-03 2:46
membertopus10-Apr-03 2:46 
GeneralBetter tesselation Pin
jeckle8-Feb-02 9:00
memberjeckle8-Feb-02 9:00 
GeneralRe: Better tesselation Pin
pepito10-Feb-02 22:05
memberpepito10-Feb-02 22:05 
GeneralRe: Better tesselation Pin
jeckle11-Feb-02 10:46
memberjeckle11-Feb-02 10:46 
GeneralRe: Better tesselation Pin
Rick York11-Feb-02 10:53
memberRick York11-Feb-02 10:53 
GeneralBetter tesselation Pin
Tim Aidley8-Feb-02 2:45
memberTim Aidley8-Feb-02 2:45 
GeneralRe: Better tesselation Pin
kilowatt8-Feb-02 8:21
memberkilowatt8-Feb-02 8:21 
GeneralRe: Better tesselation Pin
pepito sbarzeguti10-Feb-02 22:03
memberpepito sbarzeguti10-Feb-02 22:03 
QuestionGuareschiano anche tu? Pin
Anonymous7-Feb-02 21:19
memberAnonymous7-Feb-02 21:19 
AnswerRe: Guareschiano anche tu? Pin
Dieter Hammer8-Feb-02 0:04
memberDieter Hammer8-Feb-02 0:04 
GeneralRe: Guareschiano anche tu? Pin
pepito8-Feb-02 1:38
memberpepito8-Feb-02 1:38 
GeneralRe: Guareschiano anche tu? Pin
Anonymous10-Feb-02 22:37
memberAnonymous10-Feb-02 22:37 
GeneralVery poor article on very interesting subject... Pin
George7-Feb-02 14:25
memberGeorge7-Feb-02 14:25 
GeneralRe: Very poor article on very interesting subject... Pin
Pepito Sbarzeguti7-Feb-02 21:46
memberPepito Sbarzeguti7-Feb-02 21:46 
GeneralRe: Very poor article on very interesting subject... Pin
Anonymous7-Feb-02 21:47
memberAnonymous7-Feb-02 21:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04-2016 | 2.8.180712.1 | Last Updated 7 Feb 2002
Article Copyright 2002 by Peppino Sbargzeguti
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid