Code

moving trunk for module inkscape
[inkscape.git] / src / livarot / Shape.h
1 /*
2  *  Digraph.h
3  *  nlivarot
4  *
5  *  Created by fred on Thu Jun 12 2003.
6  *
7  */
9 #ifndef my_shape
10 #define my_shape
12 #include <cmath>
13 #include <cstdio>
14 #include <cstdlib>
15 #include <inttypes.h>
16 #include <cstring>
17 #include <vector>
19 #include "libnr/nr-point.h"
20 #include "livarot/livarot-forward.h"
21 #include "livarot/LivarotDefs.h"
23 struct SweepTree;
24 struct SweepTreeList;
25 struct SweepEventQueue;
27 /*
28  * the Shape class (was the Digraph class, as the header says) stores digraphs (no kidding!) of which 
29  * a very interesting kind are polygons.
30  * the main use of this class is the ConvertToShape() (or Booleen(), quite the same) function, which
31  * removes all problems a polygon can present: duplicate points or edges, self-intersection. you end up with a
32  * full-fledged polygon
33  */
35 // possible values for the "type" field in the Shape class:
36 enum
37 {
38   shape_graph = 0,              // it's just a graph; a bunch of edges, maybe intersections
39   shape_polygon = 1,            // a polygon: intersection-free, edges oriented so that the inside is on their left
40   shape_polypatch = 2           // a graph without intersection; each face is a polygon (not yet used)
41 };
43 class IntLigne;
44 class BitLigne;
45 class AlphaLigne;
47 class Shape
48 {
49 public:
51     struct back_data
52     {
53         int pathID, pieceID;
54         double tSt, tEn;
55     };
56     
57     struct voronoi_point
58     {                           // info for points treated as points of a voronoi diagram (obtained by MakeShape())
59         double value;           // distance to source
60         int winding;            // winding relatively to source
61     };
62     
63     struct voronoi_edge
64     {                           // info for edges, treated as approximation of edges of the voronoi diagram
65         int leF, riF;           // left and right site
66         double leStX, leStY, riStX, riStY;      // on the left side: (leStX,leStY) is the smallest vector from the source to st
67         // etc...
68         double leEnX, leEnY, riEnX, riEnY;
69     };
71     struct quick_raster_data
72     {
73         double x;                           // x-position on the sweepline
74         int    bord;                    // index of the edge
75         int    ind;       // index of qrsData elem for edge (ie inverse of the bord)
76         int    next,prev; // dbl linkage
77     };
79     enum sTreeChangeType
80     {
81         EDGE_INSERTED = 0,
82         EDGE_REMOVED = 1,
83         INTERSECTION = 2
84     };
85   
86     struct sTreeChange
87     {
88         sTreeChangeType type;           // type of modification to the sweepline:
89         int ptNo;                       // point at which the modification takes place
90         
91         Shape *src;                     // left edge (or unique edge if not an intersection) involved in the event
92         int bord;
93         Shape *osrc;            // right edge (if intersection)
94         int obord;
95         Shape *lSrc;            // edge directly on the left in the sweepline at the moment of the event
96         int lBrd;
97         Shape *rSrc;            // edge directly on the right
98         int rBrd;
99     };
100     
101     struct incidenceData
102     {
103         int nextInc;            // next incidence in the linked list
104         int pt;                 // point incident to the edge (there is one list per edge)
105         double theta;           // coordinate of the incidence on the edge
106     };
107     
108     Shape();
109     ~Shape();
111     void MakeBackData(bool nVal);
112     void MakeVoronoiData(bool nVal);
114     void Affiche();
116     // insertion/deletion/movement of elements in the graph
117     void Copy(Shape *a);
118     // -reset the graph, and ensure there's room for n points and m edges
119     void Reset(int n = 0, int m = 0);
120     //  -points:
121     int AddPoint(const NR::Point x);    // as the function name says
122     // returns the index at which the point has been added in the array
123     void SubPoint(int p);       // removes the point at index p
124     // nota: this function relocates the last point to the index p
125     // so don't trust point indices if you use SubPoint
126     void SwapPoints(int a, int b);      // swaps 2 points at indices a and b
127     void SwapPoints(int a, int b, int c);       // swaps 3 points: c <- a <- b <- c
128     void SortPoints();  // sorts the points if needed (checks the need_points_sorting flag)
130     //  -edges:
131     // add an edge between points of indices st and en    
132     int AddEdge(int st, int en);
133     // return the edge index in the array
134     
135     // add an edge between points of indices st and en    
136     int AddEdge(int st, int en, int leF, int riF);
137     // return the edge index in the array
138     
139     // version for the voronoi (with faces IDs)
140     void SubEdge(int e);                // removes the edge at index e (same remarks as for SubPoint)
141     void SwapEdges(int a, int b);       // swaps 2 edges
142     void SwapEdges(int a, int b, int c);        // swaps 3 edges
143     void SortEdges();   // sort the edges if needed (checks the need_edges_sorting falg)
145     // primitives for topological manipulations
146   
147     // endpoint of edge at index b that is different from the point p      
148     inline int Other(int p, int b) const
149     {
150         if (getEdge(b).st == p) {
151             return getEdge(b).en;
152         }
153         return getEdge(b).st;
154     }
156     // next edge (after edge b) in the double-linked list at point p  
157     inline int NextAt(int p, int b) const       
158     {
159         if (p == getEdge(b).st) {
160             return getEdge(b).nextS;
161         }
162         else if (p == getEdge(b).en) {
163             return getEdge(b).nextE;
164         }
165         
166         return -1;
167     }
169     // previous edge
170     inline int PrevAt(int p, int b) const
171     {
172         if (p == getEdge(b).st) {
173             return getEdge(b).prevS;
174         }
175         else if (p == getEdge(b).en) {
176             return getEdge(b).prevE;
177         }
178         
179         return -1;
180     }
182     // same as NextAt, but the list is considered circular  
183     inline int CycleNextAt(int p, int b) const  
184     {
185         if (p == getEdge(b).st) {
186             if (getEdge(b).nextS < 0) {
187                 return getPoint(p).incidentEdge[FIRST];
188             }
189             return getEdge(b).nextS;
190         } else if (p == getEdge(b).en) {
191             if (getEdge(b).nextE < 0) {
192                 return getPoint(p).incidentEdge[FIRST];
193             }
194             
195             return getEdge(b).nextE;
196         }
197         
198         return -1;
199     }
201     // same as PrevAt, but the list is considered circular  
202     inline int CyclePrevAt(int p, int b) const
203     {
204         if (p == getEdge(b).st) {
205             if (getEdge(b).prevS < 0) {
206                 return getPoint(p).incidentEdge[LAST];
207             }
208             return getEdge(b).prevS;
209         } else if (p == getEdge(b).en) {
210             if (getEdge(b).prevE < 0) {
211                 return getPoint(p).incidentEdge[LAST];
212             }
213             return getEdge(b).prevE;
214         }
215         
216         return -1;
217     }
218     
219     void ConnectStart(int p, int b);    // set the point p as the start of edge b
220     void ConnectEnd(int p, int b);      // set the point p as the end of edge b
221     void DisconnectStart(int b);        // disconnect edge b from its start point
222     void DisconnectEnd(int b);  // disconnect edge b from its end point
224     // reverses edge b (start <-> end)    
225     void Inverse(int b);                
226     // calc bounding box and sets leftX,rightX,topY and bottomY to their values
227     void CalcBBox(bool strict_degree = false);
228     
229     // debug function: plots the graph (mac only)
230     void Plot(double ix, double iy, double ir, double mx, double my, bool doPoint,
231               bool edgesNo, bool pointNo, bool doDir, char *fileName);
233     // transforms a polygon in a "forme" structure, ie a set of contours, which can be holes (see ShapeUtils.h)
234     // return NULL in case it's not possible
235     void ConvertToForme(Path *dest);
236     
237     // version to use when conversion was done with ConvertWithBackData(): will attempt to merge segment belonging to 
238     // the same curve
239     // nota: apparently the function doesn't like very small segments of arc
240     void ConvertToForme(Path *dest, int nbP, Path **orig, bool splitWhenForced = false);
241     // version trying to recover the nesting of subpaths (ie: holes)
242     void ConvertToFormeNested(Path *dest, int nbP, Path **orig, int wildPath, int &nbNest,
243                               int *&nesting, int *&contStart, bool splitWhenForced = false);
244   
245     // sweeping a digraph to produce a intersection-free polygon
246     // return 0 if everything is ok and a return code otherwise (see LivarotDefs.h)
247     // the input is the Shape "a"
248     // directed=true <=> non-zero fill rule    
249     int ConvertToShape(Shape *a, FillRule directed = fill_nonZero, bool invert = false);
250     // directed=false <=> even-odd fill rule
251     // invert=true: make as if you inverted all edges in the source
252     int Reoriente(Shape *a);    // subcase of ConvertToShape: the input a is already intersection-free
253     // all that's missing are the correct directions of the edges
254     // Reoriented is equivalent to ConvertToShape(a,false,false) , but faster sicne
255     // it doesn't computes interections nor adjacencies
256     void ForceToPolygon();      // force the Shape to believe it's a polygon (eulerian+intersection-free+no
257     // duplicate edges+no duplicate points)
258     // be careful when using this function
260     // the coordinate rounding function
261     inline static double Round(double x)
262     {
263         return ldexp(rint(ldexp(x, 5)), -5);
264     }
265     
266     // 2 miscannellous variations on it, to scale to and back the rounding grid
267     inline static double HalfRound(double x)
268     {
269         return ldexp(x, -5);
270     }
271     
272     inline static double IHalfRound(double x)
273     {
274         return ldexp(x, 5);
275     }
277     // boolean operations on polygons (requests intersection-free poylygons)
278     // boolean operation types are defined in LivarotDefs.h
279     // same return code as ConvertToShape
280     int Booleen(Shape *a, Shape *b, BooleanOp mod, int cutPathID = -1);
282     // create a graph that is an offseted version of the graph "of"
283     // the offset is dec, with joins between edges of type "join" (see LivarotDefs.h)
284     // the result is NOT a polygon; you need a subsequent call to ConvertToShape to get a real polygon
285     int MakeOffset(Shape *of, double dec, JoinType join, double miter);
286   
287     int PtWinding(const NR::Point px) const; // plus rapide
288     int Winding(const NR::Point px) const;
289   
290     // rasterization
291     void BeginRaster(float &pos, int &curPt);
292     void EndRaster();
293     void BeginQuickRaster(float &pos, int &curPt);
294     void EndQuickRaster();
295   
296     void Scan(float &pos, int &curP, float to, float step);
297     void QuickScan(float &pos, int &curP, float to, bool doSort, float step);
298     void DirectScan(float &pos, int &curP, float to, float step);
299     void DirectQuickScan(float &pos, int &curP, float to, bool doSort, float step);
301     void Scan(float &pos, int &curP, float to, FloatLigne *line, bool exact, float step);
302     void Scan(float &pos, int &curP, float to, FillRule directed, BitLigne *line, bool exact, float step);
303     void Scan(float &pos, int &curP, float to, AlphaLigne *line, bool exact, float step);
305     void QuickScan(float &pos, int &curP, float to, FloatLigne* line, float step);
306     void QuickScan(float &pos, int &curP, float to, FillRule directed, BitLigne* line, float step);
307     void QuickScan(float &pos, int &curP, float to, AlphaLigne* line, float step);
309     void Transform(NR::Matrix const &tr)
310         {for(std::vector<dg_point>::iterator it=_pts.begin();it!=_pts.end();it++) it->x*=tr;}
312     std::vector<back_data> ebData;
313     std::vector<voronoi_point> vorpData;
314     std::vector<voronoi_edge> voreData;
316     int nbQRas;
317     int firstQRas;
318     int lastQRas;
319     std::vector<quick_raster_data> qrsData;
321     std::vector<sTreeChange> chgts;
322     int nbInc;
323     int maxInc;
325     incidenceData *iData;
326     // these ones are allocated at the beginning of each sweep and freed at the end of the sweep
327     SweepTreeList *sTree;
328     SweepEventQueue *sEvts;
329     
330     // bounding box stuff
331     double leftX, topY, rightX, bottomY;
333     // topological information: who links who?
334     struct dg_point
335     {
336         NR::Point x;            // position
337         int dI, dO;             // indegree and outdegree
338         int incidentEdge[2];    // first and last incident edge
339         int oldDegree;
340         
341         int totalDegree() const { return dI + dO; }
342     };
343     
344     struct dg_arete
345     {
346         NR::Point dx;           // edge vector
347         int st, en;             // start and end points of the edge
348         int nextS, prevS;       // next and previous edge in the double-linked list at the start point
349         int nextE, prevE;       // next and previous edge in the double-linked list at the end point
350     };
352     // lists of the nodes and edges
353     int maxPt; // [FIXME: remove this]
354     int maxAr; // [FIXME: remove this]
355     
356     // flags
357     int type;
358     
359     inline int numberOfPoints() const { return _pts.size(); }
360     inline bool hasPoints() const { return (_pts.empty() == false); }
361     inline int numberOfEdges() const { return _aretes.size(); }
362     inline bool hasEdges() const { return (_aretes.empty() == false); }
364     inline void needPointsSorting() { _need_points_sorting = true; }
365     inline void needEdgesSorting()  { _need_edges_sorting = true; }
366     
367     inline bool hasBackData() const { return _has_back_data; }
368     
369     inline dg_point const &getPoint(int n) const { return _pts[n]; }
370     inline dg_arete const &getEdge(int n) const { return _aretes[n]; }
372 private:
374     friend class SweepTree;
375     friend class SweepEvent;
376     friend class SweepEventQueue;
377   
378     // temporary data for the various algorithms
379     struct edge_data
380     {
381         int weight;                     // weight of the edge (to handle multiple edges)
382         NR::Point rdx;          // rounded edge vector
383         double length, sqlength, ilength, isqlength;    // length^2, length, 1/length^2, 1/length
384         double siEd, coEd;              // siEd=abs(rdy/length) and coEd=rdx/length
385         edge_data() : weight(0), length(0.0), sqlength(0.0), ilength(0.0), isqlength(0.0), siEd(0.0), coEd(0.0) {}
386         // used to determine the "most horizontal" edge between 2 edges
387     };
388     
389     struct sweep_src_data
390     {
391         void *misc;                     // pointer to the SweepTree* in the sweepline
392         int firstLinkedPoint;   // not used
393         int stPt, enPt;         // start- end end- points for this edge in the resulting polygon
394         int ind;                        // for the GetAdjacencies function: index in the sliceSegs array (for quick deletions)
395         int leftRnd, rightRnd;  // leftmost and rightmost points (in the result polygon) that are incident to
396         // the edge, for the current sweep position
397         // not set if the edge doesn't start/end or intersect at the current sweep position
398         Shape *nextSh;          // nextSh and nextBo identify the next edge in the list
399         int nextBo;                     // they are used to maintain a linked list of edge that start/end or intersect at
400         // the current sweep position
401         int curPoint, doneTo;
402         double curT;
403     };
404     
405     struct sweep_dest_data
406     {
407         void *misc;                     // used to check if an edge has already been seen during the depth-first search
408         int suivParc, precParc; // previous and current next edge in the depth-first search
409         int leW, riW;           // left and right winding numbers for this edge
410         int ind;                        // order of the edges during the depth-first search
411     };
412     
413     struct raster_data
414     {
415         SweepTree *misc;                // pointer to the associated SweepTree* in the sweepline
416         double lastX, lastY, curX, curY;        // curX;curY is the current intersection of the edge with the sweepline
417         // lastX;lastY is the intersection with the previous sweepline
418         bool sens;                      // true if the edge goes down, false otherwise
419         double calcX;           // horizontal position of the intersection of the edge with the
420         // previous sweepline
421         double dxdy, dydx;              // horizontal change per unit vertical move of the intersection with the sweepline
422         int guess;
423     };
424     
425     struct point_data
426     {
427         int oldInd, newInd;             // back and forth indices used when sorting the points, to know where they have
428         // been relocated in the array
429         int pending;            // number of intersection attached to this edge, and also used when sorting arrays
430         int edgeOnLeft;         // not used (should help speeding up winding calculations)
431         int nextLinkedPoint;    // not used
432         Shape *askForWindingS;
433         int askForWindingB;
434         NR::Point  rx;          // rounded coordinates of the point
435     };
436     
437     
438     struct edge_list
439     {                           // temporary array of edges for easier sorting
440         int no;
441         bool starting;
442         NR::Point x;
443     };
445     void initialisePointData();
446     void initialiseEdgeData();
447     void clearIncidenceData();
449     void _countUpDown(int P, int *numberUp, int *numberDown, int *upEdge, int *downEdge) const;
450     void _countUpDownTotalDegree2(int P, int *numberUp, int *numberDown, int *upEdge, int *downEdge) const;
451     void _updateIntersection(int e, int p);
452   
453     // activation/deactivation of the temporary data arrays
454     void MakePointData(bool nVal);
455     void MakeEdgeData(bool nVal);
456     void MakeSweepSrcData(bool nVal);
457     void MakeSweepDestData(bool nVal);
458     void MakeRasterData(bool nVal);
459     void MakeQuickRasterData(bool nVal);
461     void SortPoints(int s, int e);
462     void SortPointsByOldInd(int s, int e);
464     // fonctions annexes pour ConvertToShape et Booleen
465     void ResetSweep();  // allocates sweep structures
466     void CleanupSweep();        // deallocates them
468     // edge sorting function    
469     void SortEdgesList(edge_list *edges, int s, int e);
470   
471     void TesteIntersection(SweepTree *t, Side s, bool onlyDiff);        // test if there is an intersection
472     bool TesteIntersection(SweepTree *iL, SweepTree *iR, NR::Point &atx, double &atL, double &atR, bool onlyDiff);
473     bool TesteIntersection(Shape *iL, Shape *iR, int ilb, int irb,
474                            NR::Point &atx, double &atL, double &atR,
475                            bool onlyDiff);
476     bool TesteAdjacency(Shape *iL, int ilb, const NR::Point atx, int nPt,
477                         bool push);
478     int PushIncidence(Shape *a, int cb, int pt, double theta);
479     int CreateIncidence(Shape *a, int cb, int pt);
480     void AssemblePoints(Shape *a);
481     int AssemblePoints(int st, int en);
482     void AssembleAretes(FillRule directed = fill_nonZero);
483     void AddChgt(int lastPointNo, int lastChgtPt, Shape *&shapeHead,
484                  int &edgeHead, sTreeChangeType type, Shape *lS, int lB, Shape *rS,
485                  int rB);
486     void CheckAdjacencies(int lastPointNo, int lastChgtPt, Shape *shapeHead, int edgeHead);
487     void CheckEdges(int lastPointNo, int lastChgtPt, Shape *a, Shape *b, BooleanOp mod);
488     void Avance(int lastPointNo, int lastChgtPt, Shape *iS, int iB, Shape *a, Shape *b, BooleanOp mod);
489     void DoEdgeTo(Shape *iS, int iB, int iTo, bool direct, bool sens);
490     void GetWindings(Shape *a, Shape *b = NULL, BooleanOp mod = bool_op_union, bool brutal = false);
492     void Validate();
494     int Winding(int nPt) const;
495     void SortPointsRounded();
496     void SortPointsRounded(int s, int e);
497     
498     void CreateEdge(int no, float to, float step);
499     void AvanceEdge(int no, float to, bool exact, float step);
500     void DestroyEdge(int no, float to, FloatLigne *line);
501     void AvanceEdge(int no, float to, FloatLigne *line, bool exact, float step);
502     void DestroyEdge(int no, BitLigne *line);
503     void AvanceEdge(int no, float to, BitLigne *line, bool exact, float step);
504     void DestroyEdge(int no, AlphaLigne *line);
505     void AvanceEdge(int no, float to, AlphaLigne *line, bool exact, float step);
506   
507     void AddContour(Path * dest, int nbP, Path **orig, int startBord,
508                    int curBord, bool splitWhenForced);
509     int ReFormeLineTo(int bord, int curBord, Path *dest, Path *orig);
510     int ReFormeArcTo(int bord, int curBord, Path *dest, Path *orig);
511     int ReFormeCubicTo(int bord, int curBord, Path *dest, Path *orig);
512     int ReFormeBezierTo(int bord, int curBord, Path *dest, Path *orig);
513     void ReFormeBezierChunk(const NR::Point px, const NR::Point nx,
514                             Path *dest, int inBezier, int nbInterm,
515                             Path *from, int p, double ts, double te);
517     int QuickRasterChgEdge(int oBord, int nbord, double x);
518     int QuickRasterAddEdge(int bord, double x, int guess);
519     void QuickRasterSubEdge(int bord);
520     void QuickRasterSwapEdge(int a, int b);
521     void QuickRasterSort();
523     bool _need_points_sorting;  ///< points have been added or removed: we need to sort the points again
524     bool _need_edges_sorting;   ///< edges have been added: maybe they are not ordered clockwise
525     ///< nota: if you remove an edge, the clockwise order still holds
526     bool _has_points_data;      ///< the pData array is allocated
527     bool _has_edges_data;       ///< the eData array is allocated
528     bool _has_sweep_src_data;   ///< the swsData array is allocated
529     bool _has_sweep_dest_data;  ///< the swdData array is allocated
530     bool _has_raster_data;      ///< the swrData array is allocated
531     bool _has_quick_raster_data;///< the swrData array is allocated
532     bool _has_back_data;        //< the ebData array is allocated
533     bool _has_voronoi_data;
535     std::vector<dg_point> _pts;
536     std::vector<dg_arete> _aretes;
537   
538     // the arrays of temporary data
539     // these ones are dynamically kept at a length of maxPt or maxAr
540     std::vector<edge_data> eData;
541     std::vector<sweep_src_data> swsData;
542     std::vector<sweep_dest_data> swdData;
543     std::vector<raster_data> swrData;
544     std::vector<point_data> pData;
545     
546     static int CmpQRs(const quick_raster_data &p1, const quick_raster_data &p2) {
547         if ( fabs(p1.x - p2.x) < 0.00001 ) {
548             return 0;
549         }
550         
551         return ( ( p1.x < p2.x ) ? -1 : 1 );
552     };
554     // edge direction comparison function    
555     static int CmpToVert(const NR::Point ax, const NR::Point bx, bool as, bool bs);
556 };
558 bool directedEulerian(Shape const *s);
559 double distance(Shape const *s, NR::Point const &p);
560 bool distanceLessThanOrEqual(Shape const *s, NR::Point const &p, double const max_l2);
562 #endif