Blender  V3.3
CurveIterators.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
10 #include "Curve.h"
11 #include "Stroke.h"
12 
13 namespace Freestyle {
14 
15 namespace CurveInternal {
16 
23  public:
24  friend class Freestyle::Curve;
25 
26  public:
28  float _step;
29  Curve::vertex_container::iterator __A;
30  Curve::vertex_container::iterator __B;
31  Curve::vertex_container::iterator _begin;
32  Curve::vertex_container::iterator _end;
33  int _n;
34  int _currentn;
35  float _t;
36  mutable CurvePoint _Point;
37  float _CurveLength;
38 
39  public:
40  inline CurvePointIterator(float step = 0.0f) : Interface0DIteratorNested()
41  {
42  _step = step;
43  _CurvilinearLength = 0.0f;
44  _t = 0.0f;
45  //_Point = 0;
46  _n = 0;
47  _currentn = 0;
48  _CurveLength = 0;
49  }
50 
52  {
53  __A = iBrother.__A;
54  __B = iBrother.__B;
55  _begin = iBrother._begin;
56  _end = iBrother._end;
58  _step = iBrother._step;
59  _t = iBrother._t;
60  _Point = iBrother._Point;
61  _n = iBrother._n;
62  _currentn = iBrother._currentn;
63  _CurveLength = iBrother._CurveLength;
64  }
65 
67  {
68  __A = iBrother.__A;
69  __B = iBrother.__B;
70  _begin = iBrother._begin;
71  _end = iBrother._end;
73  _step = iBrother._step;
74  _t = iBrother._t;
75  _Point = iBrother._Point;
76  _n = iBrother._n;
77  _currentn = iBrother._currentn;
78  _CurveLength = iBrother._CurveLength;
79  return *this;
80  }
81 
83  {
84  }
85 
86  protected:
87  inline CurvePointIterator(Curve::vertex_container::iterator iA,
88  Curve::vertex_container::iterator iB,
89  Curve::vertex_container::iterator ibegin,
90  Curve::vertex_container::iterator iend,
91  int currentn,
92  int n,
93  float iCurveLength,
94  float step,
95  float t = 0.0f,
96  float iCurvilinearLength = 0.0f)
98  {
99  __A = iA;
100  __B = iB;
101  _begin = ibegin;
102  _end = iend;
103  _CurvilinearLength = iCurvilinearLength;
104  _step = step;
105  _t = t;
106  _n = n;
107  _currentn = currentn;
108  _CurveLength = iCurveLength;
109  }
110 
111  public:
112  virtual CurvePointIterator *copy() const
113  {
114  return new CurvePointIterator(*this);
115  }
116 
118  {
120  return ret;
121  }
122 
123  virtual string getExactTypeName() const
124  {
125  return "CurvePointIterator";
126  }
127 
128  // operators
129  inline CurvePointIterator &operator++() // operator corresponding to ++i
130  {
131  increment();
132  return *this;
133  }
134 
135  inline CurvePointIterator &operator--() // operator corresponding to --i
136  {
137  decrement();
138  return *this;
139  }
140 
141  // comparibility
142  virtual bool operator==(const Interface0DIteratorNested &b) const
143  {
144  const CurvePointIterator *it_exact = dynamic_cast<const CurvePointIterator *>(&b);
145  if (!it_exact) {
146  return false;
147  }
148  return ((__A == it_exact->__A) && (__B == it_exact->__B) && (_t == it_exact->_t));
149  }
150 
151  // dereferencing
153  {
154  return (_Point = CurvePoint(*__A, *__B, _t));
155  }
156 
158  {
159  return &(operator*());
160  }
161 
162  virtual bool isBegin() const
163  {
164  if ((__A == _begin) && (_t < (float)M_EPSILON)) {
165  return true;
166  }
167  return false;
168  }
169 
170  virtual bool isEnd() const
171  {
172  if (__B == _end) {
173  return true;
174  }
175  return false;
176  }
177 
178  // protected:
179  virtual int increment()
180  {
181  if ((_currentn == _n - 1) && (_t == 1.0f)) {
182  // we're setting the iterator to end
183  ++__A;
184  ++__B;
185  ++_currentn;
186  _t = 0.0f;
187  return 0;
188  }
189 
190  if (0 == _step) { // means we iterate over initial vertices
191  Vec3r vec_tmp((*__B)->point2d() - (*__A)->point2d());
192  _CurvilinearLength += (float)vec_tmp.norm();
193  if (_currentn == _n - 1) {
194  _t = 1.0f;
195  return 0;
196  }
197  ++__B;
198  ++__A;
199  ++_currentn;
200  return 0;
201  }
202 
203  // compute the new position:
204  Vec3r vec_tmp2((*__A)->point2d() - (*__B)->point2d());
205  float normAB = (float)vec_tmp2.norm();
206 
207  if (normAB > M_EPSILON) {
209  _t = _t + _step / normAB;
210  }
211  else {
212  _t = 1.0f; // AB is a null segment, we're directly at its end
213  }
214  // if normAB ~= 0, we don't change these values
215  if (_t >= 1) {
216  _CurvilinearLength -= normAB * (_t - 1);
217  if (_currentn == _n - 1) {
218  _t = 1.0f;
219  }
220  else {
221  _t = 0.0f;
222  ++_currentn;
223  ++__A;
224  ++__B;
225  }
226  }
227  return 0;
228  }
229 
230  virtual int decrement()
231  {
232  if (_t == 0.0f) { // we're at the beginning of the edge
233  _t = 1.0f;
234  --_currentn;
235  --__A;
236  --__B;
237  if (_currentn == _n - 1) {
238  return 0;
239  }
240  }
241 
242  if (0 == _step) { // means we iterate over initial vertices
243  Vec3r vec_tmp((*__B)->point2d() - (*__A)->point2d());
244  _CurvilinearLength -= (float)vec_tmp.norm();
245  _t = 0;
246  return 0;
247  }
248 
249  // compute the new position:
250  Vec3r vec_tmp2((*__A)->point2d() - (*__B)->point2d());
251  float normAB = (float)vec_tmp2.norm();
252 
253  if (normAB > M_EPSILON) {
255  _t = _t - _step / normAB;
256  }
257  else {
258  _t = -1.0f; // We just need a negative value here
259  }
260 
261  // round value
262  if (fabs(_t) < (float)M_EPSILON) {
263  _t = 0.0f;
264  }
265  if (_t < 0) {
266  if (_currentn == 0) {
267  _CurvilinearLength = 0.0f;
268  }
269  else {
270  _CurvilinearLength += normAB * (-_t);
271  }
272  _t = 0.0f;
273  }
274  return 0;
275  }
276 
277  virtual float t() const
278  {
279  return _CurvilinearLength;
280  }
281 
282  virtual float u() const
283  {
285  }
286 };
287 
288 } // end of namespace CurveInternal
289 
290 } /* namespace Freestyle */
typedef float(TangentPoint)[2]
Class to define a container for curves.
Classes to define a stroke.
Curve::vertex_container::iterator _begin
Curve::vertex_container::iterator _end
virtual bool operator==(const Interface0DIteratorNested &b) const
Curve::vertex_container::iterator __A
CurvePointIterator(const CurvePointIterator &iBrother)
virtual CurvePointIterator * copy() const
CurvePointIterator(Curve::vertex_container::iterator iA, Curve::vertex_container::iterator iB, Curve::vertex_container::iterator ibegin, Curve::vertex_container::iterator iend, int currentn, int n, float iCurveLength, float step, float t=0.0f, float iCurvilinearLength=0.0f)
CurvePointIterator & operator=(const CurvePointIterator &iBrother)
Curve::vertex_container::iterator __B
Interface0DIterator castToInterface0DIterator() const
SVertex * __B
Definition: Curve.h:186
SVertex * __A
Definition: Curve.h:185
value_type norm() const
Definition: VecMat.h:95
ccl_device_inline float2 fabs(const float2 &a)
Definition: math_float2.h:222
inherits from class Rep
Definition: AppCanvas.cpp:18
static const real M_EPSILON
Definition: Precision.h:15
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
return ret