-
Notifications
You must be signed in to change notification settings - Fork 19
/
AreaOrderer.cpp
153 lines (124 loc) · 3.35 KB
/
AreaOrderer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// AreaOrderer.cpp
// Copyright (c) 2010, Dan Heeks
// This program is released under the BSD license. See the file COPYING for details.
#include "AreaOrderer.h"
#include "Area.h"
CAreaOrderer* CInnerCurves::area_orderer = NULL;
CInnerCurves::CInnerCurves(CInnerCurves* pOuter, const CCurve* curve)
{
m_pOuter = pOuter;
m_curve = curve;
m_unite_area = NULL;
}
CInnerCurves::~CInnerCurves()
{
delete m_unite_area;
}
void CInnerCurves::Insert(const CCurve* pcurve)
{
std::list<CInnerCurves*> outside_of_these;
std::list<CInnerCurves*> crossing_these;
// check all inner curves
for(std::set<CInnerCurves*>::iterator It = m_inner_curves.begin(); It != m_inner_curves.end(); It++)
{
CInnerCurves* c = *It;
switch(GetOverlapType(*pcurve, *(c->m_curve)))
{
case eOutside:
outside_of_these.push_back(c);
break;
case eInside:
// insert in this inner curve
c->Insert(pcurve);
return;
case eSiblings:
break;
case eCrossing:
crossing_these.push_back(c);
break;
}
}
// add as a new inner
CInnerCurves* new_item = new CInnerCurves(this, pcurve);
this->m_inner_curves.insert(new_item);
for(std::list<CInnerCurves*>::iterator It = outside_of_these.begin(); It != outside_of_these.end(); It++)
{
// move items
CInnerCurves* c = *It;
c->m_pOuter = new_item;
new_item->m_inner_curves.insert(c);
this->m_inner_curves.erase(c);
}
for(std::list<CInnerCurves*>::iterator It = crossing_these.begin(); It != crossing_these.end(); It++)
{
// unite these
CInnerCurves* c = *It;
new_item->Unite(c);
this->m_inner_curves.erase(c);
}
}
void CInnerCurves::GetArea(CArea &area, bool outside, bool use_curve)const
{
if(use_curve && m_curve)
{
area.m_curves.push_back(*m_curve);
outside = !outside;
}
std::list<const CInnerCurves*> do_after;
for(std::set<CInnerCurves*>::const_iterator It = m_inner_curves.begin(); It != m_inner_curves.end(); It++)
{
const CInnerCurves* c = *It;
area.m_curves.push_back(*c->m_curve);
if(!outside)area.m_curves.back().Reverse();
if(outside)c->GetArea(area, !outside, false);
else do_after.push_back(c);
}
for(std::list<const CInnerCurves*>::iterator It = do_after.begin(); It != do_after.end(); It++)
{
const CInnerCurves* c = *It;
c->GetArea(area, !outside, false);
}
}
void CInnerCurves::Unite(const CInnerCurves* c)
{
// unite all the curves in c, with this one
CArea* new_area = new CArea();
new_area->m_curves.push_back(*m_curve);
delete m_unite_area;
m_unite_area = new_area;
CArea a2;
c->GetArea(a2);
m_unite_area->Union(a2);
m_unite_area->Reorder();
for(std::list<CCurve>::iterator It = m_unite_area->m_curves.begin(); It != m_unite_area->m_curves.end(); It++)
{
CCurve &curve = *It;
if(It == m_unite_area->m_curves.begin())
m_curve = &curve;
else
{
if(curve.IsClockwise())curve.Reverse();
Insert(&curve);
}
}
}
CAreaOrderer::CAreaOrderer()
{
m_top_level = new CInnerCurves(NULL, NULL);
}
void CAreaOrderer::Insert(CCurve* pcurve)
{
CInnerCurves::area_orderer = this;
// make them all anti-clockwise as they come in
if(pcurve->IsClockwise())pcurve->Reverse();
m_top_level->Insert(pcurve);
}
CArea CAreaOrderer::ResultArea()const
{
CArea a;
if(m_top_level)
{
m_top_level->GetArea(a);
}
return a;
}