File: polygontriangulation.h

package info (click to toggle)
asc 2.6.1.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 81,740 kB
  • sloc: cpp: 158,704; sh: 11,544; ansic: 6,736; makefile: 604; perl: 138
file content (382 lines) | stat: -rw-r--r-- 10,734 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*! \file polygontriangulation.h
    \brief Interface to the polygon triangulation library
*/

//     $Id: polygontriangulation.h,v 1.6 2009-04-18 13:48:38 mbickel Exp $
/*
    This file is part of Advanced Strategic Command; http://www.asc-hq.de
    Copyright (C) 1994-2010  Martin Bickel  and  Marc Schellenberger

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; see the file COPYING. If not, write to the
    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    Boston, MA  02111-1307  USA
*/

#ifndef polygontriangulationH
 #define polygontriangulationH

 #include <math.h>
 #include <sys/types.h>
 #include <stdlib.h>
 #include <stdio.h>

 namespace PolygonTriangulationLibrary {
 #include "libs/triangul/interfac.h"
 #include "libs/triangul/triangul.h"
 };

template <class Poly>
class  PolygonPainter {
         public:
             bool  paintPolygon ( const Poly& poly );
         protected:
             virtual void   sortpolygon    ( Poly& a );
             virtual void   painttriangle  ( typename Poly::Point p[] );
             virtual void   painthorizline ( int x1, int x2, int y );
             virtual void   paintline      ( int x1, int y1, int x2, int y2 );
             virtual void   setpoint       ( int x,  int y  ) = 0;
             virtual int    getpolydir     ( const Poly& a);
             virtual double getsegmentdir  ( int dx, int dy );
             //! returns true if polygon correct
             virtual bool checkpolygon   ( const Poly& poly );
             //! returns true if lines a-b and c-d don't intersect
             virtual bool checkline      ( typename Poly::Point a, typename Poly::Point b, typename Poly::Point d, typename Poly::Point e );
             virtual ~PolygonPainter() {};
          };

template <class Poly>
bool  PolygonPainter<Poly>::paintPolygon ( const Poly& poly )
{
   if ( !poly.vertex.size() )
      return true;

//   displayLogMessage ( 10, "pp1 ");
   if (checkpolygon ( poly )) {
      if ( poly.vertex.size() == 1 ) {
         setpoint ( poly.vertex[0].x, poly.vertex[0].y );
         return true;
       }
       if ( poly.vertex.size() == 2 ) {
         paintline ( poly.vertex[0].x, poly.vertex[0].y, poly.vertex[1].x, poly.vertex[1].y  );
         return true;
       }

//       displayLogMessage ( 10, "pp2 ");
       Poly pol2 = poly;
       sortpolygon ( pol2 );


       typedef double double2[2];
       double2*  floatpoints = new double2 [ pol2.vertex.size()+1 ];
       for ( int i=0; i<pol2.vertex.size() ; i++ ) {
          floatpoints[i+1][0] = (double) pol2.vertex[i].x;
          floatpoints[i+1][1] = (double) pol2.vertex[i].y;
       } /* endfor */

       typedef int int3[3];

       int3* triangles = new int3  [ pol2.vertex.size() ];

       int contours = pol2.vertex.size();

//       displayLogMessage ( 10, "pp3 ");
       PolygonTriangulationLibrary::triangulate_polygon ( 1, &contours, floatpoints, triangles );
//       displayLogMessage ( 10, "pp4 ");

       for ( int i=0; i<pol2.vertex.size()-2 ;i++ ) {
          typename Poly::Point tripos[3];
          tripos[0] = pol2.vertex[triangles[i][0]-1];
          tripos[1] = pol2.vertex[triangles[i][1]-1];
          tripos[2] = pol2.vertex[triangles[i][2]-1];
          painttriangle ( tripos );
       } /* endfor */

       delete[] triangles;
       delete[] floatpoints;

       return true;
   } else
     return false;
}

static const double pi = 3.141592654;


