Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SweepLine.h « geometry « intern « freestyle « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1165e1bf0640ccc90f53b29769e663d8908072f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* SPDX-License-Identifier: GPL-2.0-or-later */

#pragma once

/** \file
 * \ingroup freestyle
 * \brief Class to define a Sweep Line
 */

#include <list>
#include <vector>

#ifdef WITH_CXX_GUARDEDALLOC
#  include "MEM_guardedalloc.h"
#endif

namespace Freestyle {

/** Class to define the intersection between two segments. */
template<class Edge> class Intersection {
 public:
  template<class EdgeClass> Intersection(EdgeClass *eA, real ta, EdgeClass *eB, real tb)
  {
    EdgeA = eA;
    EdgeB = eB;
    tA = ta;
    tB = tb;
    userdata = 0;
  }

  Intersection(const Intersection &iBrother)
  {
    EdgeA = iBrother.EdgeA;
    EdgeB = iBrother.EdgeB;
    tA = iBrother.tA;
    tB = iBrother.tB;
    userdata = 0;
  }

  /** returns the parameter giving the intersection, for the edge iEdge */
  real getParameter(Edge *iEdge)
  {
    if (iEdge == EdgeA) {
      return tA;
    }
    if (iEdge == EdgeB) {
      return tB;
    }
    return 0;
  }

 public:
  void *userdata;  // FIXME

  Edge *EdgeA;  // first segment
  Edge *EdgeB;  // second segment
  real tA;      // parameter defining the intersection point with respect to the segment EdgeA.
  real tB;      // parameter defining the intersection point with respect to the segment EdgeB.

#ifdef WITH_CXX_GUARDEDALLOC
  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Intersection")
#endif
};

#ifdef _MSC_VER
#  pragma warning(push)
#  pragma warning(disable : 4521)  // disable warning C4521: multiple copy constructors specified
#endif

template<class T, class Point> class Segment {
 public:
  Segment()
  {
  }

  Segment(T &s, const Point &iA, const Point &iB)
  {
    _edge = s;
    if (iA < iB) {
      A = iA;
      B = iB;
      _order = true;
    }
    else {
      A = iB;
      B = iA;
      _order = false;
    }
  }

  Segment(Segment<T, Point> &iBrother)
  {
    _edge = iBrother.edge();
    A = iBrother.A;
    B = iBrother.B;
    _Intersections = iBrother._Intersections;
    _order = iBrother._order;
  }

  Segment(const Segment<T, Point> &iBrother)
  {
    _edge = iBrother._edge;
    A = iBrother.A;
    B = iBrother.B;
    _Intersections = iBrother._Intersections;
    _order = iBrother._order;
  }

  ~Segment()
  {
    _Intersections.clear();
  }

  inline Point operator[](const unsigned short int &i) const
  {
    return (i % 2 == 0) ? A : B;
  }

  inline bool operator==(const Segment<T, Point> &iBrother)
  {
    if (_edge == iBrother._edge) {
      return true;
    }
    return false;
  }

  /* Adds an intersection for this segment */
  inline void AddIntersection(Intersection<Segment<T, Point>> *i)
  {
    _Intersections.push_back(i);
  }

  /** Checks for a common vertex with another edge */
  inline bool CommonVertex(const Segment<T, Point> &S, Point &CP)
  {
    if ((A == S[0]) || (A == S[1])) {
      CP = A;
      return true;
    }
    if ((B == S[0]) || (B == S[1])) {
      CP = B;
      return true;
    }
    return false;
  }

  inline vector<Intersection<Segment<T, Point>> *> &intersections()
  {
    return _Intersections;
  }

  inline bool order()
  {
    return _order;
  }

  inline T &edge()
  {
    return _edge;
  }

 private:
  T _edge;
  Point A;
  Point B;
  std::vector<Intersection<Segment<T, Point>> *>
      _Intersections;  // list of intersections parameters
  bool _order;  // true if A and B are in the same order than _edge.A and _edge.B. false otherwise.

#ifdef WITH_CXX_GUARDEDALLOC
  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Segment")
#endif
};

#ifdef _MSC_VER
#  pragma warning(pop)
#endif

/** defines a binary function that can be overload by the user to specify at each condition the
 * intersection between 2 edges must be computed
 */
template<class T1, class T2> struct binary_rule {
  binary_rule()
  {
  }
  template<class T3, class T4> binary_rule(const binary_rule<T3, T4> &brother)
  {
  }
  virtual ~binary_rule()
  {
  }

  virtual bool operator()(T1 &, T2 &)
  {
    return true;
  }
};

template<class T, class Point> class SweepLine {
 public:
  SweepLine()
  {
  }
  ~SweepLine()
  {
    for (typename vector<Intersection<Segment<T, Point>> *>::iterator i = _Intersections.begin(),
                                                                      iend = _Intersections.end();
         i != iend;
         i++) {
      delete (*i);
    }
  }

  inline void process(Point &p,
                      vector<Segment<T, Point> *> &segments,
#if 0
                      binary_rule<Segment<T, Point>, Segment<T, Point>> &binrule =
                          binary_rule<Segment<T, Point>, Segment<T, Point>>(),
#else
                      binary_rule<Segment<T, Point>, Segment<T, Point>> &binrule,
#endif
                      real epsilon = M_EPSILON)
  {
    // first we remove the segments that need to be removed and then we add the segments to add
    vector<Segment<T, Point> *> toadd;
    typename vector<Segment<T, Point> *>::iterator s, send;
    for (s = segments.begin(), send = segments.end(); s != send; s++) {
      if (p == (*(*s))[0]) {
        toadd.push_back((*s));
      }
      else {
        remove((*s));
      }
    }
    for (s = toadd.begin(), send = toadd.end(); s != send; s++) {
      add((*s), binrule, epsilon);
    }
  }

  inline void add(Segment<T, Point> *S,
#if 0
                  binary_rule<Segment<T, Point>, Segment<T, Point>> &binrule =
                      binary_rule<Segment<T, Point>, Segment<T, Point>>(),
#else
                  binary_rule<Segment<T, Point>, Segment<T, Point>> &binrule,
#endif
                  real epsilon)
  {
    real t, u;
    Point CP;
    Vec2r v0, v1, v2, v3;
    if (true == S->order()) {
      v0[0] = ((*S)[0])[0];
      v0[1] = ((*S)[0])[1];
      v1[0] = ((*S)[1])[0];
      v1[1] = ((*S)[1])[1];
    }
    else {
      v1[0] = ((*S)[0])[0];
      v1[1] = ((*S)[0])[1];
      v0[0] = ((*S)[1])[0];
      v0[1] = ((*S)[1])[1];
    }
    for (typename std::list<Segment<T, Point> *>::iterator s = _set.begin(), send = _set.end();
         s != send;
         s++) {
      Segment<T, Point> *currentS = (*s);
      if (true != binrule(*S, *currentS)) {
        continue;
      }

      if (true == currentS->order()) {
        v2[0] = ((*currentS)[0])[0];
        v2[1] = ((*currentS)[0])[1];
        v3[0] = ((*currentS)[1])[0];
        v3[1] = ((*currentS)[1])[1];
      }
      else {
        v3[0] = ((*currentS)[0])[0];
        v3[1] = ((*currentS)[0])[1];
        v2[0] = ((*currentS)[1])[0];
        v2[1] = ((*currentS)[1])[1];
      }
      if (S->CommonVertex(*currentS, CP)) {
        continue;  // the two edges have a common vertex->no need to check
      }

      if (GeomUtils::intersect2dSeg2dSegParametric(v0, v1, v2, v3, t, u, epsilon) ==
          GeomUtils::DO_INTERSECT) {
        // create the intersection
        Intersection<Segment<T, Point>> *inter = new Intersection<Segment<T, Point>>(
            S, t, currentS, u);
        // add it to the intersections list
        _Intersections.push_back(inter);
        // add this intersection to the first edge intersections list
        S->AddIntersection(inter);
        // add this intersection to the second edge intersections list
        currentS->AddIntersection(inter);
      }
    }
    // add the added segment to the list of active segments
    _set.push_back(S);
  }

  inline void remove(Segment<T, Point> *s)
  {
    if (s->intersections().size() > 0) {
      _IntersectedEdges.push_back(s);
    }
    _set.remove(s);
  }

  vector<Segment<T, Point> *> &intersectedEdges()
  {
    return _IntersectedEdges;
  }

  vector<Intersection<Segment<T, Point>> *> &intersections()
  {
    return _Intersections;
  }

 private:
  std::list<Segment<T, Point> *>
      _set;  // set of active edges for a given position of the sweep line
  std::vector<Segment<T, Point> *> _IntersectedEdges;             // the list of intersected edges
  std::vector<Intersection<Segment<T, Point>> *> _Intersections;  // the list of all intersections.

#ifdef WITH_CXX_GUARDEDALLOC
  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:SweepLine")
#endif
};

} /* namespace Freestyle */