ViSP  2.6.2
vpMbtHiddenFace.cpp
1 /****************************************************************************
2  *
3  * $Id: vpMbtHiddenFace.cpp 3530 2012-01-03 10:52:12Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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  * Make the complete tracking of an object by using its CAD model
36  *
37  * Authors:
38  * Nicolas Melchior
39  * Romain Tallonneau
40  * Eric Marchand
41  *
42  *****************************************************************************/
43 #include <visp/vpConfig.h>
44 #ifndef DOXYGEN_SHOULD_SKIP_THIS
45 
50 #include <visp/vpMbtHiddenFace.h>
51 
55 vpMbtPolygon::vpMbtPolygon()
56 {
57  nbpt = 0 ;
58  p = NULL ;
59  isappearing = false;
60  negative = 0;
61  angle_1 = -1e6;
62 }
63 
67 vpMbtPolygon::~vpMbtPolygon()
68 {
69 // cout << "Deleting Polygon " << index << endl ;
70  if (p!=NULL)
71  {
72  delete[] p;
73  p = NULL;
74  }
75 }
76 
82 void
83 vpMbtPolygon::setNbPoint(const unsigned int nb)
84 {
85  nbpt = nb ;
86  if (p != NULL)
87  delete[] p;
88  p = new vpPoint[nb] ;
89 }
90 
97 void
98 vpMbtPolygon::addPoint(const unsigned int n, const vpPoint &P)
99 {
100  //if( p!NULL && n < nbpt )
101  p[n] = P ;
102 }
103 
109 void
110 vpMbtPolygon::changeFrame(const vpHomogeneousMatrix &cMo)
111 {
112  for (unsigned int i = 0 ; i < nbpt ; i++)
113  {
114  p[i].changeFrame(cMo) ;
115  p[i].projection() ;
116  }
117 }
118 
126 bool
127 vpMbtPolygon::isVisible(const vpHomogeneousMatrix &cMo)
128 {
129  changeFrame(cMo) ;
130 
131  if(nbpt <= 2){
132  /* a line is allways visible */
133  isvisible = true;
134  isappearing = false;
135  return true ;
136  }
137 
138  vpColVector e1(3) ;
139  vpColVector e2(3) ;
140  vpColVector facenormal(3) ;
141 
142  e1[0] = p[1].get_X() - p[0].get_X() ;
143  e1[1] = p[1].get_Y() - p[0].get_Y() ;
144  e1[2] = p[1].get_Z() - p[0].get_Z() ;
145 
146  e2[0] = p[2].get_X() - p[1].get_X() ;
147  e2[1] = p[2].get_Y() - p[1].get_Y() ;
148  e2[2] = p[2].get_Z() - p[1].get_Z() ;
149 
150  facenormal = vpColVector::crossProd(e1,e2) ;
151 
152  double angle = p[0].get_X()*facenormal[0] + p[0].get_Y()*facenormal[1] + p[0].get_Z()*facenormal[2] ;
153 
154  double diff = angle - angle_1;
155  if (diff < 0) negative++;
156  else negative =0;
157 
158  if(angle < -0.00001 )
159  {
160  isvisible = true;
161  isappearing = false;
162  return true ;
163  }
164  else
165  {
166  if (angle < 0.0000001 )//&& negative >=1)
167  isappearing = true;
168  else
169  isappearing = false;
170  isvisible = false ;
171  return false ;
172  }
173 }
174 
175 
176 
187 bool
188 vpMbtPolygon::isVisible(const vpHomogeneousMatrix &cMo, const double alpha)
189 {
190  changeFrame(cMo) ;
191 
192  if(nbpt <= 2){
193  /* a line is allways visible */
194  return true ;
195  }
196 
197  vpColVector e1(3) ;
198  vpColVector e2(3) ;
199  vpColVector facenormal(3) ;
200 
201  e1[0] = p[1].get_X() - p[0].get_X() ;
202  e1[1] = p[1].get_Y() - p[0].get_Y() ;
203  e1[2] = p[1].get_Z() - p[0].get_Z() ;
204 
205  e2[0] = p[2].get_X() - p[1].get_X() ;
206  e2[1] = p[2].get_Y() - p[1].get_Y() ;
207  e2[2] = p[2].get_Z() - p[1].get_Z() ;
208 
209  facenormal = vpColVector::crossProd(e1,e2) ;
210 
211  vpColVector n_plan(3);
212  n_plan[0] = 0;
213  n_plan[1] = 0;
214  n_plan[2] = 1;
215 
216  vpColVector n_cam(3);
217  n_cam = facenormal;
218 
219  double angle = p[0].get_X()*facenormal[0] + p[0].get_Y()*facenormal[1] + p[0].get_Z()*facenormal[2] ;
220 
221  double n_cam_dot_n_plan = vpColVector::dotProd (n_cam, n_plan);
222  double cos_angle = n_cam_dot_n_plan * (1 / ( n_cam.euclideanNorm() * n_plan.euclideanNorm() ));
223  double my_angle = acos(cos_angle);
224 
225  if(angle < 0 && ( my_angle > static_cast<double>(M_PI - alpha) || my_angle < static_cast<double>(-M_PI + alpha) ) ){
226  return true;
227  }
228  return false;
229 }
230 
231 
235 vpMbtHiddenFaces::vpMbtHiddenFaces()
236 {
237 }
238 
239 
243 vpMbtHiddenFaces::~vpMbtHiddenFaces()
244 {
245 // cout << "Deleting Hidden Face struxture "<<endl ;
246  vpMbtPolygon *p ;
247 
248  for(std::list<vpMbtPolygon*>::const_iterator it=Lpol.begin(); it!=Lpol.end(); ++it){
249  p = *it;
250  if (p!=NULL){
251  delete p ;
252  }
253  p = NULL ;
254  }
255 
256  Lpol.clear() ;
257 }
258 
264 void
265 vpMbtHiddenFaces::addPolygon(vpMbtPolygon *p)
266 {
267  vpMbtPolygon *p_new = new vpMbtPolygon;
268  p_new->index = p->index;
269  p_new->setNbPoint(p->nbpt);
270  p_new->isvisible = p->isvisible;
271  for(unsigned int i = 0; i < p->nbpt; i++)
272  p_new->p[i]= p->p[i];
273  Lpol.push_back(p_new);
274 }
275 
276 
284 unsigned int
285 vpMbtHiddenFaces::setVisible(const vpHomogeneousMatrix &cMo)
286 {
287  unsigned int nbvisiblepolygone = 0 ;
288  vpMbtPolygon *p ;
289 
290  for(std::list<vpMbtPolygon*>::const_iterator it=Lpol.begin(); it!=Lpol.end(); ++it){
291  p = *it;
292  if (p->isVisible(cMo))
293  nbvisiblepolygone++;
294  }
295  return nbvisiblepolygone ;
296 }
297 
305 bool
306 vpMbtHiddenFaces::isVisible(const int index)
307 {
308  vpMbtPolygon *p ;
309  for(std::list<vpMbtPolygon*>::const_iterator it=Lpol.begin(); it!=Lpol.end(); ++it){
310  p = *it;
311  if (p->getIndex() == index) return p->isVisible() ;
312  }
313  return false ;
314 }
315 
323 bool
324 vpMbtHiddenFaces::isAppearing(const int index)
325 {
326  vpMbtPolygon *p ;
327  for(std::list<vpMbtPolygon*>::const_iterator it=Lpol.begin(); it!=Lpol.end(); ++it){
328  p = *it;
329  if (p->getIndex() == index) return p->isAppearing() ;
330  }
331  return false ;
332 }
333 
337 void
338 vpMbtHiddenFaces::reset()
339 {
340  vpMbtPolygon *p ;
341 
342  for(std::list<vpMbtPolygon*>::const_iterator it=Lpol.begin(); it!=Lpol.end(); ++it){
343  p = *it;
344  if (p!=NULL){
345  delete p ;
346  }
347  p = NULL ;
348  }
349 
350  Lpol.clear();
351 }
352 
353 #endif
354 
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
Class that defines what is a point.
Definition: vpPoint.h:65
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
static double dotProd(const vpColVector &a, const vpColVector &b)
Dot Product.
static vpColVector crossProd(const vpColVector &a, const vpColVector &b)
normalise the vector
void changeFrame(const vpHomogeneousMatrix &cMo, vpColVector &_cP)
Definition: vpPoint.cpp:150