Code

Patch from Debian bug tracker from Daniel Kahn Gillmor
[inkscape.git] / src / livarot / PathCutting.cpp
1 /*
2  *  PathCutting.cpp
3  *  nlivarot
4  *
5  *  Created by fred on someday in 2004.
6  *  public domain
7  *
8  *  Additional Code by Authors:
9  *   Richard Hughes <cyreve@users.sf.net>
10  *
11  *  Copyright (C) 2005 Richard Hughes
12  *
13  *  Released under GNU GPL, read the file 'COPYING' for more information
14  */
16 #include "Path.h"
17 #include "style.h"
18 #include "livarot/path-description.h"
19 #include "libnr/n-art-bpath.h"
20 #include "libnr/nr-point-matrix-ops.h"
22 void  Path::DashPolyline(float head,float tail,float body,int nbD,float *dashs,bool stPlain,float stOffset)
23 {
24   if ( nbD <= 0 || body <= 0.0001 ) return; // pas de tirets, en fait
26   std::vector<path_lineto> orig_pts = pts;
27   pts.clear();
28   
29   int       lastMI=-1;
30   int curP = 0;
31   int lastMP = -1;
32   
33   for (int i = 0; i < int(orig_pts.size()); i++) {
34     if ( orig_pts[curP].isMoveTo == polyline_moveto ) {
35       if ( lastMI >= 0 && lastMI < i-1 ) { // au moins 2 points
36         DashSubPath(i-lastMI,lastMP, orig_pts, head,tail,body,nbD,dashs,stPlain,stOffset);
37       }
38       lastMI=i;
39       lastMP=curP;
40     }
41     curP++;
42   }
43   if ( lastMI >= 0 && lastMI < int(orig_pts.size()) - 1 ) {
44     DashSubPath(orig_pts.size() - lastMI, lastMP, orig_pts, head, tail, body, nbD, dashs, stPlain, stOffset);
45   }
46 }
48 void  Path::DashPolylineFromStyle(SPStyle *style, float scale, float min_len)
49 {
50     if (style->stroke_dash.n_dash) {
52         double dlen = 0.0;
53         for (int i = 0; i < style->stroke_dash.n_dash; i++) {
54             dlen += style->stroke_dash.dash[i] * scale;
55         }
56         if (dlen >= min_len) {
57             NRVpathDash dash;
58             dash.offset = style->stroke_dash.offset * scale;
59             dash.n_dash = style->stroke_dash.n_dash;
60             dash.dash = g_new(double, dash.n_dash);
61             for (int i = 0; i < dash.n_dash; i++) {
62                 dash.dash[i] = style->stroke_dash.dash[i] * scale;
63             }
64             int    nbD=dash.n_dash;
65             float  *dashs=(float*)malloc((nbD+1)*sizeof(float));
66             while ( dash.offset >= dlen ) dash.offset-=dlen;
67             dashs[0]=dash.dash[0];
68             for (int i=1; i<nbD; i++) {
69                 dashs[i]=dashs[i-1]+dash.dash[i];
70             }
71             // modulo dlen
72             this->DashPolyline(0.0, 0.0, dlen, nbD, dashs, true, dash.offset);
73             free(dashs);
74             g_free(dash.dash);
75         }
76     }
77 }
80 void Path::DashSubPath(int spL, int spP, std::vector<path_lineto> const &orig_pts, float head,float tail,float body,int nbD,float *dashs,bool stPlain,float stOffset)
81 {
82   if ( spL <= 0 || spP == -1 ) return;
83   
84   double      totLength=0;
85   NR::Point   lastP;
86   lastP = orig_pts[spP].p;
87   for (int i=1;i<spL;i++) {
88     NR::Point const n = orig_pts[spP + i].p;
89     NR::Point d=n-lastP;
90     double    nl=NR::L2(d);
91     if ( nl > 0.0001 ) {
92       totLength+=nl;
93       lastP=n;
94     }
95   }
96   
97   if ( totLength <= head+tail ) return; // tout mange par la tete et la queue
98   
99   double    curLength=0;
100   double    dashPos=0;
101   int       dashInd=0;
102   bool      dashPlain=false;
103   double    lastT=0;
104   int       lastPiece=-1;
105   lastP = orig_pts[spP].p;
106   for (int i=1;i<spL;i++) {
107     NR::Point   n;
108     int         nPiece=-1;
109     double      nT=0;
110     if ( back ) {
111       n = orig_pts[spP + i].p;
112       nPiece = orig_pts[spP + i].piece;
113       nT = orig_pts[spP + i].t;
114     } else {
115       n = orig_pts[spP + i].p;
116     }
117     NR::Point d=n-lastP;
118     double    nl=NR::L2(d);
119     if ( nl > 0.0001 ) {
120       double   stLength=curLength;
121       double   enLength=curLength+nl;
122       // couper les bouts en trop
123       if ( curLength <= head && curLength+nl > head ) {
124         nl-=head-curLength;
125         curLength=head;
126         dashInd=0;
127         dashPos=stOffset;
128         bool nPlain=stPlain;
129         while ( dashs[dashInd] < stOffset ) {
130           dashInd++;
131           nPlain=!(nPlain);
132           if ( dashInd >= nbD ) {
133             dashPos=0;
134             dashInd=0;
135             break;
136           }
137         }
138         if ( nPlain == true && dashPlain == false ) {
139           NR::Point  p=(enLength-curLength)*lastP+(curLength-stLength)*n;
140           p/=(enLength-stLength);
141           if ( back ) {
142             double pT=0;
143             if ( nPiece == lastPiece ) {
144               pT=(lastT*(enLength-curLength)+nT*(curLength-stLength))/(enLength-stLength);
145             } else {
146               pT=(nPiece*(curLength-stLength))/(enLength-stLength);
147             }
148             AddPoint(p,nPiece,pT,true);
149           } else {
150             AddPoint(p,true);
151           }
152         } else if ( nPlain == false && dashPlain == true ) {
153         }
154         dashPlain=nPlain;
155       }
156       // faire les tirets
157       if ( curLength >= head /*&& curLength+nl <= totLength-tail*/ ) {
158         while ( curLength <= totLength-tail && nl > 0 ) {
159           if ( enLength <= totLength-tail ) nl=enLength-curLength; else nl=totLength-tail-curLength;
160           double  leftInDash=body-dashPos;
161           if ( dashInd < nbD ) {
162             leftInDash=dashs[dashInd]-dashPos;
163           }
164           if ( leftInDash <= nl ) {
165             bool nPlain=false;
166             if ( dashInd < nbD ) {
167               dashPos=dashs[dashInd];
168               dashInd++;
169               if ( dashPlain ) nPlain=false; else nPlain=true;
170             } else {
171               dashInd=0;
172               dashPos=0;
173               //nPlain=stPlain;
174               nPlain=dashPlain;
175             }
176             if ( nPlain == true && dashPlain == false ) {
177               NR::Point  p=(enLength-curLength-leftInDash)*lastP+(curLength+leftInDash-stLength)*n;
178               p/=(enLength-stLength);
179               if ( back ) {
180                 double pT=0;
181                 if ( nPiece == lastPiece ) {
182                   pT=(lastT*(enLength-curLength-leftInDash)+nT*(curLength+leftInDash-stLength))/(enLength-stLength);
183                 } else {
184                   pT=(nPiece*(curLength+leftInDash-stLength))/(enLength-stLength);
185                 }
186                 AddPoint(p,nPiece,pT,true);
187               } else {
188                 AddPoint(p,true);
189               }
190             } else if ( nPlain == false && dashPlain == true ) {
191               NR::Point  p=(enLength-curLength-leftInDash)*lastP+(curLength+leftInDash-stLength)*n;
192               p/=(enLength-stLength);
193               if ( back ) {
194                 double pT=0;
195                 if ( nPiece == lastPiece ) {
196                   pT=(lastT*(enLength-curLength-leftInDash)+nT*(curLength+leftInDash-stLength))/(enLength-stLength);
197                 } else {
198                   pT=(nPiece*(curLength+leftInDash-stLength))/(enLength-stLength);
199                 }
200                 AddPoint(p,nPiece,pT,false);
201               } else {
202                 AddPoint(p,false);
203               }
204             }
205             dashPlain=nPlain;
206             
207             curLength+=leftInDash;
208             nl-=leftInDash;
209           } else {
210             dashPos+=nl;
211             curLength+=nl;
212             nl=0;
213           }
214         }
215         if ( dashPlain ) {
216           if ( back ) {
217             AddPoint(n,nPiece,nT,false);
218           } else {
219             AddPoint(n,false);
220           }
221         }
222         nl=enLength-curLength;
223       }
224       if ( curLength <= totLength-tail && curLength+nl > totLength-tail ) {
225         nl=totLength-tail-curLength;
226         dashInd=0;
227         dashPos=0;
228         bool nPlain=false;
229         if ( nPlain == true && dashPlain == false ) {
230         } else if ( nPlain == false && dashPlain == true ) {
231           NR::Point  p=(enLength-curLength)*lastP+(curLength-stLength)*n;
232           p/=(enLength-stLength);
233           if ( back ) {
234             double pT=0;
235             if ( nPiece == lastPiece ) {
236               pT=(lastT*(enLength-curLength)+nT*(curLength-stLength))/(enLength-stLength);
237             } else {
238               pT=(nPiece*(curLength-stLength))/(enLength-stLength);
239             }
240             AddPoint(p,nPiece,pT,false);
241           } else {
242             AddPoint(p,false);
243           }
244         }
245         dashPlain=nPlain;
246       }
247       // continuer
248       curLength=enLength;
249       lastP=n;
250       lastPiece=nPiece;
251       lastT=nT;
252     }
253   }
255 #include "../display/canvas-bpath.h"
257 void* Path::MakeArtBPath(void)
259         int                              nb_cmd=0,max_cmd=0;
260         NArtBpath* bpath=(NArtBpath*)g_malloc((max_cmd+1)*sizeof(NArtBpath));
261         
262         NR::Point   lastP,bezSt,bezEn,lastMP;
263         int         lastM=-1,bezNb=0;
264   for (int i=0;i<int(descr_cmd.size());i++) {
265     int const typ = descr_cmd[i]->getType();
266     switch ( typ ) {
267       case descr_close:
268       {
269                                 if ( lastM >= 0 ) {
270                                         bpath[lastM].code=NR_MOVETO;
271                                         if ( nb_cmd >= max_cmd ) {
272                                                 max_cmd=2*nb_cmd+1;
273                                                 bpath=(NArtBpath*)g_realloc(bpath,(max_cmd+1)*sizeof(NArtBpath));
274                                         }
275                                         bpath[nb_cmd].code=NR_LINETO;
276                                         bpath[nb_cmd].x3=lastMP[0];
277                                         bpath[nb_cmd].y3=lastMP[1];
278                                         nb_cmd++;
279                                 }
280                                 lastM=-1;
281                         } 
282                                 break;
283                         case descr_lineto:
284                                 {
285         PathDescrLineTo *nData = dynamic_cast<PathDescrLineTo *>(descr_cmd[i]);
286                                 if ( nb_cmd >= max_cmd ) {
287                                         max_cmd=2*nb_cmd+1;
288                                         bpath=(NArtBpath*)g_realloc(bpath,(max_cmd+1)*sizeof(NArtBpath));
289                                 }
290                                 bpath[nb_cmd].code=NR_LINETO;
291                                 bpath[nb_cmd].x3=nData->p[0];
292                                 bpath[nb_cmd].y3=nData->p[1];
293                                 nb_cmd++;
294                                 lastP=nData->p;
295       }
296         break;
297       case descr_moveto:
298       {
299         PathDescrMoveTo *nData = dynamic_cast<PathDescrMoveTo *>(descr_cmd[i]);
300                                 if ( nb_cmd >= max_cmd ) {
301                                         max_cmd=2*nb_cmd+1;
302                                         bpath=(NArtBpath*)g_realloc(bpath,(max_cmd+1)*sizeof(NArtBpath));
303                                 }
304                                 bpath[nb_cmd].code=NR_MOVETO_OPEN;
305                                 bpath[nb_cmd].x3=nData->p[0];
306                                 bpath[nb_cmd].y3=nData->p[1];
307                                 lastM=nb_cmd;
308                                 nb_cmd++;
309                                 lastP=lastMP=nData->p;
310       }
311         break;
312       case descr_arcto:
313       {
314         PathDescrArcTo *nData = dynamic_cast<PathDescrArcTo *>(descr_cmd[i]);
315                                 lastP=nData->p;
316       }
317         break;
318       case descr_cubicto:
319       {
320         PathDescrCubicTo *nData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[i]);
321                                 if ( nb_cmd >= max_cmd ) {
322                                         max_cmd=2*nb_cmd+1;
323                                         bpath=(NArtBpath*)g_realloc(bpath,(max_cmd+1)*sizeof(NArtBpath));
324                                 }
325                                 bpath[nb_cmd].code=NR_CURVETO;
326                                 bpath[nb_cmd].x1=lastP[0]+0.333333*nData->start[0];
327                                 bpath[nb_cmd].y1=lastP[1]+0.333333*nData->start[1];
328                                 bpath[nb_cmd].x2=nData->p[0]-0.333333*nData->end[0];
329                                 bpath[nb_cmd].y2=nData->p[1]-0.333333*nData->end[1];
330                                 bpath[nb_cmd].x3=nData->p[0];
331                                 bpath[nb_cmd].y3=nData->p[1];
332                                 nb_cmd++;
333                                 lastP=nData->p;
334       }
335         break;
336       case descr_bezierto:
337       {
338         PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[i]);
339                                 if ( nb_cmd >= max_cmd ) {
340                                         max_cmd=2*nb_cmd+1;
341                                         bpath=(NArtBpath*)g_realloc(bpath,(max_cmd+1)*sizeof(NArtBpath));
342                                 }
343                                 if ( nData->nb <= 0 ) {
344                                         bpath[nb_cmd].code=NR_LINETO;
345                                         bpath[nb_cmd].x3=nData->p[0];
346                                         bpath[nb_cmd].y3=nData->p[1];
347                                         nb_cmd++;
348                                         bezNb=0;
349                                 } else if ( nData->nb == 1 ){
350                                         PathDescrIntermBezierTo *iData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[i+1]);
351                                         bpath[nb_cmd].code=NR_CURVETO;
352                                         bpath[nb_cmd].x1=0.333333*(lastP[0]+2*iData->p[0]);
353                                         bpath[nb_cmd].y1=0.333333*(lastP[1]+2*iData->p[1]);
354                                         bpath[nb_cmd].x2=0.333333*(nData->p[0]+2*iData->p[0]);
355                                         bpath[nb_cmd].y2=0.333333*(nData->p[1]+2*iData->p[1]);
356                                         bpath[nb_cmd].x3=nData->p[0];
357                                         bpath[nb_cmd].y3=nData->p[1];
358                                         nb_cmd++;
359                                         bezNb=0;
360                                 } else {
361                                         bezSt=2*lastP-nData->p;
362                                         bezEn=nData->p;
363                                         bezNb=nData->nb;
364                                 }
365                                 lastP=nData->p;
366       }
367         break;
368       case descr_interm_bezier:
369       {
370                                 if ( bezNb > 0 ) {
371                                         PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[i]);
372                                         NR::Point p_m=nData->p,p_s=0.5*(bezSt+p_m),p_e;
373                                         if ( bezNb > 1 ) {
374                                                 PathDescrIntermBezierTo *iData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[i+1]);
375                                                 p_e=0.5*(p_m+iData->p);
376                                         } else {
377                                                 p_e=bezEn;
378                                         }
379                                         
380                                         if ( nb_cmd >= max_cmd ) {
381                                                 max_cmd=2*nb_cmd+1;
382                                                 bpath=(NArtBpath*)g_realloc(bpath,(max_cmd+1)*sizeof(NArtBpath));
383                                         }
384                                         bpath[nb_cmd].code=NR_CURVETO;
385                                         NR::Point  cp1=0.333333*(p_s+2*p_m),cp2=0.333333*(2*p_m+p_e);
386                                         bpath[nb_cmd].x1=cp1[0];
387                                         bpath[nb_cmd].y1=cp1[1];
388                                         bpath[nb_cmd].x2=cp2[0];
389                                         bpath[nb_cmd].y2=cp2[1];
390                                         bpath[nb_cmd].x3=p_e[0];
391                                         bpath[nb_cmd].y3=p_e[1];
392                                         nb_cmd++;
393                                         
394                                         bezNb--;
395                                 }
396                         }
397         break;
398     }
399   }
400         bpath[nb_cmd].code=NR_END;
401         return bpath;
404 void  Path::LoadArtBPath(void *iV,NR::Matrix const &trans,bool doTransformation)
406   if ( iV == NULL ) return;
407   NArtBpath *bpath = (NArtBpath*)iV;
408   
409   SetBackData (false);
410   Reset();
411   {
412     int   i;
413     bool  closed = false;
414     NR::Point lastX(0,0);
415     
416     for (i = 0; bpath[i].code != NR_END; i++)
417     {
418       switch (bpath[i].code)
419       {
420         case NR_LINETO:
421           lastX[0] = bpath[i].x3;
422           lastX[1] = bpath[i].y3;
423           if ( doTransformation ) {
424             lastX*=trans;
425           }
426             LineTo (lastX);
427             break;
428           
429         case NR_CURVETO:
430         {
431           NR::Point  tmp,tms(0,0),tme(0,0),tm1,tm2;
432           tmp[0]=bpath[i].x3;
433           tmp[1]=bpath[i].y3;
434           tm1[0]=bpath[i].x1;
435           tm1[1]=bpath[i].y1;
436           tm2[0]=bpath[i].x2;
437           tm2[1]=bpath[i].y2;
438           if ( doTransformation ) {
439             tmp*=trans;
440             tm1*=trans;
441             tm2*=trans;
442           }
443           tms=3 * (tm1 - lastX);
444           tme=3 * (tmp - tm2);
445           CubicTo (tmp,tms,tme);
446         }
447           lastX[0] = bpath[i].x3;
448           lastX[1] = bpath[i].y3;
449           if ( doTransformation ) {
450             lastX*=trans;
451           }
452           break;
453           
454         case NR_MOVETO_OPEN:
455         case NR_MOVETO:
456           if (closed) Close ();
457           closed = (bpath[i].code == NR_MOVETO);
458           lastX[0] = bpath[i].x3;
459           lastX[1] = bpath[i].y3;
460           if ( doTransformation ) {
461             lastX*=trans;
462           }
463             MoveTo (lastX);
464             break;
465         default:
466           break;
467       }
468     }
469     if (closed) Close ();
470   }
474 /**
475  *    \return Length of the lines in the pts vector.
476  */
478 double Path::Length()
480     if ( pts.empty() ) {
481         return 0;
482     }
484     NR::Point lastP = pts[0].p;
486     double len = 0;
487     for (std::vector<path_lineto>::const_iterator i = pts.begin(); i != pts.end(); i++) {
489         if ( i->isMoveTo != polyline_moveto ) {
490             len += NR::L2(i->p - lastP);
491         }
493         lastP = i->p;
494     }
495     
496     return len;
500 double Path::Surface()
502     if ( pts.empty() ) {
503         return 0;
504     }
505     
506     NR::Point lastM = pts[0].p;
507     NR::Point lastP = lastM;
509     double surf = 0;
510     for (std::vector<path_lineto>::const_iterator i = pts.begin(); i != pts.end(); i++) {
512         if ( i->isMoveTo == polyline_moveto ) {
513             surf += NR::cross(lastM - lastP, lastM);
514             lastP = lastM = i->p;
515         } else {
516             surf += NR::cross(i->p - lastP, i->p);
517             lastP = i->p;
518         }
519         
520     }
521     
522   return surf;
526 Path**      Path::SubPaths(int &outNb,bool killNoSurf)
528   int      nbRes=0;
529   Path**   res=NULL;
530   Path*    curAdd=NULL;
531   
532   for (int i=0;i<int(descr_cmd.size());i++) {
533     int const typ = descr_cmd[i]->getType();
534     switch ( typ ) {
535       case descr_moveto:
536         if ( curAdd ) {
537           if ( curAdd->descr_cmd.size() > 1 ) {
538             curAdd->Convert(1.0);
539             double addSurf=curAdd->Surface();
540             if ( fabs(addSurf) > 0.0001 || killNoSurf == false ) {
541               res=(Path**)g_realloc(res,(nbRes+1)*sizeof(Path*));
542               res[nbRes++]=curAdd;
543             } else { 
544               delete curAdd;
545             }
546           } else {
547             delete curAdd;
548           }
549           curAdd=NULL;
550         }
551         curAdd=new Path;
552         curAdd->SetBackData(false);
553         {
554           PathDescrMoveTo *nData = dynamic_cast<PathDescrMoveTo *>(descr_cmd[i]);
555           curAdd->MoveTo(nData->p);
556         }
557           break;
558       case descr_close:
559       {
560         curAdd->Close();
561       }
562         break;        
563       case descr_lineto:
564       {
565         PathDescrLineTo *nData = dynamic_cast<PathDescrLineTo *>(descr_cmd[i]);
566         curAdd->LineTo(nData->p);
567       }
568         break;
569       case descr_cubicto:
570       {
571         PathDescrCubicTo *nData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[i]);
572         curAdd->CubicTo(nData->p,nData->start,nData->end);
573       }
574         break;
575       case descr_arcto:
576       {
577         PathDescrArcTo *nData = dynamic_cast<PathDescrArcTo *>(descr_cmd[i]);
578         curAdd->ArcTo(nData->p,nData->rx,nData->ry,nData->angle,nData->large,nData->clockwise);
579       }
580         break;
581       case descr_bezierto:
582       {
583         PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[i]);
584         curAdd->BezierTo(nData->p);
585       }
586         break;
587       case descr_interm_bezier:
588       {
589         PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[i]);
590         curAdd->IntermBezierTo(nData->p);
591       }
592         break;
593       default:
594         break;
595     }
596   }
597   if ( curAdd ) {
598     if ( curAdd->descr_cmd.size() > 1 ) {
599       curAdd->Convert(1.0);
600       double addSurf=curAdd->Surface();
601       if ( fabs(addSurf) > 0.0001 || killNoSurf == false  ) {
602         res=(Path**)g_realloc(res,(nbRes+1)*sizeof(Path*));
603         res[nbRes++]=curAdd;
604       } else {
605         delete curAdd;
606       }
607     } else {
608       delete curAdd;
609     }
610   }
611   curAdd=NULL;
612   
613   outNb=nbRes;
614   return res;
616 Path**      Path::SubPathsWithNesting(int &outNb,bool killNoSurf,int nbNest,int* nesting,int* conts)
618   int      nbRes=0;
619   Path**   res=NULL;
620   Path*    curAdd=NULL;
621   bool     increment=false;
622   
623   for (int i=0;i<int(descr_cmd.size());i++) {
624     int const typ = descr_cmd[i]->getType();
625     switch ( typ ) {
626       case descr_moveto:
627       {
628         if ( curAdd && increment == false ) {
629           if ( curAdd->descr_cmd.size() > 1 ) {
630             // sauvegarder descr_cmd[0]->associated
631             int savA=curAdd->descr_cmd[0]->associated;
632             curAdd->Convert(1.0);
633             curAdd->descr_cmd[0]->associated=savA; // associated n'est pas utilise apres
634             double addSurf=curAdd->Surface();
635             if ( fabs(addSurf) > 0.0001 || killNoSurf == false ) {
636               res=(Path**)g_realloc(res,(nbRes+1)*sizeof(Path*));
637               res[nbRes++]=curAdd;
638             } else { 
639               delete curAdd;
640             }
641           } else {
642             delete curAdd;
643           }
644           curAdd=NULL;
645         }
646         Path*  hasDad=NULL;
647         for (int j=0;j<nbNest;j++) {
648           if ( conts[j] == i && nesting[j] >= 0 ) {
649             int  dadMvt=conts[nesting[j]];
650             for (int k=0;k<nbRes;k++) {
651               if ( res[k] && res[k]->descr_cmd.empty() == false && res[k]->descr_cmd[0]->associated == dadMvt ) {
652                 hasDad=res[k];
653                 break;
654               }
655             }
656           }
657           if ( conts[j] > i  ) break;
658         }
659         if ( hasDad ) {
660           curAdd=hasDad;
661           increment=true;
662         } else {
663           curAdd=new Path;
664           curAdd->SetBackData(false);
665           increment=false;
666         }
667         PathDescrMoveTo *nData = dynamic_cast<PathDescrMoveTo *>(descr_cmd[i]);
668         int mNo=curAdd->MoveTo(nData->p);
669         curAdd->descr_cmd[mNo]->associated=i;
670         }
671         break;
672       case descr_close:
673       {
674         curAdd->Close();
675       }
676         break;        
677       case descr_lineto:
678       {
679         PathDescrLineTo *nData = dynamic_cast<PathDescrLineTo *>(descr_cmd[i]);
680         curAdd->LineTo(nData->p);
681       }
682         break;
683       case descr_cubicto:
684       {
685         PathDescrCubicTo *nData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[i]);
686         curAdd->CubicTo(nData->p,nData->start,nData->end);
687       }
688         break;
689       case descr_arcto:
690       {
691         PathDescrArcTo *nData = dynamic_cast<PathDescrArcTo *>(descr_cmd[i]);
692         curAdd->ArcTo(nData->p,nData->rx,nData->ry,nData->angle,nData->large,nData->clockwise);
693       }
694         break;
695       case descr_bezierto:
696       {
697         PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[i]);
698         curAdd->BezierTo(nData->p);
699       }
700         break;
701       case descr_interm_bezier:
702       {
703         PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[i]);
704         curAdd->IntermBezierTo(nData->p);
705       }
706         break;
707       default:
708         break;
709     }
710   }
711   if ( curAdd && increment == false ) {
712     if ( curAdd->descr_cmd.size() > 1 ) {
713       curAdd->Convert(1.0);
714       double addSurf=curAdd->Surface();
715       if ( fabs(addSurf) > 0.0001 || killNoSurf == false  ) {
716         res=(Path**)g_realloc(res,(nbRes+1)*sizeof(Path*));
717         res[nbRes++]=curAdd;
718       } else {
719         delete curAdd;
720       }
721     } else {
722       delete curAdd;
723     }
724   }
725   curAdd=NULL;
726   
727   outNb=nbRes;
728   return res;
732 void Path::ConvertForcedToVoid()
733 {  
734     for (int i=0; i < int(descr_cmd.size()); i++) {
735         if ( descr_cmd[i]->getType() == descr_forced) {
736             delete descr_cmd[i];
737             descr_cmd.erase(descr_cmd.begin() + i);
738         }
739     }
743 void Path::ConvertForcedToMoveTo()
744 {  
745     NR::Point lastSeen(0, 0);
746     NR::Point lastMove(0, 0);
747     
748     {
749         NR::Point lastPos(0, 0);
750         for (int i = int(descr_cmd.size()) - 1; i >= 0; i--) {
751             int const typ = descr_cmd[i]->getType();
752             switch ( typ ) {
753             case descr_forced:
754             {
755                 PathDescrForced *d = dynamic_cast<PathDescrForced *>(descr_cmd[i]);
756                 d->p = lastPos;
757                 break;
758             }
759             case descr_close:
760             {
761                 PathDescrClose *d = dynamic_cast<PathDescrClose *>(descr_cmd[i]);
762                 d->p = lastPos;
763                 break;
764             }
765             case descr_moveto:
766             {
767                 PathDescrMoveTo *d = dynamic_cast<PathDescrMoveTo *>(descr_cmd[i]);
768                 lastPos = d->p;
769                 break;
770             }
771             case descr_lineto:
772             {
773                 PathDescrLineTo *d = dynamic_cast<PathDescrLineTo *>(descr_cmd[i]);
774                 lastPos = d->p;
775                 break;
776             }
777             case descr_arcto:
778             {
779                 PathDescrArcTo *d = dynamic_cast<PathDescrArcTo *>(descr_cmd[i]);
780                 lastPos = d->p;
781                 break;
782             }
783             case descr_cubicto:
784             {
785                 PathDescrCubicTo *d = dynamic_cast<PathDescrCubicTo *>(descr_cmd[i]);
786                 lastPos = d->p;
787                 break;
788             }
789             case descr_bezierto:
790             {
791                 PathDescrBezierTo *d = dynamic_cast<PathDescrBezierTo *>(descr_cmd[i]);
792                 lastPos = d->p;
793                 break;
794             }
795             case descr_interm_bezier:
796             {
797                 PathDescrIntermBezierTo *d = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[i]);
798                 lastPos = d->p;
799                 break;
800             }
801             default:
802                 break;
803             }
804         }
805     }
807     bool hasMoved = false;
808     for (int i = 0; i < int(descr_cmd.size()); i++) {
809         int const typ = descr_cmd[i]->getType();
810         switch ( typ ) {
811         case descr_forced:
812             if ( i < int(descr_cmd.size()) - 1 && hasMoved ) { // sinon il termine le chemin
814                 delete descr_cmd[i];
815                 descr_cmd[i] = new PathDescrMoveTo(lastSeen);
816                 lastMove = lastSeen;
817                 hasMoved = true;
818             }
819             break;
820             
821         case descr_moveto:
822         {
823           PathDescrMoveTo *nData = dynamic_cast<PathDescrMoveTo *>(descr_cmd[i]);
824           lastMove = lastSeen = nData->p;
825           hasMoved = true;
826         }
827         break;
828       case descr_close:
829       {
830         lastSeen=lastMove;
831       }
832         break;        
833       case descr_lineto:
834       {
835         PathDescrLineTo *nData = dynamic_cast<PathDescrLineTo *>(descr_cmd[i]);
836         lastSeen=nData->p;
837       }
838         break;
839       case descr_cubicto:
840       {
841         PathDescrCubicTo *nData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[i]);
842         lastSeen=nData->p;
843      }
844         break;
845       case descr_arcto:
846       {
847         PathDescrArcTo *nData = dynamic_cast<PathDescrArcTo *>(descr_cmd[i]);
848         lastSeen=nData->p;
849       }
850         break;
851       case descr_bezierto:
852       {
853         PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[i]);
854         lastSeen=nData->p;
855      }
856         break;
857       case descr_interm_bezier:
858       {
859         PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[i]);
860         lastSeen=nData->p;
861       }
862         break;
863       default:
864         break;
865     }
866   }
868 static int       CmpPosition(const void * p1, const void * p2) {
869   Path::cut_position *cp1=(Path::cut_position*)p1;
870   Path::cut_position *cp2=(Path::cut_position*)p2;
871   if ( cp1->piece < cp2->piece ) return -1;
872   if ( cp1->piece > cp2->piece ) return 1;
873   if ( cp1->t < cp2->t ) return -1;
874   if ( cp1->t > cp2->t ) return 1;
875   return 0;
877 static int       CmpCurv(const void * p1, const void * p2) {
878   double *cp1=(double*)p1;
879   double *cp2=(double*)p2;
880   if ( *cp1 < *cp2 ) return -1;
881   if ( *cp1 > *cp2 ) return 1;
882   return 0;
886 Path::cut_position* Path::CurvilignToPosition(int nbCv, double *cvAbs, int &nbCut)
888     if ( nbCv <= 0 || pts.empty() || back == false ) {
889         return NULL;
890     }
891   
892     qsort(cvAbs, nbCv, sizeof(double), CmpCurv);
893   
894     cut_position *res = NULL;
895     nbCut = 0;
896     int curCv = 0;
897   
898     double len = 0;
899     double lastT = 0;
900     int lastPiece = -1;
902     NR::Point lastM = pts[0].p;
903     NR::Point lastP = lastM;
905     for (std::vector<path_lineto>::const_iterator i = pts.begin(); i != pts.end(); i++) {
907         if ( i->isMoveTo == polyline_moveto ) {
909             lastP = lastM = i->p;
910             lastT = i->t;
911             lastPiece = i->piece;
913         } else {
914             
915             double const add = NR::L2(i->p - lastP);
916             double curPos = len;
917             double curAdd = add;
918             
919             while ( curAdd > 0.0001 && curCv < nbCv && curPos + curAdd >= cvAbs[curCv] ) {
920                 double const theta = (cvAbs[curCv] - len) / add;
921                 res = (cut_position*) g_realloc(res, (nbCut + 1) * sizeof(cut_position));
922                 res[nbCut].piece = i->piece;
923                 res[nbCut].t = theta * i->t + (1 - theta) * ( (lastPiece != i->piece) ? 0 : lastT);
924                 nbCut++;
925                 curAdd -= cvAbs[curCv] - curPos;
926                 curPos = cvAbs[curCv];
927                 curCv++;
928             }
929             
930             len += add;
931             lastPiece = i->piece;
932             lastP = i->p;
933             lastT = i->t;
934         }
935     }
936     
937     return res;
940 /* 
941 Moved from Layout-TNG-OutIter.cpp
942 TODO: clean up uses of the original function and remove
944 Original Comment:
945 "this function really belongs to Path. I'll probably move it there eventually,
946 hence the Path-esque coding style"
948 */
949 template<typename T> inline static T square(T x) {return x*x;}
950 Path::cut_position Path::PointToCurvilignPosition(NR::Point const &pos) const
952     unsigned bestSeg = 0;
953     double bestRangeSquared = DBL_MAX;
954     double bestT = 0.0; // you need a sentinel, or make sure that you prime with correct values.
956     for (unsigned i = 1 ; i < pts.size() ; i++) {
957         if (pts[i].isMoveTo == polyline_moveto) continue;
958         NR::Point p1, p2, localPos;
959         double thisRangeSquared;
960         double t;
962         if (pts[i - 1].p == pts[i].p) {
963             thisRangeSquared = square(pts[i].p[NR::X] - pos[NR::X]) + square(pts[i].p[NR::Y] - pos[NR::Y]);
964             t = 0.0;
965         } else {
966             // we rotate all our coordinates so we're always looking at a mostly vertical line.
967             if (fabs(pts[i - 1].p[NR::X] - pts[i].p[NR::X]) < fabs(pts[i - 1].p[NR::Y] - pts[i].p[NR::Y])) {
968                 p1 = pts[i - 1].p;
969                 p2 = pts[i].p;
970                 localPos = pos;
971             } else {
972                 p1 = pts[i - 1].p.cw();
973                 p2 = pts[i].p.cw();
974                 localPos = pos.cw();
975             }
976             double gradient = (p2[NR::X] - p1[NR::X]) / (p2[NR::Y] - p1[NR::Y]);
977             double intersection = p1[NR::X] - gradient * p1[NR::Y];
978             /*
979               orthogonalGradient = -1.0 / gradient; // you are going to have numerical problems here.
980               orthogonalIntersection = localPos[NR::X] - orthogonalGradient * localPos[NR::Y];
981               nearestY = (orthogonalIntersection - intersection) / (gradient - orthogonalGradient);
983               expand out nearestY fully :
984               nearestY = (localPos[NR::X] - (-1.0 / gradient) * localPos[NR::Y] - intersection) / (gradient - (-1.0 / gradient));
986               multiply top and bottom by gradient:
987               nearestY = (localPos[NR::X] * gradient - (-1.0) * localPos[NR::Y] - intersection * gradient) / (gradient * gradient - (-1.0));
989               and simplify to get:
990             */
991             double nearestY =  (localPos[NR::X] * gradient + localPos[NR::Y] - intersection * gradient)
992                              / (gradient * gradient + 1.0);
993             t = (nearestY - p1[NR::Y]) / (p2[NR::Y] - p1[NR::Y]);
994             if (t <= 0.0) thisRangeSquared = square(p1[NR::X] - localPos[NR::X]) + square(p1[NR::Y] - localPos[NR::Y]);
995             else if (t >= 1.0) thisRangeSquared = square(p2[NR::X] - localPos[NR::X]) + square(p2[NR::Y] - localPos[NR::Y]);
996             else thisRangeSquared = square(nearestY * gradient + intersection - localPos[NR::X]) + square(nearestY - localPos[NR::Y]);
997         }
999         if (thisRangeSquared < bestRangeSquared) {
1000             bestSeg = i;
1001             bestRangeSquared = thisRangeSquared;
1002             bestT = t;
1003         }
1004     }
1005     Path::cut_position result;
1006     if (bestSeg == 0) {
1007         result.piece = 0;
1008         result.t = 0.0;
1009     } else {
1010         result.piece = pts[bestSeg].piece;
1011         if (result.piece == pts[bestSeg - 1].piece)
1012             result.t = pts[bestSeg - 1].t * (1.0 - bestT) + pts[bestSeg].t * bestT;
1013         else
1014             result.t = pts[bestSeg].t * bestT;
1015     }
1016     return result;
1018 /*
1019     this one also belongs to Path
1020     returns the length of the path up to the position indicated by t (0..1)
1022     TODO: clean up uses of the original function and remove
1024     should this take a cut_position as a parameter?
1025 */
1026 double Path::PositionToLength(int piece, double t)
1028     double length = 0.0;
1029     for (unsigned i = 1 ; i < pts.size() ; i++) {
1030         if (pts[i].isMoveTo == polyline_moveto) continue;
1031         if (pts[i].piece == piece && t < pts[i].t) {
1032             length += NR::L2((t - pts[i - 1].t) / (pts[i].t - pts[i - 1].t) * (pts[i].p - pts[i - 1].p));
1033             break;
1034         }
1035         length += NR::L2(pts[i].p - pts[i - 1].p);
1036     }
1037     return length;
1040 void Path::ConvertPositionsToForced(int nbPos, cut_position *poss)
1042     if ( nbPos <= 0 ) {
1043         return;
1044     }
1045     
1046     {
1047         NR::Point lastPos(0, 0);
1048         for (int i = int(descr_cmd.size()) - 1; i >= 0; i--) {
1049             int const typ = descr_cmd[i]->getType();
1050             switch ( typ ) {
1051                 
1052             case descr_forced:
1053             {
1054                 PathDescrForced *d = dynamic_cast<PathDescrForced *>(descr_cmd[i]);
1055                 d->p = lastPos;
1056                 break;
1057             }
1058                 
1059             case descr_close:
1060             {
1061                 delete descr_cmd[i];
1062                 descr_cmd[i] = new PathDescrLineTo(NR::Point(0, 0));
1064                 int fp = i - 1;
1065                 while ( fp >= 0 && (descr_cmd[fp]->getType()) != descr_moveto ) {
1066                     fp--;
1067                 }
1068                 
1069                 if ( fp >= 0 ) {
1070                     PathDescrMoveTo *oData = dynamic_cast<PathDescrMoveTo *>(descr_cmd[fp]);
1071                     dynamic_cast<PathDescrLineTo*>(descr_cmd[i])->p = oData->p;
1072                 }
1073             }
1074             break;
1075             
1076             case descr_bezierto:
1077             {
1078                 PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[i]);
1079                 NR::Point theP = nData->p;
1080                 if ( nData->nb == 0 ) {
1081                     lastPos = theP;
1082                 }
1083             }
1084             break;
1085             
1086         case descr_moveto:
1087         {
1088             PathDescrMoveTo *d = dynamic_cast<PathDescrMoveTo *>(descr_cmd[i]);
1089             lastPos = d->p;
1090             break;
1091         }
1092         case descr_lineto:
1093         {
1094             PathDescrLineTo *d = dynamic_cast<PathDescrLineTo *>(descr_cmd[i]);
1095             lastPos = d->p;
1096             break;
1097         }
1098         case descr_arcto:
1099         {
1100             PathDescrArcTo *d = dynamic_cast<PathDescrArcTo *>(descr_cmd[i]);
1101             lastPos = d->p;
1102             break;
1103         }
1104         case descr_cubicto:
1105         {
1106             PathDescrCubicTo *d = dynamic_cast<PathDescrCubicTo *>(descr_cmd[i]);
1107             lastPos = d->p;
1108             break;
1109         }
1110         case descr_interm_bezier:
1111         {
1112             PathDescrIntermBezierTo *d = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[i]);
1113             lastPos = d->p;
1114             break;
1115         }
1116         default:
1117           break;
1118       }
1119     }
1120   }
1122   qsort(poss, nbPos, sizeof(cut_position), CmpPosition);
1124   for (int curP=0;curP<nbPos;curP++) {
1125     int   cp=poss[curP].piece;
1126     if ( cp < 0 || cp >= int(descr_cmd.size()) ) break;
1127     float ct=poss[curP].t;
1128     if ( ct < 0 ) continue;
1129     if ( ct > 1 ) continue;
1130         
1131     int const typ = descr_cmd[cp]->getType();
1132     if ( typ == descr_moveto || typ == descr_forced || typ == descr_close ) {
1133       // ponctuel= rien a faire
1134     } else if ( typ == descr_lineto || typ == descr_arcto || typ == descr_cubicto ) {
1135       // facile: creation d'un morceau et d'un forced -> 2 commandes
1136       NR::Point        theP;
1137       NR::Point        theT;
1138       NR::Point        startP;
1139       startP=PrevPoint(cp-1);
1140       if ( typ == descr_cubicto ) {
1141         double           len,rad;
1142         NR::Point        stD,enD,endP;
1143         {
1144           PathDescrCubicTo *oData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[cp]);
1145           stD=oData->start;
1146           enD=oData->end;
1147           endP=oData->p;
1148           TangentOnCubAt (ct, startP, *oData,true, theP,theT,len,rad);
1149         }
1150         
1151         theT*=len;
1152         
1153         InsertCubicTo(endP,(1-ct)*theT,(1-ct)*enD,cp+1);
1154         InsertForcePoint(cp+1);
1155         {
1156           PathDescrCubicTo *nData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[cp]);
1157           nData->start=ct*stD;
1158           nData->end=ct*theT;
1159           nData->p=theP;
1160         }
1161         // decalages dans le tableau des positions de coupe
1162         for (int j=curP+1;j<nbPos;j++) {
1163           if ( poss[j].piece == cp ) {
1164             poss[j].piece+=2;
1165             poss[j].t=(poss[j].t-ct)/(1-ct);
1166           } else {
1167             poss[j].piece+=2;
1168           }
1169         }
1170       } else if ( typ == descr_lineto ) {
1171         NR::Point        endP;
1172         {
1173           PathDescrLineTo *oData = dynamic_cast<PathDescrLineTo *>(descr_cmd[cp]);
1174           endP=oData->p;
1175         }
1177         theP=ct*endP+(1-ct)*startP;
1178         
1179         InsertLineTo(endP,cp+1);
1180         InsertForcePoint(cp+1);
1181         {
1182           PathDescrLineTo *nData = dynamic_cast<PathDescrLineTo *>(descr_cmd[cp]);
1183           nData->p=theP;
1184         }
1185         // decalages dans le tableau des positions de coupe
1186        for (int j=curP+1;j<nbPos;j++) {
1187           if ( poss[j].piece == cp ) {
1188             poss[j].piece+=2;
1189             poss[j].t=(poss[j].t-ct)/(1-ct);
1190           } else {
1191             poss[j].piece+=2;
1192           }
1193         }
1194       } else if ( typ == descr_arcto ) {
1195         NR::Point        endP;
1196         double           rx,ry,angle;
1197         bool             clockw,large;
1198         double   delta=0;
1199         {
1200           PathDescrArcTo *oData = dynamic_cast<PathDescrArcTo *>(descr_cmd[cp]);
1201           endP=oData->p;
1202           rx=oData->rx;
1203           ry=oData->ry;
1204           angle=oData->angle;
1205           clockw=oData->clockwise;
1206           large=oData->large;
1207         }
1208         {
1209           double      sang,eang;
1210           ArcAngles(startP,endP,rx,ry,angle,large,clockw,sang,eang);
1211           
1212           if (clockw) {
1213             if ( sang < eang ) sang += 2*M_PI;
1214             delta=eang-sang;
1215           } else {
1216             if ( sang > eang ) sang -= 2*M_PI;
1217             delta=eang-sang;
1218           }
1219           if ( delta < 0 ) delta=-delta;
1220         }
1221         
1222         PointAt (cp,ct, theP);
1223         
1224         if ( delta*(1-ct) > M_PI ) {
1225           InsertArcTo(endP,rx,ry,angle,true,clockw,cp+1);
1226         } else {
1227           InsertArcTo(endP,rx,ry,angle,false,clockw,cp+1);
1228         }
1229         InsertForcePoint(cp+1);
1230         {
1231           PathDescrArcTo *nData = dynamic_cast<PathDescrArcTo *>(descr_cmd[cp]);
1232           nData->p=theP;
1233           if ( delta*ct > M_PI ) {
1234             nData->clockwise=true;
1235           } else {
1236             nData->clockwise=false;
1237           }
1238         }
1239         // decalages dans le tableau des positions de coupe
1240         for (int j=curP+1;j<nbPos;j++) {
1241           if ( poss[j].piece == cp ) {
1242             poss[j].piece+=2;
1243             poss[j].t=(poss[j].t-ct)/(1-ct);
1244           } else {
1245             poss[j].piece+=2;
1246           }
1247         }
1248       }
1249     } else if ( typ == descr_bezierto || typ == descr_interm_bezier ) {
1250       // dur
1251       int theBDI=cp;
1252       while ( theBDI >= 0 && (descr_cmd[theBDI]->getType()) != descr_bezierto ) theBDI--;
1253       if ( (descr_cmd[theBDI]->getType()) == descr_bezierto ) {
1254         PathDescrBezierTo theBD=*(dynamic_cast<PathDescrBezierTo *>(descr_cmd[theBDI]));
1255         if ( cp >= theBDI && cp < theBDI+theBD.nb ) {
1256           if ( theBD.nb == 1 ) {
1257             NR::Point        endP=theBD.p;
1258             NR::Point        midP;
1259             NR::Point        startP;
1260             startP=PrevPoint(theBDI-1);
1261             {
1262               PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[theBDI+1]);
1263               midP=nData->p;
1264             }
1265             NR::Point       aP=ct*midP+(1-ct)*startP;
1266             NR::Point       bP=ct*endP+(1-ct)*midP;
1267             NR::Point       knotP=ct*bP+(1-ct)*aP;
1268                         
1269             InsertIntermBezierTo(bP,theBDI+2);
1270             InsertBezierTo(knotP,1,theBDI+2);
1271             InsertForcePoint(theBDI+2);
1272             {
1273               PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[theBDI+1]);
1274               nData->p=aP;
1275             }
1276             {
1277               PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[theBDI]);
1278               nData->p=knotP;
1279             }
1280             // decalages dans le tableau des positions de coupe
1281             for (int j=curP+1;j<nbPos;j++) {
1282               if ( poss[j].piece == cp ) {
1283                 poss[j].piece+=3;
1284                 poss[j].t=(poss[j].t-ct)/(1-ct);
1285               } else {
1286                 poss[j].piece+=3;
1287               }
1288             }
1289             
1290           } else {
1291             // decouper puis repasser
1292             if ( cp > theBDI ) {
1293               NR::Point   pcP,ncP;
1294               {
1295                 PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[cp]);
1296                 pcP=nData->p;
1297               }
1298               {
1299                 PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[cp+1]);
1300                 ncP=nData->p;
1301               }
1302               NR::Point knotP=0.5*(pcP+ncP);
1303               
1304               InsertBezierTo(knotP,theBD.nb-(cp-theBDI),cp+1);
1305               {
1306                 PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[theBDI]);
1307                 nData->nb=cp-theBDI;
1308               }
1309               
1310               // decalages dans le tableau des positions de coupe
1311               for (int j=curP;j<nbPos;j++) {
1312                 if ( poss[j].piece == cp ) {
1313                   poss[j].piece+=1;
1314                 } else {
1315                   poss[j].piece+=1;
1316                 }
1317               }
1318               curP--;
1319             } else {
1320               NR::Point   pcP,ncP;
1321               {
1322                 PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[cp+1]);
1323                 pcP=nData->p;
1324               }
1325               {
1326                 PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[cp+2]);
1327                 ncP=nData->p;
1328               }
1329               NR::Point knotP=0.5*(pcP+ncP);
1330               
1331               InsertBezierTo(knotP,theBD.nb-1,cp+2);
1332               {
1333                 PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[theBDI]);
1334                 nData->nb=1;
1335               }
1336               
1337               // decalages dans le tableau des positions de coupe
1338               for (int j=curP;j<nbPos;j++) {
1339                 if ( poss[j].piece == cp ) {
1340 //                  poss[j].piece+=1;
1341                 } else {
1342                   poss[j].piece+=1;
1343                 }
1344               }
1345               curP--;
1346             }
1347           }
1348         } else {
1349           // on laisse aussi tomber
1350         }
1351       } else {
1352         // on laisse tomber
1353       }
1354     }
1355   }
1358 void        Path::ConvertPositionsToMoveTo(int nbPos,cut_position* poss)
1360   ConvertPositionsToForced(nbPos,poss);
1361 //  ConvertForcedToMoveTo();
1362   // on fait une version customizee a la place
1364   Path*  res=new Path;
1365   
1366   NR::Point    lastP(0,0);
1367   for (int i=0;i<int(descr_cmd.size());i++) {
1368     int const typ = descr_cmd[i]->getType();
1369     if ( typ == descr_moveto ) {
1370       NR::Point  np;
1371       {
1372         PathDescrMoveTo *nData = dynamic_cast<PathDescrMoveTo *>(descr_cmd[i]);
1373         np=nData->p;
1374       }
1375       NR::Point  endP;
1376       bool       hasClose=false;
1377       int        hasForced=-1;
1378       bool       doesClose=false;
1379       int        j=i+1;
1380       for (;j<int(descr_cmd.size());j++) {
1381         int const ntyp = descr_cmd[j]->getType();
1382         if ( ntyp == descr_moveto ) {
1383           j--;
1384           break;
1385         } else if ( ntyp == descr_forced ) {
1386           if ( hasForced < 0 ) hasForced=j;
1387         } else if ( ntyp == descr_close ) {
1388           hasClose=true;
1389           break;
1390         } else if ( ntyp == descr_lineto ) {
1391           PathDescrLineTo *nData = dynamic_cast<PathDescrLineTo *>(descr_cmd[j]);
1392           endP=nData->p;
1393         } else if ( ntyp == descr_arcto ) {
1394           PathDescrArcTo *nData = dynamic_cast<PathDescrArcTo *>(descr_cmd[j]);
1395           endP=nData->p;
1396         } else if ( ntyp == descr_cubicto ) {
1397           PathDescrCubicTo *nData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[j]);
1398           endP=nData->p;
1399         } else if ( ntyp == descr_bezierto ) {
1400           PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[j]);
1401           endP=nData->p;
1402         } else {
1403         }
1404       }
1405       if ( NR::LInfty(endP-np) < 0.00001 ) {
1406         doesClose=true;
1407       }
1408       if ( ( doesClose || hasClose ) && hasForced >= 0 ) {
1409  //       printf("nasty i=%i j=%i frc=%i\n",i,j,hasForced);
1410         // aghhh.
1411         NR::Point   nMvtP=PrevPoint(hasForced);
1412         res->MoveTo(nMvtP);
1413         NR::Point   nLastP=nMvtP;
1414         for (int k = hasForced + 1; k < j; k++) {
1415           int ntyp=descr_cmd[k]->getType();
1416           if ( ntyp == descr_moveto ) {
1417             // ne doit pas arriver
1418           } else if ( ntyp == descr_forced ) {
1419             res->MoveTo(nLastP);
1420           } else if ( ntyp == descr_close ) {
1421             // rien a faire ici; de plus il ne peut y en avoir qu'un
1422           } else if ( ntyp == descr_lineto ) {
1423             PathDescrLineTo *nData = dynamic_cast<PathDescrLineTo *>(descr_cmd[k]);
1424             res->LineTo(nData->p);
1425             nLastP=nData->p;
1426           } else if ( ntyp == descr_arcto ) {
1427             PathDescrArcTo *nData = dynamic_cast<PathDescrArcTo *>(descr_cmd[k]);
1428             res->ArcTo(nData->p,nData->rx,nData->ry,nData->angle,nData->large,nData->clockwise);
1429             nLastP=nData->p;
1430           } else if ( ntyp == descr_cubicto ) {
1431             PathDescrCubicTo *nData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[k]);
1432             res->CubicTo(nData->p,nData->start,nData->end);
1433             nLastP=nData->p;
1434           } else if ( ntyp == descr_bezierto ) {
1435             PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[k]);
1436             res->BezierTo(nData->p);
1437             nLastP=nData->p;
1438           } else if ( ntyp == descr_interm_bezier ) {
1439             PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[k]);
1440             res->IntermBezierTo(nData->p);
1441           } else {
1442           }
1443         }
1444         if ( doesClose == false ) res->LineTo(np);
1445         nLastP=np;
1446         for (int k=i+1;k<hasForced;k++) {
1447           int ntyp=descr_cmd[k]->getType();
1448           if ( ntyp == descr_moveto ) {
1449             // ne doit pas arriver
1450           } else if ( ntyp == descr_forced ) {
1451             res->MoveTo(nLastP);
1452           } else if ( ntyp == descr_close ) {
1453             // rien a faire ici; de plus il ne peut y en avoir qu'un
1454           } else if ( ntyp == descr_lineto ) {
1455             PathDescrLineTo *nData = dynamic_cast<PathDescrLineTo *>(descr_cmd[k]);
1456             res->LineTo(nData->p);
1457             nLastP=nData->p;
1458           } else if ( ntyp == descr_arcto ) {
1459             PathDescrArcTo *nData = dynamic_cast<PathDescrArcTo *>(descr_cmd[k]);
1460             res->ArcTo(nData->p,nData->rx,nData->ry,nData->angle,nData->large,nData->clockwise);
1461             nLastP=nData->p;
1462           } else if ( ntyp == descr_cubicto ) {
1463             PathDescrCubicTo *nData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[k]);
1464             res->CubicTo(nData->p,nData->start,nData->end);
1465             nLastP=nData->p;
1466           } else if ( ntyp == descr_bezierto ) {
1467             PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[k]);
1468             res->BezierTo(nData->p);
1469             nLastP=nData->p;
1470           } else if ( ntyp == descr_interm_bezier ) {
1471             PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[k]);
1472             res->IntermBezierTo(nData->p);
1473           } else {
1474           }
1475         }
1476         lastP=nMvtP;
1477         i=j;
1478       } else {
1479         // regular, just move on
1480         res->MoveTo(np);
1481         lastP=np;
1482       }
1483     } else if ( typ == descr_close ) {
1484       res->Close();
1485     } else if ( typ == descr_forced ) {
1486       res->MoveTo(lastP);
1487     } else if ( typ == descr_lineto ) {
1488       PathDescrLineTo *nData = dynamic_cast<PathDescrLineTo *>(descr_cmd[i]);
1489       res->LineTo(nData->p);
1490       lastP=nData->p;
1491     } else if ( typ == descr_arcto ) {
1492       PathDescrArcTo *nData = dynamic_cast<PathDescrArcTo *>(descr_cmd[i]);
1493       res->ArcTo(nData->p,nData->rx,nData->ry,nData->angle,nData->large,nData->clockwise);
1494       lastP=nData->p;
1495     } else if ( typ == descr_cubicto ) {
1496       PathDescrCubicTo *nData = dynamic_cast<PathDescrCubicTo *>(descr_cmd[i]);
1497       res->CubicTo(nData->p,nData->start,nData->end);
1498       lastP=nData->p;
1499     } else if ( typ == descr_bezierto ) {
1500       PathDescrBezierTo *nData = dynamic_cast<PathDescrBezierTo *>(descr_cmd[i]);
1501       res->BezierTo(nData->p);
1502       lastP=nData->p;
1503     } else if ( typ == descr_interm_bezier ) {
1504       PathDescrIntermBezierTo *nData = dynamic_cast<PathDescrIntermBezierTo *>(descr_cmd[i]);
1505       res->IntermBezierTo(nData->p);
1506     } else {
1507     }
1508   }
1510   Copy(res);
1511   delete res;
1512   return;
1515 /*
1516   Local Variables:
1517   mode:c++
1518   c-file-style:"stroustrup"
1519   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1520   indent-tabs-mode:nil
1521   fill-column:99
1522   End:
1523 */
1524 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :