]> Shamusworld >> Repos - architektonas/blob - src/base/rs_line.cpp
Refactoring: Moved RS_GraphicView to GraphicView.
[architektonas] / src / base / rs_line.cpp
1 // rs_line.cpp
2 //
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
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  06/01/2010  Added this text. :-)
13 //
14
15 #include "rs_line.h"
16
17 #include "rs_debug.h"
18 #include "drawing.h"
19 #include "graphicview.h"
20 #include "rs_linetypepattern.h"
21 #include "paintintf.h"
22
23 /**
24  * Constructor.
25  */
26 RS_Line::RS_Line(RS_EntityContainer * parent, const RS_LineData & d):
27         RS_AtomicEntity(parent), data(d)
28 {
29         calculateBorders();
30 }
31
32 /**
33  * Destructor.
34  */
35 RS_Line::~RS_Line()
36 {
37 }
38
39 RS_Entity * RS_Line::clone()
40 {
41         RS_Line * l = new RS_Line(*this);
42         l->initId();
43         return l;
44 }
45
46 void RS_Line::calculateBorders()
47 {
48         minV = Vector::minimum(data.startpoint, data.endpoint);
49         maxV = Vector::maximum(data.startpoint, data.endpoint);
50 }
51
52 /**     @return RS2::EntityLine */
53 RS2::EntityType RS_Line::rtti() const
54 {
55         return RS2::EntityLine;
56 }
57
58 /** @return true */
59 bool RS_Line::isEdge() const
60 {
61         return true;
62 }
63
64 /** @return Copy of data that defines the line. */
65 RS_LineData RS_Line::getData() const
66 {
67         return data;
68 }
69
70 VectorSolutions RS_Line::getRefPoints()
71 {
72         VectorSolutions ret(data.startpoint, data.endpoint);
73         return ret;
74 }
75
76 /** @return Start point of the entity */
77 Vector RS_Line::getStartpoint() const
78 {
79         return data.startpoint;
80 }
81
82 /** @return End point of the entity */
83 Vector RS_Line::getEndpoint() const
84 {
85         return data.endpoint;
86 }
87
88 /** Sets the startpoint */
89 void RS_Line::setStartpoint(Vector s)
90 {
91         data.startpoint = s;
92         calculateBorders();
93 }
94
95 /** Sets the endpoint */
96 void RS_Line::setEndpoint(Vector e)
97 {
98         data.endpoint = e;
99         calculateBorders();
100 }
101
102 /**
103 * @return Direction 1. The angle at which the line starts at
104 * the startpoint.
105 */
106 double RS_Line::getDirection1() const
107 {
108         return getAngle1();
109 }
110
111 /**
112 * @return Direction 2. The angle at which the line starts at
113 * the endpoint.
114 */
115 double RS_Line::getDirection2() const
116 {
117         return getAngle2();
118 }
119
120 Vector RS_Line::getNearestEndpoint(const Vector & coord, double * dist)
121 {
122         double dist1, dist2;
123         Vector * nearerPoint;
124
125         dist1 = data.startpoint.distanceTo(coord);
126         dist2 = data.endpoint.distanceTo(coord);
127
128         if (dist2 < dist1)
129         {
130                 if (dist != NULL)
131                         *dist = dist2;
132
133                 nearerPoint = &data.endpoint;
134         }
135         else
136         {
137                 if (dist != NULL)
138                         *dist = dist1;
139
140                 nearerPoint = &data.startpoint;
141         }
142
143         return *nearerPoint;
144 }
145
146 Vector RS_Line::getNearestPointOnEntity(const Vector & coord,
147         bool onEntity, double * dist, RS_Entity ** entity)
148 {
149         if (entity != NULL)
150                 *entity = this;
151
152         Vector ae = data.endpoint-data.startpoint;
153         Vector ea = data.startpoint-data.endpoint;
154         Vector ap = coord-data.startpoint;
155         Vector ep = coord-data.endpoint;
156
157         if (ae.magnitude() < 1.0e-6 || ea.magnitude() < 1.0e-6)
158         {
159                 if (dist != NULL)
160                         *dist = RS_MAXDOUBLE;
161
162                 return Vector(false);
163         }
164
165         // Orthogonal projection from both sides:
166         Vector ba = ae * Vector::dotP(ae, ap) / (ae.magnitude() * ae.magnitude());
167         Vector be = ea * Vector::dotP(ea, ep) / (ea.magnitude() * ea.magnitude());
168
169         // Check if the projection is within this line:
170         if (onEntity == true && (ba.magnitude() > ae.magnitude() || be.magnitude() > ea.magnitude()))
171         {
172                 return getNearestEndpoint(coord, dist);
173         }
174         else
175         {
176                 if (dist != NULL)
177                         *dist = coord.distanceTo(data.startpoint + ba);
178
179                 return data.startpoint + ba;
180         }
181 }
182
183 Vector RS_Line::getNearestCenter(const Vector & coord, double * dist)
184 {
185         Vector p = (data.startpoint + data.endpoint) / 2.0;
186
187         if (dist != NULL)
188                 *dist = p.distanceTo(coord);
189
190         return p;
191 }
192
193 Vector RS_Line::getNearestMiddle(const Vector & coord, double * dist)
194 {
195         return getNearestCenter(coord, dist);
196 }
197
198 Vector RS_Line::getNearestDist(double distance, const Vector & coord, double * dist)
199 {
200         double a1 = getAngle1();
201
202         Vector dv;
203         dv.setPolar(distance, a1);
204
205         Vector p1 = data.startpoint + dv;
206         Vector p2 = data.endpoint - dv;
207
208         double dist1, dist2;
209         Vector * nearerPoint;
210
211         dist1 = p1.distanceTo(coord);
212         dist2 = p2.distanceTo(coord);
213
214         if (dist2 < dist1)
215         {
216                 if (dist != NULL)
217                 {
218                         *dist = dist2;
219                 }
220
221                 nearerPoint = &p2;
222         }
223         else
224         {
225                 if (dist != NULL)
226                 {
227                         *dist = dist1;
228                 }
229
230                 nearerPoint = &p1;
231         }
232
233         return *nearerPoint;
234 }
235
236 Vector RS_Line::getNearestDist(double distance, bool startp)
237 {
238         double a1 = getAngle1();
239
240         Vector dv;
241         dv.setPolar(distance, a1);
242         Vector ret;
243
244         if (startp)
245                 ret = data.startpoint + dv;
246         else
247                 ret = data.endpoint - dv;
248
249         return ret;
250 }
251
252 /*Vector RS_Line::getNearestRef(const Vector& coord, double* dist)
253 {
254         double d1, d2, d;
255         Vector p;
256         Vector p1 = getNearestEndpoint(coord, &d1);
257         Vector p2 = getNearestMiddle(coord, &d2);
258
259         if (d1<d2) {
260                 d = d1;
261                 p = p1;
262         } else {
263                 d = d2;
264                 p = p2;
265         }
266
267         if (dist!=NULL) {
268                 *dist = d;
269         }
270
271         return p;
272 }*/
273
274 double RS_Line::getDistanceToPoint(const Vector & coord, RS_Entity ** entity,
275         RS2::ResolveLevel /*level*/, double /*solidDist*/)
276 {
277         RS_DEBUG->print("RS_Line::getDistanceToPoint");
278
279         if (entity != NULL)
280                 *entity = this;
281
282         // check endpoints first:
283         double dist = coord.distanceTo(getStartpoint());
284
285         if (dist < 1.0e-4)
286         {
287                 RS_DEBUG->print("RS_Line::getDistanceToPoint: OK1");
288                 return dist;
289         }
290
291         dist = coord.distanceTo(getEndpoint());
292
293         if (dist < 1.0e-4)
294         {
295                 RS_DEBUG->print("RS_Line::getDistanceToPoint: OK2");
296                 return dist;
297         }
298
299         dist = RS_MAXDOUBLE;
300         Vector ae = data.endpoint-data.startpoint;
301         Vector ea = data.startpoint-data.endpoint;
302         Vector ap = coord-data.startpoint;
303         Vector ep = coord-data.endpoint;
304
305         if (ae.magnitude() < 1.0e-6 || ea.magnitude() < 1.0e-6)
306         {
307                 RS_DEBUG->print("RS_Line::getDistanceToPoint: OK2a");
308                 return dist;
309         }
310
311         // Orthogonal projection from both sides:
312         Vector ba = ae * Vector::dotP(ae, ap) / RS_Math::pow(ae.magnitude(), 2);
313         Vector be = ea * Vector::dotP(ea, ep) / RS_Math::pow(ea.magnitude(), 2);
314
315         // Check if the projection is outside this line:
316         if (ba.magnitude() > ae.magnitude() || be.magnitude() > ea.magnitude())
317         {
318                 // return distance to endpoint
319                 getNearestEndpoint(coord, &dist);
320                 RS_DEBUG->print("RS_Line::getDistanceToPoint: OK3");
321                 return dist;
322         }
323         //RS_DEBUG->print("ba: %f", ba.magnitude());
324         //RS_DEBUG->print("ae: %f", ae.magnitude());
325
326         Vector cp = Vector::crossP(ap, ae);
327         dist = cp.magnitude() / ae.magnitude();
328
329         RS_DEBUG->print("RS_Line::getDistanceToPoint: OK4");
330
331         return dist;
332 }
333
334 void RS_Line::moveStartpoint(const Vector & pos)
335 {
336         data.startpoint = pos;
337         calculateBorders();
338 }
339
340 void RS_Line::moveEndpoint(const Vector & pos)
341 {
342         data.endpoint = pos;
343         calculateBorders();
344 }
345
346 RS2::Ending RS_Line::getTrimPoint(const Vector & coord, const Vector & trimPoint)
347 {
348         double angEl = getAngle1();
349         double angM = trimPoint.angleTo(coord);
350         double angDif = angEl - angM;
351
352         if (angDif < 0.0)
353                 angDif *= -1.0;
354
355         if (angDif > M_PI)
356                 angDif = 2 * M_PI - angDif;
357
358         if (angDif < M_PI / 2.0)
359                 return RS2::EndingStart;
360         else
361                 return RS2::EndingEnd;
362 }
363
364 void RS_Line::reverse()
365 {
366         Vector v = data.startpoint;
367         data.startpoint = data.endpoint;
368         data.endpoint = v;
369 }
370
371 /** @return the center point of the line. */
372 Vector RS_Line::getMiddlepoint()
373 {
374         return (data.startpoint + data.endpoint) / 2.0;
375 }
376
377 /** Sets the y coordinate of the startpoint */
378 void RS_Line::setStartpointY(double val)
379 {
380         data.startpoint.y = val;
381         calculateBorders();
382 }
383
384 /** Sets the y coordinate of the endpoint */
385 void RS_Line::setEndpointY(double val)
386 {
387         data.endpoint.y = val;
388         calculateBorders();
389 }
390
391 /**
392  * @return The length of the line.
393  */
394 double RS_Line::getLength()
395 {
396         return data.startpoint.distanceTo(data.endpoint);
397 }
398
399 /**
400  * @return The angle of the line (from start to endpoint).
401  */
402 double RS_Line::getAngle1() const
403 {
404         return data.startpoint.angleTo(data.endpoint);
405 }
406
407 /**
408  * @return The angle of the line (from end to startpoint).
409  */
410 double RS_Line::getAngle2() const
411 {
412         return data.endpoint.angleTo(data.startpoint);
413 }
414
415 bool RS_Line::hasEndpointsWithinWindow(Vector v1, Vector v2)
416 {
417         if (data.startpoint.isInWindow(v1, v2) || data.endpoint.isInWindow(v1, v2))
418                 return true;
419
420         return false;
421 }
422
423 void RS_Line::move(Vector offset)
424 {
425         RS_DEBUG->print("RS_Line::move1: sp: %f/%f, ep: %f/%f",
426                 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
427         RS_DEBUG->print("RS_Line::move1: offset: %f/%f", offset.x, offset.y);
428         data.startpoint.move(offset);
429         data.endpoint.move(offset);
430         calculateBorders();
431         RS_DEBUG->print("RS_Line::move2: sp: %f/%f, ep: %f/%f",
432                 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
433 }
434
435 void RS_Line::rotate(Vector center, double angle)
436 {
437         RS_DEBUG->print("RS_Line::rotate");
438         RS_DEBUG->print("RS_Line::rotate1: sp: %f/%f, ep: %f/%f",
439                 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
440         data.startpoint.rotate(center, angle);
441         data.endpoint.rotate(center, angle);
442         RS_DEBUG->print("RS_Line::rotate2: sp: %f/%f, ep: %f/%f",
443                 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
444         calculateBorders();
445         RS_DEBUG->print("RS_Line::rotate: OK");
446 }
447
448 void RS_Line::scale(Vector center, Vector factor)
449 {
450         RS_DEBUG->print("RS_Line::scale1: sp: %f/%f, ep: %f/%f",
451                 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
452         data.startpoint.scale(center, factor);
453         data.endpoint.scale(center, factor);
454         RS_DEBUG->print("RS_Line::scale2: sp: %f/%f, ep: %f/%f",
455                 data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
456         calculateBorders();
457 }
458
459 void RS_Line::mirror(Vector axisPoint1, Vector axisPoint2)
460 {
461         data.startpoint.mirror(axisPoint1, axisPoint2);
462         data.endpoint.mirror(axisPoint1, axisPoint2);
463         calculateBorders();
464 }
465
466 /**
467  * Stretches the given range of the entity by the given offset.
468  */
469 void RS_Line::stretch(Vector firstCorner, Vector secondCorner, Vector offset)
470 {
471         if (getStartpoint().isInWindow(firstCorner, secondCorner))
472                 moveStartpoint(getStartpoint() + offset);
473
474         if (getEndpoint().isInWindow(firstCorner, secondCorner))
475                 moveEndpoint(getEndpoint() + offset);
476 }
477
478 void RS_Line::moveRef(const Vector& ref, const Vector& offset)
479 {
480     if (ref.distanceTo(data.startpoint)<1.0e-4) {
481         moveStartpoint(data.startpoint+offset);
482     }
483     if (ref.distanceTo(data.endpoint)<1.0e-4) {
484         moveEndpoint(data.endpoint+offset);
485     }
486 }
487
488 void RS_Line::draw(PaintInterface * painter, GraphicView * view, double patternOffset)
489 {
490         if (painter == NULL || view == NULL)
491 //{
492 //printf("RS_Line::draw(): Bailing out!!! painter=%08X, view=%08X\n", painter, view);
493                 return;
494 //}
495
496         double styleFactor = getStyleFactor(view);
497
498         if (getPen().getLineType() == RS2::SolidLine || isSelected()
499                 || view->getDrawingMode() == RS2::ModePreview
500                 || styleFactor < 0.0)
501         {
502 //printf("RS_Line::draw(): Drawing line...\n");
503                 painter->drawLine(view->toGui(getStartpoint()), view->toGui(getEndpoint()));
504                 return;
505         }
506
507         // Pattern:
508 #if 0
509         RS_LineTypePattern * pat;
510
511         if (isSelected())
512                 pat = &patternSelected;
513         else
514                 pat = view->getPattern(getPen().getLineType());
515 #else
516         RS_LineTypePattern * pat = (isSelected() ? &patternSelected : view->getPattern(getPen().getLineType()));
517 #endif
518
519         if (pat == NULL)
520         {
521 //printf("RS_Line::draw(): Pattern == NULL!\n");
522                 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_Line::draw: Invalid line pattern");
523                 return;
524         }
525
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);
531
532         // index counter
533         int i;
534
535         // line data:
536         double length = getLength();
537         double angle = getAngle1();
538
539         // pattern segment length:
540         double patternSegmentLength = 0.0;
541
542         // create pattern:
543         Vector * dp = new Vector[pat->num];
544
545         for (i=0; i<pat->num; ++i)
546         {
547                 dp[i] = Vector(cos(angle) * fabs(pat->pattern[i] * styleFactor),
548                         sin(angle) * fabs(pat->pattern[i] * styleFactor));
549
550                 patternSegmentLength += fabs(pat->pattern[i] * styleFactor);
551         }
552
553         // handle pattern offset:
554         int m;
555
556         if (patternOffset < 0.0)
557                 m = (int)ceil(patternOffset / patternSegmentLength);
558         else
559                 m = (int)floor(patternOffset / patternSegmentLength);
560
561         patternOffset -= (m * patternSegmentLength);
562         //if (patternOffset<0.0) {
563         //      patternOffset+=patternSegmentLength;
564         //}
565         //RS_DEBUG->print("pattern. offset: %f", patternOffset);
566         Vector patternOffsetVec;
567         patternOffsetVec.setPolar(patternOffset, angle);
568
569         double tot = patternOffset;
570         i = 0;
571 //      bool cutStartpoint, cutEndpoint, drop;
572         Vector curP = getStartpoint() + patternOffsetVec;
573         bool done = false;
574
575         do
576         {
577                 // line segment (otherwise space segment)
578                 if (pat->pattern[i] > 0.0)
579                 {
580                         bool cutStartpoint = false;
581                         bool cutEndpoint = false;
582                         bool drop = false;
583
584                         // drop the whole pattern segment line:
585                         if ((tot + pat->pattern[i] * styleFactor) < 0.0)
586                         {
587                                 drop = true;
588                         }
589                         else
590                         {
591                                 // trim startpoint of pattern segment line to line startpoint
592                                 if (tot < 0.0)
593                                         cutStartpoint = true;
594
595                                 // trim endpoint of pattern segment line to line endpoint
596                                 if ((tot + pat->pattern[i] * styleFactor) > length)
597                                         cutEndpoint = true;
598                         }
599
600                         if (!drop)
601                         {
602                                 Vector p1 = curP;
603                                 Vector p2 = curP + dp[i];
604
605                                 if (cutStartpoint)
606                                         p1 = getStartpoint();
607
608                                 if (cutEndpoint)
609                                         p2 = getEndpoint();
610
611                                 painter->drawLine(view->toGui(p1), view->toGui(p2));
612                         }
613                 }
614
615                 curP += dp[i];
616                 tot += fabs(pat->pattern[i] * styleFactor);
617                 //RS_DEBUG->print("pattern. tot: %f", tot);
618
619                 i++;
620
621                 if (i >= pat->num)
622                         i = 0;
623
624                 done = (tot > length);
625         }
626         while (!done);
627
628         delete[] dp;
629 }
630
631 /**
632  * Dumps the point's data to stdout.
633  */
634 std::ostream & operator<<(std::ostream & os, const RS_Line & l)
635 {
636         os << " Line: " << l.getData() << "\n";
637         return os;
638 }