File size: 7,712 Bytes
a5ffdcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/


#include "rs_constructionline.h"

#include "lc_quadratic.h"
#include "rs_debug.h"
#include "rs_math.h"
#include "rs_painter.h"


RS_ConstructionLineData::RS_ConstructionLineData(const RS_Vector& point1,
						const RS_Vector& point2):
	point1(point1)
	,point2(point2)
{
}

std::ostream& operator << (std::ostream& os,
								  const RS_ConstructionLineData& ld)
{
	os << "(" << ld.point1 <<
	"/" << ld.point2 <<
	")";
	return os;
}

/**
 * Constructor.
 */
RS_ConstructionLine::RS_ConstructionLine(RS_EntityContainer* parent,
        const RS_ConstructionLineData& d)
        :RS_AtomicEntity(parent), data(d) {

    calculateBorders();
}

RS_ConstructionLine::RS_ConstructionLine(const RS_Vector& point1, const RS_Vector& point2)
    :RS_AtomicEntity(nullptr), data(point1, point2){
}

RS_Entity* RS_ConstructionLine::clone() const {
    RS_ConstructionLine* c = new RS_ConstructionLine(*this);
    return c;
}

void RS_ConstructionLine::calculateBorders() {
    minV = RS_Vector::minimum(data.point1, data.point2);
    maxV = RS_Vector::maximum(data.point1, data.point2);
}

RS_Vector RS_ConstructionLine::getNearestEndpoint(const RS_Vector& coord,
        double* dist) const{

    const double dist1 = (data.point1-coord).squared();
    const double dist2 = (data.point2-coord).squared();

    if (dist2<dist1) {
		if (dist) {
            *dist = sqrt(dist2);
        }
        return data.point2;
    } else {
		if (dist) {
            *dist = sqrt(dist1);
        }
        return data.point1;
    }
}

RS_Vector RS_ConstructionLine::getNearestPointOnEntity(const RS_Vector& coord,
        bool /*onEntity*/, double* /*dist*/, RS_Entity** entity) const{

	if (entity) {
        *entity = const_cast<RS_ConstructionLine*>(this);
    }

    RS_Vector ae = data.point2-data.point1;
    RS_Vector ea = data.point1-data.point2;
    RS_Vector ap = coord-data.point1;
//    RS_Vector ep = coord-data.point2;

        if (ae.magnitude()<1.0e-6 || ea.magnitude()<1.0e-6) {
                return RS_Vector(false);
        }

    // Orthogonal projection from both sides:
    RS_Vector ba = ae * RS_Vector::dotP(ae, ap)
                   / (ae.magnitude()*ae.magnitude());
//    RS_Vector be = ea * RS_Vector::dotP(ea, ep)
//                   / (ea.magnitude()*ea.magnitude());

    return data.point1+ba;
}

RS_Vector RS_ConstructionLine::getNearestCenter(const RS_Vector& /*coord*/,
		double* dist) const{

	if (dist) {
        *dist = RS_MAXDOUBLE;
    }

    return RS_Vector(false);
}

/** @return Copy of data that defines the line. */
RS_ConstructionLineData const& RS_ConstructionLine::getData() const {
	return data;
}

/** @return First definition point. */
RS_Vector const& RS_ConstructionLine::getPoint1() const {
	return data.point1;
}
/** @return Second definition point. */
RS_Vector const& RS_ConstructionLine::getPoint2() const {
	return data.point2;
}

/** @return Start point of the entity */
RS_Vector RS_ConstructionLine::getStartpoint() const
{
    return data.point1;
}

/** @return End point of the entity */
RS_Vector RS_ConstructionLine::getEndpoint() const
{
    return data.point2;
}

/**
 * @return Direction 1. The angle at which the arc starts at
 * the startpoint.
 */
double RS_ConstructionLine::getDirection1(void) const
{
    return RS_Math::correctAngle( data.point1.angleTo( data.point2));
}

/**
 * @return Direction 2. The angle at which the arc starts at
 * the endpoint.
 */
double RS_ConstructionLine::getDirection2(void) const
{
    return RS_Math::correctAngle( data.point2.angleTo( data.point1));
}

/** return the equation of the entity
for quadratic,

return a vector contains:
m0 x^2 + m1 xy + m2 y^2 + m3 x + m4 y + m5 =0

for linear:
m0 x + m1 y + m2 =0
**/
LC_Quadratic RS_ConstructionLine::getQuadratic() const
{
    std::vector<double> ce(3,0.);
	auto dvp=data.point2 - data.point1;
    RS_Vector normal(-dvp.y,dvp.x);
    ce[0]=normal.x;
    ce[1]=normal.y;
    ce[2]= -normal.dotP(data.point2);
    return LC_Quadratic(ce);
}

RS_Vector RS_ConstructionLine::getMiddlePoint() const{
    return RS_Vector(false);
}
RS_Vector RS_ConstructionLine::getNearestMiddle(const RS_Vector& /*coord*/,
        double* dist, const int /*middlePoints*/)const {
	   if (dist) {
        *dist = RS_MAXDOUBLE;
    }
    return RS_Vector(false);
}

RS_Vector RS_ConstructionLine::getNearestDist(double /*distance*/,
        const RS_Vector& /*coord*/,
		double* dist) const{
	if (dist) {
        *dist = RS_MAXDOUBLE;
    }
    return RS_Vector(false);
}


double RS_ConstructionLine::getDistanceToPoint(const RS_Vector& coord,
        RS_Entity** entity,
        RS2::ResolveLevel /*level*/, double /*solidDist*/) const {

    RS_DEBUG->print("RS_ConstructionLine::getDistanceToPoint");

	if (entity) {
        *entity = const_cast<RS_ConstructionLine*>(this);
    }
    //double dist = RS_MAXDOUBLE;
    RS_Vector se = data.point2-data.point1;
    double d(se.magnitude());
    if(d<RS_TOLERANCE) {
        //line too short
        return RS_MAXDOUBLE;
    }
    se.set( se.x/d,-se.y/d); //normalized
    RS_Vector vpc= coord - data.point1;
    vpc.rotate(se); // rotate to use the line as x-axis, and the distance is fabs(y)
    return ( fabs(vpc.y) );
}

void RS_ConstructionLine::move(const RS_Vector& offset) {
    data.point1.move(offset);
    data.point2.move(offset);
    //calculateBorders();
}

void RS_ConstructionLine::rotate(const RS_Vector& center, double angle) {
    RS_Vector angleVector(angle);
    data.point1.rotate(center, angleVector);
    data.point2.rotate(center, angleVector);
    //calculateBorders();
}

void RS_ConstructionLine::rotate(const RS_Vector& center, const RS_Vector& angleVector) {
    data.point1.rotate(center, angleVector);
    data.point2.rotate(center, angleVector);
    //calculateBorders();
}

void RS_ConstructionLine::scale(const RS_Vector& center, const RS_Vector& factor) {
    data.point1.scale(center, factor);
    data.point2.scale(center, factor);
    //calculateBorders();
}

void RS_ConstructionLine::mirror(const RS_Vector& axisPoint1, const RS_Vector& axisPoint2) {
    data.point1.mirror(axisPoint1, axisPoint2);
    data.point2.mirror(axisPoint1, axisPoint2);
}

RS_Entity& RS_ConstructionLine::shear(double k){
    data.point1.shear(k);
    data.point2.shear(k);
    return *this;
}

void RS_ConstructionLine::draw(RS_Painter *painter) {
    painter->drawInfiniteWCS(data.point1, data.point2);
}

/**
 * Dumps the point's data to stdout.
 */
std::ostream& operator << (std::ostream& os, const RS_ConstructionLine& l) {
    os << " ConstructionLine: " << l.getData() << "\n";
    return os;
}