-
Notifications
You must be signed in to change notification settings - Fork 0
/
Segment2D.cpp
240 lines (213 loc) · 6.76 KB
/
Segment2D.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
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
/*
* Line3D.cpp
*
* Created on: Jul 4, 2013
* Author: nao
*/
#include "Segment2D.h"
#include <math.h>
#include <pcl/pcl_base.h>
using std::cout;
using std::endl;
Segment3D::Segment3D(Point3D p1,Point3D p2):ExactSegment3D( p1, p2)
{
}
Segment3D::Segment3D():ExactSegment3D(Point3D(0,0,0),Point3D(0,0,0))
{}
Segment3D::~Segment3D() {
}
bool Segment3D::isCollinear(const Segment3D &otherLine, double Threshold) const
{
ExactLine3D l = supporting_line();
double d1 = squared_distance(l,otherLine.source());
double d2 = squared_distance(l,otherLine.target());
return sqrt(d1+d2)< Threshold;
}
void Segment3D::mergeWithCollinear(const Segment3D& otherLine)
{
Point3D p[4];
Point3D minX,minY,minZ,maxX,maxY,maxZ;
double minXVal = 1000000000;
double maxXVal = -1000000000;
double minYVal = 1000000000;
double maxYVal = -1000000000;
double minZVal = 1000000000;
double maxZVal = -1000000000;
p[0] = otherLine.source();
p[1] = otherLine.target();
p[2] = this->source();
p[3] = this->target();
for(int i=0;i<4;i++)
{
if(p[i].x() < minXVal)
{
minX = p[i];
minXVal = minX.x();
}
if(p[i].y() < minYVal)
{
minY = p[i];
minYVal = minY.y();
}
if(p[i].z() < minZVal)
{
minZ = p[i];
minZVal = minZ.y();
}
if(p[i].x() > maxXVal)
{
maxX = p[i];
maxXVal = maxX.x();
}
if(p[i].y() > maxYVal)
{
maxY = p[i];
maxYVal = maxY.y();
}
if(p[i].z() < minZVal)
{
minZ = p[i];
minZVal = minZ.y();
}
if(p[i].z() > maxZVal)
{
maxZ = p[i];
maxZVal = maxZ.y();
}
}
if(direction().dx() < direction().dy() && direction().dz() < direction().dy())
*this = Segment3D(minY,maxY);
else if(direction().dy() < direction().dx() && direction().dz() < direction().dx())
*this = Segment3D(minX,maxX);
else
*this = Segment3D(minZ,maxZ);
}
void Segment3D::mergeWithParallel(const Segment3D& otherLine)
{
// if(source().x() > target().x() && otherLine.source().x() > otherLine.target().x())
// {
Segment3D p(Point3D((otherLine.source().x() + source().x())/2,(otherLine.source().y() + source().y())/2,(otherLine.source().z() + source().z())/2),
Point3D((otherLine.target().x() + target().x())/2,(otherLine.target().y() + target().y())/2,(otherLine.target().z() + target().z())/2));
*this = p;
// }
// else
// {
// Segment3D p(Point3D((otherLine.target().x() + source().x())/2,(otherLine.target().y() + source().y())/2,(otherLine.target().z() + source().z())/2),
// Point3D((otherLine.source().x() + target().x())/2,(otherLine.source().y() + target().y())/2,(otherLine.source().z() + target().z())/2));
// *this = p;
// }
}
bool Segment3D::Intersection(const Segment3D &otherLine,const Plane3D& pl, Point3D& intersection, Point2D& intersection2D,double Threshold)
{
Threshold = Threshold*Threshold;
// ExactLine3D l1(pl.projection(source()),pl.projection(target()));
// ExactLine3D l2(pl.projection(otherLine.source()),pl.projection(otherLine.target()));
Point2D twoD1(pl.projection(source()).x(),pl.projection(source()).y());
Point2D twoD2(pl.projection(target()).x(),pl.projection(target()).y());
Point2D twoD3(pl.projection(otherLine.source()).x(),pl.projection(otherLine.source()).y());
Point2D twoD4(pl.projection(otherLine.target()).x(),pl.projection(otherLine.target()).y());
ExactLine2D l1(twoD1,
twoD2),
l2(twoD3,
twoD4);
CGAL::Object obj = CGAL::intersection(l1,l2);
const Point2D *p2 = CGAL::object_cast<Point2D >(&obj);
if(!p2)
return false;
intersection2D = *p2;
Point3D near1, near2;
if(squared_distance(twoD1,*p2) < squared_distance(twoD2,*p2))
near1 = pl.projection(source());
else
near1 = pl.projection(target());
if(squared_distance(twoD3,*p2) < squared_distance(twoD4,*p2))
near2 = pl.projection(otherLine.source());
else
near2 = pl.projection(otherLine.target());
Point3D p3 = supporting_line().projection(Point3D(p2->x(),p2->y(),(near1.z()+near2.z())/2.0));
double d1 = squared_distance(*this,p3);
double d2 = squared_distance(otherLine,p3);
double p2p11 = squared_distance(source(),p3);
double p2p12 = squared_distance(target(),p3);
double p2p21 = squared_distance(otherLine.source(),p3);
double p2p22 = squared_distance(otherLine.target(),p3);
bool intersectionOnBoth = (d1 < Threshold && d2 < Threshold) || (has_on(p3) && otherLine.has_on(p3));
bool intersectionOnA = d1 < Threshold && (p2p11 < Threshold || p2p12 < Threshold);
bool intersectionOnB = d2 < Threshold && (p2p21 < Threshold || p2p22 < Threshold);
intersection = p3;
// cout << "intersections : " << intersectionOnBoth << intersectionOnA<<intersectionOnB<<endl;
// cout << "int point " << p3 << endl;
// cout << " d2 : " << d2 << " , " << p2p21 << " , " << p2p22 <<endl;
// cout << " source : " << otherLine.source() << " target : " << otherLine.target() << endl;
if (intersectionOnBoth || (intersectionOnA && intersectionOnB))
return true;
else
return false;
}
Segment2D::Segment2D(Point2D p1,Point2D p2):ExactSegment2D( p1, p2)
{
}
Segment2D::Segment2D():ExactSegment2D( Point2D(0,0),Point2D(0,0))
{}
bool Segment2D::isCollinear(const Segment2D &otherLine, double Threshold) const
{
ExactLine2D l = supporting_line();
double d1 = squared_distance(l,otherLine.source());
double d2 = squared_distance(l,otherLine.target());
return sqrt((d1+d2)/2)< Threshold;
}
void Segment2D::mergeWithCollinear(const Segment2D& otherLine)
{
Point2D p[4];
Point2D minX,minY,minZ,maxX,maxY,maxZ;
double minXVal = 1000000000;
double maxXVal = -1000000000;
double minYVal = 1000000000;
double maxYVal = -1000000000;
p[0] = otherLine.source();
p[1] = otherLine.target();
p[2] = this->source();
p[3] = this->target();
for(int i=0;i<4;i++)
{
if(p[i].x() < minXVal)
{
minX = p[i];
minXVal = minX.x();
}
if(p[i].y() < minYVal)
{
minY = p[i];
minYVal = minY.y();
}
if(p[i].x() > maxXVal)
{
maxX = p[i];
maxXVal = maxX.x();
}
if(p[i].y() > maxYVal)
{
maxY = p[i];
maxYVal = maxY.y();
}
}
if(direction().dx() < direction().dy())
*this = Segment2D(minY,maxY);
else
*this = Segment2D(minX,maxX);
}
void Segment2D::mergeWithParallel(const Segment2D& otherLine)
{
// if(source().x() > target().x() && otherLine.source().x() > otherLine.target().x() )
// {
Segment2D p(Point2D((otherLine.source().x() + source().x())/2,(otherLine.source().y() + source().y())/2),
Point2D((otherLine.target().x() + target().x())/2,(otherLine.target().y() + target().y())/2));
*this = p;
// }
// else
// {
// Segment2D p(Point2D((otherLine.target().x() + source().x())/2,(otherLine.target().y() + source().y())/2),
// Point2D((otherLine.source().x() + target().x())/2,(otherLine.source().y() + target().y())/2));
// *this = p;
// }
}