-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSegmentIntersect.cpp
More file actions
317 lines (284 loc) · 9.4 KB
/
SegmentIntersect.cpp
File metadata and controls
317 lines (284 loc) · 9.4 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#define ALGORITHMLIB __declspec(dllexport)
#include "SegmentIntersect.h"
#include "..\..\DataStruct\Tree\SortedBalanceBinaryTree.h"
namespace AlLib
{
namespace Algorithm
{
namespace Geo
{
SegmentIntersection::LinePoint::LinePoint()
{
m_Type = POINT_TYPE::LEFT;
}
SegmentIntersection::LinePoint::~LinePoint()
{
}
SegmentIntersection::LinePoint::LinePoint(
Math::Point<2> poP_,
POINT_TYPE Type_)
{
m_poPos = poP_;
m_Type = Type_;
}
SegmentIntersection::ComparableLineGeometry::ComparableLineGeometry()
{
}
SegmentIntersection::ComparableLineGeometry::ComparableLineGeometry(
const PlaneGeometry::LineGeometry& line_)
{
m_Line = line_;
}
SegmentIntersection::ComparableLineGeometry::~ComparableLineGeometry()
{
}
bool SegmentIntersection::ComparableLineGeometry::operator<(
const ComparableLineGeometry& cline_)
{
// 小于
// 本线段对应向量 到 本线段起点到输入线段起点 构成向量,需要 逆时针旋转到达时,本线段 小于 输入线段
if (m_Line.m_poStart.m_nPos[0] < cline_.m_Line.m_poEnd.m_nPos[0])
{
Math::Vector<2> _vector1(m_Line.m_poStart, m_Line.m_poEnd);
Math::Vector<2> _vector2(m_Line.m_poStart, cline_.m_Line.m_poStart);
Geometry::ROTATE_DIRECTION _direc = Geometry::TestDirection(_vector1, _vector2);
return (_direc == Geometry::ROTATE_DIRECTION::ANTICLOCK);
}
else
{
assert(m_Line.m_poStart.m_nPos[0] == cline_.m_Line.m_poEnd.m_nPos[0]);
return m_Line.m_poStart.m_nPos[1] < cline_.m_Line.m_poEnd.m_nPos[1];
}
}
bool SegmentIntersection::ComparableLineGeometry::operator==(const ComparableLineGeometry& cline_)
{
if (m_Line.m_poStart.m_nPos[0] < cline_.m_Line.m_poEnd.m_nPos[0])
{
Math::Vector<2> _vector1(m_Line.m_poStart, m_Line.m_poEnd);
Math::Vector<2> _vector2(m_Line.m_poStart, cline_.m_Line.m_poStart);
Geometry::ROTATE_DIRECTION _direc = Geometry::TestDirection(_vector1, _vector2);
return (_direc == Geometry::ROTATE_DIRECTION::NO_ROTATE);
}
else
{
assert(m_Line.m_poStart.m_nPos[0] == cline_.m_Line.m_poEnd.m_nPos[0]);
return m_Line.m_poStart.m_nPos[1] == cline_.m_Line.m_poEnd.m_nPos[1];
}
}
bool SegmentIntersection::ComparableLineGeometry::operator!=(const ComparableLineGeometry& cline_)
{
return !operator==(cline_);
}
bool SegmentIntersection::ComparableLineGeometry::operator>(const ComparableLineGeometry& cline_)
{
return !(operator<(cline_) || operator==(cline_));
}
SegmentIntersection::SegmentIntersection()
{
}
SegmentIntersection::~SegmentIntersection()
{
}
bool SegmentIntersection::Run(
const PlaneGeometry::LineGeometry& line1_,
const PlaneGeometry::LineGeometry& line2_)
{
Math::Vector<2> _vec1S2S(line1_.m_poStart, line2_.m_poStart);
Math::Vector<2> _vec1S2E(line1_.m_poStart, line2_.m_poEnd);
Math::Vector<2> _vec2S1S(line2_.m_poStart, line1_.m_poStart);
Math::Vector<2> _vec2S1E(line2_.m_poStart, line1_.m_poEnd);
Math::Vector<2> _vecLine1(line1_.m_poStart, line1_.m_poEnd);
Math::Vector<2> _vecLine2(line2_.m_poStart, line2_.m_poEnd);
// 对线段2
// 考察 线段2起点到线段1起点构成的向量 如何旋转以到达 线段2代表的向量
// 考察 线段2起点到线段1终点构成的向量 如何旋转以到达 线段2代表的向量
Geometry::ROTATE_DIRECTION _dir1 = Geometry::TestDirection(_vec2S1S, _vecLine2);
Geometry::ROTATE_DIRECTION _dir2 = Geometry::TestDirection(_vec2S1E, _vecLine2);
// 对线段1
// 考察 线段1起点到线段2起点构成的向量 如何旋转以到达 线段1代表的向量
// 考察 线段1起点到线段2终点构成的向量 如何旋转以到达 线段1代表的向量
Geometry::ROTATE_DIRECTION _dir3 = Geometry::TestDirection(_vec1S2S, _vecLine1);
Geometry::ROTATE_DIRECTION _dir4 = Geometry::TestDirection(_vec1S2E, _vecLine1);
bool _bSect = false;
if (_dir1 == Geometry::ROTATE_DIRECTION::ANTICLOCK
&& _dir2 == Geometry::ROTATE_DIRECTION::CLOCK)
{
_bSect = true;
}
else if (_dir1 == Geometry::ROTATE_DIRECTION::CLOCK
&& _dir2 == Geometry::ROTATE_DIRECTION::ANTICLOCK)
{
_bSect = true;
}
else if (_dir3 == Geometry::ROTATE_DIRECTION::ANTICLOCK
&& _dir4 == Geometry::ROTATE_DIRECTION::CLOCK)
{
_bSect = true;
}
else if (_dir3 == Geometry::ROTATE_DIRECTION::CLOCK
&& _dir4 == Geometry::ROTATE_DIRECTION::ANTICLOCK)
{
_bSect = true;
}
if (_bSect)
{
return _bSect;
}
if (_dir1 == Geometry::NO_ROTATE
&& Geometry::IsPointOnLine(line2_, line1_.m_poStart))
{
_bSect = true;
}
else if (_dir2 == Geometry::ROTATE_DIRECTION::NO_ROTATE
&& Geometry::IsPointOnLine(line2_, line1_.m_poEnd))
{
_bSect = true;
}
else if (_dir3 == Geometry::ROTATE_DIRECTION::NO_ROTATE
&& Geometry::IsPointOnLine(line1_, line2_.m_poStart))
{
_bSect = true;
}
else if (_dir4 == Geometry::ROTATE_DIRECTION::NO_ROTATE
&& Geometry::IsPointOnLine(line1_, line2_.m_poEnd))
{
_bSect = true;
}
return _bSect;
}
bool SegmentIntersection::Run(
const DataStruct::Array::DynArray<PlaneGeometry::LineGeometry>& arrLines_)
{
// 基于n个线段,得到2n个含左右属性的端点
DataStruct::Array::DynArray<LinePoint*> _arrCritialPoint;
typedef DataStruct::Tree::SortedBalanceBinaryTree<LinePoint*, PlaneGeometry::LineGeometry> Map;
typedef DataStruct::Tree::SortedBalanceBinaryTree<LinePoint*, PlaneGeometry::LineGeometry>::Pair MapPair;
typedef DataStruct::Tree::SortedBalanceBinaryTree<LinePoint*, PlaneGeometry::LineGeometry>::Node MapNode;
Map _mapPointToLine;
// 通过n个线段得到2n个事件点
// 建立事件点到线段的映射
for (int _i = 0; _i < arrLines_.GetSize(); _i++)
{
if (arrLines_[_i].m_poStart.m_nPos[0] < arrLines_[_i].m_poEnd.m_nPos[0])
{
LinePoint* _pLPo = new LinePoint(arrLines_[_i].m_poStart, POINT_TYPE::LEFT);
_arrCritialPoint.Add(_pLPo);
MapPair _nPair1(_pLPo, arrLines_[_i]);
_mapPointToLine.Add(_nPair1);
_pLPo = new LinePoint(arrLines_[_i].m_poEnd, POINT_TYPE::RIGHT);
_arrCritialPoint.Add(_pLPo);
MapPair _nPair2(_pLPo, arrLines_[_i]);
_mapPointToLine.Add(_nPair2);
}
else
{
LinePoint* _pLPo = new LinePoint(arrLines_[_i].m_poStart, POINT_TYPE::RIGHT);
_arrCritialPoint.Add(_pLPo);
MapPair _nPair1(_pLPo, arrLines_[_i]);
_mapPointToLine.Add(_nPair1);
_pLPo = new LinePoint(arrLines_[_i].m_poEnd, POINT_TYPE::LEFT);
_arrCritialPoint.Add(_pLPo);
MapPair _nPair2(_pLPo, arrLines_[_i]);
_mapPointToLine.Add(_nPair2);
}
}
// 对2n个事件点
// 按优先级
// X位置从小到大
// 从左端点到右端点
// Y位置从小到大
// 排序
_arrCritialPoint.Sort(
[](const LinePoint*& lpo1_, const LinePoint*& lpo2_)->int
{
double _nDeltaX = lpo1_->m_poPos.m_nPos[0] - lpo2_->m_poPos.m_nPos[0];
if (_nDeltaX != 0.0)
{
if (_nDeltaX > 0.0)
{
return 1;
}
else
{
return -1;
}
}
if (lpo1_->m_Type != lpo2_->m_Type)
{
if (lpo1_->m_Type == POINT_TYPE::LEFT)
{
return -1;
}
else
{
return 1;
}
}
double _nDeltaY = lpo1_->m_poPos.m_nPos[1] - lpo2_->m_poPos.m_nPos[1];
if (_nDeltaY > 0.0)
{
return 1;
}
else if (_nDeltaY < 0.0)
{
return -1;
}
else
{
return 0;
}
}
);
typedef DataStruct::Tree::SortedBalanceBinaryTree<ComparableLineGeometry, PlaneGeometry::LineGeometry> LineMap;
typedef DataStruct::Tree::SortedBalanceBinaryTree<ComparableLineGeometry, PlaneGeometry::LineGeometry>::Pair LineMapPair;
typedef DataStruct::Tree::SortedBalanceBinaryTree<ComparableLineGeometry, PlaneGeometry::LineGeometry>::Node LineMapNode;
LineMap _lineMap;
for (int _i = 0; _i < _arrCritialPoint.GetSize(); _i++)
{
if (_arrCritialPoint[_i]->m_Type == POINT_TYPE::LEFT)
{
MapNode* _pNode = _mapPointToLine.Search(_arrCritialPoint[_i]);
LineMapPair _nPair(ComparableLineGeometry(_pNode->GetPair().m_nValue), _pNode->GetPair().m_nValue);
if (_lineMap.Add(_nPair))
{
return true;
}
LineMapNode* _pLineNode = _lineMap.Search(ComparableLineGeometry(_pNode->GetPair().m_nValue));
LineMapNode* _pPreLineNode = _lineMap.Pre(_pLineNode);
LineMapNode* _pSucLineNode = _lineMap.Suc(_pLineNode);
if (_pPreLineNode
&& SegmentIntersection::Run(_pPreLineNode->GetPair().m_nValue, _pLineNode->GetPair().m_nValue))
{
return true;
}
else if (_pSucLineNode
&& SegmentIntersection::Run(_pSucLineNode->GetPair().m_nValue, _pLineNode->GetPair().m_nValue))
{
return true;
}
}
else
{
MapNode* _pNode = _mapPointToLine.Search(_arrCritialPoint[_i]);
LineMapNode* _pLineNode = _lineMap.Search(ComparableLineGeometry(_pNode->GetPair().m_nValue));
LineMapNode* _pPreLineNode = _lineMap.Pre(_pLineNode);
LineMapNode* _pSucLineNode = _lineMap.Suc(_pLineNode);
if (_pPreLineNode
&& _pSucLineNode
&& SegmentIntersection::Run(_pPreLineNode->GetPair().m_nValue, _pSucLineNode->GetPair().m_nValue))
{
return true;
}
_lineMap.Delete(_pLineNode->GetPair().m_nKey);
}
}
for (int _i = 0; _arrCritialPoint.GetSize(); _i++)
{
delete _arrCritialPoint[_i];
_arrCritialPoint[_i] = nullptr;
}
return false;
}
}
}
}