[ed476e] | 1 | /*
|
---|
| 2 | * LineSegmentSet.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Jul 22, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[d2b28f] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[ad011c] | 13 | #include "CodePatterns/MemDebug.hpp"
|
---|
[d2b28f] | 14 |
|
---|
[6c438f] | 15 | #include "LinearAlgebra/LineSegmentSet.hpp"
|
---|
[ed476e] | 16 |
|
---|
[ad011c] | 17 | #include "CodePatterns/Assert.hpp"
|
---|
[6c438f] | 18 | #include "LinearAlgebra/Line.hpp"
|
---|
| 19 | #include "LinearAlgebra/LineSegment.hpp"
|
---|
[ed476e] | 20 |
|
---|
[ad011c] | 21 | #include "CodePatterns/Range.hpp"
|
---|
[ed476e] | 22 |
|
---|
| 23 | #include <algorithm>
|
---|
| 24 | #include <boost/bind.hpp>
|
---|
| 25 |
|
---|
| 26 | using namespace std;
|
---|
| 27 |
|
---|
| 28 | LineSegmentSet::LineSegmentSet(const Line &_line) :
|
---|
| 29 | line(new Line(_line))
|
---|
| 30 | {}
|
---|
| 31 |
|
---|
| 32 | LineSegmentSet::LineSegmentSet(const LineSegmentSet &src)
|
---|
| 33 | {
|
---|
| 34 | line.reset(new Line(*src.line));
|
---|
| 35 | segments=src.segments;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | LineSegmentSet& LineSegmentSet::operator=(const LineSegmentSet &src){
|
---|
| 39 | if(this!=&src){
|
---|
| 40 | lineptr _line(new Line(*src.line));
|
---|
| 41 | segments=src.segments;
|
---|
| 42 | line = _line;
|
---|
| 43 | }
|
---|
| 44 | return *this;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | void LineSegmentSet::insert(const LineSegment &lineSeg){
|
---|
| 48 | ASSERT(*line==lineSeg.getLine(),"Inserted LineSegment was not from the line of the set");
|
---|
| 49 | // we might get messed up sets
|
---|
| 50 | if(lineSeg.hasZeroLength()){
|
---|
| 51 | // nothing to do here... there is practically nothing that is being inserted
|
---|
| 52 | return;
|
---|
| 53 | }
|
---|
| 54 | if(lineSeg.getBegin()>lineSeg.getEnd()){
|
---|
| 55 | // just insert the reversed set
|
---|
| 56 | insert(LineSegment(lineSeg.getEnd(),lineSeg.getBegin()));
|
---|
| 57 | return;
|
---|
| 58 | }
|
---|
| 59 | // get the first overlapping segment
|
---|
| 60 | set_t::iterator begin = find_if(segments.begin(),
|
---|
| 61 | segments.end(),
|
---|
| 62 | boost::bind(&LineSegment::overlaps,lineSeg,_1));
|
---|
| 63 | // see if we have overlapping segments
|
---|
| 64 | if(begin==segments.end()){
|
---|
| 65 | // no overlapping segments... we can just insert and get going
|
---|
| 66 | segments.insert(lineSeg);
|
---|
| 67 | return;
|
---|
| 68 | }
|
---|
| 69 | // get the last overlapping segment
|
---|
| 70 | set_t::iterator last = begin;
|
---|
| 71 | while(last!=segments.end() && lineSeg.overlaps(*last))++last;
|
---|
| 72 | last--;
|
---|
| 73 | // all segments between last and end now overlap and both are valid iterators
|
---|
| 74 | ASSERT(begin!=segments.end() && last!=segments.end(),"ERROR: last or begin are behind end of set");
|
---|
| 75 | // get the endpoints of the section
|
---|
| 76 | LinePoint beginPoint = min(begin->getBegin(),lineSeg.getBegin());
|
---|
| 77 | LinePoint endPoint = max(last->getEnd(),lineSeg.getEnd());
|
---|
| 78 | LineSegment insertSeg(beginPoint,endPoint);
|
---|
| 79 | // delete all overlapping segments
|
---|
| 80 | last++;
|
---|
| 81 | segments.erase(begin,last);
|
---|
| 82 | // insert the new combined segments
|
---|
| 83 | segments.insert(insertSeg);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void LineSegmentSet::erase(const LineSegment &lineSeg){
|
---|
| 87 | ASSERT(*line==lineSeg.getLine(),"Erased LineSegment was not from the line of the set");
|
---|
| 88 | // we might get messed up sets
|
---|
| 89 | if(lineSeg.hasZeroLength()){
|
---|
| 90 | // nothing to do here... there is practically nothing that is being erase
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
| 93 | if(lineSeg.getBegin()>lineSeg.getEnd()){
|
---|
| 94 | // just erase the reversed set
|
---|
| 95 | erase(LineSegment(lineSeg.getEnd(),lineSeg.getBegin()));
|
---|
| 96 | return;
|
---|
| 97 | }
|
---|
| 98 | // get the first overlapping segment
|
---|
| 99 | set_t::iterator begin = find_if(segments.begin(),
|
---|
| 100 | segments.end(),
|
---|
| 101 | boost::bind(&LineSegment::overlaps,lineSeg,_1));
|
---|
| 102 | if(begin==segments.end()){
|
---|
| 103 | // nothing to erase
|
---|
| 104 | return;
|
---|
| 105 | }
|
---|
| 106 | // get the last overlapping segment
|
---|
| 107 | set_t::iterator last = begin;
|
---|
| 108 | while(last!=segments.end() && lineSeg.overlaps(*last))++last;
|
---|
| 109 | last--;
|
---|
| 110 | // at the ends there might be two overlapping lineSegments of which parts remain
|
---|
| 111 | LineSegment beginSegment(begin->getBegin(),lineSeg.getBegin());
|
---|
| 112 | LineSegment endSegment(lineSeg.getEnd(),last->getEnd());
|
---|
| 113 | // erase all elements that have been overlapped
|
---|
| 114 | last++;
|
---|
| 115 | segments.erase(begin,last);
|
---|
| 116 | // insert the two segments if they still have space left
|
---|
| 117 | if(beginSegment.getBegin()<beginSegment.getEnd())
|
---|
| 118 | segments.insert(beginSegment);
|
---|
| 119 | if(endSegment.getBegin()<endSegment.getEnd())
|
---|
| 120 | segments.insert(endSegment);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[d6f395] | 123 | LineSegmentSet::iterator LineSegmentSet::begin(){
|
---|
| 124 | return segments.begin();
|
---|
| 125 | }
|
---|
| 126 | LineSegmentSet::const_iterator LineSegmentSet::begin() const{
|
---|
| 127 | return segments.begin();
|
---|
| 128 | }
|
---|
| 129 | LineSegmentSet::iterator LineSegmentSet::end(){
|
---|
| 130 | return segments.end();
|
---|
| 131 | }
|
---|
| 132 | LineSegmentSet::const_iterator LineSegmentSet::end() const{
|
---|
| 133 | return segments.end();
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[ed476e] | 136 | bool LineSegmentSet::isContained(const Vector &point) const{
|
---|
| 137 | if(!line->isContained(point)){
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 | LinePoint lp = line->getLinePoint(point);
|
---|
| 141 | for(set_t::iterator iter=segments.begin();iter!=segments.end();++iter){
|
---|
| 142 | if(iter->isContained(point)){
|
---|
| 143 | return true;
|
---|
| 144 | }
|
---|
| 145 | if(iter->getBegin()>lp){
|
---|
| 146 | break;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | return false;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | Line LineSegmentSet::getLine(){
|
---|
| 153 | return *line;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | LineSegmentSet merge(const LineSegmentSet &x,const LineSegmentSet &y){
|
---|
| 157 | typedef range<LineSegmentSet::set_t::iterator> itRange;
|
---|
| 158 | ASSERT(*x.line==*y.line,"Lines of merges LineSegmentSets don't match");
|
---|
| 159 | LineSegmentSet res(*x.line);
|
---|
| 160 | // standard merge algorithm with a few modifications to deal with overlapping lines
|
---|
| 161 | itRange small(x.segments.begin(),x.segments.end());
|
---|
| 162 | itRange large(y.segments.begin(),y.segments.end());
|
---|
| 163 | while(small.first!=small.last && large.first != large.last){
|
---|
| 164 | if(large.first->getBegin()<small.first->getBegin()){
|
---|
| 165 | itRange tmp = small;
|
---|
| 166 | small = large;
|
---|
| 167 | large = tmp;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | if(small.first->overlaps(*large.first)){
|
---|
| 171 | // get all overlapping segments from larger part
|
---|
| 172 | LineSegmentSet::set_t::iterator last = large.first;
|
---|
| 173 | while(last!=large.last && small.first->overlaps(*last))++last;
|
---|
| 174 | last--;
|
---|
| 175 | // get the endpoint of the new segment to be inserted
|
---|
| 176 | LinePoint lpEnd = max(small.first->getEnd(),last->getEnd());
|
---|
| 177 | res.segments.insert(LineSegment(small.first->getBegin(),lpEnd));
|
---|
| 178 | large.first=last; ++large.first; // all overlapping segments of the larger part have been dealt with
|
---|
| 179 | }
|
---|
| 180 | else{
|
---|
| 181 | // no overlap, we can just insert
|
---|
| 182 | res.segments.insert(*small.first);
|
---|
| 183 | }
|
---|
| 184 | ++small.first;
|
---|
| 185 | }
|
---|
| 186 | // deal with the rest of the iterator
|
---|
| 187 | // we don't know which one is done, so we just do both
|
---|
| 188 | res.segments.insert(small.first,small.last);
|
---|
| 189 | res.segments.insert(large.first,large.last);
|
---|
| 190 | return res;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | LineSegmentSet intersect(const LineSegmentSet &x, const LineSegmentSet &y){
|
---|
| 194 | typedef range<LineSegmentSet::set_t::iterator> itRange;
|
---|
| 195 | ASSERT(*x.line==*y.line,"Lines of merges LineSegmentSets don't match");
|
---|
| 196 | LineSegmentSet res(*x.line);
|
---|
| 197 | itRange small(x.segments.begin(),x.segments.end());
|
---|
| 198 | itRange large(y.segments.begin(),y.segments.end());
|
---|
| 199 |
|
---|
| 200 | while(small.first!=small.last && large.first != large.last){
|
---|
| 201 | if(large.first->getBegin()<small.first->getBegin()){
|
---|
| 202 | itRange tmp = small;
|
---|
| 203 | small = large;
|
---|
| 204 | large = tmp;
|
---|
| 205 | }
|
---|
| 206 | // add overlapping parts of segments from large
|
---|
| 207 | while(large.first!=large.last && small.first->overlaps(*large.first)){
|
---|
| 208 | LinePoint lpEnd = min(small.first->getEnd(),large.first->getEnd());
|
---|
| 209 | res.segments.insert(LineSegment(large.first->getBegin(),lpEnd));
|
---|
| 210 | ++large.first;
|
---|
| 211 | }
|
---|
| 212 | ++small.first;
|
---|
| 213 | }
|
---|
| 214 | return res;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | LineSegmentSet invert(const LineSegmentSet &x){
|
---|
| 218 | LineSegmentSet res(*x.line);
|
---|
| 219 | LinePoint lpBegin = x.line->negEndpoint();
|
---|
| 220 | for(LineSegmentSet::set_t::iterator iter = x.segments.begin();iter!=x.segments.end();++iter){
|
---|
| 221 | if(lpBegin!=iter->getBegin())
|
---|
| 222 | res.segments.insert(LineSegment(lpBegin,iter->getBegin()));
|
---|
| 223 | // the end of the current scanned segment is the beginning of the next one to insert
|
---|
| 224 | lpBegin = iter->getEnd();
|
---|
| 225 | }
|
---|
| 226 | // we might need to extend to infinity
|
---|
| 227 | if(!lpBegin.isPosInfinity())
|
---|
| 228 | res.segments.insert(LineSegment(lpBegin,x.line->posEndpoint()));
|
---|
| 229 | return res;
|
---|
| 230 | }
|
---|