1
/****************************************************************************
3
** This file is part of the LibreCAD project, a 2D CAD program
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
9
** This file may be distributed and/or modified under the terms of the
10
** GNU General Public License version 2 as published by the Free Software
11
** Foundation and appearing in the file gpl-2.0.txt included in the
12
** packaging of this file.
14
** This program is distributed in the hope that it will be useful,
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
** GNU General Public License for more details.
19
** You should have received a copy of the GNU General Public License
20
** along with this program; if not, write to the Free Software
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
** This copyright notice MUST APPEAR in all copies of the script!
25
**********************************************************************/
28
#include "rs_polyline.h"
33
#include "rs_graphicview.h"
34
//#include "rs_modification.h"
35
#include "rs_information.h"
41
RS_Polyline::RS_Polyline(RS_EntityContainer* parent)
42
:RS_EntityContainer(parent) {
51
* @param d Polyline data
53
RS_Polyline::RS_Polyline(RS_EntityContainer* parent,
54
const RS_PolylineData& d)
55
:RS_EntityContainer(parent), data(d) {
66
RS_Polyline::~RS_Polyline() {}
70
* Removes the last vertex of this polyline.
72
void RS_Polyline::removeLastVertex() {
73
RS_Entity* last = lastEntity();
78
if (last->isAtomic()) {
79
data.endpoint = ((RS_AtomicEntity*)last)->getEndpoint();
82
RS_DEBUG->print(RS_Debug::D_WARNING,
83
"RS_Polyline::removeLastVertex: "
84
"polyline contains non-atomic entity");
92
* Adds a vertex from the endpoint of the last segment or
93
* from the startpoint of the first segment to 'v' or
94
* sets the startpoint to the point 'v'.
96
* The very first vertex added with this method is the startpoint.
98
* @param v vertex coordinate to be added
99
* @param bulge The bulge of the arc or 0 for a line segment (see DXF documentation)
100
* @param prepend true: prepend at start instead of append at end
102
* @return Pointer to the entity that was addded or NULL if this
103
* was the first vertex added.
105
RS_Entity* RS_Polyline::addVertex(const RS_Vector& v, double bulge, bool prepend) {
107
RS_Entity* entity=NULL;
108
//static double nextBulge = 0.0;
110
// very first vertex:
111
if (!data.startpoint.valid) {
112
data.startpoint = data.endpoint = v;
116
// consequent vertices:
118
// add entity to the polyline:
119
entity = createVertex(v, nextBulge, prepend);
121
if (prepend==false) {
122
RS_EntityContainer::addEntity(entity);
126
RS_EntityContainer::insertEntity(0, entity);
140
* Appends a vertex list from the endpoint of the last segment
141
* sets the startpoint to the first point if not exist.
143
* The very first vertex added with this method is the startpoint if not exists.
145
* @param vl list of vertexs coordinate to be added
146
* @param Pair are RS_Vector of coord and the bulge of the arc or 0 for a line segment (see DXF documentation)
150
void RS_Polyline::appendVertexs(const QList< QPair<RS_Vector*, double> > vl) {
151
RS_Entity* entity=NULL;
152
//static double nextBulge = 0.0;
153
if (vl.isEmpty()) return;
155
// very first vertex:
156
if (!data.startpoint.valid) {
157
data.startpoint = data.endpoint = *(vl.at(idx).first);
158
nextBulge = vl.at(idx++).second;
161
// consequent vertices:
162
for (; idx< vl.size();idx++){
163
entity = createVertex(*(vl.at(idx).first), nextBulge, false);
164
data.endpoint = entity->getEndpoint();
165
RS_EntityContainer::addEntity(entity);
166
nextBulge = vl.at(idx).second;
174
* Creates a vertex from the endpoint of the last element or
175
* sets the startpoint to the point 'v'.
177
* The very first vertex added is the starting point.
179
* @param v vertex coordinate
180
* @param bulge The bulge of the arc (see DXF documentation)
181
* @param prepend true: Prepend instead of append at end
183
* @return Pointer to the entity that was created or NULL if this
184
* was the first vertex added.
186
RS_Entity* RS_Polyline::createVertex(const RS_Vector& v, double bulge, bool prepend) {
188
RS_Entity* entity=NULL;
190
RS_DEBUG->print("RS_Polyline::createVertex: %f/%f to %f/%f bulge: %f",
191
data.endpoint.x, data.endpoint.y, v.x, v.y, bulge);
193
// create line for the polyline:
194
if (fabs(bulge)<RS_TOLERANCE) {
195
if (prepend==false) {
196
entity = new RS_Line(this, RS_LineData(data.endpoint, v));
199
entity = new RS_Line(this, RS_LineData(v, data.startpoint));
201
entity->setSelected(isSelected());
202
entity->setPen(RS_Pen(RS2::FlagInvalid));
203
entity->setLayer(NULL);
204
//RS_EntityContainer::addEntity(entity);
208
// create arc for the polyline:
210
bool reversed = (bulge<0.0);
211
double alpha = atan(bulge)*4.0;
219
if (prepend==false) {
220
middle = (data.endpoint+v)/2.0;
221
dist = data.endpoint.distanceTo(v)/2.0;
222
angle = data.endpoint.angleTo(v);
225
middle = (data.startpoint+v)/2.0;
226
dist = data.startpoint.distanceTo(v)/2.0;
227
angle = v.angleTo(data.startpoint);
230
// alpha can't be 0.0 at this point
231
radius = fabs(dist / sin(alpha/2.0));
233
double wu = fabs(RS_Math::pow(radius, 2.0) - RS_Math::pow(dist, 2.0));
242
if (fabs(alpha)>M_PI) {
246
center.setPolar(h, angle);
252
if (prepend==false) {
253
a1 = center.angleTo(data.endpoint);
254
a2 = center.angleTo(v);
257
a1 = center.angleTo(v);
258
a2 = center.angleTo(data.startpoint);
261
RS_ArcData d(center, radius,
265
entity = new RS_Arc(this, d);
266
entity->setSelected(isSelected());
267
entity->setPen(RS_Pen(RS2::FlagInvalid));
268
entity->setLayer(NULL);
276
* Ends polyline and adds the last entity if the polyline is closed
278
void RS_Polyline::endPolyline() {
279
RS_DEBUG->print("RS_Polyline::endPolyline");
282
RS_DEBUG->print("RS_Polyline::endPolyline: adding closing entity");
284
// remove old closing entity:
285
if (closingEntity!=NULL) {
286
removeEntity(closingEntity);
289
// add closing entity to the polyline:
290
closingEntity = createVertex(data.startpoint, nextBulge);
291
if (closingEntity!=NULL) {
292
RS_EntityContainer::addEntity(closingEntity);
293
//data.endpoint = data.startpoint;
300
void RS_Polyline::setClosed(bool cl, double bulge) {
302
bool areClosed = isClosed();
306
} else if (areClosed){
313
* @return The bulge of the closing entity.
315
double RS_Polyline::getClosingBulge() {
317
RS_Entity* e = lastEntity();
318
if (e!=NULL && e->rtti()==RS2::EntityArc) {
319
return ((RS_Arc*)e)->getBulge();
328
* Sets the polylines start and endpoint to match the first and last vertex.
330
void RS_Polyline::updateEndpoints() {
331
RS_Entity* e1 = firstEntity();
332
if (e1!=NULL && e1->isAtomic()) {
333
RS_Vector v = ((RS_AtomicEntity*)e1)->getStartpoint();
337
RS_Entity* e2 = lastEntity();
341
if (e2!=NULL && e2->isAtomic()) {
342
RS_Vector v = ((RS_AtomicEntity*)e2)->getEndpoint();
350
* Reimplementation of the addEntity method for a normal container.
351
* This reimplementation deletes the given entity!
353
* To add entities use addVertex() or addSegment() instead.
355
void RS_Polyline::addEntity(RS_Entity* entity) {
356
RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Polyline::addEntity:"
357
" should never be called");
367
* Adds a segment to the polyline.
369
/*void RS_Polyline::addSegment(RS_Entity* entity) {
370
RS_EntityContainer::addEntity(entity);
371
// TODO: reorder and check polyline
376
RS_VectorSolutions RS_Polyline::getRefPoints() {
377
RS_VectorSolutions ret;
379
ret.push_back(data.startpoint);
381
for (RS_Entity* e=firstEntity(RS2::ResolveNone);
383
e = nextEntity(RS2::ResolveNone)) {
385
ret.push_back(((RS_AtomicEntity*)e)->getEndpoint());
389
ret.push_back( data.endpoint);
394
RS_Vector RS_Polyline::getNearestRef(const RS_Vector& coord,
397
return RS_Entity::getNearestRef(coord, dist);
400
RS_Vector RS_Polyline::getNearestSelectedRef(const RS_Vector& coord,
403
return RS_Entity::getNearestSelectedRef(coord, dist);
409
void RS_Polyline::reorder() {
422
* this should handle modifyOffset
423
*@ coord, indicate direction of offset
424
*@ distance of offset
428
bool RS_Polyline::offset(const RS_Vector& coord, const double& distance){
430
//find the nearest one
432
QVector<RS_Vector> intersections(length);
433
if(length>1){//sort the polyline entity start/end point order
436
RS_Entity* en0(entityAt(0));
437
RS_Entity* en1(entityAt(1));
439
RS_Vector vStart(en0->getStartpoint());
440
RS_Vector vEnd(en0->getEndpoint());
441
en1->getNearestEndpoint(vStart,&d0);
442
en1->getNearestEndpoint(vEnd,&d1);
443
if(d0<d1) en0->revertDirection();
444
for(i=1;i<length;en0=en1){
445
//linked to head-tail chain
447
vStart=en1->getStartpoint();
448
vEnd=en1->getEndpoint();
449
en0->getNearestEndpoint(vStart,&d0);
450
en0->getNearestEndpoint(vEnd,&d1);
451
if(d0>d1) en1->revertDirection();
452
intersections[i-1]=(en0->getEndpoint()+en1->getStartpoint())*0.5;
457
RS_Entity* en(getNearestEntity(coord, &dist, RS2::ResolveNone));
458
if(en==NULL) return false;
459
int indexNearest=findEntity(en);
460
// RS_Vector vp(en->getNearestPointOnEntity(coord,false));
461
// RS_Vector direction(en->getTangentDirection(vp));
462
// RS_Vector vp1(-direction.y,direction.x);//normal direction
463
// double a2(vp1.squared());
464
// if(a2<RS_TOLERANCE2) return false;
465
// vp1 *= distance/sqrt(a2);
469
RS_Polyline* pnew= static_cast<RS_Polyline*>(clone());
472
int previousIndex(i);
473
pnew->entityAt(i)->offset(coord,distance);
476
//fixme, this is too ugly
477
for(i=indexNearest-1;i>=0;i--){
478
RS_VectorSolutions sol0=RS_Information::getIntersection(pnew->entityAt(previousIndex),entityAt(i),true);
479
// RS_VectorSolutions sol1;
480
double dmax(RS_TOLERANCE15);
481
RS_Vector trimP(false);
482
for(int j=0;j<sol0.getNumber();j++){
484
double d0( (sol0.get(j) - pnew->entityAt(previousIndex)->getStartpoint()).squared());//potential bug, need to trim better
491
static_cast<RS_AtomicEntity*>(pnew->entityAt(previousIndex))->trimStartpoint(trimP);
492
static_cast<RS_AtomicEntity*>(pnew->entityAt(i))->trimEndpoint(trimP);
493
vp=pnew->entityAt(previousIndex)->getMiddlePoint();
495
vp=pnew->entityAt(previousIndex)->getStartpoint();
496
vp.rotate(entityAt(previousIndex)->getStartpoint(),entityAt(i)->getDirection2()-entityAt(previousIndex)->getDirection1()+M_PI);
498
pnew->entityAt(i)->offset(vp,distance);
502
previousIndex=indexNearest;
503
for(i=indexNearest+1;i<length;i++){
504
RS_VectorSolutions sol0=RS_Information::getIntersection(pnew->entityAt(previousIndex),entityAt(i),true);
505
// RS_VectorSolutions sol1;
506
double dmax(RS_TOLERANCE15);
507
RS_Vector trimP(false);
508
for(int j=0;j<sol0.getNumber();j++){
509
double d0( (sol0.get(j) - pnew->entityAt(previousIndex)->getEndpoint()).squared());//potential bug, need to trim better
516
static_cast<RS_AtomicEntity*>(pnew->entityAt(previousIndex))->trimEndpoint(trimP);
517
static_cast<RS_AtomicEntity*>(pnew->entityAt(i))->trimStartpoint(trimP);
518
vp=pnew->entityAt(previousIndex)->getMiddlePoint();
520
vp=pnew->entityAt(previousIndex)->getEndpoint();
521
vp.rotate(entityAt(previousIndex)->getEndpoint(),entityAt(i)->getDirection1()-entityAt(previousIndex)->getDirection2()+M_PI);
523
pnew->entityAt(i)->offset(vp,distance);
527
//connect and trim RS_Modification m(*container, graphicView);
528
for(i=0;i<length-1;i++){
529
RS_VectorSolutions sol0=RS_Information::getIntersection(pnew->entityAt(i),pnew->entityAt(i+1),true);
530
if(sol0.getNumber()==0) {
531
sol0=RS_Information::getIntersection(pnew->entityAt(i),pnew->entityAt(i+1));
532
// RS_Vector vp0(pnew->entityAt(i)->getEndpoint());
533
// RS_Vector vp1(pnew->entityAt(i+1)->getStartpoint());
534
// double a0(intersections.at(i).angleTo(vp0));
535
// double a1(intersections.at(i).angleTo(vp1));
536
RS_VectorSolutions sol1;
537
for(int j=0;j<sol0.getNumber();j++){
538
if(RS_Math::isAngleBetween(intersections.at(i).angleTo(sol0.get(j)),
539
pnew->entityAt(i)->getDirection2(),
540
pnew->entityAt(i+1)->getDirection1(),
542
sol1.push_back(sol0.get(j));
545
if(sol1.getNumber()==0) continue;
546
RS_Vector trimP(sol1.getClosest(intersections.at(i)));
547
static_cast<RS_AtomicEntity*>(pnew->entityAt(i))->trimEndpoint(trimP);
548
static_cast<RS_AtomicEntity*>(pnew->entityAt(i+1))->trimStartpoint(trimP);
550
RS_Vector trimP(sol0.getClosest(pnew->entityAt(i)->getStartpoint()));
551
static_cast<RS_AtomicEntity*>(pnew->entityAt(i))->trimEndpoint(trimP);
552
static_cast<RS_AtomicEntity*>(pnew->entityAt(i+1))->trimStartpoint(trimP);
563
void RS_Polyline::move(const RS_Vector& offset) {
564
RS_EntityContainer::move(offset);
565
data.startpoint.move(offset);
566
data.endpoint.move(offset);
571
void RS_Polyline::rotate(const RS_Vector& center, const double& angle) {
572
rotate(center, RS_Vector(angle));
576
void RS_Polyline::rotate(const RS_Vector& center, const RS_Vector& angleVector) {
577
RS_EntityContainer::rotate(center, angleVector);
578
data.startpoint.rotate(center, angleVector);
579
data.endpoint.rotate(center, angleVector);
584
void RS_Polyline::scale(const RS_Vector& center, const RS_Vector& factor) {
585
RS_EntityContainer::scale(center, factor);
586
data.startpoint.scale(center, factor);
587
data.endpoint.scale(center, factor);
592
void RS_Polyline::mirror(const RS_Vector& axisPoint1, const RS_Vector& axisPoint2) {
593
RS_EntityContainer::mirror(axisPoint1, axisPoint2);
594
data.startpoint.mirror(axisPoint1, axisPoint2);
595
data.endpoint.mirror(axisPoint1, axisPoint2);
600
void RS_Polyline::moveRef(const RS_Vector& ref, const RS_Vector& offset) {
601
RS_EntityContainer::moveRef(ref, offset);
602
if (ref.distanceTo(data.startpoint)<1.0e-4) {
603
data.startpoint.move(offset);
605
if (ref.distanceTo(data.endpoint)<1.0e-4) {
606
data.endpoint.move(offset);
611
void RS_Polyline::revertDirection() {
612
RS_EntityContainer::revertDirection();
613
RS_Vector tmp = data.startpoint;
614
data.startpoint = data.endpoint;
618
void RS_Polyline::stretch(const RS_Vector& firstCorner,
619
const RS_Vector& secondCorner,
620
const RS_Vector& offset) {
622
if (data.startpoint.isInWindow(firstCorner, secondCorner)) {
623
data.startpoint.move(offset);
625
if (data.endpoint.isInWindow(firstCorner, secondCorner)) {
626
data.endpoint.move(offset);
629
RS_EntityContainer::stretch(firstCorner, secondCorner, offset);
634
* Slightly optimized drawing for polylines.
636
void RS_Polyline::draw(RS_Painter* painter,RS_GraphicView* view, double& /*patternOffset*/) {
642
// draw first entity and set correct pen:
643
RS_Entity* e = firstEntity(RS2::ResolveNone);
644
// We get the pen from the entitycontainer and apply it to the
645
// first line so that subsequent line are draw in the right color
646
//prevent segfault if polyline is empty
648
RS_Pen p=this->getPen(true);
650
double patternOffset=0.;
651
view->drawEntity(painter, e, patternOffset);
653
e = nextEntity(RS2::ResolveNone);
655
view->drawEntityPlain(painter, e, patternOffset);
656
e = nextEntity(RS2::ResolveNone);
657
//RS_DEBUG->print("offset: %f\nlength was: %f", offset, e->getLength());
666
* Dumps the point's data to stdout.
668
std::ostream& operator << (std::ostream& os, const RS_Polyline& l) {
669
os << " Polyline: " << l.getData() << " {\n";
671
os << (RS_EntityContainer&)l;