template <class Poly>
bool PolygonPainter<Poly>::checkline( typename Poly::Point a, typename Poly::Point b, typename Poly::Point d, typename Poly::Point e )
{
   double r,s,t;

   b.x -= a.x;
   b.y -= a.y;

   e.x -= d.x;
   e.y -= d.y;

   /*
   if ( b.y != 0 && e.y != 0 ) {
      if ( abs( b.x / b.y - e.x / e.y ) < 1e-6 )
         return false;
   } else
      if ( b.y == 0 && e.y == 0 )
         return false;
   */


   t = b.x * e.y - b.y * e.x;
   if (t != 0) {
     double m = ( a.x * b.y - a.y * b.x + d.y * b.x - d.x * b.y );
     r = m / -t;

     double n = ( a.y * e.x - a.x * e.y + d.x * e.y - d.y * e.x );
     s = n / t;
     if ( s>=0 && s <= 1  &&  r>=0 && r <= 1)
        return false;

   } /* endif */
   return true;
}


template <class Poly>
bool PolygonPainter<Poly>::checkpolygon   ( const Poly& poly )
{
    if ( poly.vertex.size() > 1  && poly.vertex.size() < 3 )
       return true;
    else {
       size_t i,j,k;

       for ( i = 0; i < poly.vertex.size(); i++ )
          for ( j = 0; j < poly.vertex.size(); j++ )
             if ( i != j )
               if ( poly.vertex[i].x == poly.vertex[j].x  &&
                    poly.vertex[i].y == poly.vertex[j].y )
                  return false;

       k = poly.vertex.size() -1;
       for (i=0; i < k ; i++) {
          for (j = i +2; j < k ; j++)
              if (checkline ( poly.vertex[i], poly.vertex[i+1], poly.vertex[j], poly.vertex[j+1] ) == false)
                  return false;
          if (i > 0 && j == k)
            if (checkline ( poly.vertex[i], poly.vertex[i+1], poly.vertex[j], poly.vertex[0] ) == false)
               return false;
       } /* endfor */
       return true;
    }
}



template <class Poly>
double  PolygonPainter<Poly>::getsegmentdir   ( int dx, int dy )
{
   double alpha;

   if (dx) {
      alpha = atan ( (double) dy / (double) dx );
      if ( dy < 0 ) {
         if ( dx < 0)
            alpha = -pi + alpha;
      } else
        if ( dx < 0)
           alpha = pi - alpha;

   } else {
      if (dy > 0)
         alpha = pi/2;
      else
         alpha = -pi/2;
   } /* endif */

   return alpha;
}

template <class Poly>
int PolygonPainter<Poly>::getpolydir     ( const Poly& a)
{
    int p ;
    double alpha, beta, gamma;
    int   r = 0;
    for (p=0; p< a.vertex.size(); p++) {

       if (p == 0)
          alpha = getsegmentdir( a.vertex[p].x - a.vertex[a.vertex.size()-1].x , a.vertex[p].y - a.vertex[a.vertex.size()-1].y );
       else
          alpha = getsegmentdir( a.vertex[p].x - a.vertex[p-1].x, a.vertex[p].y - a.vertex[p-1].y );

       if (p+1 >= a.vertex.size())
          beta =  getsegmentdir( a.vertex[0].x - a.vertex[p].x , a.vertex[0].y - a.vertex[p].y  );
       else
          beta =  getsegmentdir( a.vertex[p+1].x - a.vertex[p].x,  a.vertex[p+1].y - a.vertex[p].y );

       gamma = beta - alpha ;
       if (gamma > pi)
          gamma = pi - gamma;
       if (gamma < -pi)
          gamma = -pi - gamma;
       r+= (int) ( gamma * 1000 );
    }
    return r;
}


template <class Poly>
void PolygonPainter<Poly>::sortpolygon   ( Poly& a )
{
   if ( a.vertex.size() >= 3 )
      if ( getpolydir(a) < 0 ) {
         for ( int i=0;i< a.vertex.size()/2 ;i++ ) {
            typename Poly::Point p = a.vertex[i];
            a.vertex[i] = a.vertex[a.vertex.size()-i-1];
            a.vertex[a.vertex.size()-i-1] = p;
         } /* endfor */
      }
}


