Code

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