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