template <class Poly>
void PolygonPainter<Poly>::painttriangle  ( typename Poly::Point p[] )
{
   int x,y,dx1,dx2,dy1,dy2;

   for ( int i=0; i<2 ; ) {
      if ( p[i+1].y < p[i].y || ( p[i+1].y == p[i].y && p[i+1].x < p[i].x )) {
         x        = p[i].x;
         p[i].x   = p[i+1].x;
         p[i+1].x = x;
         y        = p[i].y;
         p[i].y   = p[i+1].y;
         p[i+1].y = y;
         i        = 0;
       } else
         i++;

   } /* endfor */


   if ( p[0].y < p[1].y ) {
      dx1 = p[1].x - p[0].x;
      dx2 = p[2].x - p[0].x;
      dy1 = p[1].y - p[0].y;
      dy2 = p[2].y - p[0].y;

      for (y=p[0].y; y<=p[1].y ; y++)
          painthorizline ( p[0].x + dx1 * (y - p[0].y) / dy1 , p[0].x + dx2 * (y - p[0].y) / dy2, y );

      if (p[1].y < p[2].y) {
         dx1 = p[2].x - p[1].x;
         dy1 = p[2].y - p[1].y;

         for ( ; y<=p[2].y ; y++)
             painthorizline ( p[1].x + dx1 * (y - p[1].y) / dy1 , p[0].x + dx2 * (y - p[0].y) / dy2, y );
      }
   } else {
      if ( p[1].y < p[2].y ) {

         dx1 = p[2].x - p[1].x;
         dx2 = p[2].x - p[0].x;
         dy1 = p[2].y - p[0].y;

         for (y=p[0].y; y<=p[2].y ; y++)
             painthorizline ( p[1].x + dx1 * (y - p[0].y) / dy1 , p[0].x + dx2 * (y - p[0].y) / dy1, y );
      } else
        painthorizline ( p[0].x, p[2].x, p[0].y );
   }

}


template <class Poly>
void   PolygonPainter<Poly>::painthorizline ( int x1, int x2, int y )
{
   int dx;
   if (x1 > x2)
      dx = -1;
   else
      if (x1 < x2)
         dx = 1;
      else {
         setpoint ( x2, y );
         return;
      }

   int x= x1;

   do {
      setpoint ( x, y );
      x += dx;
   } while ( x != x2 ); /* enddo */
   setpoint ( x, y );
}


template <class Poly>
void   PolygonPainter<Poly>::paintline   ( int x1, int y1, int x2, int y2 )
{
        float           m, b;
        int             w;
        float           yy1, yy2, xx1, xx2;



    if ( x1 == x2) {
        for (w=y1;w<=y2 ;w++ )
           setpoint ( x1, w );
    } else {
       if ( y1 == y2) {
          for (w=x1;w<=x2 ;w++ )
               setpoint( w, y1 );
       } else {
                yy1 = y1;
                yy2 = y2;
                xx1 = x1;
                xx2 = x2;
                m = (yy2 - yy1) / (xx2 - xx1);
                b = y1 - m * x1;
                if ((m <= 1) && (m >= -1)) {
                        if (x2 < x1) {
                                w = x2;
                                x2 = x1;
                                x1 = w;
                                w = y2;
                                y2 = y1;
                                y1 = w;
                        }
                        for (w = x1; w <= x2; w++)
                                setpoint(w, (int) (m * w + b) );

                } else {
                        if (y2 < y1) {
                                w = x2;
                                x2 = x1;
                                x1 = w;
                                w = y2;
                                y2 = y1;
                                y1 = w;
                        }
                        for (w = y1; w <= y2; w++) {
                                setpoint( (int) ((w - b) / m), w );
                        }

                }
         } /* endif */
    }
}





#endif