propagation up to the toplevel error handler in BPY_txt_do_python_Text().
Before these changes were made, the operator() methods of predicates
and functions, for example, returned a value of various types such as
bool, double and Vec2f. These returned values were not capable to
represent an error state in many cases.
Now the operator() methods always return 0 on normal exit and -1 on
error. The original returned values are stored in the "result" member
variables of the predicate/function classes.
This means that if we have a code fragment like below:
UnaryPredicate1D& pred;
Interface1D& inter;
if (pred(inter)) {
/* do something */
}
then we have to rewrite it as follows:
UnaryPredicate1D& pred;
Interface1D& inter;
if (pred(inter) < 0)
return -1; /* an error in pred() is propagated */
if (pred.result) {
/* do something */
}
Suppose that pred is a user-defined predicate in Python, i.e. the predicate
is likely error-prone (especially when debugging the predicate). The first
code fragment shown above prevents the proper error propagation because
the boolean return value of UnaryPredicate1D::operator() cannot inform the
occurrence of an error to the caller; the second code fragment can.
In addition to the operator() methods of predicates and functions, similar
improvements have been made to all other C++ API functions and methods that
are involved in the execution of user-defined Python code snippets. Changes
in the signatures of functions and methods are summarized as follows (note
that all subclasses of listed classes are also subject to the changes).
Old signatures:
virtual void Iterator::increment();
virtual void Iterator::decrement();
virtual void ChainingIterator::init();
virtual ViewEdge * ChainingIterator::traverse(const AdjacencyIterator &it);
static void Operators::select(UnaryPredicate1D& pred);
static void Operators::chain(ViewEdgeInternal::ViewEdgeIterator& it,
UnaryPredicate1D& pred, UnaryFunction1D_void& modifier);
static void Operators::chain(ViewEdgeInternal::ViewEdgeIterator& it,
UnaryPredicate1D& pred);
static void Operators::bidirectionalChain(ChainingIterator& it,
UnaryPredicate1D& pred);
static void Operators::bidirectionalChain(ChainingIterator& it);
static void Operators::sequentialSplit(UnaryPredicate0D& startingPred,
UnaryPredicate0D& stoppingPred, float sampling = 0);
static void Operators::sequentialSplit(UnaryPredicate0D& pred, float sampling = 0);
static void Operators::recursiveSplit(UnaryFunction0D<double>& func,
UnaryPredicate1D& pred, float sampling = 0);
static void Operators::recursiveSplit(UnaryFunction0D<double>& func,
UnaryPredicate0D& pred0d, UnaryPredicate1D& pred, float sampling = 0);
static void Operators::sort(BinaryPredicate1D& pred);
static void Operators::create(UnaryPredicate1D& pred, vector<StrokeShader*> shaders);
virtual bool UnaryPredicate0D::operator()(Interface0DIterator& it);
virtual bool BinaryPredicate0D::operator()(Interface0D& inter1, Interface0D& inter2);
virtual bool UnaryPredicate1D::operator()(Interface1D& inter);
virtual bool BinaryPredicate1D::operator()(Interface1D& inter1, Interface1D& inter2);
virtual void StrokeShader::shade(Stroke& ioStroke) const;
virtual T UnaryFunction0D::operator()(Interface0DIterator& iter);
virtual T UnaryFunction1D::operator()(Interface1D& inter);
New signatures:
virtual int Iterator::increment();
virtual int Iterator::decrement();
virtual int ChainingIterator::init();
virtual int ChainingIterator::traverse(const AdjacencyIterator &it);
static int Operators::select(UnaryPredicate1D& pred);
static int Operators::chain(ViewEdgeInternal::ViewEdgeIterator& it,
UnaryPredicate1D& pred, UnaryFunction1D_void& modifier);
static int Operators::chain(ViewEdgeInternal::ViewEdgeIterator& it,
UnaryPredicate1D& pred);
static int Operators::bidirectionalChain(ChainingIterator& it,
UnaryPredicate1D& pred);
static int Operators::bidirectionalChain(ChainingIterator& it);
static int Operators::sequentialSplit(UnaryPredicate0D& startingPred,
UnaryPredicate0D& stoppingPred, float sampling = 0);
static int Operators::sequentialSplit(UnaryPredicate0D& pred, float sampling = 0);
static int Operators::recursiveSplit(UnaryFunction0D<double>& func,
UnaryPredicate1D& pred, float sampling = 0);
static int Operators::recursiveSplit(UnaryFunction0D<double>& func,
UnaryPredicate0D& pred0d, UnaryPredicate1D& pred, float sampling = 0);
static int Operators::sort(BinaryPredicate1D& pred);
static int Operators::create(UnaryPredicate1D& pred, vector<StrokeShader*> shaders);
virtual int UnaryPredicate0D::operator()(Interface0DIterator& it);
virtual int BinaryPredicate0D::operator()(Interface0D& inter1, Interface0D& inter2);
virtual int UnaryPredicate1D::operator()(Interface1D& inter);
virtual int BinaryPredicate1D::operator()(Interface1D& inter1, Interface1D& inter2);
virtual int StrokeShader::shade(Stroke& ioStroke) const;
virtual int UnaryFunction0D::operator()(Interface0DIterator& iter);
virtual int UnaryFunction1D::operator()(Interface1D& inter);
300 lines
7.7 KiB
C++
Executable File
300 lines
7.7 KiB
C++
Executable File
//
|
|
// Filename : CurveIterators.h
|
|
// Author(s) : Stephane Grabli
|
|
// Purpose : Iterators used to iterate over the elements of the Curve
|
|
// Date of creation : 01/08/2003
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
//
|
|
// Copyright (C) : Please refer to the COPYRIGHT file distributed
|
|
// with this source distribution.
|
|
//
|
|
// This program is free software; you can redistribute it and/or
|
|
// modify it under the terms of the GNU General Public License
|
|
// as published by the Free Software Foundation; either version 2
|
|
// of the License, or (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program; if not, write to the Free Software
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
//
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#ifndef CURVEITERATORS_H
|
|
# define CURVEITERATORS_H
|
|
|
|
#include "Stroke.h"
|
|
#include "Curve.h"
|
|
|
|
namespace CurveInternal {
|
|
|
|
/*! iterator on a curve. Allows an iterating outside
|
|
* initial vertices. A CurvePoint is instanciated an returned
|
|
* when the iterator is dereferenced.
|
|
*/
|
|
|
|
class CurvePointIterator : public Interface0DIteratorNested
|
|
{
|
|
public:
|
|
friend class ::Curve;
|
|
public:
|
|
float _CurvilinearLength;
|
|
float _step;
|
|
::Curve::vertex_container::iterator __A;
|
|
::Curve::vertex_container::iterator __B;
|
|
::Curve::vertex_container::iterator _begin;
|
|
::Curve::vertex_container::iterator _end;
|
|
int _n;
|
|
int _currentn;
|
|
float _t;
|
|
mutable CurvePoint _Point;
|
|
float _CurveLength;
|
|
|
|
public:
|
|
|
|
public:
|
|
inline CurvePointIterator(float step = 0.f)
|
|
: Interface0DIteratorNested()
|
|
{
|
|
_step = step;
|
|
_CurvilinearLength = 0.f;
|
|
_t = 0.f;
|
|
//_Point = 0;
|
|
_n = 0;
|
|
_currentn = 0;
|
|
_CurveLength=0;
|
|
}
|
|
|
|
inline CurvePointIterator(const CurvePointIterator& iBrother)
|
|
: Interface0DIteratorNested()
|
|
{
|
|
__A = iBrother.__A;
|
|
__B = iBrother.__B;
|
|
_begin = iBrother._begin;
|
|
_end = iBrother._end;
|
|
_CurvilinearLength = iBrother._CurvilinearLength;
|
|
_step = iBrother._step;
|
|
_t = iBrother._t;
|
|
_Point = iBrother._Point;
|
|
_n = iBrother._n;
|
|
_currentn = iBrother._currentn;
|
|
_CurveLength = iBrother._CurveLength;
|
|
}
|
|
inline CurvePointIterator& operator=(const CurvePointIterator& iBrother)
|
|
{
|
|
__A = iBrother.__A;
|
|
__B = iBrother.__B;
|
|
_begin = iBrother._begin;
|
|
_end = iBrother._end;
|
|
_CurvilinearLength = iBrother._CurvilinearLength;
|
|
_step = iBrother._step;
|
|
_t = iBrother._t;
|
|
_Point = iBrother._Point;
|
|
_n = iBrother._n;
|
|
_currentn = iBrother._currentn;
|
|
_CurveLength = iBrother._CurveLength;
|
|
return *this;
|
|
}
|
|
virtual ~CurvePointIterator()
|
|
{
|
|
}
|
|
protected:
|
|
inline 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.f, float iCurvilinearLength = 0.f)
|
|
: Interface0DIteratorNested()
|
|
{
|
|
__A = iA;
|
|
__B = iB;
|
|
_begin = ibegin;
|
|
_end = iend;
|
|
_CurvilinearLength = iCurvilinearLength;
|
|
_step = step;
|
|
_t = t;
|
|
_n = n;
|
|
_currentn = currentn;
|
|
_CurveLength = iCurveLength;
|
|
}
|
|
|
|
public:
|
|
|
|
virtual CurvePointIterator* copy() const {
|
|
return new CurvePointIterator(*this);
|
|
}
|
|
|
|
inline Interface0DIterator castToInterface0DIterator() const{
|
|
Interface0DIterator ret(new CurveInternal::CurvePointIterator(*this));
|
|
return ret;
|
|
}
|
|
virtual string getExactTypeName() const {
|
|
return "CurvePointIterator";
|
|
}
|
|
|
|
// operators
|
|
inline CurvePointIterator& operator++() // operator corresponding to ++i
|
|
{
|
|
increment();
|
|
return *this;
|
|
}
|
|
|
|
inline CurvePointIterator& operator--() // operator corresponding to ++i
|
|
{
|
|
decrement();
|
|
return *this;
|
|
}
|
|
|
|
// comparibility
|
|
virtual bool operator==(const Interface0DIteratorNested& b) const
|
|
{
|
|
const CurvePointIterator* it_exact = dynamic_cast<const CurvePointIterator*>(&b);
|
|
if (!it_exact)
|
|
return false;
|
|
return ((__A==it_exact->__A) && (__B==it_exact->__B) && (_t == it_exact->_t));
|
|
}
|
|
|
|
// dereferencing
|
|
virtual CurvePoint& operator*()
|
|
{
|
|
return (_Point = CurvePoint(*__A,*__B,_t));
|
|
}
|
|
virtual CurvePoint* operator->() { return &(operator*());}
|
|
public:
|
|
virtual bool isBegin() const
|
|
{
|
|
if((__A == _begin) && (_t < (float)M_EPSILON))
|
|
return true;
|
|
return false;
|
|
}
|
|
virtual bool isEnd() const
|
|
{
|
|
if(__B == _end)
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
// protected:
|
|
|
|
virtual int increment()
|
|
{
|
|
if((_currentn == _n-1) && (_t == 1.f))
|
|
{
|
|
// we're setting the iterator to end
|
|
++__A;
|
|
++__B;
|
|
++_currentn;
|
|
_t = 0.f;
|
|
return 0;
|
|
}
|
|
|
|
if(0 == _step) // means we iterate over initial vertices
|
|
{
|
|
Vec3r vec_tmp((*__B)->point2d() - (*__A)->point2d());
|
|
_CurvilinearLength += (float)vec_tmp.norm();
|
|
if(_currentn == _n-1)
|
|
{
|
|
_t = 1.f;
|
|
return 0;
|
|
}
|
|
++__B;
|
|
++__A;
|
|
++_currentn;
|
|
return 0;
|
|
}
|
|
|
|
// compute the new position:
|
|
Vec3r vec_tmp2((*__A)->point2d() - (*__B)->point2d());
|
|
float normAB = (float)vec_tmp2.norm();
|
|
|
|
if(normAB > M_EPSILON)
|
|
{
|
|
_CurvilinearLength += _step;
|
|
_t = _t + _step/normAB;
|
|
}
|
|
else
|
|
_t = 1.f; // AB is a null segment, we're directly at its end
|
|
//if normAB ~= 0, we don't change these values
|
|
if(_t >= 1)
|
|
{
|
|
_CurvilinearLength -= normAB*(_t-1);
|
|
if(_currentn == _n-1)
|
|
_t=1.f;
|
|
else
|
|
{
|
|
_t = 0.f;
|
|
++_currentn;
|
|
++__A;++__B;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
virtual int decrement()
|
|
{
|
|
if(_t == 0.f) //we're at the beginning of the edge
|
|
{
|
|
_t = 1.f;
|
|
--_currentn;
|
|
--__A; --__B;
|
|
if(_currentn == _n-1)
|
|
return 0;
|
|
}
|
|
|
|
if(0 == _step) // means we iterate over initial vertices
|
|
{
|
|
Vec3r vec_tmp((*__B)->point2d() - (*__A)->point2d());
|
|
_CurvilinearLength -= (float)vec_tmp.norm();
|
|
_t = 0;
|
|
return 0;
|
|
}
|
|
|
|
// compute the new position:
|
|
Vec3r vec_tmp2((*__A)->point2d() - (*__B)->point2d());
|
|
float normAB = (float)vec_tmp2.norm();
|
|
|
|
if(normAB >M_EPSILON)
|
|
{
|
|
_CurvilinearLength -= _step;
|
|
_t = _t - _step/normAB;
|
|
}
|
|
else
|
|
_t = -1.f; // We just need a negative value here
|
|
|
|
// round value
|
|
if(fabs(_t) < (float)M_EPSILON)
|
|
_t = 0.0;
|
|
if(_t < 0)
|
|
{
|
|
if(_currentn == 0)
|
|
_CurvilinearLength = 0.f;
|
|
else
|
|
_CurvilinearLength += normAB*(-_t);
|
|
_t = 0.f;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
virtual float t() const{
|
|
return _CurvilinearLength;
|
|
}
|
|
virtual float u() const{
|
|
return _CurvilinearLength/_CurveLength;
|
|
}
|
|
};
|
|
|
|
|
|
|
|
} // end of namespace StrokeInternal
|
|
|
|
#endif // CURVEITERATORS_H
|