vgl_infinite_line_3d.hxx
Go to the documentation of this file.
1 // This is core/vgl/vgl_infinite_line_3d.hxx
2 #ifndef vgl_infinite_line_3d_hxx_
3 #define vgl_infinite_line_3d_hxx_
4 
5 #include <iostream>
6 #include <cmath>
7 #include <string>
8 #include "vgl_infinite_line_3d.h"
9 #include <cassert>
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 template <class Type>
15  vgl_point_3d<Type> const& p2)
16 {
17  vgl_vector_3d<Type> dir = p2-p1;
19  x0_ = l.x0();
20  t_ = dir;
21 }
22 
23 template <class Type>
26 {
27  // define the plane coordinate system (u, v)
28  // v is given by the cross product of t with x, unless t is nearly
29  // parallel to x, in which case v is given by z X t.
30  vgl_vector_3d<Type> x(Type(1), Type(0), Type(0));
31  v = cross_product(t_,x);
32  Type vmag = static_cast<Type>(v.length());
33  double vmagd = static_cast<double>(vmag);
34  if (vmagd < 1.0e-8) {
35  vgl_vector_3d<Type> z(Type(0), Type(0), Type(1));
36  v = cross_product(z, t_);
37  vmag = static_cast<Type>(v.length());
38  assert(vmag>Type(0));
39  v/=vmag;
40  }
41  else v/=vmag;
42  // The other plane coordinate vector is perpendicular to both t and v
43  u = cross_product(v,t_);
44  Type umag = static_cast<Type>(u.length());
45  u/=umag;
46 }
47 
48 template <class Type>
51  vgl_vector_3d<Type> const& dir)
52 {
53  // reconcile direction so that tangent is in the positive hemisphere
54  double ttx = std::fabs(static_cast<double>(dir.x()));
55  double tty = std::fabs(static_cast<double>(dir.y()));
56  double ttz = std::fabs(static_cast<double>(dir.z()));
57  double max_comp = ttx;
58  double sign = static_cast<double>(dir.x());
59  if (max_comp < tty) {
60  max_comp = tty;
61  sign = static_cast<double>(dir.y());
62  }
63  if (max_comp < ttz) {
64  max_comp = ttz;
65  sign = static_cast<double>(dir.z());
66  }
67  // switch sense if max component is negative
68  Type sense = static_cast<Type>(sign/max_comp);
69  t_ = normalized(dir*sense);
70  // Define the plane perpendicular to the line passing through the origin
71  // the plane normal is t_ the distance of the plane from the origin is 0
72  // it follows that the intersection of the line with the perpendicular plane
73  // is as follows:
74  Type mag = static_cast<Type>(t_.length());
75  assert(mag>Type(0));
76  vgl_vector_3d<Type> pv(p.x(), p.y(), p.z());
77  Type dp = dot_product(pv, t_);
78  Type k = -dp/(mag*mag);
79  // The intersection point
80  vgl_vector_3d<Type> p0 = pv + k*t_, u, v;
81  this->compute_uv_vectors(u, v);
82  // The location of the intersection point in plane coordinates can now be computed
83  Type u0 = dot_product(u, p0), v0 = dot_product(v, p0);
84  x0_.set(u0, v0);
85 }
86 
87 // the point on the line closest to the origin
88 template <class Type>
90 {
91  // u,v plane coordinate vectors
92  vgl_vector_3d<Type> u, v, pv;
93  this->compute_uv_vectors(u, v);
94  pv = x0_.x()*u + x0_.y()*v;
95  return vgl_point_3d<Type>(pv.x(), pv.y(), pv.z());
96 }
97 
98 template <class Type>
100 {
101  vgl_point_3d<Type> point1 = this->point();
102  vgl_point_3d<Type> point2 = this->point_t(Type(1));
103  double seg = 1.0;
104  double len1 = static_cast<double>((point1 - p).length());
105  double len2 = static_cast<double>((point2 - p).length());
106  // two cases: point inside (point1, point2) segment;
107  // point outside (point1, point2) segment
108  double r = seg -(len1 + len2);
109  if (len1>seg||len2>seg)
110  r = seg - std::fabs(len1-len2);
111  return r < 1e-8 && r > -1e-8;
112 }
113 
114 // stream operators
115 template <class Type>
116 std::ostream& operator<<(std::ostream& s, vgl_infinite_line_3d<Type> const & p)
117 {
118  return s << "<vgl_infinite_line_3d: origin " << p.x0() << " dir " << p.direction() << " >";
119 }
120 
121 template <class Type>
122 std::istream& operator>>(std::istream& s, vgl_infinite_line_3d<Type>& p)
123 {
124  s >> std::ws; // skip leading whitespace
125  std::string temp;
126  s >> temp >> temp; // skip "<vgl_infinite_line_3d: origin"
127  s >> std::ws; // skip whitespace
129  s >> x_0;
130  s >> std::ws; // skip whitespace
131  s >> temp;// skip "dir "
132  s >> std::ws; // skip whitespace
134  s >> dir >> std::ws >> temp; // trailing " >"
135  p.set(x_0, dir);
136  return s;
137 }
138 
139 #undef VGL_INFINITE_LINE_3D_INSTANTIATE
140 #define VGL_INFINITE_LINE_3D_INSTANTIATE(Type) \
141 template class vgl_infinite_line_3d<Type >;\
142 template std::istream& operator>>(std::istream&, vgl_infinite_line_3d<Type >&);\
143 template std::ostream& operator<<(std::ostream&, vgl_infinite_line_3d<Type > const&)
144 
145 #endif // vgl_infinite_line_3d_hxx_
v normalized(v const &a)
Return a normalised version of a.
T dot_product(v const &a, v const &b)
dot product or inner product of two vectors.
vgl_vector_2d< Type > x0() const
Accessors.
vgl_vector_3d< Type > direction() const
Represents a cartesian 3D point.
Definition: vgl_fwd.h:11
bool contains(const vgl_point_3d< Type > &p) const
Check if point p is on the line.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
double length(v const &a)
Return the length of a vector.
Definition: vgl_vector_2d.h:94
Type & z()
Definition: vgl_point_3d.h:73
#define v
Definition: vgl_vector_2d.h:74
double length() const
Return the length of this vector.
T cross_product(v const &a, v const &b)
cross product of two vectors (area of enclosed parallellogram).
T y() const
Definition: vgl_vector_3d.h:37
void set(vgl_vector_2d< Type > const &x_0, vgl_vector_3d< Type > const &direction)
Assignment.
T x() const
Definition: vgl_vector_3d.h:36
T z() const
Definition: vgl_vector_3d.h:38
Type & x()
Definition: vgl_point_3d.h:71
Represents a 3-d line with position defined in the orthogonal plane passing through the origin.
Definition: vgl_fwd.h:20
vgl_infinite_line_3d()=default
Default constructor - does not initialise!.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
#define l
A 3-d infinite line with position parameterized by orthogonal plane coordinates.
void compute_uv_vectors(vgl_vector_3d< Type > &u, vgl_vector_3d< Type > &v) const
The unit vectors perpendicular to the line direction.
vgl_point_3d< Type > point() const
Return the point on the line closest to the origin.
Type & y()
Definition: vgl_point_3d.h:72