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