9ba365f6e869b48c75731b090628ecdf592594d9
1 /*
2 * Path.h
3 * nlivarot
4 *
5 * Created by fred on Tue Jun 17 2003.
6 *
7 */
9 #ifndef my_path
10 #define my_path
12 #include <vector>
13 #include "LivarotDefs.h"
14 #include "livarot/livarot-forward.h"
15 #include "libnr/nr-point.h"
16 #include <libnr/nr-rect-l.h>
17 #include <2geom/forward.h>
19 struct SPStyle;
21 /*
22 * the Path class: a structure to hold path description and their polyline approximation (not kept in sync)
23 * the path description is built with regular commands like MoveTo() LineTo(), etc
24 * the polyline approximation is built by a call to Convert() or its variants
25 * another possibility would be to call directly the AddPoint() functions, but that is not encouraged
26 * the conversion to polyline can salvage data as to where on the path each polyline's point lies; use
27 * ConvertWithBackData() for this. after this call, it's easy to rewind the polyline: sequences of points
28 * of the same path command can be reassembled in a command
29 */
31 // polyline description commands
32 enum
33 {
34 polyline_lineto = 0, // a lineto
35 polyline_moveto = 1, // a moveto
36 polyline_forced = 2 // a forced point, ie a point that was an angle or an intersection in a previous life
37 // or more realistically a control point in the path description that created the polyline
38 // forced points are used as "breakable" points for the polyline -> cubic bezier patch operations
39 // each time the bezier fitter encounters such a point in the polyline, it decreases its treshhold,
40 // so that it is more likely to cut the polyline at that position and produce a bezier patch
41 };
43 class Shape;
45 // path creation: 2 phases: first the path is given as a succession of commands (MoveTo, LineTo, CurveTo...); then it
46 // is converted in a polyline
47 // a polylone can be stroked or filled to make a polygon
48 class Path
49 {
50 friend class Shape;
52 public:
54 // flags for the path construction
55 enum
56 {
57 descr_ready = 0,
58 descr_adding_bezier = 1, // we're making a bezier spline, so you can expect pending_bezier_* to have a value
59 descr_doing_subpath = 2, // we're doing a path, so there is a moveto somewhere
60 descr_delayed_bezier = 4,// the bezier spline we're doing was initiated by a TempBezierTo(), so we'll need an endpoint
61 descr_dirty = 16 // the path description was modified
62 };
64 // some data for the construction: what's pending, and some flags
65 int descr_flags;
66 int pending_bezier_cmd;
67 int pending_bezier_data;
68 int pending_moveto_cmd;
69 int pending_moveto_data;
70 // the path description
71 std::vector<PathDescr*> descr_cmd;
73 // polyline storage: a series of coordinates (and maybe weights)
74 // also back data: info on where this polyline's segment comes from, ie wich command in the path description: "piece"
75 // and what abcissis on the chunk of path for this command: "t"
76 // t=0 means it's at the start of the command's chunk, t=1 it's at the end
77 struct path_lineto
78 {
79 path_lineto(bool m, NR::Point pp) : isMoveTo(m), p(pp), piece(-1), t(0) {}
80 path_lineto(bool m, NR::Point pp, int pie, double tt) : isMoveTo(m), p(pp), piece(pie), t(tt) {}
82 int isMoveTo;
83 NR::Point p;
84 int piece;
85 double t;
86 };
88 std::vector<path_lineto> pts;
90 bool back;
92 Path();
93 virtual ~Path();
95 // creation of the path description
96 void Reset(); // reset to the empty description
97 void Copy (Path * who);
99 // the commands...
100 int ForcePoint();
101 int Close();
102 int MoveTo ( NR::Point const &ip);
103 int LineTo ( NR::Point const &ip);
104 int CubicTo ( NR::Point const &ip, NR::Point const &iStD, NR::Point const &iEnD);
105 int ArcTo ( NR::Point const &ip, double iRx, double iRy, double angle, bool iLargeArc, bool iClockwise);
106 int IntermBezierTo ( NR::Point const &ip); // add a quadratic bezier spline control point
107 int BezierTo ( NR::Point const &ip); // quadratic bezier spline to this point (control points can be added after this)
108 int TempBezierTo(); // start a quadratic bezier spline (control points can be added after this)
109 int EndBezierTo();
110 int EndBezierTo ( NR::Point const &ip); // ends a quadratic bezier spline (for curves started with TempBezierTo)
112 // transforms a description in a polyline (for stroking and filling)
113 // treshhold is the max length^2 (sort of)
114 void Convert (double treshhold);
115 void Convert(NRRectL *area, double treshhold);
116 void ConvertEvenLines (double treshhold); // decomposes line segments too, for later recomposition
117 // same function for use when you want to later recompose the curves from the polyline
118 void ConvertWithBackData (double treshhold);
120 // creation of the polyline (you can tinker with these function if you want)
121 void SetBackData (bool nVal); // has back data?
122 void ResetPoints(); // resets to the empty polyline
123 int AddPoint ( NR::Point const &iPt, bool mvto = false); // add point
124 int AddPoint ( NR::Point const &iPt, int ip, double it, bool mvto = false);
125 int AddForcedPoint ( NR::Point const &iPt); // add point
126 int AddForcedPoint ( NR::Point const &iPt, int ip, double it);
127 int ReplacePoint(NR::Point const &iPt); // replace point
129 // transform in a polygon (in a graph, in fact; a subsequent call to ConvertToShape is needed)
130 // - fills the polyline; justAdd=true doesn't reset the Shape dest, but simply adds the polyline into it
131 // closeIfNeeded=false prevent the function from closing the path (resulting in a non-eulerian graph
132 // pathID is a identification number for the path, and is used for recomposing curves from polylines
133 // give each different Path a different ID, and feed the appropriate orig[] to the ConvertToForme() function
134 void Fill(Shape *dest, int pathID = -1, bool justAdd = false,
135 bool closeIfNeeded = true, bool invert = false);
137 // - stroke the path; usual parameters: type of cap=butt, type of join=join and miter (see LivarotDefs.h)
138 // doClose treat the path as closed (ie a loop)
139 void Stroke(Shape *dest, bool doClose, double width, JoinType join,
140 ButtType butt, double miter, bool justAdd = false);
142 // build a Path that is the outline of the Path instance's description (the result is stored in dest)
143 // it doesn't compute the exact offset (it's way too complicated, but an approximation made of cubic bezier patches
144 // and segments. the algorithm was found in a plugin for Impress (by Chris Cox), but i can't find it back...
145 void Outline(Path *dest, double width, JoinType join, ButtType butt,
146 double miter);
148 // half outline with edges having the same direction as the original
149 void OutsideOutline(Path *dest, double width, JoinType join, ButtType butt,
150 double miter);
152 // half outline with edges having the opposite direction as the original
153 void InsideOutline (Path * dest, double width, JoinType join, ButtType butt,
154 double miter);
156 // polyline to cubic bezier patches
157 void Simplify (double treshhold);
159 // description simplification
160 void Coalesce (double tresh);
162 // utilities
163 // piece is a command no in the command list
164 // "at" is an abcissis on the path portion associated with this command
165 // 0=beginning of portion, 1=end of portion.
166 void PointAt (int piece, double at, NR::Point & pos);
167 void PointAndTangentAt (int piece, double at, NR::Point & pos, NR::Point & tgt);
169 // last control point before the command i (i included)
170 // used when dealing with quadratic bezier spline, cause these can contain arbitrarily many commands
171 const NR::Point PrevPoint (const int i) const;
173 // dash the polyline
174 // the result is stored in the polyline, so you lose the original. make a copy before if needed
175 void DashPolyline(float head,float tail,float body,int nbD,float *dashs,bool stPlain,float stOffset);
177 void DashPolylineFromStyle(SPStyle *style, float scale, float min_len);
179 //utilitaire pour inkscape
180 void LoadPath(Geom::Path const &path, Geom::Matrix const &tr, bool doTransformation, bool append = false);
181 void LoadPathVector(Geom::PathVector const &pv, Geom::Matrix const &tr, bool doTransformation);
182 void LoadPathVector(Geom::PathVector const &pv);
183 void* MakeArtBPath();
185 void Transform(const NR::Matrix &trans);
187 // decompose le chemin en ses sous-chemin
188 // killNoSurf=true -> oublie les chemins de surface nulle
189 Path** SubPaths(int &outNb,bool killNoSurf);
190 // pour recuperer les trous
191 // nbNest= nombre de contours
192 // conts= debut de chaque contour
193 // nesting= parent de chaque contour
194 Path** SubPathsWithNesting(int &outNb,bool killNoSurf,int nbNest,int* nesting,int* conts);
195 // surface du chemin (considere comme ferme)
196 double Surface();
197 void PolylineBoundingBox(double &l,double &t,double &r,double &b);
198 void FastBBox(double &l,double &t,double &r,double &b);
199 // longueur (totale des sous-chemins)
200 double Length();
202 void ConvertForcedToMoveTo();
203 void ConvertForcedToVoid();
204 struct cut_position {
205 int piece;
206 double t;
207 };
208 cut_position* CurvilignToPosition(int nbCv,double* cvAbs,int &nbCut);
209 cut_position PointToCurvilignPosition(NR::Point const &pos, unsigned seg = 0) const;
210 //Should this take a cut_position as a param?
211 double PositionToLength(int piece, double t);
213 // caution: not tested on quadratic b-splines, most certainly buggy
214 void ConvertPositionsToMoveTo(int nbPos,cut_position* poss);
215 void ConvertPositionsToForced(int nbPos,cut_position* poss);
217 void Affiche();
218 char *svg_dump_path() const;
220 bool IsLineSegment(int piece);
222 private:
223 // utilitary functions for the path contruction
224 void CancelBezier ();
225 void CloseSubpath();
226 void InsertMoveTo (NR::Point const &iPt,int at);
227 void InsertForcePoint (int at);
228 void InsertLineTo (NR::Point const &iPt,int at);
229 void InsertArcTo (NR::Point const &ip, double iRx, double iRy, double angle, bool iLargeArc, bool iClockwise,int at);
230 void InsertCubicTo (NR::Point const &ip, NR::Point const &iStD, NR::Point const &iEnD,int at);
231 void InsertBezierTo (NR::Point const &iPt,int iNb,int at);
232 void InsertIntermBezierTo (NR::Point const &iPt,int at);
234 // creation of dashes: take the polyline given by spP (length spL) and dash it according to head, body, etc. put the result in
235 // the polyline of this instance
236 void 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);
238 // Functions used by the conversion.
239 // they append points to the polyline
240 void DoArc ( NR::Point const &iS, NR::Point const &iE, double rx, double ry,
241 double angle, bool large, bool wise, double tresh);
242 void RecCubicTo ( NR::Point const &iS, NR::Point const &iSd, NR::Point const &iE, NR::Point const &iEd, double tresh, int lev,
243 double maxL = -1.0);
244 void RecBezierTo ( NR::Point const &iPt, NR::Point const &iS, NR::Point const &iE, double treshhold, int lev, double maxL = -1.0);
246 void DoArc ( NR::Point const &iS, NR::Point const &iE, double rx, double ry,
247 double angle, bool large, bool wise, double tresh, int piece);
248 void RecCubicTo ( NR::Point const &iS, NR::Point const &iSd, NR::Point const &iE, NR::Point const &iEd, double tresh, int lev,
249 double st, double et, int piece);
250 void RecBezierTo ( NR::Point const &iPt, NR::Point const &iS, const NR::Point &iE, double treshhold, int lev, double st, double et,
251 int piece);
253 // don't pay attention
254 struct offset_orig
255 {
256 Path *orig;
257 int piece;
258 double tSt, tEn;
259 double off_dec;
260 };
261 void DoArc ( NR::Point const &iS, NR::Point const &iE, double rx, double ry,
262 double angle, bool large, bool wise, double tresh, int piece,
263 offset_orig & orig);
264 void RecCubicTo ( NR::Point const &iS, NR::Point const &iSd, NR::Point const &iE, NR::Point const &iEd, double tresh, int lev,
265 double st, double et, int piece, offset_orig & orig);
266 void RecBezierTo ( NR::Point const &iPt, NR::Point const &iS, NR::Point const &iE, double treshhold, int lev, double st, double et,
267 int piece, offset_orig & orig);
269 static void ArcAngles ( NR::Point const &iS, NR::Point const &iE, double rx,
270 double ry, double angle, bool large, bool wise,
271 double &sang, double &eang);
272 static void QuadraticPoint (double t, NR::Point &oPt, NR::Point const &iS, NR::Point const &iM, NR::Point const &iE);
273 static void CubicTangent (double t, NR::Point &oPt, NR::Point const &iS,
274 NR::Point const &iSd, NR::Point const &iE,
275 NR::Point const &iEd);
277 struct outline_callback_data
278 {
279 Path *orig;
280 int piece;
281 double tSt, tEn;
282 Path *dest;
283 double x1, y1, x2, y2;
284 union
285 {
286 struct
287 {
288 double dx1, dy1, dx2, dy2;
289 }
290 c;
291 struct
292 {
293 double mx, my;
294 }
295 b;
296 struct
297 {
298 double rx, ry, angle;
299 bool clock, large;
300 double stA, enA;
301 }
302 a;
303 }
304 d;
305 };
307 typedef void (outlineCallback) (outline_callback_data * data, double tol, double width);
308 struct outline_callbacks
309 {
310 outlineCallback *cubicto;
311 outlineCallback *bezierto;
312 outlineCallback *arcto;
313 };
315 void SubContractOutline (int off, int num_pd,
316 Path * dest, outline_callbacks & calls,
317 double tolerance, double width, JoinType join,
318 ButtType butt, double miter, bool closeIfNeeded,
319 bool skipMoveto, NR::Point & lastP, NR::Point & lastT);
320 void DoStroke(int off, int N, Shape *dest, bool doClose, double width, JoinType join,
321 ButtType butt, double miter, bool justAdd = false);
323 static void TangentOnSegAt(double at, NR::Point const &iS, PathDescrLineTo const &fin,
324 NR::Point &pos, NR::Point &tgt, double &len);
325 static void TangentOnArcAt(double at, NR::Point const &iS, PathDescrArcTo const &fin,
326 NR::Point &pos, NR::Point &tgt, double &len, double &rad);
327 static void TangentOnCubAt (double at, NR::Point const &iS, PathDescrCubicTo const &fin, bool before,
328 NR::Point &pos, NR::Point &tgt, double &len, double &rad);
329 static void TangentOnBezAt (double at, NR::Point const &iS,
330 PathDescrIntermBezierTo & mid,
331 PathDescrBezierTo & fin, bool before,
332 NR::Point & pos, NR::Point & tgt, double &len, double &rad);
333 static void OutlineJoin (Path * dest, NR::Point pos, NR::Point stNor, NR::Point enNor,
334 double width, JoinType join, double miter);
336 static bool IsNulCurve (std::vector<PathDescr*> const &cmd, int curD, NR::Point const &curX);
338 static void RecStdCubicTo (outline_callback_data * data, double tol,
339 double width, int lev);
340 static void StdCubicTo (outline_callback_data * data, double tol,
341 double width);
342 static void StdBezierTo (outline_callback_data * data, double tol,
343 double width);
344 static void RecStdArcTo (outline_callback_data * data, double tol,
345 double width, int lev);
346 static void StdArcTo (outline_callback_data * data, double tol, double width);
349 // fonctions annexes pour le stroke
350 static void DoButt (Shape * dest, double width, ButtType butt, NR::Point pos,
351 NR::Point dir, int &leftNo, int &rightNo);
352 static void DoJoin (Shape * dest, double width, JoinType join, NR::Point pos,
353 NR::Point prev, NR::Point next, double miter, double prevL,
354 double nextL, int *stNo, int *enNo);
355 static void DoLeftJoin (Shape * dest, double width, JoinType join, NR::Point pos,
356 NR::Point prev, NR::Point next, double miter, double prevL,
357 double nextL, int &leftStNo, int &leftEnNo,int pathID=-1,int pieceID=0,double tID=0.0);
358 static void DoRightJoin (Shape * dest, double width, JoinType join, NR::Point pos,
359 NR::Point prev, NR::Point next, double miter, double prevL,
360 double nextL, int &rightStNo, int &rightEnNo,int pathID=-1,int pieceID=0,double tID=0.0);
361 static void RecRound (Shape * dest, int sNo, int eNo,
362 NR::Point const &iS, NR::Point const &iE,
363 NR::Point const &nS, NR::Point const &nE,
364 NR::Point &origine,float width);
367 void DoSimplify(int off, int N, double treshhold);
368 bool AttemptSimplify(int off, int N, double treshhold, PathDescrCubicTo &res, int &worstP);
369 static bool FitCubic(NR::Point const &start,
370 PathDescrCubicTo &res,
371 double *Xk, double *Yk, double *Qk, double *tk, int nbPt);
373 struct fitting_tables {
374 int nbPt,maxPt,inPt;
375 double *Xk;
376 double *Yk;
377 double *Qk;
378 double *tk;
379 double *lk;
380 char *fk;
381 double totLen;
382 };
383 bool AttemptSimplify (fitting_tables &data,double treshhold, PathDescrCubicTo & res,int &worstP);
384 bool ExtendFit(int off, int N, fitting_tables &data,double treshhold, PathDescrCubicTo & res,int &worstP);
385 double RaffineTk (NR::Point pt, NR::Point p0, NR::Point p1, NR::Point p2, NR::Point p3, double it);
386 void FlushPendingAddition(Path* dest,PathDescr *lastAddition,PathDescrCubicTo &lastCubic,int lastAD);
388 private:
389 void AddCurve(Geom::Curve const &c);
391 };
392 #endif
394 /*
395 Local Variables:
396 mode:c++
397 c-file-style:"stroustrup"
398 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
399 indent-tabs-mode:nil
400 fill-column:99
401 End:
402 */
403 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :