2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2008-04-30 15:41:54 +00:00
|
|
|
|
2013-01-02 01:55:30 +00:00
|
|
|
#pragma once
|
2008-04-30 15:41:54 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup freestyle
|
|
|
|
|
* \brief Interface 1D and related tools definitions
|
2013-01-02 01:55:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <string>
|
2008-04-30 15:41:54 +00:00
|
|
|
|
2013-01-02 01:55:30 +00:00
|
|
|
#include "Functions0D.h"
|
2008-04-30 15:41:54 +00:00
|
|
|
|
2013-01-02 01:55:30 +00:00
|
|
|
#include "../system/Id.h"
|
|
|
|
|
#include "../system/Precision.h"
|
|
|
|
|
|
|
|
|
|
#include "../winged_edge/Nature.h"
|
2008-04-30 15:41:54 +00:00
|
|
|
|
2024-11-13 13:39:49 +01:00
|
|
|
#include "MEM_guardedalloc.h"
|
2013-05-13 22:58:27 +00:00
|
|
|
|
2008-04-30 15:41:54 +00:00
|
|
|
using namespace std;
|
2013-01-02 01:55:30 +00:00
|
|
|
|
2013-04-09 00:46:49 +00:00
|
|
|
namespace Freestyle {
|
|
|
|
|
|
2008-04-30 15:41:54 +00:00
|
|
|
// Integration method
|
2022-04-11 11:41:00 +10:00
|
|
|
/**
|
|
|
|
|
* The different integration methods that can be invoked to integrate into a single value the set
|
2019-04-30 17:50:57 +10:00
|
|
|
* of values obtained from each 0D element of a 1D element.
|
2008-04-30 15:41:54 +00:00
|
|
|
*/
|
|
|
|
|
typedef enum {
|
2022-04-11 11:41:00 +10:00
|
|
|
/**
|
|
|
|
|
* The value computed for the 1D element is the mean of the values obtained for the 0D elements.
|
|
|
|
|
*/
|
|
|
|
|
MEAN,
|
|
|
|
|
/**
|
|
|
|
|
* The value computed for the 1D element is the minimum of the values obtained for the 0D
|
|
|
|
|
* elements.
|
|
|
|
|
*/
|
|
|
|
|
MIN,
|
|
|
|
|
/**
|
|
|
|
|
* The value computed for the 1D element is the maximum of the values obtained for the 0D
|
|
|
|
|
* elements.
|
|
|
|
|
*/
|
|
|
|
|
MAX,
|
|
|
|
|
/**
|
|
|
|
|
* The value computed for the 1D element is the first of the values obtained for the 0D
|
|
|
|
|
* elements.
|
|
|
|
|
*/
|
|
|
|
|
FIRST,
|
|
|
|
|
/**
|
|
|
|
|
* The value computed for the 1D element is the last of the values obtained for the 0D
|
|
|
|
|
* elements.
|
|
|
|
|
*/
|
|
|
|
|
LAST,
|
2008-04-30 15:41:54 +00:00
|
|
|
} IntegrationType;
|
|
|
|
|
|
2022-04-11 11:41:00 +10:00
|
|
|
/**
|
|
|
|
|
* Returns a single value from a set of values evaluated at each 0D element of this 1D element.
|
|
|
|
|
*
|
|
|
|
|
* \param fun: The UnaryFunction0D used to compute a value at each Interface0D.
|
|
|
|
|
* \param it: The Interface0DIterator used to iterate over the 0D elements of this 1D element.
|
|
|
|
|
* The integration will occur over the 0D elements starting from the one pointed by it.
|
|
|
|
|
* \param it_end: The Interface0DIterator pointing the end of the 0D elements of the 1D element.
|
|
|
|
|
* \param integration_type: The integration method used to compute a single value from a set of
|
|
|
|
|
* values.
|
2019-02-18 08:08:12 +11:00
|
|
|
* \return the single value obtained for the 1D element.
|
2008-04-30 15:41:54 +00:00
|
|
|
*/
|
|
|
|
|
template<class T>
|
2013-01-02 01:55:30 +00:00
|
|
|
T integrate(UnaryFunction0D<T> &fun,
|
|
|
|
|
Interface0DIterator it,
|
|
|
|
|
Interface0DIterator it_end,
|
|
|
|
|
IntegrationType integration_type = MEAN)
|
|
|
|
|
{
|
|
|
|
|
T res;
|
2023-07-25 12:51:50 +10:00
|
|
|
uint size;
|
2013-01-02 01:55:30 +00:00
|
|
|
switch (integration_type) {
|
|
|
|
|
case MIN:
|
|
|
|
|
fun(it);
|
|
|
|
|
res = fun.result;
|
|
|
|
|
++it;
|
|
|
|
|
for (; !it.isEnd(); ++it) {
|
|
|
|
|
fun(it);
|
2019-05-31 22:51:19 +10:00
|
|
|
if (fun.result < res) {
|
2013-01-02 01:55:30 +00:00
|
|
|
res = fun.result;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2013-01-02 01:55:30 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case MAX:
|
|
|
|
|
fun(it);
|
|
|
|
|
res = fun.result;
|
|
|
|
|
++it;
|
|
|
|
|
for (; !it.isEnd(); ++it) {
|
|
|
|
|
fun(it);
|
2019-05-31 22:51:19 +10:00
|
|
|
if (fun.result > res) {
|
2013-01-02 01:55:30 +00:00
|
|
|
res = fun.result;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2013-01-02 01:55:30 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case FIRST:
|
|
|
|
|
fun(it);
|
|
|
|
|
res = fun.result;
|
|
|
|
|
break;
|
|
|
|
|
case LAST:
|
|
|
|
|
fun(--it_end);
|
|
|
|
|
res = fun.result;
|
|
|
|
|
break;
|
|
|
|
|
case MEAN:
|
|
|
|
|
default:
|
|
|
|
|
fun(it);
|
|
|
|
|
res = fun.result;
|
|
|
|
|
++it;
|
|
|
|
|
for (size = 1; !it.isEnd(); ++it, ++size) {
|
|
|
|
|
fun(it);
|
|
|
|
|
res += fun.result;
|
|
|
|
|
}
|
|
|
|
|
res /= (size ? size : 1);
|
|
|
|
|
break;
|
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
|
|
|
}
|
2013-01-02 01:55:30 +00:00
|
|
|
return res;
|
2008-04-30 15:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Interface1D
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Base class for any 1D element. */
|
2008-04-30 15:41:54 +00:00
|
|
|
class Interface1D {
|
|
|
|
|
public:
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Default constructor */
|
2013-01-02 01:55:30 +00:00
|
|
|
Interface1D()
|
|
|
|
|
{
|
|
|
|
|
_timeStamp = 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Destructor */
|
2014-04-17 12:45:22 +09:00
|
|
|
virtual ~Interface1D(){};
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Returns the string "Interface1D". */
|
2013-01-02 01:55:30 +00:00
|
|
|
virtual string getExactTypeName() const
|
|
|
|
|
{
|
|
|
|
|
return "Interface1D";
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-01-02 01:55:30 +00:00
|
|
|
// Iterator access
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Returns an iterator over the Interface1D vertices, pointing to the first vertex. */
|
2014-04-17 12:37:08 +09:00
|
|
|
virtual Interface0DIterator verticesBegin();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Returns an iterator over the Interface1D vertices, pointing after the last vertex. */
|
2014-04-17 12:37:08 +09:00
|
|
|
virtual Interface0DIterator verticesEnd();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Returns an iterator over the Interface1D points, pointing to the first point. The difference
|
2019-04-30 17:50:57 +10:00
|
|
|
* with verticesBegin() is that here we can iterate over points of the 1D element at a any given
|
2019-10-10 10:25:46 +11:00
|
|
|
* sampling. Indeed, for each iteration, a virtual point is created.
|
|
|
|
|
*
|
|
|
|
|
* \param t: The sampling with which we want to iterate over points of this 1D element.
|
2013-01-02 01:55:30 +00:00
|
|
|
*/
|
2014-04-17 12:37:08 +09:00
|
|
|
virtual Interface0DIterator pointsBegin(float t = 0.0f);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Returns an iterator over the Interface1D points, pointing after the last point. The
|
2019-04-30 17:50:57 +10:00
|
|
|
* difference with verticesEnd() is that here we can iterate over points of the 1D element at a
|
2019-10-10 10:25:46 +11:00
|
|
|
* any given sampling. Indeed, for each iteration, a virtual point is created.
|
|
|
|
|
*
|
|
|
|
|
* \param t: The sampling with which we want to iterate over points of this 1D element.
|
2013-01-02 01:55:30 +00:00
|
|
|
*/
|
2014-04-17 12:37:08 +09:00
|
|
|
virtual Interface0DIterator pointsEnd(float t = 0.0f);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-01-02 01:55:30 +00:00
|
|
|
// Data access methods
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Returns the 2D length of the 1D element. */
|
2014-04-17 12:37:08 +09:00
|
|
|
virtual real getLength2D() const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Returns the Id of the 1D element. */
|
2014-04-17 12:37:08 +09:00
|
|
|
virtual Id getId() const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-01-02 01:55:30 +00:00
|
|
|
// FIXME: ce truc n'a rien a faire la...(c une requete complexe qui doit etre ds les Function1D)
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Returns the nature of the 1D element. */
|
2014-04-17 12:37:08 +09:00
|
|
|
virtual Nature::EdgeNature getNature() const;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Returns the time stamp of the 1D element. Mainly used for selection. */
|
2023-07-25 12:51:50 +10:00
|
|
|
virtual uint getTimeStamp() const
|
2013-01-02 01:55:30 +00:00
|
|
|
{
|
|
|
|
|
return _timeStamp;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 15:57:33 +10:00
|
|
|
/** Sets the time stamp for the 1D element. */
|
2023-07-25 12:51:50 +10:00
|
|
|
inline void setTimeStamp(uint iTimeStamp)
|
2013-01-02 01:55:30 +00:00
|
|
|
{
|
|
|
|
|
_timeStamp = iTimeStamp;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-04-30 15:41:54 +00:00
|
|
|
protected:
|
2023-07-25 12:51:50 +10:00
|
|
|
uint _timeStamp;
|
2013-05-13 22:58:27 +00:00
|
|
|
|
|
|
|
|
MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Interface1D")
|
2008-04-30 15:41:54 +00:00
|
|
|
};
|
|
|
|
|
|
2013-04-09 00:46:49 +00:00
|
|
|
} /* namespace Freestyle */
|