Files
test2/source/blender/freestyle/intern/geometry/SweepLine.h

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

322 lines
7.8 KiB
C
Raw Normal View History

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
/** \file
* \ingroup freestyle
* \brief Class to define a Sweep Line
*/
#include <list>
#include <vector>
2008-04-30 15:41:54 +00:00
#include "MEM_guardedalloc.h"
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 {
/** Class to define the intersection between two segments. */
template<class Edge> class Intersection {
public:
template<class EdgeClass> Intersection(EdgeClass *eA, real ta, EdgeClass *eB, real tb)
{
EdgeA = eA;
EdgeB = eB;
tA = ta;
tB = tb;
userdata = 0;
}
Intersection(const Intersection &iBrother)
{
EdgeA = iBrother.EdgeA;
EdgeB = iBrother.EdgeB;
tA = iBrother.tA;
tB = iBrother.tB;
userdata = 0;
}
/** returns the parameter giving the intersection, for the edge iEdge */
real getParameter(Edge *iEdge)
{
if (iEdge == EdgeA) {
return tA;
}
if (iEdge == EdgeB) {
return tB;
}
return 0;
}
2008-04-30 15:41:54 +00:00
public:
2018-10-11 08:49:28 +11:00
void *userdata; // FIXME
Edge *EdgeA; // first segment
Edge *EdgeB; // second segment
real tA; // parameter defining the intersection point with respect to the segment EdgeA.
real tB; // parameter defining the intersection point with respect to the segment EdgeB.
MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Intersection")
2008-04-30 15:41:54 +00:00
};
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable : 4521) // disable warning C4521: multiple copy constructors specified
#endif
2008-04-30 15:41:54 +00:00
template<class T, class Point> class Segment {
public:
Segment() {}
Segment(T &s, const Point &iA, const Point &iB)
{
_edge = s;
if (iA < iB) {
A = iA;
B = iB;
_order = true;
}
else {
A = iB;
B = iA;
_order = false;
}
}
Segment(Segment<T, Point> &iBrother)
{
_edge = iBrother.edge();
A = iBrother.A;
B = iBrother.B;
_Intersections = iBrother._Intersections;
_order = iBrother._order;
}
Segment(const Segment<T, Point> &iBrother)
{
_edge = iBrother._edge;
A = iBrother.A;
B = iBrother.B;
_Intersections = iBrother._Intersections;
_order = iBrother._order;
}
~Segment()
{
_Intersections.clear();
}
inline Point operator[](const ushort &i) const
{
return (i % 2 == 0) ? A : B;
}
inline bool operator==(const Segment<T, Point> &iBrother)
{
if (_edge == iBrother._edge) {
return true;
}
return false;
}
/* Adds an intersection for this segment */
inline void AddIntersection(Intersection<Segment<T, Point>> *i)
{
_Intersections.push_back(i);
}
/** Checks for a common vertex with another edge */
inline bool CommonVertex(const Segment<T, Point> &S, Point &CP)
{
if ((A == S[0]) || (A == S[1])) {
CP = A;
return true;
}
if ((B == S[0]) || (B == S[1])) {
CP = B;
return true;
}
return false;
}
inline vector<Intersection<Segment<T, Point>> *> &intersections()
{
return _Intersections;
}
inline bool order()
{
return _order;
}
inline T &edge()
{
return _edge;
}
private:
T _edge;
Point A;
Point B;
std::vector<Intersection<Segment<T, Point>> *>
_Intersections; // list of intersections parameters
bool _order; // true if A and B are in the same order than _edge.A and _edge.B. false otherwise.
MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Segment")
2008-04-30 15:41:54 +00:00
};
#ifdef _MSC_VER
# pragma warning(pop)
#endif
/** defines a binary function that can be overload by the user to specify at each condition the
* intersection between 2 edges must be computed
2008-04-30 15:41:54 +00:00
*/
template<class T1, class T2> struct binary_rule {
binary_rule() {}
template<class T3, class T4> binary_rule(const binary_rule<T3, T4> & /*brother*/) {}
virtual ~binary_rule() {}
virtual bool operator()(T1 &, T2 &)
{
return true;
}
2008-04-30 15:41:54 +00:00
};
template<class T, class Point> class SweepLine {
2008-04-30 15:41:54 +00:00
public:
SweepLine() {}
~SweepLine()
{
for (typename vector<Intersection<Segment<T, Point>> *>::iterator i = _Intersections.begin(),
iend = _Intersections.end();
i != iend;
i++)
{
delete (*i);
}
}
2018-06-17 17:05:14 +02:00
inline void process(Point &p,
vector<Segment<T, Point> *> &segments,
#if 0
binary_rule<Segment<T, Point>, Segment<T, Point>> &binrule =
binary_rule<Segment<T, Point>, Segment<T, Point>>(),
#else
binary_rule<Segment<T, Point>, Segment<T, Point>> &binrule,
#endif
real epsilon = M_EPSILON)
{
// first we remove the segments that need to be removed and then we add the segments to add
vector<Segment<T, Point> *> toadd;
typename vector<Segment<T, Point> *>::iterator s, send;
for (s = segments.begin(), send = segments.end(); s != send; s++) {
if (p == (*(*s))[0]) {
toadd.push_back((*s));
}
else {
remove((*s));
}
}
for (s = toadd.begin(), send = toadd.end(); s != send; s++) {
add((*s), binrule, epsilon);
}
}
inline void add(Segment<T, Point> *S,
#if 0
binary_rule<Segment<T, Point>, Segment<T, Point>> &binrule =
binary_rule<Segment<T, Point>, Segment<T, Point>>(),
#else
binary_rule<Segment<T, Point>, Segment<T, Point>> &binrule,
#endif
real epsilon)
{
real t, u;
Point CP;
Vec2r v0, v1, v2, v3;
if (true == S->order()) {
v0[0] = ((*S)[0])[0];
v0[1] = ((*S)[0])[1];
v1[0] = ((*S)[1])[0];
v1[1] = ((*S)[1])[1];
}
else {
v1[0] = ((*S)[0])[0];
v1[1] = ((*S)[0])[1];
v0[0] = ((*S)[1])[0];
v0[1] = ((*S)[1])[1];
}
for (typename std::list<Segment<T, Point> *>::iterator s = _set.begin(), send = _set.end();
s != send;
s++)
{
Segment<T, Point> *currentS = (*s);
if (true != binrule(*S, *currentS)) {
continue;
}
if (true == currentS->order()) {
v2[0] = ((*currentS)[0])[0];
v2[1] = ((*currentS)[0])[1];
v3[0] = ((*currentS)[1])[0];
v3[1] = ((*currentS)[1])[1];
}
else {
v3[0] = ((*currentS)[0])[0];
v3[1] = ((*currentS)[0])[1];
v2[0] = ((*currentS)[1])[0];
v2[1] = ((*currentS)[1])[1];
}
if (S->CommonVertex(*currentS, CP)) {
continue; // the two edges have a common vertex->no need to check
}
if (GeomUtils::intersect2dSeg2dSegParametric(v0, v1, v2, v3, t, u, epsilon) ==
GeomUtils::DO_INTERSECT)
{
// create the intersection
Intersection<Segment<T, Point>> *inter = new Intersection<Segment<T, Point>>(
S, t, currentS, u);
// add it to the intersections list
_Intersections.push_back(inter);
// add this intersection to the first edge intersections list
S->AddIntersection(inter);
// add this intersection to the second edge intersections list
currentS->AddIntersection(inter);
}
}
// add the added segment to the list of active segments
_set.push_back(S);
}
inline void remove(Segment<T, Point> *s)
{
if (s->intersections().size() > 0) {
_IntersectedEdges.push_back(s);
}
_set.remove(s);
}
vector<Segment<T, Point> *> &intersectedEdges()
{
return _IntersectedEdges;
}
vector<Intersection<Segment<T, Point>> *> &intersections()
{
return _Intersections;
}
2008-04-30 15:41:54 +00:00
private:
std::list<Segment<T, Point> *>
_set; // set of active edges for a given position of the sweep line
std::vector<Segment<T, Point> *> _IntersectedEdges; // the list of intersected edges
std::vector<Intersection<Segment<T, Point>> *> _Intersections; // the list of all intersections.
MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:SweepLine")
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 */