-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHLine.cpp
More file actions
194 lines (165 loc) · 4.43 KB
/
HLine.cpp
File metadata and controls
194 lines (165 loc) · 4.43 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
// HLine.cpp
// Copyright (c) 2009, Dan Heeks
// This program is released under the BSD license. See the file COPYING for details.
#include "stdafx.h"
#include "HLine.h"
#include "HILine.h"
#include "HCircle.h"
#include "HArc.h"
#include "Sketch.h"
#include "HPoint.h"
HLine::HLine(const HLine &line):EndedObject(){
operator=(line);
}
HLine::HLine(const gp_Pnt &a, const gp_Pnt &b, const HeeksColor* col):EndedObject(){
A = a;
B = b;
SetColor(*col);
}
HLine::~HLine(){
}
const HLine& HLine::operator=(const HLine &b){
EndedObject::operator=(b);
return *this;
}
HeeksObj *HLine::MakeACopy(void)const{
HLine *new_object = new HLine(*this);
return new_object;
}
bool HLine::GetMidPoint(double* pos)
{
extract((A.XYZ() + B.XYZ()) / 2, pos);
return true;
}
void HLine::GetBox(CBox &box){
box.Insert(A.X(), A.Y(), A.Z());
box.Insert(B.X(), B.Y(), B.Z());
}
bool HLine::FindNearPoint(const double* ray_start, const double* ray_direction, double *point){
// The OpenCascade libraries throw an exception when one tries to
// create a gp_Lin() object using a vector that doesn't point
// anywhere. If this is a zero-length line then we're in
// trouble. Don't bother with it.
if ((A.X() == B.X()) &&
(A.Y() == B.Y()) &&
(A.Z() == B.Z())) return(false);
gp_Lin ray(make_point(ray_start), make_vector(ray_direction));
gp_Pnt p1, p2;
ClosestPointsOnLines(GetLine(), ray, p1, p2);
if(!Intersects(p1))
return false;
extract(p1, point);
return true;
}
bool HLine::FindPossTangentPoint(const double* ray_start, const double* ray_direction, double *point){
// any point on this line is a possible tangent point
return FindNearPoint(ray_start, ray_direction, point);
}
gp_Lin HLine::GetLine()const{
gp_Vec v(A, B);
return gp_Lin(A, v);
}
int HLine::Intersects(const HeeksObj *object, std::list< double > *rl)const{
int numi = 0;
switch(object->GetType())
{
case SketchType:
return( ((CSketch *)object)->Intersects( this, rl ));
case LineType:
{
// The OpenCascade libraries throw an exception when one tries to
// create a gp_Lin() object using a vector that doesn't point
// anywhere. If this is a zero-length line then we're in
// trouble. Don't bother with it.
if ((A.X() == B.X()) &&
(A.Y() == B.Y()) &&
(A.Z() == B.Z())) break;
gp_Pnt pnt;
if(intersect(GetLine(), ((HLine*)object)->GetLine(), pnt))
{
if(Intersects(pnt) && ((HLine*)object)->Intersects(pnt)){
if(rl)add_pnt_to_doubles(pnt, *rl);
numi++;
}
}
}
break;
case ILineType:
{
gp_Pnt pnt;
if(intersect(GetLine(), ((HILine*)object)->GetLine(), pnt))
{
if(Intersects(pnt)){
if(rl)add_pnt_to_doubles(pnt, *rl);
numi++;
}
}
}
break;
case ArcType:
{
std::list<gp_Pnt> plist;
intersect(GetLine(), ((HArc*)object)->GetCircle(), plist);
for(std::list<gp_Pnt>::iterator It = plist.begin(); It != plist.end(); It++)
{
gp_Pnt& pnt = *It;
if(Intersects(pnt) && ((HArc*)object)->Intersects(pnt))
{
if(rl)add_pnt_to_doubles(pnt, *rl);
numi++;
}
}
}
break;
case CircleType:
{
std::list<gp_Pnt> plist;
intersect(GetLine(), ((HCircle*)object)->GetCircle(), plist);
for(std::list<gp_Pnt>::iterator It = plist.begin(); It != plist.end(); It++)
{
gp_Pnt& pnt = *It;
if(Intersects(pnt))
{
if(rl)add_pnt_to_doubles(pnt, *rl);
numi++;
}
}
}
break;
}
return numi;
}
bool HLine::Intersects(const gp_Pnt &pnt)const
{
gp_Lin this_line = GetLine();
if(!intersect(pnt, this_line))return false;
// check it lies between A and B
gp_Vec v = this_line.Direction();
double dpA = gp_Vec(A.XYZ()) * v;
double dpB = gp_Vec(B.XYZ()) * v;
double dp = gp_Vec(pnt.XYZ()) * v;
return dp >= dpA - theApp.m_geom_tol && dp <= dpB + theApp.m_geom_tol;
}
void HLine::GetSegments(void(*callbackfunc)(const double *p), double pixels_per_mm, bool want_start_point)const{
if(want_start_point)
{
double p[3];
extract(A, p);
(*callbackfunc)(p);
}
double p[3];
extract(B, p);
(*callbackfunc)(p);
}
gp_Vec HLine::GetSegmentVector(double fraction)
{
gp_Vec line_vector(A, B);
if(line_vector.Magnitude() < 0.000000001)return gp_Vec(0, 0, 0);
return gp_Vec(A, B).Normalized();
}
void HLine::Reverse()
{
gp_Pnt temp = A;
A = B;
B = temp;
}