Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpVwstack.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Le module "vwstack.c" contient les procedures de gestion
32  * de la pile des points de vue (VieW STACK).
33  *
34  * Authors:
35  * Jean-Luc CORRE
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpConfig.h>
40 
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 
43 #include <stdio.h>
44 #include <string.h>
45 #include <limits>
46 #include <cmath> // std::fabs()
47 #include <stdarg.h>
48 
49 #include "vpMy.h"
50 #include "vpArit.h"
51 #include "vpView.h"
52 #include "vpVwstack.h"
53 
54 
55 #define STACKSIZE 4
56 
57 
58 static View_parameters stack[STACKSIZE]= { vpDEFAULT_VIEW };
59 static View_parameters *sp = stack;
60 
61 /*
62  * La procedure "fprintf_vwstack" affiche un parametre du sommet
63  * de la pile des prises de vue.
64  * Entree :
65  * fp Fichier de sortie.
66  * argv Argument a afficher.
67  * Si argv est nul, tous les parametres sont affiches.
68  */
69 void
70 fprintf_vwstack (FILE *fp, char *argv)
71 {
72  if (argv == NULL || strcmp (argv, "type") == 0) {
73  const char *typetoa;
74 
75  switch (sp->type) {
76  case PARALLEL :
77  typetoa = "parallel";
78  break;
79  case PERSPECTIVE :
80  typetoa = "perspective";
81  break;
82  default :
83  typetoa = "unknown";
84  break;
85  }
86  fprintf (fp, "(type\t%s)\n", typetoa);
87  if (argv != NULL) return;
88  }
89  if (argv == NULL || strcmp (argv, "cop") == 0) {
90  fprintf (fp, "(cop\t%.3f\t%.3f\t%.3f)\n",
91  sp->cop.x, sp->cop.y, sp->cop.z);
92  if (argv != NULL) return;
93  }
94  if (argv == NULL || strcmp (argv, "vrp") == 0) {
95  fprintf (fp, "(vrp\t%.3f\t%.3f\t%.3f)\n",
96  sp->vrp.x, sp->vrp.y, sp->vrp.z);
97  if (argv != NULL) return;
98  }
99  if (argv == NULL || strcmp (argv, "vpn") == 0) {
100  fprintf (fp, "(vpn\t%.3f\t%.3f\t%.3f)\n",
101  sp->vpn.x, sp->vpn.y, sp->vpn.z);
102  if (argv != NULL) return;
103  }
104  if (argv == NULL || strcmp (argv, "vup") == 0) {
105  fprintf (fp, "(vup\t%.3f\t%.3f\t%.3f)\n",
106  sp->vup.x, sp->vup.y, sp->vup.z);
107  if (argv != NULL) return;
108  }
109  if (argv == NULL || strcmp (argv, "window") == 0) {
110  fprintf (fp, "(window\t%.3f\t%.3f\t%.3f\t%.3f)\n",
111  sp->vwd.umin,sp->vwd.umax,sp->vwd.vmin,sp->vwd.vmax);
112  if (argv != NULL) return;
113  }
114  if (argv == NULL || strcmp (argv, "depth") == 0) {
115  fprintf (fp, "(depth\t%.3f\t%.3f)\n",
116  sp->depth.front, sp->depth.back);
117  if (argv != NULL) return;
118  }
119  if (argv != NULL) {
120  static char proc_name[] = "fprintf_vwstack";
121  fprintf (stderr, "%s: argument unknown\n", proc_name);
122  }
123 }
124 
125 /*
126  * La procedure "get_vwstack" retourne le point de vue au sommet
127  * de la pile des points de vue.
128  * Sortie :
129  * Pointeur sur le point de vue du sommet de la pile.
130  */
131 View_parameters *
132 get_vwstack (void)
133 {
134  return (sp);
135 }
136 
137 /*
138  * La procedure "load_vwstack" charge un point de vue au sommet
139  * de la pile des points de vue.
140  * Entree :
141  * vp Point de vue a charger.
142  */
143 void
144 load_vwstack (View_parameters *vp)
145 {
146  *sp = *vp;
147 }
148 
149 /*
150  * La procedure "pop_vwstack" depile le point de vue au sommet
151  * de la pile des points de vue.
152  */
153 void
154 pop_vwstack (void)
155 {
156  if (sp == stack) {
157  static char proc_name[] = "pop_vwstack";
158  fprintf (stderr, "%s: stack underflow\n", proc_name);
159  return;
160  }
161  else sp--;
162 }
163 
164 /*
165  * La procedure "push_vwstack" empile et duplique le point de vue au sommet
166  * de la pile des points de vue.
167  */
168 void
169 push_vwstack (void)
170 {
171  if (sp == stack + STACKSIZE - 1) {
172  static char proc_name[] = "push_vwstack";
173  fprintf (stderr, "%s: stack overflow\n", proc_name);
174  return;
175  }
176  sp++;
177  *sp = *(sp - 1);
178 }
179 
180 /*
181  * La procedure "swap_vwstack" echange les deux premiers elements
182  * de la pile des points de vue.
183  */
184 void
185 swap_vwstack (void)
186 {
187  View_parameters *vp, tmp;
188 
189  vp = (sp == stack) ? sp + 1 : sp - 1;
190  SWAP(*sp, *vp, tmp);
191 }
192 
193 /*
194  * La procedure "add_vwstack" modifie un agrument du point de vue au sommet
195  * de la pile des points de vue.
196  * Entree :
197  * va_alist Nom de l'argument a modifier suivi de ses parametres.
198  */
199 
200 void
201 add_vwstack (const char* path, ... )
202 //add_vwstack (va_alist)
203 // va_dcl
204 {
205  va_list ap;
206  char *argv;
207 
208  va_start (ap,path);
209  argv = va_arg (ap, char *);
210  if (strcmp (argv, "cop") == 0) {
211  /* initialise le centre de projection */
212  SET_COORD3(sp->cop,
213  (float) va_arg (ap, double),
214  (float) va_arg (ap, double),
215  (float) va_arg (ap, double));
216  }
217  else if (strcmp (argv, "depth") == 0) {
218  /* initialise les distances des plans de decoupage */
219  sp->depth.front = (float) va_arg (ap, double);
220  sp->depth.back = (float) va_arg (ap, double);
221  }
222  else if (strcmp (argv, "type") == 0) {
223  /* initialise le type de projection */
224  sp->type = (Type) va_arg (ap, int);
225  }
226  else if (strcmp (argv, "vpn") == 0) {
227  /* initialise le vecteur normal au plan */
228  float x = (float) va_arg (ap, double);
229  float y = (float) va_arg (ap, double);
230  float z = (float) va_arg (ap, double);
231 
232  //if (x == 0 && y == 0 && z == 0)
233  if (std::fabs(x) <= std::numeric_limits<double>::epsilon() && std::fabs(y) <= std::numeric_limits<double>::epsilon() && std::fabs(z) <= std::numeric_limits<double>::epsilon()) {
234  static char proc_name[] = "add_vwstack";
235  fprintf (stderr, "%s: bad vpn\n", proc_name);
236  }
237  else {
238  SET_COORD3(sp->vpn,x,y,z);
239  }
240  }
241  else if (strcmp (argv, "vrp") == 0) {
242  /* initialise le vecteur de reference */
243  SET_COORD3(sp->vrp,
244  (float) va_arg (ap, double),
245  (float) va_arg (ap, double),
246  (float) va_arg (ap, double));
247  }
248  else if (strcmp (argv, "vup") == 0) {
249  /* initialise le vecteur haut du plan */
250  float x = (float) va_arg (ap, double);
251  float y = (float) va_arg (ap, double);
252  float z = (float) va_arg (ap, double);
253 
254  //if (x == 0 && y == 0 && z == 0)
255  if (std::fabs(x) <= std::numeric_limits<double>::epsilon() && std::fabs(y) <= std::numeric_limits<double>::epsilon() && std::fabs(z) <= std::numeric_limits<double>::epsilon()) {
256  static char proc_name[] = "add_vwstack";
257  fprintf (stderr, "%s: bad vup\n", proc_name);
258  }
259 else {
260  SET_COORD3(sp->vup,x,y,z);
261  }
262  }
263  else if (strcmp (argv, "window") == 0) {
264  /* initialise la fenetre de projection */
265  sp->vwd.umin = (float) va_arg (ap, double);
266  sp->vwd.umax = (float) va_arg (ap, double);
267  sp->vwd.vmin = (float) va_arg (ap, double);
268  sp->vwd.vmax = (float) va_arg (ap, double);
269  }
270  else {
271  static char proc_name[] = "add_vwstack";
272  fprintf (stderr, "%s: bad argument\n", proc_name);
273  }
274  va_end (ap);
275 }
276 
277 #endif
278