3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 06/01/2010 Added this text. :-)
18 #include "rs_graphicview.h"
20 #include "paintintf.h"
25 RS_Line::RS_Line(RS_EntityContainer * parent, const RS_LineData & d):
26 RS_AtomicEntity(parent), data(d)
38 RS_Entity * RS_Line::clone()
40 RS_Line * l = new RS_Line(*this);
45 void RS_Line::calculateBorders()
47 minV = Vector::minimum(data.startpoint, data.endpoint);
48 maxV = Vector::maximum(data.startpoint, data.endpoint);
51 /** @return RS2::EntityLine */
52 RS2::EntityType RS_Line::rtti() const
54 return RS2::EntityLine;
58 bool RS_Line::isEdge() const
63 /** @return Copy of data that defines the line. */
64 RS_LineData RS_Line::getData() const
69 VectorSolutions RS_Line::getRefPoints()
71 VectorSolutions ret(data.startpoint, data.endpoint);
75 /** @return Start point of the entity */
76 Vector RS_Line::getStartpoint() const
78 return data.startpoint;
81 /** @return End point of the entity */
82 Vector RS_Line::getEndpoint() const
87 /** Sets the startpoint */
88 void RS_Line::setStartpoint(Vector s)
94 /** Sets the endpoint */
95 void RS_Line::setEndpoint(Vector e)
102 * @return Direction 1. The angle at which the line starts at
105 double RS_Line::getDirection1() const
111 * @return Direction 2. The angle at which the line starts at
114 double RS_Line::getDirection2() const
119 Vector RS_Line::getNearestEndpoint(const Vector & coord, double * dist)
122 Vector * nearerPoint;
124 dist1 = data.startpoint.distanceTo(coord);
125 dist2 = data.endpoint.distanceTo(coord);
132 nearerPoint = &data.endpoint;
139 nearerPoint = &data.startpoint;
145 Vector RS_Line::getNearestPointOnEntity(const Vector & coord,
146 bool onEntity, double * dist, RS_Entity ** entity)
151 Vector ae = data.endpoint-data.startpoint;
152 Vector ea = data.startpoint-data.endpoint;
153 Vector ap = coord-data.startpoint;
154 Vector ep = coord-data.endpoint;
156 if (ae.magnitude() < 1.0e-6 || ea.magnitude() < 1.0e-6)
159 *dist = RS_MAXDOUBLE;
161 return Vector(false);
164 // Orthogonal projection from both sides:
165 Vector ba = ae * Vector::dotP(ae, ap) / (ae.magnitude() * ae.magnitude());
166 Vector be = ea * Vector::dotP(ea, ep) / (ea.magnitude() * ea.magnitude());
168 // Check if the projection is within this line:
169 if (onEntity == true && (ba.magnitude() > ae.magnitude() || be.magnitude() > ea.magnitude()))
171 return getNearestEndpoint(coord, dist);
176 *dist = coord.distanceTo(data.startpoint + ba);
178 return data.startpoint + ba;
182 Vector RS_Line::getNearestCenter(const Vector & coord, double * dist)
184 Vector p = (data.startpoint + data.endpoint) / 2.0;
187 *dist = p.distanceTo(coord);
192 Vector RS_Line::getNearestMiddle(const Vector & coord, double * dist)
194 return getNearestCenter(coord, dist);
197 Vector RS_Line::getNearestDist(double distance, const Vector & coord, double * dist)
199 double a1 = getAngle1();
202 dv.setPolar(distance, a1);
204 Vector p1 = data.startpoint + dv;
205 Vector p2 = data.endpoint - dv;
208 Vector * nearerPoint;
210 dist1 = p1.distanceTo(coord);
211 dist2 = p2.distanceTo(coord);
235 Vector RS_Line::getNearestDist(double distance, bool startp)
237 double a1 = getAngle1();
240 dv.setPolar(distance, a1);
244 ret = data.startpoint + dv;
246 ret = data.endpoint - dv;
251 /*Vector RS_Line::getNearestRef(const Vector& coord, double* dist)
255 Vector p1 = getNearestEndpoint(coord, &d1);
256 Vector p2 = getNearestMiddle(coord, &d2);
273 double RS_Line::getDistanceToPoint(const Vector & coord, RS_Entity ** entity,
274 RS2::ResolveLevel /*level*/, double /*solidDist*/)
276 RS_DEBUG->print("RS_Line::getDistanceToPoint");
281 // check endpoints first:
282 double dist = coord.distanceTo(getStartpoint());
286 RS_DEBUG->print("RS_Line::getDistanceToPoint: OK1");
290 dist = coord.distanceTo(getEndpoint());
294 RS_DEBUG->print("RS_Line::getDistanceToPoint: OK2");
299 Vector ae = data.endpoint-data.startpoint;
300 Vector ea = data.startpoint-data.endpoint;
301 Vector ap = coord-data.startpoint;
302 Vector ep = coord-data.endpoint;
304 if (ae.magnitude() < 1.0e-6 || ea.magnitude() < 1.0e-6)
306 RS_DEBUG->print("RS_Line::getDistanceToPoint: OK2a");
310 // Orthogonal projection from both sides:
311 Vector ba = ae * Vector::dotP(ae, ap) / RS_Math::pow(ae.magnitude(), 2);
312 Vector be = ea * Vector::dotP(ea, ep) / RS_Math::pow(ea.magnitude(), 2);
314 // Check if the projection is outside this line:
315 if (ba.magnitude() > ae.magnitude() || be.magnitude() > ea.magnitude())
317 // return distance to endpoint
318 getNearestEndpoint(coord, &dist);
319 RS_DEBUG->print("RS_Line::getDistanceToPoint: OK3");
322 //RS_DEBUG->print("ba: %f", ba.magnitude());
323 //RS_DEBUG->print("ae: %f", ae.magnitude());
325 Vector cp = Vector::crossP(ap, ae);
326 dist = cp.magnitude() / ae.magnitude();
328 RS_DEBUG->print("RS_Line::getDistanceToPoint: OK4");
333 void RS_Line::moveStartpoint(const Vector & pos)
335 data.startpoint = pos;
339 void RS_Line::moveEndpoint(const Vector & pos)
345 RS2::Ending RS_Line::getTrimPoint(const Vector & coord, const Vector & trimPoint)
347 double angEl = getAngle1();
348 double angM = trimPoint.angleTo(coord);
349 double angDif = angEl - angM;
355 angDif = 2 * M_PI - angDif;
357 if (angDif < M_PI / 2.0)
358 return RS2::EndingStart;
360 return RS2::EndingEnd;
363 void RS_Line::reverse()
365 Vector v = data.startpoint;
366 data.startpoint = data.endpoint;
370 /** @return the center point of the line. */
371 Vector RS_Line::getMiddlepoint()
373 return (data.startpoint + data.endpoint) / 2.0;
376 /** Sets the y coordinate of the startpoint */
377 void RS_Line::setStartpointY(double val)
379 data.startpoint.y = val;
383 /** Sets the y coordinate of the endpoint */
384 void RS_Line::setEndpointY(double val)
386 data.endpoint.y = val;
391 * @return The length of the line.
393 double RS_Line::getLength()
395 return data.startpoint.distanceTo(data.endpoint);
399 * @return The angle of the line (from start to endpoint).
401 double RS_Line::getAngle1() const
403 return data.startpoint.angleTo(data.endpoint);
407 * @return The angle of the line (from end to startpoint).
409 double RS_Line::getAngle2() const
411 return data.endpoint.angleTo(data.startpoint);
414 bool RS_Line::hasEndpointsWithinWindow(Vector v1, Vector v2)
416 if (data.startpoint.isInWindow(v1, v2) || data.endpoint.isInWindow(v1, v2))
422 void RS_Line::move(Vector offset)
424 RS_DEBUG->print("RS_Line::move1: sp: %f/%f, ep: %f/%f",
425 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
426 RS_DEBUG->print("RS_Line::move1: offset: %f/%f", offset.x, offset.y);
427 data.startpoint.move(offset);
428 data.endpoint.move(offset);
430 RS_DEBUG->print("RS_Line::move2: sp: %f/%f, ep: %f/%f",
431 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
434 void RS_Line::rotate(Vector center, double angle)
436 RS_DEBUG->print("RS_Line::rotate");
437 RS_DEBUG->print("RS_Line::rotate1: sp: %f/%f, ep: %f/%f",
438 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
439 data.startpoint.rotate(center, angle);
440 data.endpoint.rotate(center, angle);
441 RS_DEBUG->print("RS_Line::rotate2: sp: %f/%f, ep: %f/%f",
442 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
444 RS_DEBUG->print("RS_Line::rotate: OK");
447 void RS_Line::scale(Vector center, Vector factor)
449 RS_DEBUG->print("RS_Line::scale1: sp: %f/%f, ep: %f/%f",
450 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
451 data.startpoint.scale(center, factor);
452 data.endpoint.scale(center, factor);
453 RS_DEBUG->print("RS_Line::scale2: sp: %f/%f, ep: %f/%f",
454 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
458 void RS_Line::mirror(Vector axisPoint1, Vector axisPoint2)
460 data.startpoint.mirror(axisPoint1, axisPoint2);
461 data.endpoint.mirror(axisPoint1, axisPoint2);
466 * Stretches the given range of the entity by the given offset.
468 void RS_Line::stretch(Vector firstCorner, Vector secondCorner, Vector offset)
470 if (getStartpoint().isInWindow(firstCorner, secondCorner))
471 moveStartpoint(getStartpoint() + offset);
473 if (getEndpoint().isInWindow(firstCorner, secondCorner))
474 moveEndpoint(getEndpoint() + offset);
477 void RS_Line::moveRef(const Vector& ref, const Vector& offset)
479 if (ref.distanceTo(data.startpoint)<1.0e-4) {
480 moveStartpoint(data.startpoint+offset);
482 if (ref.distanceTo(data.endpoint)<1.0e-4) {
483 moveEndpoint(data.endpoint+offset);
487 //void RS_Line::draw(RS_Painter * painter, RS_GraphicView * view, double patternOffset)
488 void RS_Line::draw(PaintInterface * painter, RS_GraphicView * view, double patternOffset)
490 if (painter == NULL || view == NULL)
492 //printf("RS_Line::draw(): Bailing out!!! painter=%08X, view=%08X\n", painter, view);
496 double styleFactor = getStyleFactor(view);
498 if (getPen().getLineType() == RS2::SolidLine || isSelected()
499 || view->getDrawingMode() == RS2::ModePreview
500 || styleFactor < 0.0)
502 //printf("RS_Line::draw(): Drawing line...\n");
503 painter->drawLine(view->toGui(getStartpoint()), view->toGui(getEndpoint()));
509 RS_LineTypePattern * pat;
512 pat = &patternSelected;
514 pat = view->getPattern(getPen().getLineType());
516 RS_LineTypePattern * pat = (isSelected() ? &patternSelected : view->getPattern(getPen().getLineType()));
521 //printf("RS_Line::draw(): Pattern == NULL!\n");
522 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Line::draw: Invalid line pattern");
526 //printf("RS_Line::draw(): Drawing a patterned line...(?)\n");
527 // Pen to draw pattern is always solid:
528 RS_Pen pen = painter->getPen();
529 pen.setLineType(RS2::SolidLine);
530 painter->setPen(pen);
536 double length = getLength();
537 double angle = getAngle1();
539 // pattern segment length:
540 double patternSegmentLength = 0.0;
543 Vector * dp = new Vector[pat->num];
545 for (i=0; i<pat->num; ++i)
547 dp[i] = Vector(cos(angle) * fabs(pat->pattern[i] * styleFactor),
548 sin(angle) * fabs(pat->pattern[i] * styleFactor));
550 patternSegmentLength += fabs(pat->pattern[i] * styleFactor);
553 // handle pattern offset:
556 if (patternOffset < 0.0)
557 m = (int)ceil(patternOffset / patternSegmentLength);
559 m = (int)floor(patternOffset / patternSegmentLength);
561 patternOffset -= (m * patternSegmentLength);
562 //if (patternOffset<0.0) {
563 // patternOffset+=patternSegmentLength;
565 //RS_DEBUG->print("pattern. offset: %f", patternOffset);
566 Vector patternOffsetVec;
567 patternOffsetVec.setPolar(patternOffset, angle);
569 double tot = patternOffset;
571 // bool cutStartpoint, cutEndpoint, drop;
572 Vector curP = getStartpoint() + patternOffsetVec;
577 // line segment (otherwise space segment)
578 if (pat->pattern[i] > 0.0)
580 bool cutStartpoint = false;
581 bool cutEndpoint = false;
584 // drop the whole pattern segment line:
585 if ((tot + pat->pattern[i] * styleFactor) < 0.0)
591 // trim startpoint of pattern segment line to line startpoint
593 cutStartpoint = true;
595 // trim endpoint of pattern segment line to line endpoint
596 if ((tot + pat->pattern[i] * styleFactor) > length)
603 Vector p2 = curP + dp[i];
606 p1 = getStartpoint();
611 painter->drawLine(view->toGui(p1), view->toGui(p2));
616 tot += fabs(pat->pattern[i] * styleFactor);
617 //RS_DEBUG->print("pattern. tot: %f", tot);
624 done = (tot > length);
632 * Dumps the point's data to stdout.
634 std::ostream & operator<<(std::ostream & os, const RS_Line & l)
636 os << " Line: " << l.getData() << "\n";