Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1346 lines
34 KiB
C++
Raw Permalink Normal View History

/* SPDX-FileCopyrightText: 2008-2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup freestyle
* \brief Class gathering stroke creation algorithms
*/
2008-04-30 15:41:54 +00:00
#include <algorithm>
#include <stdexcept>
2008-04-30 15:41:54 +00:00
#include "Canvas.h"
#include "CurveIterators.h"
#include "Operators.h"
2008-04-30 15:41:54 +00:00
#include "Stroke.h"
#include "StrokeIterators.h"
2008-04-30 15:41:54 +00:00
#include "BLI_sys_types.h"
#include "BKE_global.hh"
Attempt to fix a potential name conflict between Freestyle and the compositor. A crash in the Freestyle renderer was reported by Ton on IRC with a stack trace below. Note that #2 is in Freestyle, whereas #1 is in the compositor. The problem was observed in a debug build on OS X 10.7 (gcc 4.2, openmp disabled, no llvm). ---------------------------------------------------------------------- Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: 13 at address: 0x0000000000000000 [Switching to process 72386 thread 0xf303] 0x0000000100c129f3 in NodeBase::~NodeBase (this=0x10e501c80) at COM_NodeBase.cpp:43 43 delete (this->m_outputsockets.back()); Current language: auto; currently c++ (gdb) where #0 0x0000000100c129f3 in NodeBase::~NodeBase (this=0x10e501c80) at COM_NodeBase.cpp:43 #1 0x0000000100c29066 in Node::~Node (this=0x10e501c80) at COM_Node.h:49 #2 0x000000010089c273 in NodeShape::~NodeShape (this=0x10e501c80) at NodeShape.cpp:43 #3 0x000000010089910b in NodeGroup::destroy (this=0x10e501da0) at NodeGroup.cpp:61 #4 0x00000001008990cd in NodeGroup::destroy (this=0x10e5014b0) at NodeGroup.cpp:59 #5 0x00000001008990cd in NodeGroup::destroy (this=0x114e18da0) at NodeGroup.cpp:59 #6 0x00000001007e6602 in Controller::ClearRootNode (this=0x114e19640) at Controller.cpp:329 #7 0x00000001007ea52e in Controller::LoadMesh (this=0x114e19640, re=0x10aba4638, srl=0x1140f5258) at Controller.cpp:302 #8 0x00000001008030ad in prepare (re=0x10aba4638, srl=0x1140f5258) at FRS_freestyle.cpp:302 #9 0x000000010080457a in FRS_do_stroke_rendering (re=0x10aba4638, srl=0x1140f5258) at FRS_freestyle.cpp:600 #10 0x00000001006aeb9d in add_freestyle (re=0x10aba4638) at pipeline.c:1584 #11 0x00000001006aceb7 in do_render_3d (re=0x10aba4638) at pipeline.c:1094 #12 0x00000001006ae061 in do_render_fields_blur_3d (re=0x10aba4638) at pipeline.c:1367 #13 0x00000001006afa16 in do_render_composite_fields_blur_3d (re=0x10aba4638) at pipeline.c:1815 #14 0x00000001006b04e4 in do_render_all_options (re=0x10aba4638) at pipeline.c:2021 ---------------------------------------------------------------------- Apparently a name conflict between the two Blender modules is taking place. The present commit hence intends to address it by putting all the Freestyle C++ classes in the namespace 'Freestyle'. This revision will also prevent potential name conflicts with other Blender modules in the future. Special thanks to Lukas Toenne for the help with C++ namespace.
2013-04-09 00:46:49 +00:00
namespace Freestyle {
Operators::I1DContainer Operators::_current_view_edges_set;
Operators::I1DContainer Operators::_current_chains_set;
Operators::I1DContainer *Operators::_current_set = nullptr;
Operators::StrokesContainer Operators::_current_strokes_set;
int Operators::select(UnaryPredicate1D &pred)
{
if (!_current_set) {
return 0;
}
if (_current_set->empty()) {
return 0;
}
I1DContainer new_set;
I1DContainer rejected;
Functions1D::ChainingTimeStampF1D cts;
Functions1D::TimeStampF1D ts;
I1DContainer::iterator it = _current_set->begin();
I1DContainer::iterator itbegin = it;
while (it != _current_set->end()) {
Interface1D *i1d = *it;
cts(*i1d); // mark everyone's chaining time stamp anyway
if (pred(*i1d) < 0) {
new_set.clear();
rejected.clear();
return -1;
}
if (pred.result) {
new_set.push_back(i1d);
ts(*i1d);
}
else {
rejected.push_back(i1d);
}
++it;
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
}
if ((*itbegin)->getExactTypeName() != "ViewEdge") {
for (it = rejected.begin(); it != rejected.end(); ++it) {
delete *it;
}
}
rejected.clear();
_current_set->clear();
*_current_set = new_set;
return 0;
2008-04-30 15:41:54 +00:00
}
int Operators::chain(ViewEdgeInternal::ViewEdgeIterator &it,
UnaryPredicate1D &pred,
UnaryFunction1D_void &modifier)
{
if (_current_view_edges_set.empty()) {
return 0;
}
uint id = 0;
ViewEdge *edge;
I1DContainer new_chains_set;
for (I1DContainer::iterator it_edge = _current_view_edges_set.begin();
it_edge != _current_view_edges_set.end();
++it_edge)
{
if (pred(**it_edge) < 0) {
goto error;
}
if (pred.result) {
continue;
}
edge = dynamic_cast<ViewEdge *>(*it_edge);
it.setBegin(edge);
it.setCurrentEdge(edge);
Chain *new_chain = new Chain(id);
++id;
while (true) {
new_chain->push_viewedge_back(*it, it.getOrientation());
if (modifier(**it) < 0) {
delete new_chain;
goto error;
}
++it;
if (it.isEnd()) {
break;
}
if (pred(**it) < 0) {
delete new_chain;
goto error;
}
if (pred.result) {
break;
}
}
new_chains_set.push_back(new_chain);
}
if (!new_chains_set.empty()) {
for (I1DContainer::iterator it = new_chains_set.begin(); it != new_chains_set.end(); ++it) {
_current_chains_set.push_back(*it);
}
new_chains_set.clear();
_current_set = &_current_chains_set;
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
}
return 0;
error:
for (I1DContainer::iterator it = new_chains_set.begin(); it != new_chains_set.end(); ++it) {
delete (*it);
}
new_chains_set.clear();
return -1;
2008-04-30 15:41:54 +00:00
}
int Operators::chain(ViewEdgeInternal::ViewEdgeIterator &it, UnaryPredicate1D &pred)
{
if (_current_view_edges_set.empty()) {
return 0;
}
uint id = 0;
Functions1D::IncrementChainingTimeStampF1D ts;
Predicates1D::EqualToChainingTimeStampUP1D pred_ts(TimeStamp::instance()->getTimeStamp() + 1);
ViewEdge *edge;
I1DContainer new_chains_set;
for (I1DContainer::iterator it_edge = _current_view_edges_set.begin();
it_edge != _current_view_edges_set.end();
++it_edge)
{
if (pred(**it_edge) < 0) {
goto error;
}
if (pred.result) {
continue;
}
if (pred_ts(**it_edge) < 0) {
goto error;
}
if (pred_ts.result) {
continue;
}
edge = dynamic_cast<ViewEdge *>(*it_edge);
it.setBegin(edge);
it.setCurrentEdge(edge);
Chain *new_chain = new Chain(id);
++id;
while (true) {
new_chain->push_viewedge_back(*it, it.getOrientation());
ts(**it);
++it;
if (it.isEnd()) {
break;
}
if (pred(**it) < 0) {
delete new_chain;
goto error;
}
if (pred.result) {
break;
}
if (pred_ts(**it) < 0) {
delete new_chain;
goto error;
}
if (pred_ts.result) {
break;
}
}
new_chains_set.push_back(new_chain);
}
if (!new_chains_set.empty()) {
for (I1DContainer::iterator it = new_chains_set.begin(); it != new_chains_set.end(); ++it) {
_current_chains_set.push_back(*it);
}
new_chains_set.clear();
_current_set = &_current_chains_set;
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
}
return 0;
error:
for (I1DContainer::iterator it = new_chains_set.begin(); it != new_chains_set.end(); ++it) {
delete (*it);
}
new_chains_set.clear();
return -1;
2008-04-30 15:41:54 +00:00
}
#if 0
void Operators::bidirectionalChain(ViewEdgeIterator &it,
UnaryPredicate1D &pred,
UnaryFunction1D_void &modifier)
{
if (_current_view_edges_set.empty()) {
return;
}
uint id = 0;
ViewEdge *edge;
Chain *new_chain;
for (I1DContainer::iterator it_edge = _current_view_edges_set.begin();
it_edge != _current_view_edges_set.end();
2023-08-09 10:47:43 +10:00
++it_edge)
{
if (pred(**it_edge)) {
continue;
}
edge = dynamic_cast<ViewEdge *>(*it_edge);
it.setBegin(edge);
it.setCurrentEdge(edge);
Chain *new_chain = new Chain(id);
++id;
# if 0 // FIXME
ViewEdgeIterator it_back(it);
--it_back;
# endif
do {
new_chain->push_viewedge_back(*it, it.getOrientation());
modifier(**it);
++it;
} while (!it.isEnd() && !pred(**it));
it.setBegin(edge);
it.setCurrentEdge(edge);
--it;
while (!it.isEnd() && !pred(**it)) {
new_chain->push_viewedge_front(*it, it.getOrientation());
modifier(**it);
--it;
}
_current_chains_set.push_back(new_chain);
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
}
if (!_current_chains_set.empty()) {
_current_set = &_current_chains_set;
}
}
void Operators::bidirectionalChain(ViewEdgeIterator &it, UnaryPredicate1D &pred)
{
if (_current_view_edges_set.empty()) {
return;
}
uint id = 0;
Functions1D::IncrementChainingTimeStampF1D ts;
Predicates1D::EqualToChainingTimeStampUP1D pred_ts(TimeStamp::instance()->getTimeStamp() + 1);
ViewEdge *edge;
Chain *new_chain;
for (I1DContainer::iterator it_edge = _current_view_edges_set.begin();
it_edge != _current_view_edges_set.end();
2023-08-09 10:47:43 +10:00
++it_edge)
{
if (pred(**it_edge) || pred_ts(**it_edge)) {
continue;
}
edge = dynamic_cast<ViewEdge *>(*it_edge);
it.setBegin(edge);
it.setCurrentEdge(edge);
Chain *new_chain = new Chain(id);
++id;
# if 0 // FIXME
ViewEdgeIterator it_back(it);
--it_back;
# endif
do {
new_chain->push_viewedge_back(*it, it.getOrientation());
ts(**it);
++it;
} while (!it.isEnd() && !pred(**it) && !pred_ts(**it));
it.setBegin(edge);
it.setCurrentEdge(edge);
--it;
while (!it.isEnd() && !pred(**it) && !pred_ts(**it)) {
new_chain->push_viewedge_front(*it, it.getOrientation());
ts(**it);
--it;
}
_current_chains_set.push_back(new_chain);
}
if (!_current_chains_set.empty()) {
_current_set = &_current_chains_set;
}
2008-04-30 15:41:54 +00:00
}
#endif
int Operators::bidirectionalChain(ChainingIterator &it, UnaryPredicate1D &pred)
{
if (_current_view_edges_set.empty()) {
return 0;
}
uint id = 0;
Functions1D::IncrementChainingTimeStampF1D ts;
Predicates1D::EqualToChainingTimeStampUP1D pred_ts(TimeStamp::instance()->getTimeStamp() + 1);
ViewEdge *edge;
I1DContainer new_chains_set;
for (I1DContainer::iterator it_edge = _current_view_edges_set.begin();
it_edge != _current_view_edges_set.end();
++it_edge)
{
if (pred(**it_edge) < 0) {
goto error;
}
if (pred.result) {
continue;
}
if (pred_ts(**it_edge) < 0) {
goto error;
}
if (pred_ts.result) {
continue;
}
edge = dynamic_cast<ViewEdge *>(*it_edge);
// re-init iterator
it.setBegin(edge);
it.setCurrentEdge(edge);
it.setOrientation(true);
if (it.init() < 0) {
goto error;
}
Chain *new_chain = new Chain(id);
++id;
#if 0 // FIXME
ViewEdgeIterator it_back(it);
--it_back;
#endif
while (true) {
new_chain->push_viewedge_back(*it, it.getOrientation());
ts(**it);
if (it.increment() < 0) {
delete new_chain;
goto error;
}
if (it.isEnd()) {
break;
}
if (pred(**it) < 0) {
delete new_chain;
goto error;
}
if (pred.result) {
break;
}
}
it.setBegin(edge);
it.setCurrentEdge(edge);
it.setOrientation(true);
if (it.decrement() < 0) {
delete new_chain;
goto error;
}
while (!it.isEnd()) {
if (pred(**it) < 0) {
delete new_chain;
goto error;
}
if (pred.result) {
break;
}
new_chain->push_viewedge_front(*it, it.getOrientation());
ts(**it);
if (it.decrement() < 0) {
delete new_chain;
goto error;
}
}
new_chains_set.push_back(new_chain);
}
if (!new_chains_set.empty()) {
for (I1DContainer::iterator it = new_chains_set.begin(); it != new_chains_set.end(); ++it) {
_current_chains_set.push_back(*it);
}
new_chains_set.clear();
_current_set = &_current_chains_set;
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
}
return 0;
error:
for (I1DContainer::iterator it = new_chains_set.begin(); it != new_chains_set.end(); ++it) {
delete (*it);
}
new_chains_set.clear();
return -1;
}
int Operators::bidirectionalChain(ChainingIterator &it)
{
if (_current_view_edges_set.empty()) {
return 0;
}
uint id = 0;
Functions1D::IncrementChainingTimeStampF1D ts;
Predicates1D::EqualToChainingTimeStampUP1D pred_ts(TimeStamp::instance()->getTimeStamp() + 1);
ViewEdge *edge;
I1DContainer new_chains_set;
for (I1DContainer::iterator it_edge = _current_view_edges_set.begin();
it_edge != _current_view_edges_set.end();
++it_edge)
{
if (pred_ts(**it_edge) < 0) {
goto error;
}
if (pred_ts.result) {
continue;
}
edge = dynamic_cast<ViewEdge *>(*it_edge);
// re-init iterator
it.setBegin(edge);
it.setCurrentEdge(edge);
it.setOrientation(true);
if (it.init() < 0) {
goto error;
}
Chain *new_chain = new Chain(id);
++id;
#if 0 // FIXME
ViewEdgeIterator it_back(it);
--it_back;
#endif
do {
new_chain->push_viewedge_back(*it, it.getOrientation());
ts(**it);
if (it.increment() < 0) { // FIXME
delete new_chain;
goto error;
}
} while (!it.isEnd());
it.setBegin(edge);
it.setCurrentEdge(edge);
it.setOrientation(true);
if (it.decrement() < 0) { // FIXME
delete new_chain;
goto error;
}
while (!it.isEnd()) {
new_chain->push_viewedge_front(*it, it.getOrientation());
ts(**it);
if (it.decrement() < 0) { // FIXME
delete new_chain;
goto error;
}
}
new_chains_set.push_back(new_chain);
}
if (!new_chains_set.empty()) {
for (I1DContainer::iterator it = new_chains_set.begin(); it != new_chains_set.end(); ++it) {
_current_chains_set.push_back(*it);
}
new_chains_set.clear();
_current_set = &_current_chains_set;
}
return 0;
error:
for (I1DContainer::iterator it = new_chains_set.begin(); it != new_chains_set.end(); ++it) {
delete (*it);
}
new_chains_set.clear();
return -1;
2008-04-30 15:41:54 +00:00
}
int Operators::sequentialSplit(UnaryPredicate0D &pred, float sampling)
2008-04-30 15:41:54 +00:00
{
if (_current_chains_set.empty()) {
cerr << "Warning: current set empty" << endl;
return 0;
}
CurvePoint *point;
Chain *new_curve;
I1DContainer splitted_chains;
Interface0DIterator first;
Interface0DIterator end;
Interface0DIterator last;
Interface0DIterator it;
I1DContainer::iterator cit = _current_chains_set.begin(), citend = _current_chains_set.end();
for (; cit != citend; ++cit) {
Id currentId = (*cit)->getId();
new_curve = new Chain(currentId);
first = (*cit)->pointsBegin(sampling);
end = (*cit)->pointsEnd(sampling);
last = end;
--last;
it = first;
point = dynamic_cast<CurvePoint *>(&(*it));
new_curve->push_vertex_back(point);
++it;
for (; it != end; ++it) {
point = dynamic_cast<CurvePoint *>(&(*it));
new_curve->push_vertex_back(point);
if (pred(it) < 0) {
delete new_curve;
goto error;
}
if (pred.result && (it != last)) {
splitted_chains.push_back(new_curve);
currentId.setSecond(currentId.getSecond() + 1);
new_curve = new Chain(currentId);
new_curve->push_vertex_back(point);
}
}
if (new_curve->nSegments() == 0) {
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
delete new_curve;
return 0;
}
splitted_chains.push_back(new_curve);
}
// Update the current set of chains:
cit = _current_chains_set.begin();
for (; cit != citend; ++cit) {
delete (*cit);
}
_current_chains_set.clear();
#if 0
_current_chains_set = splitted_chains;
#else
for (cit = splitted_chains.begin(), citend = splitted_chains.end(); cit != citend; ++cit) {
if ((*cit)->getLength2D() < M_EPSILON) {
delete (*cit);
continue;
}
_current_chains_set.push_back(*cit);
}
#endif
splitted_chains.clear();
2008-04-30 15:41:54 +00:00
if (!_current_chains_set.empty()) {
_current_set = &_current_chains_set;
}
return 0;
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
error:
cit = splitted_chains.begin();
citend = splitted_chains.end();
for (; cit != citend; ++cit) {
delete (*cit);
}
splitted_chains.clear();
return -1;
2008-04-30 15:41:54 +00:00
}
int Operators::sequentialSplit(UnaryPredicate0D &startingPred,
UnaryPredicate0D &stoppingPred,
float sampling)
2008-04-30 15:41:54 +00:00
{
if (_current_chains_set.empty()) {
cerr << "Warning: current set empty" << endl;
return 0;
}
CurvePoint *point;
Chain *new_curve;
I1DContainer splitted_chains;
Interface0DIterator first;
Interface0DIterator end;
Interface0DIterator last;
Interface0DIterator itStart;
Interface0DIterator itStop;
I1DContainer::iterator cit = _current_chains_set.begin(), citend = _current_chains_set.end();
for (; cit != citend; ++cit) {
Id currentId = (*cit)->getId();
first = (*cit)->pointsBegin(sampling);
end = (*cit)->pointsEnd(sampling);
last = end;
--last;
itStart = first;
do {
itStop = itStart;
++itStop;
new_curve = new Chain(currentId);
currentId.setSecond(currentId.getSecond() + 1);
point = dynamic_cast<CurvePoint *>(&(*itStart));
new_curve->push_vertex_back(point);
do {
point = dynamic_cast<CurvePoint *>(&(*itStop));
new_curve->push_vertex_back(point);
++itStop;
if (itStop == end) {
break;
}
if (stoppingPred(itStop) < 0) {
delete new_curve;
goto error;
}
} while (!stoppingPred.result);
if (itStop != end) {
point = dynamic_cast<CurvePoint *>(&(*itStop));
new_curve->push_vertex_back(point);
}
if (new_curve->nSegments() == 0) {
delete new_curve;
}
else {
splitted_chains.push_back(new_curve);
}
// find next start
do {
++itStart;
if (itStart == end) {
break;
}
if (startingPred(itStart) < 0) {
goto error;
}
} while (!startingPred.result);
2020-11-06 12:30:59 +11:00
} while (!ELEM(itStart, end, last));
}
// Update the current set of chains:
cit = _current_chains_set.begin();
for (; cit != citend; ++cit) {
delete (*cit);
}
_current_chains_set.clear();
#if 0
_current_chains_set = splitted_chains;
#else
for (cit = splitted_chains.begin(), citend = splitted_chains.end(); cit != citend; ++cit) {
if ((*cit)->getLength2D() < M_EPSILON) {
delete (*cit);
continue;
}
_current_chains_set.push_back(*cit);
}
#endif
splitted_chains.clear();
if (!_current_chains_set.empty()) {
_current_set = &_current_chains_set;
}
return 0;
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
error:
cit = splitted_chains.begin();
citend = splitted_chains.end();
for (; cit != citend; ++cit) {
delete (*cit);
}
splitted_chains.clear();
return -1;
2008-04-30 15:41:54 +00:00
}
// Internal function
static int __recursiveSplit(Chain *_curve,
UnaryFunction0D<double> &func,
UnaryPredicate1D &pred,
float sampling,
Operators::I1DContainer &newChains,
Operators::I1DContainer &splitted_chains)
2008-04-30 15:41:54 +00:00
{
if (((_curve->nSegments() == 1) && (sampling == 0)) || (_curve->getLength2D() <= sampling)) {
newChains.push_back(_curve);
return 0;
}
CurveInternal::CurvePointIterator first = _curve->curvePointsBegin(sampling);
CurveInternal::CurvePointIterator second = first;
++second;
CurveInternal::CurvePointIterator end = _curve->curvePointsEnd(sampling);
CurveInternal::CurvePointIterator it = second;
CurveInternal::CurvePointIterator split = second;
Interface0DIterator it0d = it.castToInterface0DIterator();
real _min = FLT_MAX; // func(it0d);
++it;
CurveInternal::CurvePointIterator next = it;
++next;
bool bsplit = false;
for (; ((it != end) && (next != end)); ++it, ++next) {
it0d = it.castToInterface0DIterator();
if (func(it0d) < 0) {
return -1;
}
if (func.result < _min) {
_min = func.result;
split = it;
bsplit = true;
}
}
if (!bsplit) { // we didn't find any minimum
newChains.push_back(_curve);
return 0;
}
// retrieves the current splitting id
Id *newId = _curve->getSplittingId();
if (newId == nullptr) {
newId = new Id(_curve->getId());
_curve->setSplittingId(newId);
2018-06-17 17:05:14 +02:00
}
Chain *new_curve_a = new Chain(*newId);
newId->setSecond(newId->getSecond() + 1);
new_curve_a->setSplittingId(newId);
Chain *new_curve_b = new Chain(*newId);
newId->setSecond(newId->getSecond() + 1);
new_curve_b->setSplittingId(newId);
CurveInternal::CurvePointIterator vit = _curve->curveVerticesBegin(),
vitend = _curve->curveVerticesEnd();
CurveInternal::CurvePointIterator vnext = vit;
++vnext;
for (; (vit != vitend) && (vnext != vitend) &&
(vnext._CurvilinearLength < split._CurvilinearLength);
++vit, ++vnext)
{
new_curve_a->push_vertex_back(&(*vit));
}
if ((vit == vitend) || (vnext == vitend)) {
if (G.debug & G_DEBUG_FREESTYLE) {
cout << "The split takes place in bad location" << endl;
}
newChains.push_back(_curve);
delete new_curve_a;
delete new_curve_b;
return 0;
}
// build the two resulting chains
new_curve_a->push_vertex_back(&(*vit));
new_curve_a->push_vertex_back(&(*split));
new_curve_b->push_vertex_back(&(*split));
for (vit = vnext; vit != vitend; ++vit) {
new_curve_b->push_vertex_back(&(*vit));
}
// let's check whether one or two of the two new curves satisfy the stopping condition or not.
// (if one of them satisfies it, we don't split)
if (pred(*new_curve_a) < 0 || (!pred.result && pred(*new_curve_b) < 0)) {
delete new_curve_a;
delete new_curve_b;
return -1;
}
if (pred.result) {
// we don't actually create these two chains
newChains.push_back(_curve);
delete new_curve_a;
delete new_curve_b;
return 0;
}
// here we know we'll split _curve:
splitted_chains.push_back(_curve);
__recursiveSplit(new_curve_a, func, pred, sampling, newChains, splitted_chains);
__recursiveSplit(new_curve_b, func, pred, sampling, newChains, splitted_chains);
return 0;
2008-04-30 15:41:54 +00:00
}
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
int Operators::recursiveSplit(UnaryFunction0D<double> &func,
UnaryPredicate1D &pred,
float sampling)
2008-04-30 15:41:54 +00:00
{
if (_current_chains_set.empty()) {
cerr << "Warning: current set empty" << endl;
return 0;
}
Chain *currentChain = nullptr;
I1DContainer splitted_chains;
I1DContainer newChains;
I1DContainer::iterator cit = _current_chains_set.begin(), citend = _current_chains_set.end();
for (; cit != citend; ++cit) {
currentChain = dynamic_cast<Chain *>(*cit);
if (!currentChain) {
continue;
}
// let's check the first one:
if (pred(*currentChain) < 0) {
return -1;
}
if (!pred.result) {
__recursiveSplit(currentChain, func, pred, sampling, newChains, splitted_chains);
}
else {
newChains.push_back(currentChain);
}
}
// Update the current set of chains:
if (!splitted_chains.empty()) {
for (cit = splitted_chains.begin(), citend = splitted_chains.end(); cit != citend; ++cit) {
delete (*cit);
2018-06-17 17:05:14 +02:00
}
splitted_chains.clear();
2018-06-17 17:05:14 +02:00
}
_current_chains_set.clear();
#if 0
_current_chains_set = newChains;
#else
for (cit = newChains.begin(), citend = newChains.end(); cit != citend; ++cit) {
if ((*cit)->getLength2D() < M_EPSILON) {
delete (*cit);
continue;
}
_current_chains_set.push_back(*cit);
}
#endif
newChains.clear();
2008-04-30 15:41:54 +00:00
if (!_current_chains_set.empty()) {
_current_set = &_current_chains_set;
}
return 0;
2008-04-30 15:41:54 +00:00
}
// recursive split with pred 0D
static int __recursiveSplit(Chain *_curve,
UnaryFunction0D<double> &func,
UnaryPredicate0D &pred0d,
UnaryPredicate1D &pred,
float sampling,
Operators::I1DContainer &newChains,
Operators::I1DContainer &splitted_chains)
2008-04-30 15:41:54 +00:00
{
if (((_curve->nSegments() == 1) && (sampling == 0)) || (_curve->getLength2D() <= sampling)) {
newChains.push_back(_curve);
return 0;
}
CurveInternal::CurvePointIterator first = _curve->curvePointsBegin(sampling);
CurveInternal::CurvePointIterator second = first;
++second;
CurveInternal::CurvePointIterator end = _curve->curvePointsEnd(sampling);
CurveInternal::CurvePointIterator it = second;
CurveInternal::CurvePointIterator split = second;
Interface0DIterator it0d = it.castToInterface0DIterator();
#if 0
real _min = func(it0d);
++it;
#endif
real _min = FLT_MAX;
++it;
// real mean = 0.0f;
// soc unused - real variance = 0.0f;
// uint count = 0;
CurveInternal::CurvePointIterator next = it;
++next;
bool bsplit = false;
for (; ((it != end) && (next != end)); ++it, ++next) {
// ++count;
it0d = it.castToInterface0DIterator();
if (pred0d(it0d) < 0) {
return -1;
}
if (!pred0d.result) {
continue;
}
if (func(it0d) < 0) {
return -1;
}
// mean += func.result;
if (func.result < _min) {
_min = func.result;
split = it;
bsplit = true;
}
}
// mean /= float(count);
// if ((!bsplit) || (mean - _min > mean)) { // we didn't find any minimum
if (!bsplit) { // we didn't find any minimum
newChains.push_back(_curve);
return 0;
}
// retrieves the current splitting id
Id *newId = _curve->getSplittingId();
if (newId == nullptr) {
newId = new Id(_curve->getId());
_curve->setSplittingId(newId);
}
Chain *new_curve_a = new Chain(*newId);
newId->setSecond(newId->getSecond() + 1);
new_curve_a->setSplittingId(newId);
Chain *new_curve_b = new Chain(*newId);
newId->setSecond(newId->getSecond() + 1);
new_curve_b->setSplittingId(newId);
CurveInternal::CurvePointIterator vit = _curve->curveVerticesBegin(),
vitend = _curve->curveVerticesEnd();
CurveInternal::CurvePointIterator vnext = vit;
++vnext;
for (; (vit != vitend) && (vnext != vitend) &&
(vnext._CurvilinearLength < split._CurvilinearLength);
++vit, ++vnext)
{
new_curve_a->push_vertex_back(&(*vit));
}
if ((vit == vitend) || (vnext == vitend)) {
if (G.debug & G_DEBUG_FREESTYLE) {
cout << "The split takes place in bad location" << endl;
}
newChains.push_back(_curve);
delete new_curve_a;
delete new_curve_b;
return 0;
}
// build the two resulting chains
new_curve_a->push_vertex_back(&(*vit));
new_curve_a->push_vertex_back(&(*split));
new_curve_b->push_vertex_back(&(*split));
for (vit = vnext; vit != vitend; ++vit) {
new_curve_b->push_vertex_back(&(*vit));
}
// let's check whether one or two of the two new curves satisfy the stopping condition or not.
// (if one of them satisfies it, we don't split)
if (pred(*new_curve_a) < 0 || (!pred.result && pred(*new_curve_b) < 0)) {
delete new_curve_a;
delete new_curve_b;
return -1;
}
if (pred.result) {
// we don't actually create these two chains
newChains.push_back(_curve);
delete new_curve_a;
delete new_curve_b;
return 0;
}
// here we know we'll split _curve:
splitted_chains.push_back(_curve);
__recursiveSplit(new_curve_a, func, pred0d, pred, sampling, newChains, splitted_chains);
__recursiveSplit(new_curve_b, func, pred0d, pred, sampling, newChains, splitted_chains);
return 0;
2008-04-30 15:41:54 +00:00
}
int Operators::recursiveSplit(UnaryFunction0D<double> &func,
UnaryPredicate0D &pred0d,
UnaryPredicate1D &pred,
float sampling)
2008-04-30 15:41:54 +00:00
{
if (_current_chains_set.empty()) {
cerr << "Warning: current set empty" << endl;
return 0;
}
Chain *currentChain = nullptr;
I1DContainer splitted_chains;
I1DContainer newChains;
I1DContainer::iterator cit = _current_chains_set.begin(), citend = _current_chains_set.end();
for (; cit != citend; ++cit) {
currentChain = dynamic_cast<Chain *>(*cit);
if (!currentChain) {
continue;
}
// let's check the first one:
if (pred(*currentChain) < 0) {
return -1;
}
if (!pred.result) {
__recursiveSplit(currentChain, func, pred0d, pred, sampling, newChains, splitted_chains);
}
else {
newChains.push_back(currentChain);
}
}
// Update the current set of chains:
if (!splitted_chains.empty()) {
for (cit = splitted_chains.begin(), citend = splitted_chains.end(); cit != citend; ++cit) {
delete (*cit);
}
splitted_chains.clear();
}
_current_chains_set.clear();
#if 0
_current_chains_set = newChains;
#else
for (cit = newChains.begin(), citend = newChains.end(); cit != citend; ++cit) {
if ((*cit)->getLength2D() < M_EPSILON) {
delete (*cit);
continue;
}
_current_chains_set.push_back(*cit);
}
#endif
newChains.clear();
2008-04-30 15:41:54 +00:00
if (!_current_chains_set.empty()) {
_current_set = &_current_chains_set;
}
return 0;
2008-04-30 15:41:54 +00:00
}
2008-04-30 15:41:54 +00:00
// Internal class
class PredicateWrapper {
public:
inline PredicateWrapper(BinaryPredicate1D &pred)
{
_pred = &pred;
}
inline bool operator()(Interface1D *i1, Interface1D *i2)
{
if (i1 == i2) {
return false;
}
if ((*_pred)(*i1, *i2) < 0) {
throw std::runtime_error("comparison failed");
}
return _pred->result;
}
2008-04-30 15:41:54 +00:00
private:
BinaryPredicate1D *_pred;
2008-04-30 15:41:54 +00:00
};
int Operators::sort(BinaryPredicate1D &pred)
{
if (!_current_set) {
return 0;
}
PredicateWrapper wrapper(pred);
try {
std::sort(_current_set->begin(), _current_set->end(), wrapper);
}
catch (std::runtime_error &e) {
cerr << "Warning: Operator.sort(): " << e.what() << endl;
return -1;
}
return 0;
2008-04-30 15:41:54 +00:00
}
static Stroke *createStroke(Interface1D &inter)
{
Stroke *stroke = new Stroke;
stroke->setId(inter.getId());
float currentCurvilignAbscissa = 0.0f;
Interface0DIterator it = inter.verticesBegin(), itend = inter.verticesEnd();
Interface0DIterator itfirst = it;
Vec2r current(it->getPoint2D());
Vec2r previous = current;
SVertex *sv;
CurvePoint *cp;
StrokeVertex *stroke_vertex = nullptr;
bool hasSingularity = false;
do {
cp = dynamic_cast<CurvePoint *>(&(*it));
if (!cp) {
sv = dynamic_cast<SVertex *>(&(*it));
if (!sv) {
cerr << "Warning: unexpected Vertex type" << endl;
continue;
}
stroke_vertex = new StrokeVertex(sv);
}
else {
stroke_vertex = new StrokeVertex(cp);
}
current = stroke_vertex->getPoint2D();
Vec2r vec_tmp(current - previous);
real dist = vec_tmp.norm();
if (dist < 1.0e-6) {
hasSingularity = true;
}
currentCurvilignAbscissa += dist;
stroke_vertex->setCurvilinearAbscissa(currentCurvilignAbscissa);
stroke->push_back(stroke_vertex);
previous = current;
++it;
2020-11-06 12:30:59 +11:00
} while (!ELEM(it, itend, itfirst));
if (it == itfirst) {
// Add last vertex:
cp = dynamic_cast<CurvePoint *>(&(*it));
if (!cp) {
sv = dynamic_cast<SVertex *>(&(*it));
if (!sv) {
cerr << "Warning: unexpected Vertex type" << endl;
}
else {
stroke_vertex = new StrokeVertex(sv);
}
}
else {
stroke_vertex = new StrokeVertex(cp);
}
current = stroke_vertex->getPoint2D();
Vec2r vec_tmp(current - previous);
real dist = vec_tmp.norm();
if (dist < 1.0e-6) {
hasSingularity = true;
}
currentCurvilignAbscissa += dist;
stroke_vertex->setCurvilinearAbscissa(currentCurvilignAbscissa);
stroke->push_back(stroke_vertex);
}
// Discard the stroke if the number of stroke vertices is less than two
if (stroke->strokeVerticesSize() < 2) {
delete stroke;
return nullptr;
}
stroke->setLength(currentCurvilignAbscissa);
if (hasSingularity) {
// Try to address singular points such that the distance between two subsequent vertices
// are smaller than epsilon.
StrokeInternal::StrokeVertexIterator v = stroke->strokeVerticesBegin();
StrokeInternal::StrokeVertexIterator vnext = v;
++vnext;
Vec2r next((*v).getPoint());
while (!vnext.isEnd()) {
current = next;
next = (*vnext).getPoint();
if ((next - current).norm() < 1.0e-6) {
StrokeInternal::StrokeVertexIterator vprevious = v;
if (!vprevious.isBegin()) {
--vprevious;
}
// collect a set of overlapping vertices
std::vector<StrokeVertex *> overlapping_vertices;
overlapping_vertices.push_back(&(*v));
do {
overlapping_vertices.push_back(&(*vnext));
current = next;
++v;
++vnext;
if (vnext.isEnd()) {
break;
}
next = (*vnext).getPoint();
} while ((next - current).norm() < 1.0e-6);
Vec2r target;
bool reverse;
if (!vnext.isEnd()) {
target = (*vnext).getPoint();
reverse = false;
}
else if (!vprevious.isBegin()) {
target = (*vprevious).getPoint();
reverse = true;
}
else {
// Discard the stroke because all stroke vertices are overlapping
delete stroke;
return nullptr;
}
current = overlapping_vertices.front()->getPoint();
Vec2r dir(target - current);
real dist = dir.norm();
real len = 1.0e-3; // default offset length
int nvert = overlapping_vertices.size();
if (dist < len * nvert) {
len = dist / nvert;
}
dir.normalize();
Vec2r offset(dir * len);
// add the offset to the overlapping vertices
StrokeVertex *sv;
std::vector<StrokeVertex *>::iterator it = overlapping_vertices.begin();
if (!reverse) {
for (int n = 0; n < nvert; n++) {
sv = (*it);
sv->setPoint(sv->getPoint() + offset * (n + 1));
++it;
}
}
else {
for (int n = 0; n < nvert; n++) {
sv = (*it);
sv->setPoint(sv->getPoint() + offset * (nvert - n));
++it;
}
}
if (vnext.isEnd()) {
break;
}
}
++v;
++vnext;
}
}
{
// Check if the stroke no longer contains singular points
Interface0DIterator v = stroke->verticesBegin();
Interface0DIterator vnext = v;
++vnext;
Vec2r next((*v).getPoint2D());
bool warning = false;
while (!vnext.isEnd()) {
current = next;
next = (*vnext).getPoint2D();
if ((next - current).norm() < 1.0e-6) {
warning = true;
break;
}
++v;
++vnext;
}
if (warning && G.debug & G_DEBUG_FREESTYLE) {
printf("Warning: stroke contains singular points.\n");
}
}
return stroke;
2008-04-30 15:41:54 +00:00
}
inline int applyShading(Stroke &stroke, vector<StrokeShader *> &shaders)
{
for (vector<StrokeShader *>::iterator it = shaders.begin(); it != shaders.end(); ++it) {
if ((*it)->shade(stroke) < 0) {
return -1;
}
Made changes to the C++ API in order to allow for proper error 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);
2009-03-20 22:55:07 +00:00
}
return 0;
2008-04-30 15:41:54 +00:00
}
int Operators::create(UnaryPredicate1D &pred, vector<StrokeShader *> shaders)
{
// Canvas* canvas = Canvas::getInstance();
if (!_current_set) {
cerr << "Warning: current set empty" << endl;
return 0;
}
StrokesContainer new_strokes_set;
for (Operators::I1DContainer::iterator it = _current_set->begin(); it != _current_set->end();
++it)
{
if (pred(**it) < 0) {
goto error;
}
if (!pred.result) {
continue;
}
Stroke *stroke = createStroke(**it);
if (stroke) {
if (applyShading(*stroke, shaders) < 0) {
delete stroke;
goto error;
}
// canvas->RenderStroke(stroke);
new_strokes_set.push_back(stroke);
}
}
for (StrokesContainer::iterator it = new_strokes_set.begin(); it != new_strokes_set.end(); ++it)
{
_current_strokes_set.push_back(*it);
}
new_strokes_set.clear();
return 0;
error:
for (StrokesContainer::iterator it = new_strokes_set.begin(); it != new_strokes_set.end(); ++it)
{
delete (*it);
}
new_strokes_set.clear();
return -1;
2008-04-30 15:41:54 +00:00
}
void Operators::reset(bool removeStrokes)
{
ViewMap *vm = ViewMap::getInstance();
if (!vm) {
cerr << "Error: no ViewMap computed yet" << endl;
return;
}
_current_view_edges_set.clear();
for (I1DContainer::iterator it = _current_chains_set.begin(); it != _current_chains_set.end();
++it)
{
delete *it;
}
_current_chains_set.clear();
ViewMap::viewedges_container &vedges = vm->ViewEdges();
ViewMap::viewedges_container::iterator ve = vedges.begin(), veend = vedges.end();
for (; ve != veend; ++ve) {
if ((*ve)->getLength2D() < M_EPSILON) {
continue;
}
_current_view_edges_set.push_back(*ve);
}
_current_set = &_current_view_edges_set;
if (removeStrokes) {
_current_strokes_set.clear();
}
2008-04-30 15:41:54 +00:00
}
Attempt to fix a potential name conflict between Freestyle and the compositor. A crash in the Freestyle renderer was reported by Ton on IRC with a stack trace below. Note that #2 is in Freestyle, whereas #1 is in the compositor. The problem was observed in a debug build on OS X 10.7 (gcc 4.2, openmp disabled, no llvm). ---------------------------------------------------------------------- Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: 13 at address: 0x0000000000000000 [Switching to process 72386 thread 0xf303] 0x0000000100c129f3 in NodeBase::~NodeBase (this=0x10e501c80) at COM_NodeBase.cpp:43 43 delete (this->m_outputsockets.back()); Current language: auto; currently c++ (gdb) where #0 0x0000000100c129f3 in NodeBase::~NodeBase (this=0x10e501c80) at COM_NodeBase.cpp:43 #1 0x0000000100c29066 in Node::~Node (this=0x10e501c80) at COM_Node.h:49 #2 0x000000010089c273 in NodeShape::~NodeShape (this=0x10e501c80) at NodeShape.cpp:43 #3 0x000000010089910b in NodeGroup::destroy (this=0x10e501da0) at NodeGroup.cpp:61 #4 0x00000001008990cd in NodeGroup::destroy (this=0x10e5014b0) at NodeGroup.cpp:59 #5 0x00000001008990cd in NodeGroup::destroy (this=0x114e18da0) at NodeGroup.cpp:59 #6 0x00000001007e6602 in Controller::ClearRootNode (this=0x114e19640) at Controller.cpp:329 #7 0x00000001007ea52e in Controller::LoadMesh (this=0x114e19640, re=0x10aba4638, srl=0x1140f5258) at Controller.cpp:302 #8 0x00000001008030ad in prepare (re=0x10aba4638, srl=0x1140f5258) at FRS_freestyle.cpp:302 #9 0x000000010080457a in FRS_do_stroke_rendering (re=0x10aba4638, srl=0x1140f5258) at FRS_freestyle.cpp:600 #10 0x00000001006aeb9d in add_freestyle (re=0x10aba4638) at pipeline.c:1584 #11 0x00000001006aceb7 in do_render_3d (re=0x10aba4638) at pipeline.c:1094 #12 0x00000001006ae061 in do_render_fields_blur_3d (re=0x10aba4638) at pipeline.c:1367 #13 0x00000001006afa16 in do_render_composite_fields_blur_3d (re=0x10aba4638) at pipeline.c:1815 #14 0x00000001006b04e4 in do_render_all_options (re=0x10aba4638) at pipeline.c:2021 ---------------------------------------------------------------------- Apparently a name conflict between the two Blender modules is taking place. The present commit hence intends to address it by putting all the Freestyle C++ classes in the namespace 'Freestyle'. This revision will also prevent potential name conflicts with other Blender modules in the future. Special thanks to Lukas Toenne for the help with C++ namespace.
2013-04-09 00:46:49 +00:00
} /* namespace Freestyle */