2 #ifndef vgl_algo_intersection_hxx_ 3 #define vgl_algo_intersection_hxx_ 18 #include <vnl/vnl_matrix.h> 19 #include <vnl/vnl_vector.h> 20 #include <vnl/algo/vnl_matrix_inverse.h> 21 #include <vnl/algo/vnl_svd.h> 25 # include <vcl_msvc_warnings.h> 31 std::vector<vgl_homg_plane_3d<T> > planes;
32 for (
unsigned i=0; i<p.size(); ++i) {
44 if (planes.size() == 0)
48 vnl_matrix<double> Q(3,3,0.0);
49 vnl_vector<double> vd(3,0.0);
50 unsigned n = planes.size();
51 for (
typename std::list<
vgl_plane_3d<T> >::const_iterator pit = planes.begin();
52 pit != planes.end(); ++pit)
54 double a = (*pit).a(), b = (*pit).b(), c = (*pit).c(),d = (*pit).d();
55 Q[0][0] += a*a; Q[0][1] += a*b; Q[0][2] += a*c;
56 Q[1][1] += b*b; Q[1][2] += b*c;
58 vd[0]-=a*d; vd[1]-=b*d; vd[2]-=c*d;
60 Q[1][0]= Q[0][1]; Q[2][0]= Q[0][2]; Q[2][1]= Q[1][2];
63 vnl_svd<double> svd(Q);
66 vnl_vector<double> t = svd.nullvector();
68 double tx = t[0], ty = t[1], tz = t[2];
69 double atx = std::fabs(tx), aty = std::fabs(ty), atz = std::fabs(tz);
81 double det = Q[1][1]*Q[2][2] - Q[1][2]*Q[2][1];
82 double neuy = vd[1]*Q[2][2] - Q[1][2]*vd[2];
83 double neuz = Q[1][1]*vd[2] - vd[1]*Q[2][1];
84 p0d.
set(0.0, neuy/det, neuz/det);
89 double det = Q[0][0]*Q[2][2] - Q[0][2]*Q[2][0];
90 double neux = vd[0]*Q[2][2] - Q[0][2]*vd[2];
91 double neuz = Q[0][0]*vd[2] - vd[0]*Q[2][0];
92 p0d.
set(neux/det, 0.0, neuz/det);
98 double det = Q[0][0]*Q[1][1] - Q[0][1]*Q[1][0];
99 double neux = vd[0]*Q[1][1] - Q[0][1]*vd[1];
100 double neuy = Q[0][0]*vd[1] - vd[0]*Q[1][0];
101 p0d.
set(neux/det, neuy/det, 0.0);
106 static_cast<T>(p0d.
y()),
107 static_cast<T>(p0d.
z()));
121 if (planes.size() < 2)
124 vnl_matrix<double> Q(3,3,0.0);
125 vnl_vector<double> vd(3,0.0);
128 for (
typename std::list<
vgl_plane_3d<T> >::const_iterator pit = planes.begin();
129 pit != planes.end(); ++pit)
131 double a = (*pit).a(), b = (*pit).b(), c = (*pit).c(), d = (*pit).d();
134 Q[0][1] += w*a*b; Q[1][1] += w*b*b;
135 Q[0][2] += w*a*c; Q[1][2] += w*b*c; Q[2][2] += w*c*c;
136 vd[0]-=w*a*d; vd[1]-=w*b*d; vd[2]-=w*c*d;
139 Q[1][0]= Q[0][1]; Q[2][0]= Q[0][2]; Q[2][1]= Q[1][2];
142 vnl_svd<double> svd(Q);
144 vnl_vector<double> t = svd.nullvector();
145 double tx = t[0], ty = t[1], tz = t[2];
146 double atx = std::fabs(tx), aty = std::fabs(ty), atz = std::fabs(tz);
148 char component =
'x';
149 if (aty>atx&&aty>atz)
151 if (atz>atx&&atz>aty)
158 double det = Q[1][1]*Q[2][2] - Q[1][2]*Q[2][1];
159 double neuy = vd[1]*Q[2][2] - Q[1][2]*vd[2];
160 double neuz = Q[1][1]*vd[2] - vd[1]*Q[2][1];
161 p0d.
set(0.0, neuy/det, neuz/det);
166 double det = Q[0][0]*Q[2][2] - Q[0][2]*Q[2][0];
167 double neux = vd[0]*Q[2][2] - Q[0][2]*vd[2];
168 double neuz = Q[0][0]*vd[2] - vd[0]*Q[2][0];
169 p0d.
set(neux/det, 0.0, neuz/det);
175 double det = Q[0][0]*Q[1][1] - Q[0][1]*Q[1][0];
176 double neux = vd[0]*Q[1][1] - Q[0][1]*vd[1];
177 double neuy = Q[0][0]*vd[1] - vd[0]*Q[1][0];
178 p0d.
set(neux/det, neuy/det, 0.0);
183 static_cast<T>(p0d.
y()),
184 static_cast<T>(p0d.
z()));
192 for (
typename std::list<
vgl_plane_3d<T> >::const_iterator pit = planes.begin();
193 pit != planes.end(); ++pit)
195 double a = pit->normal().x(), b = pit->normal().y(), c = pit->normal().z();
196 residual+=ws[cnt]*ws[cnt]*T(a*tx+b*ty+c*tz)*T(a*tx+b*ty+c*tz);
197 sum_ws+=ws[cnt]*ws[cnt];
204 residual=std::sqrt(residual);
216 assert(poly.size() >= 3);
219 typename std::list<vgl_point_3d<T> >::iterator it=poly.begin();
220 for (; it != poly.end(); ++it)
228 for (it=poly.begin(); it != poly.end(); ++it) {
240 if (!vgl_intersection<T>(b, poly_plane))
251 vnl_matrix<T> M(3,3);
262 vnl_matrix_inverse<T> R(M);
266 for (it=poly.begin(); it != poly.end(); ++it) {
268 vnl_matrix<T> tv(3,1);
269 tv.put(0,0,temp.
x());
270 tv.put(1,0,temp.
y());
271 tv.put(2,0,temp.
z());
272 vnl_matrix<T> pi = R*tv;
273 poly2d.
push_back(pi.get(0,0), pi.get(1,0));
277 vnl_matrix<T> tv(3,1);
278 tv.put(0,0,c.
x()-p0.
x());
279 tv.put(1,0,c.
y()-p0.
y());
280 tv.put(2,0,c.
z()-p0.
z());
281 vnl_matrix<T> ci(R*tv);
282 return poly2d.
contains(ci.get(0,0),ci.get(1,0));
285 #undef VGL_ALGO_INTERSECTION_INSTANTIATE 286 #define VGL_ALGO_INTERSECTION_INSTANTIATE(T) \ 287 template vgl_point_3d<T > vgl_intersection(const std::vector<vgl_plane_3d<T > >&); \ 288 template bool vgl_intersection(vgl_box_3d<T > const&, std::list<vgl_point_3d<T > >&); \ 289 template vgl_infinite_line_3d<T > vgl_intersection(const std::list<vgl_plane_3d<T > >& planes); \ 290 template bool vgl_intersection(const std::list<vgl_plane_3d<T > >& planes, std::vector<T > ws,vgl_infinite_line_3d<T >&,T& residual) 292 #endif // vgl_algo_intersection_hxx_ Set of intersection functions.
homogeneous plane in 3D projective space
Set of intersection functions.
bool contains(point_t const &p) const
Returns true if p(x,y) is inside the polygon, else false.
bool is_empty() const
Return true if this box is empty.
static vgl_homg_point_3d< Type > intersection(const vgl_homg_plane_3d< Type > &, const vgl_homg_plane_3d< Type > &, const vgl_homg_plane_3d< Type > &)
Compute best-fit intersection of planes in a point.
Direction vector in Euclidean 2D space, templated by type of element.
v & normalize(v &a)
Normalise by dividing through by the length, thus returning a length 1 vector.
vgl_vector_3d< T > normal() const
Return the normal direction, i.e., a unit vector orthogonal to this plane.
Contains class to represent a cartesian 3D bounding box.
vgl_point_3d< T > vgl_intersection(const std::vector< vgl_plane_3d< T > > &p)
Return the intersection point of vector of planes.
void add(vgl_point_3d< Type > const &p)
Add a point to this box.
a point in 3D nonhomogeneous space
T cross_product(v const &a, v const &b)
cross product of two vectors (area of enclosed parallellogram).
void push_back(T x, T y)
Add a new point to the last sheet.
Represents a Euclidean 3D plane.
vgl_point_3d< Type > centroid() const
Get the centroid point.
Represents a 3-d line with position defined in the orthogonal plane passing through the origin.
Direction vector in Euclidean 3D space, templated by type of element.
bool contains(vgl_point_3d< Type > const &p) const
Return true iff the point p is inside this box.
void set(Type px, Type py, Type pz)
Set x, y and z.
a plane in 3D nonhomogeneous space
Represents a cartesian 3D box.