ViSP  2.10.0
vpPlane.cpp
1 /****************************************************************************
2  *
3  * $Id: vpPlane.cpp 4740 2014-05-26 07:15:44Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Plane geometrical structure.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
42 
49 #include <visp/vpPlane.h>
50 
51 #include <cmath> // std::fabs
52 #include <limits> // numeric_limits
53 
57 vpPlane&
59 {
60  A = p.A ;
61  B = p.B ;
62  C = p.C ;
63  D = p.D ;
64 
65  return *this ;
66 }
67 
71 vpPlane::vpPlane() : A(0), B(0), C(0), D(0) {}
72 
83 vpPlane::vpPlane(const double a,const double b,const double c, const double d)
84  : A(a), B(b), C(c), D(d) {}
85 
90  : A(0), B(0), C(0), D(0)
91 {
92  setA(P.getA()) ;
93  setB(P.getB()) ;
94  setC(P.getC()) ;
95  setD(P.getD()) ;
96 }
97 
117  : A(0), B(0), C(0), D(0)
118 {
119  //Equation of the plane is given by:
120  A = n[0];
121  B = n[1];
122  C = n[2];
123 
124  if (frame == vpPlane::camera_frame)
125  D=-(A*P.get_X()+B*P.get_Y()+C*P.get_Z());
126  else
127  D=-(A*P.get_oX()+B*P.get_oY()+C*P.get_oZ());
128 
129 }
130 
136 void vpPlane::init(const vpPlane& P)
137 {
138  setA(P.getA()) ;
139  setB(P.getB()) ;
140  setC(P.getC()) ;
141  setD(P.getD()) ;
142 }
143 
155 void vpPlane::init(const vpColVector & P, const vpColVector &n)
156 {
157  //Equation of the plane is given by:
158  A = n[0];
159  B = n[1];
160  C = n[2];
161 
162  D=-(A*P[0]+B*P[1]+C*P[2]);
163 }
164 
176 void
177 vpPlane::init(const vpPoint &P, const vpPoint &Q, const vpPoint &R, vpPlaneFrame frame)
178 {
179  vpColVector a(3);
180  vpColVector b(3);
181  vpColVector n(3);
182  if (frame == vpPlane::camera_frame) {
183  //Calculate vector corresponding to PQ
184  a[0]=P.get_X()-Q.get_X();
185  a[1]=P.get_Y()-Q.get_Y();
186  a[2]=P.get_Z()-Q.get_Z();
187 
188  //Calculate vector corresponding to PR
189  b[0]=P.get_X()-R.get_X();
190  b[1]=P.get_Y()-R.get_Y();
191  b[2]=P.get_Z()-R.get_Z();
192  }
193  else {
194  //Calculate vector corresponding to PQ
195  a[0]=P.get_oX()-Q.get_oX();
196  a[1]=P.get_oY()-Q.get_oY();
197  a[2]=P.get_oZ()-Q.get_oZ();
198 
199  //Calculate vector corresponding to PR
200  b[0]=P.get_oX()-R.get_oX();
201  b[1]=P.get_oY()-R.get_oY();
202  b[2]=P.get_oZ()-R.get_oZ();
203  }
204  //Calculate normal vector to plane PQ x PR
205  n=vpColVector::cross(a,b);
206 
207  //Equation of the plane is given by:
208  A = n[0];
209  B = n[1];
210  C = n[2];
211  if (frame == vpPlane::camera_frame)
212  D=-(A*P.get_X()+B*P.get_Y()+C*P.get_Z());
213  else
214  D=-(A*P.get_oX()+B*P.get_oY()+C*P.get_oZ());
215 
216  double norm = sqrt(A*A+B*B+C*C) ;
217  A /= norm ;
218  B /= norm ;
219  C /= norm ;
220  D /= norm ;
221 }
222 
223 
236 vpPlane::vpPlane(const vpPoint &P, const vpPoint &Q, const vpPoint &R, vpPlaneFrame frame)
237  : A(0), B(0), C(0), D(0)
238 {
239  init(P, Q, R, frame) ;
240 }
241 
251 {
252  vpColVector n(3);
253  n[0] = A ;
254  n[1] = B ;
255  n[2] = C ;
256 
257  return n ;
258 }
259 
271 {
272  n.resize(3) ;
273  n[0] = A ;
274  n[1] = B ;
275  n[2] = C ;
276 }
277 
284 void
286 {
287  double x0,y0,z0 ;
288  double rho ;
289 
290  x0 = P.get_X()/P.get_W() ;
291  y0 = P.get_Y()/P.get_W() ;
292  z0 = P.get_Z()/P.get_W() ;
293 
294  rho = - (A*x0+B*y0+C*z0+D)/(A*A+B*B+C*C) ;
295 
296  Pproj.set_X(x0+A*rho) ;
297  Pproj.set_Y(y0+B*rho) ;
298  Pproj.set_Z(z0+C*rho) ;
299  Pproj.set_W(1) ;
300 }
301 
302 
303 double
305  const vpPoint &M1,
306  vpColVector &H ) const
307 {
308 
309  double k,scal;
310  double R[3];
311 
312  // if(M0.get_X()!=0 || M0.get_Y()!=0 || M0.get_Z()!=0)
313  if(std::fabs(M0.get_X()) > std::numeric_limits<double>::epsilon()
314  || std::fabs(M0.get_Y()) > std::numeric_limits<double>::epsilon()
315  || std::fabs(M0.get_Z()) > std::numeric_limits<double>::epsilon())
316  {
317  R[0]= M1.get_X() - M0.get_X();
318  R[1]= M1.get_Y() - M0.get_Y();
319  R[2]= M1.get_Z() - M0.get_Z();
320 
321  scal = getA()*R[0] + getB()*R[1] + getC()*R[2];
322  //if (scal != 0)
323  if (std::fabs(scal) > std::numeric_limits<double>::epsilon())
324  k = -( getA()*M0.get_X() + getB()*M0.get_Y() + getC()*M0.get_Z() + getD())/scal;
325  else
326  k = 0;
327 
328  H[0] = M0.get_X()+ k*R[0];
329  H[1] = M0.get_Y()+ k*R[1];
330  H[2] = M0.get_Z()+ k*R[2];
331  }
332  else
333  {
334  scal = getA()*M1.get_X() + getB()*M1.get_Y() + getC()*M1.get_Z();
335  //if (scal != 0)
336  if (std::fabs(scal) > std::numeric_limits<double>::epsilon())
337  k = -getD()/scal;
338  else
339  k=0;
340  H[0] = k*M1.get_X();
341  H[1] = k*M1.get_Y();
342  H[2] = k*M1.get_Z();
343  }
344 
345  return k;
346 
347 }
348 
350 {
351 
352  double k,scal;
353 
354  scal = A*M1[0] + B*M1[1] + C*M1[2];
355  //if (scal != 0)
356  if (std::fabs(scal) > std::numeric_limits<double>::epsilon())
357  k = -getD()/scal;
358  else
359  k=0;
360  H[0] = k*M1[0];
361  H[1] = k*M1[1];
362  H[2] = k*M1[2];
363 
364  return k;
365 
366 }
367 
377 {
378  // Save current plane parameters
379  double Ao = A, Bo = B, Co = C, Do =D ;
380  A = cMo[0][0]*Ao + cMo[0][1]*Bo + cMo[0][2]*Co;
381  B = cMo[1][0]*Ao + cMo[1][1]*Bo + cMo[1][2]*Co;
382  C = cMo[2][0]*Ao + cMo[2][1]*Bo + cMo[2][2]*Co;
383  D = Do - (cMo[0][3]*A + cMo[1][3]*B + cMo[2][3]*C);
384 }
385 
392 VISP_EXPORT std::ostream& operator<< (std::ostream& os, vpPlane& p)
393 {
394  return (os << "("<<p.getA() << ","<<p.getB()
395  << ","<<p.getC()<< ","<<p.getD() <<") ") ;
396 } ;
double B
Definition: vpPlane.h:76
void setD(const double d)
Definition: vpPlane.h:100
vpPlane & operator=(const vpPlane &f)
Definition: vpPlane.cpp:58
static vpColVector cross(const vpColVector &a, const vpColVector &b)
Definition: vpColVector.h:158
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
double get_oY() const
Get the point Y coordinate in the object frame.
Definition: vpPoint.h:129
double C
Definition: vpPlane.h:76
void set_X(const double X)
Set the point X coordinate in the camera frame.
Definition: vpPoint.h:176
double get_W() const
Get the point W coordinate in the camera frame.
Definition: vpPoint.h:124
Class that defines what is a point.
Definition: vpPoint.h:65
double rayIntersection(const vpPoint &M0, const vpPoint &M1, vpColVector &H) const
Definition: vpPlane.cpp:304
double D
Definition: vpPlane.h:76
void set_Z(const double Z)
Set the point Z coordinate in the camera frame.
Definition: vpPoint.h:180
void set_W(const double W)
Set the point W coordinate in the camera frame.
Definition: vpPoint.h:182
void init(const vpPoint &P, const vpPoint &Q, const vpPoint &R, vpPlaneFrame frame=camera_frame)
Definition: vpPlane.cpp:177
void changeFrame(const vpHomogeneousMatrix &cMo)
Definition: vpPlane.cpp:376
double get_oZ() const
Get the point Z coordinate in the object frame.
Definition: vpPoint.h:131
void set_Y(const double Y)
Set the point Y coordinate in the camera frame.
Definition: vpPoint.h:178
void setA(const double a)
Definition: vpPlane.h:94
void projectionPointOnPlan(const vpPoint &P, vpPoint &Pproj) const
Definition: vpPlane.cpp:285
vpPlane()
Definition: vpPlane.cpp:71
vpColVector getNormal() const
Definition: vpPlane.cpp:250
double get_Y() const
Get the point Y coordinate in the camera frame.
Definition: vpPoint.h:120
double getIntersection(const vpColVector &M1, vpColVector &H) const
Definition: vpPlane.cpp:349
void setC(const double c)
Definition: vpPlane.h:98
double get_Z() const
Get the point Z coordinate in the camera frame.
Definition: vpPoint.h:122
double A
Definition: vpPlane.h:76
double get_oX() const
Get the point X coordinate in the object frame.
Definition: vpPoint.h:127
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
double getB() const
Definition: vpPlane.h:117
void setB(const double b)
Definition: vpPlane.h:96
double getA() const
Definition: vpPlane.h:115
double getC() const
Definition: vpPlane.h:119
This class defines the container for a plane geometrical structure.
Definition: vpPlane.h:67
double get_X() const
Get the point X coordinate in the camera frame.
Definition: vpPoint.h:118
double getD() const
Definition: vpPlane.h:121
vpPlaneFrame
Definition: vpPlane.h:79
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:98