OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimLine.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: See top level LICENSE.txt file.
5 //
6 // Author: Garrett Potts
7 //*******************************************************************
8 // $Id: ossimLine.cpp 9094 2006-06-13 19:12:40Z dburken $
9 #include <ossim/base/ossimLine.h>
10 #include <ossim/base/ossimDrect.h>
11 #include <iostream>
12 #include <algorithm>
13 using namespace std;
14 
16 {
17  return out << "line: " << rhs.theP1 << "," << rhs.theP2 << endl;
18 }
19 
21 {
22  ossimDpt result;
23  ossimDpt p3 = line.theP1;
24  ossimDpt p4 = line.theP2;
25 
26  double numerator = ((p4.x-p3.x)*(theP1.y-p3.y) - (p4.y-p3.y)*(theP1.x-p3.x));
27  double denominator = ((p4.y-p3.y)*(theP2.x-theP1.x) - (p4.x-p3.x)*(theP2.y-theP1.y));
28  result.makeNan();
29  // as long as the lines are not parallel
30  if(fabs(denominator) > FLT_EPSILON)
31  {
32  double ua = numerator/ denominator;
33  result = ossimDpt(theP1.x + ua*(theP2.x-theP1.x),
34  theP1.y + ua*(theP2.y-theP1.y));
35  }
36 
37  return result;
38 }
39 
41 {
42  ossimDpt result;
43  ossimDpt p3 = line.theP1;
44  ossimDpt p4 = line.theP2;
45 
46  double numerator = ((p4.x-p3.x)*(theP1.y-p3.y) - (p4.y-p3.y)*(theP1.x-p3.x));
47  double denominator = ((p4.y-p3.y)*(theP2.x-theP1.x) - (p4.x-p3.x)*(theP2.y-theP1.y));
48  result.makeNan();
49  // as long as the lines are not parallel
50  if(fabs(denominator) > FLT_EPSILON)
51  {
52  double ua = numerator/ denominator;
53 
54  if((ua >= -FLT_EPSILON) && (ua <= (1.0 + FLT_EPSILON)))
55  {
56  ossimDpt test = ossimDpt(theP1.x + ua*(theP2.x-theP1.x),
57  theP1.y + ua*(theP2.y-theP1.y));
58  ossimDpt midPoint = line.theP1 + (line.theP2-line.theP1)*.5;
59  if( ((test-midPoint).length()/line.length()) <= (.5+FLT_EPSILON))
60  {
61  result = test;
62  }
63  }
64  }
65 
66  return result;
67 }
68 
69 double ossimLine::length()const
70 {
71  return (theP2-theP1).length();
72 }
73 
75 {
76  return (theP1 + (theP2-theP1)*.5);
77 }
78 
79 bool ossimLine::isPointWithin(const ossimDpt& point, double delta)const
80 {
81  if(isPointOnInfiniteLine(point, delta))
82  {
83  double minx = std::min(theP1.x, theP2.x);
84  double miny = std::min(theP1.y, theP2.y);
85  double maxx = std::max(theP1.x, theP2.x);
86  double maxy = std::max(theP1.y, theP2.y);
87 
88  ossimDrect rect(minx - delta,
89  miny - delta,
90  maxx + delta,
91  maxy + delta);
92 
93  return rect.pointWithin(point);
94  }
95 
96  return false;
97 }
98 
99 bool ossimLine::isPointOnInfiniteLine(const ossimDpt& point, double delta)const
100 {
101  if((point == theP1) || (point == theP2))
102  {
103  return true;
104  }
105  else if(fabs(theP1.x-theP2.x) <= FLT_EPSILON)
106  {
107  return (fabs(point.x - theP1.x) <= delta);
108  }
109  else if(fabs(theP1.y-theP2.y) <= FLT_EPSILON)
110  {
111  return (fabs(point.y - theP1.y) <= delta);
112  }
113  else
114  {
115  ossimDpt v1 = getVector();
116  v1 = v1 * (1.0/v1.length());
117  ossimDpt v2 = (point - theP1);
118  double s = v1.x*v2.x + v1.y*v2.y;
119  ossimDpt p = theP1 + v1*s;
120  double len = (point-p).length();
121 
122  if(len < delta)
123  {
124  return true;
125  }
126  else
127  {
128  return false;
129  }
130  }
131 
132  return false;
133 }
134 
136 {
137  ossimDpt delta = getVector();
138  ossimDpt result;
139  if(fabs(delta.x) <= FLT_EPSILON)
140  {
141  result.y = 0.0;
142  result.x = -delta.y;
143  }
144  else if(fabs(delta.y) <= FLT_EPSILON)
145  {
146  result.x = 0.0;
147  result.y = delta.x;
148  }
149  else
150  {
151  result.x = -delta.y;
152  result.y = delta.x;
153  }
154  double len = result.length();
155 
156  if(len >FLT_EPSILON)
157  {
158  result = result*(1.0/len);
159  }
160 
161  return result;
162 }
ossimDpt theP1
Definition: ossimLine.h:77
bool pointWithin(const ossimDpt &pt, double epsilon=0.0) const
Definition: ossimDrect.h:781
double y
Definition: ossimDpt.h:165
double length() const
Definition: ossimDpt.h:81
ossimDpt intersectSegment(const ossimLine &line) const
Definition: ossimLine.cpp:40
ossimDpt intersectInfinite(const ossimLine &line) const
Definition: ossimLine.cpp:20
ossimDpt normal() const
Definition: ossimLine.cpp:135
#define FLT_EPSILON
ossimDpt theP2
Definition: ossimLine.h:78
ossimDpt midPoint() const
Definition: ossimLine.cpp:74
bool isPointWithin(const ossimDpt &point, double delta=FLT_EPSILON) const
Definition: ossimLine.cpp:79
std::ostream & operator<<(std::ostream &out, const ossimLine &rhs)
Definition: ossimLine.cpp:15
bool isPointOnInfiniteLine(const ossimDpt &point, double delta=FLT_EPSILON) const
Definition: ossimLine.cpp:99
#define max(a, b)
Definition: auxiliary.h:76
double x
Definition: ossimDpt.h:164
double length() const
Definition: ossimLine.cpp:69
void makeNan()
Definition: ossimDpt.h:65
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
#define min(a, b)
Definition: auxiliary.h:75