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