| 1 | /* | 
|---|
| 2 | * LineSegment.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Jul 22, 2010 | 
|---|
| 5 | *      Author: crueger | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "LinearAlgebra/LineSegment.hpp" | 
|---|
| 9 | #include "Helpers/Assert.hpp" | 
|---|
| 10 |  | 
|---|
| 11 | #include "Line.hpp" | 
|---|
| 12 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 13 |  | 
|---|
| 14 | using namespace std; | 
|---|
| 15 |  | 
|---|
| 16 | LineSegment::LineSegment(const Vector &point1,const Vector &point2){ | 
|---|
| 17 | line.reset(new Line(point1,point1-point2)); | 
|---|
| 18 | begin.reset(new LinePoint(line->getLinePoint(point1))); | 
|---|
| 19 | end.reset(new LinePoint(line->getLinePoint(point2))); | 
|---|
| 20 | } | 
|---|
| 21 |  | 
|---|
| 22 | LineSegment::LineSegment(const LinePoint &lp1,const LinePoint &lp2){ | 
|---|
| 23 | ASSERT(lp1.getLine()==lp2.getLine(),"Cannot make a lineSegment with two different lines"); | 
|---|
| 24 | line.reset(new Line(lp1.getLine())); | 
|---|
| 25 | begin.reset(new LinePoint(lp1)); | 
|---|
| 26 | end.reset(new LinePoint(lp2)); | 
|---|
| 27 | } | 
|---|
| 28 | LineSegment::LineSegment(const LineSegment &src) | 
|---|
| 29 | { | 
|---|
| 30 | line.reset(new Line(*src.line)); | 
|---|
| 31 | begin.reset(new LinePoint(*src.begin)); | 
|---|
| 32 | end.reset(new LinePoint(*src.end)); | 
|---|
| 33 | } | 
|---|
| 34 | LineSegment::~LineSegment(){ | 
|---|
| 35 | } | 
|---|
| 36 | LineSegment &LineSegment::operator=(const LineSegment &src){ | 
|---|
| 37 | if(this!=&src){ | 
|---|
| 38 | lineptr _line(new Line(*src.line)); | 
|---|
| 39 | pointptr _begin(new LinePoint(*src.begin)); | 
|---|
| 40 | pointptr _end(new LinePoint(*src.end)); | 
|---|
| 41 | line = _line; | 
|---|
| 42 | begin = _begin; | 
|---|
| 43 | end = _end; | 
|---|
| 44 | } | 
|---|
| 45 | return *this; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | bool LineSegment::isContained(const Vector &point) const{ | 
|---|
| 49 | if(!line->isContained(point)){ | 
|---|
| 50 | return false; | 
|---|
| 51 | } | 
|---|
| 52 | LinePoint lp = line->getLinePoint(point); | 
|---|
| 53 | return *begin <= lp && lp <= *end; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | bool LineSegment::isContained(const LinePoint &lp) const{ | 
|---|
| 57 | ASSERT(lp.getLine()==*line,"Cannot compare Linepoints from different Lines"); | 
|---|
| 58 | return *begin <= lp && lp <= *end; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | bool LineSegment::overlaps(const LineSegment &lineSeg) const{ | 
|---|
| 62 | ASSERT(*lineSeg.line == *line,"Cannot compare Linesegments from different Lines"); | 
|---|
| 63 | // mutual comparison of endpoints | 
|---|
| 64 | return lineSeg.isContained(*end) || isContained(*lineSeg.end); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | bool LineSegment::hasZeroLength() const{ | 
|---|
| 68 | return *begin==*end; | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | LinePoint LineSegment::getBegin() const{ | 
|---|
| 72 | return *begin; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | LinePoint LineSegment::getEnd() const{ | 
|---|
| 76 | return *end; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | Line LineSegment::getLine() const{ | 
|---|
| 80 | return *line; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | bool operator==(const LineSegment &x,const LineSegment &y){ | 
|---|
| 84 | ASSERT(*x.line==*y.line,"Comparison of Linesegments from different lines"); | 
|---|
| 85 | return *x.begin == *y.begin && *x.end == *y.end; | 
|---|
| 86 | } | 
|---|
| 87 | bool operator<(const LineSegment &x,const LineSegment &y){ | 
|---|
| 88 | ASSERT(*x.line==*y.line,"Comparison of Linesegments from different lines"); | 
|---|
| 89 | return (*x.begin != *y.begin) ? *x.begin<*y.begin:*x.end<*y.end; | 
|---|
| 90 | } | 
|---|