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