Blender  V2.93
SweepLine.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
24 #include <list>
25 #include <vector>
26 
27 #ifdef WITH_CXX_GUARDEDALLOC
28 # include "MEM_guardedalloc.h"
29 #endif
30 
31 namespace Freestyle {
32 
34 template<class Edge> class Intersection {
35  public:
36  template<class EdgeClass> Intersection(EdgeClass *eA, real ta, EdgeClass *eB, real tb)
37  {
38  EdgeA = eA;
39  EdgeB = eB;
40  tA = ta;
41  tB = tb;
42  userdata = 0;
43  }
44 
45  Intersection(const Intersection &iBrother)
46  {
47  EdgeA = iBrother.EdgeA;
48  EdgeB = iBrother.EdgeB;
49  tA = iBrother.tA;
50  tB = iBrother.tB;
51  userdata = 0;
52  }
53 
56  {
57  if (iEdge == EdgeA) {
58  return tA;
59  }
60  if (iEdge == EdgeB) {
61  return tB;
62  }
63  return 0;
64  }
65 
66  public:
67  void *userdata; // FIXME
68 
69  Edge *EdgeA; // first segment
70  Edge *EdgeB; // second segment
71  real tA; // parameter defining the intersection point with respect to the segment EdgeA.
72  real tB; // parameter defining the intersection point with respect to the segment EdgeB.
73 
74 #ifdef WITH_CXX_GUARDEDALLOC
75  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Intersection")
76 #endif
77 };
78 
79 #ifdef _MSC_VER
80 # pragma warning(push)
81 # pragma warning(disable : 4521) // disable warning C4521: multiple copy constructors specified
82 #endif
83 
84 template<class T, class Point> class Segment {
85  public:
87  {
88  }
89 
90  Segment(T &s, const Point &iA, const Point &iB)
91  {
92  _edge = s;
93  if (iA < iB) {
94  A = iA;
95  B = iB;
96  _order = true;
97  }
98  else {
99  A = iB;
100  B = iA;
101  _order = false;
102  }
103  }
104 
106  {
107  _edge = iBrother.edge();
108  A = iBrother.A;
109  B = iBrother.B;
110  _Intersections = iBrother._Intersections;
111  _order = iBrother._order;
112  }
113 
114  Segment(const Segment<T, Point> &iBrother)
115  {
116  _edge = iBrother._edge;
117  A = iBrother.A;
118  B = iBrother.B;
119  _Intersections = iBrother._Intersections;
120  _order = iBrother._order;
121  }
122 
124  {
125  _Intersections.clear();
126  }
127 
128  inline Point operator[](const unsigned short int &i) const
129  {
130  return (i % 2 == 0) ? A : B;
131  }
132 
133  inline bool operator==(const Segment<T, Point> &iBrother)
134  {
135  if (_edge == iBrother._edge) {
136  return true;
137  }
138  return false;
139  }
140 
141  /* Adds an intersection for this segment */
143  {
144  _Intersections.push_back(i);
145  }
146 
148  inline bool CommonVertex(const Segment<T, Point> &S, Point &CP)
149  {
150  if ((A == S[0]) || (A == S[1])) {
151  CP = A;
152  return true;
153  }
154  if ((B == S[0]) || (B == S[1])) {
155  CP = B;
156  return true;
157  }
158  return false;
159  }
160 
161  inline vector<Intersection<Segment<T, Point>> *> &intersections()
162  {
163  return _Intersections;
164  }
165 
166  inline bool order()
167  {
168  return _order;
169  }
170 
171  inline T &edge()
172  {
173  return _edge;
174  }
175 
176  private:
177  T _edge;
178  Point A;
179  Point B;
180  std::vector<Intersection<Segment<T, Point>> *>
181  _Intersections; // list of intersections parameters
182  bool _order; // true if A and B are in the same order than _edge.A and _edge.B. false otherwise.
183 
184 #ifdef WITH_CXX_GUARDEDALLOC
185  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Segment")
186 #endif
187 };
188 
189 #ifdef _MSC_VER
190 # pragma warning(pop)
191 #endif
192 
196 template<class T1, class T2> struct binary_rule {
198  {
199  }
200  template<class T3, class T4> binary_rule(const binary_rule<T3, T4> &brother)
201  {
202  }
203  virtual ~binary_rule()
204  {
205  }
206 
207  virtual bool operator()(T1 &, T2 &)
208  {
209  return true;
210  }
211 };
212 
213 template<class T, class Point> class SweepLine {
214  public:
216  {
217  }
219  {
220  for (typename vector<Intersection<Segment<T, Point>> *>::iterator i = _Intersections.begin(),
221  iend = _Intersections.end();
222  i != iend;
223  i++) {
224  delete (*i);
225  }
226  }
227 
228  inline void process(Point &p,
229  vector<Segment<T, Point> *> &segments,
230 #if 0
233 #else
235 #endif
237  {
238  // first we remove the segments that need to be removed and then we add the segments to add
239  vector<Segment<T, Point> *> toadd;
240  typename vector<Segment<T, Point> *>::iterator s, send;
241  for (s = segments.begin(), send = segments.end(); s != send; s++) {
242  if (p == (*(*s))[0]) {
243  toadd.push_back((*s));
244  }
245  else {
246  remove((*s));
247  }
248  }
249  for (s = toadd.begin(), send = toadd.end(); s != send; s++) {
250  add((*s), binrule, epsilon);
251  }
252  }
253 
254  inline void add(Segment<T, Point> *S,
255 #if 0
258 #else
260 #endif
261  real epsilon)
262  {
263  real t, u;
264  Point CP;
265  Vec2r v0, v1, v2, v3;
266  if (true == S->order()) {
267  v0[0] = ((*S)[0])[0];
268  v0[1] = ((*S)[0])[1];
269  v1[0] = ((*S)[1])[0];
270  v1[1] = ((*S)[1])[1];
271  }
272  else {
273  v1[0] = ((*S)[0])[0];
274  v1[1] = ((*S)[0])[1];
275  v0[0] = ((*S)[1])[0];
276  v0[1] = ((*S)[1])[1];
277  }
278  for (typename std::list<Segment<T, Point> *>::iterator s = _set.begin(), send = _set.end();
279  s != send;
280  s++) {
281  Segment<T, Point> *currentS = (*s);
282  if (true != binrule(*S, *currentS)) {
283  continue;
284  }
285 
286  if (true == currentS->order()) {
287  v2[0] = ((*currentS)[0])[0];
288  v2[1] = ((*currentS)[0])[1];
289  v3[0] = ((*currentS)[1])[0];
290  v3[1] = ((*currentS)[1])[1];
291  }
292  else {
293  v3[0] = ((*currentS)[0])[0];
294  v3[1] = ((*currentS)[0])[1];
295  v2[0] = ((*currentS)[1])[0];
296  v2[1] = ((*currentS)[1])[1];
297  }
298  if (S->CommonVertex(*currentS, CP)) {
299  continue; // the two edges have a common vertex->no need to check
300  }
301 
304  // create the intersection
306  S, t, currentS, u);
307  // add it to the intersections list
308  _Intersections.push_back(inter);
309  // add this intersection to the first edge intersections list
310  S->AddIntersection(inter);
311  // add this intersection to the second edge intersections list
312  currentS->AddIntersection(inter);
313  }
314  }
315  // add the added segment to the list of active segments
316  _set.push_back(S);
317  }
318 
319  inline void remove(Segment<T, Point> *s)
320  {
321  if (s->intersections().size() > 0) {
322  _IntersectedEdges.push_back(s);
323  }
324  _set.remove(s);
325  }
326 
327  vector<Segment<T, Point> *> &intersectedEdges()
328  {
329  return _IntersectedEdges;
330  }
331 
332  vector<Intersection<Segment<T, Point>> *> &intersections()
333  {
334  return _Intersections;
335  }
336 
337  private:
338  std::list<Segment<T, Point> *>
339  _set; // set of active edges for a given position of the sweep line
340  std::vector<Segment<T, Point> *> _IntersectedEdges; // the list of intersected edges
341  std::vector<Intersection<Segment<T, Point>> *> _Intersections; // the list of all intersections.
342 
343 #ifdef WITH_CXX_GUARDEDALLOC
344  MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:SweepLine")
345 #endif
346 };
347 
348 } /* namespace Freestyle */
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMVert * v2
Intersection(EdgeClass *eA, real ta, EdgeClass *eB, real tb)
Definition: SweepLine.h:36
real getParameter(Edge *iEdge)
Definition: SweepLine.h:55
Intersection(const Intersection &iBrother)
Definition: SweepLine.h:45
vector< Intersection< Segment< T, Point > > * > & intersections()
Definition: SweepLine.h:161
Segment(const Segment< T, Point > &iBrother)
Definition: SweepLine.h:114
Segment(T &s, const Point &iA, const Point &iB)
Definition: SweepLine.h:90
bool operator==(const Segment< T, Point > &iBrother)
Definition: SweepLine.h:133
bool CommonVertex(const Segment< T, Point > &S, Point &CP)
Definition: SweepLine.h:148
void AddIntersection(Intersection< Segment< T, Point >> *i)
Definition: SweepLine.h:142
Point operator[](const unsigned short int &i) const
Definition: SweepLine.h:128
Segment(Segment< T, Point > &iBrother)
Definition: SweepLine.h:105
vector< Intersection< Segment< T, Point > > * > & intersections()
Definition: SweepLine.h:332
void process(Point &p, vector< Segment< T, Point > * > &segments, binary_rule< Segment< T, Point >, Segment< T, Point >> &binrule, real epsilon=M_EPSILON)
Definition: SweepLine.h:228
void add(Segment< T, Point > *S, binary_rule< Segment< T, Point >, Segment< T, Point >> &binrule, real epsilon)
Definition: SweepLine.h:254
vector< Segment< T, Point > * > & intersectedEdges()
Definition: SweepLine.h:327
void remove(Segment< T, Point > *s)
Definition: SweepLine.h:319
#define T
intersection_test intersect2dSeg2dSegParametric(const Vec2r &p1, const Vec2r &p2, const Vec2r &p3, const Vec2r &p4, real &t, real &u, real epsilon)
Definition: GeomUtils.cpp:142
inherits from class Rep
Definition: AppCanvas.cpp:32
static const real M_EPSILON
Definition: Precision.h:29
double real
Definition: Precision.h:26
static double epsilon
std::vector< ElementType, Eigen::aligned_allocator< ElementType > > vector
Definition: vector.h:39
binary_rule(const binary_rule< T3, T4 > &brother)
Definition: SweepLine.h:200
virtual ~binary_rule()
Definition: SweepLine.h:203
virtual bool operator()(T1 &, T2 &)
Definition: SweepLine.h:207
#define T2
Definition: util_md5.cpp:36
#define T1
Definition: util_md5.cpp:35