ViSP
vpVwstack.cpp
1 /****************************************************************************
2  *
3  * $Id: vpVwstack.cpp 5285 2015-02-09 14:32:54Z 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  * Description:
34  * Le module "vwstack.c" contient les procedures de gestion
35  * de la pile des points de vue (VieW STACK).
36  *
37  * Authors:
38  * Jean-Luc CORRE
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpConfig.h>
43 
44 #ifndef DOXYGEN_SHOULD_SKIP_THIS
45 
46 #include <stdio.h>
47 #include <string.h>
48 #include <limits>
49 #include <cmath> // std::fabs()
50 
51 // #include <varargs.h> /* modif Fedora
52 #include <stdarg.h>
53 
54 #include <visp/vpMy.h>
55 #include <visp/vpArit.h>
56 #include <visp/vpView.h>
57 #include <visp/vpVwstack.h>
58 
59 
60 #define STACKSIZE 4
61 
62 
63 static View_parameters stack[STACKSIZE]= { vpDEFAULT_VIEW };
64 static View_parameters *sp = stack;
65 
66 /*
67  * La procedure "fprintf_vwstack" affiche un parametre du sommet
68  * de la pile des prises de vue.
69  * Entree :
70  * fp Fichier de sortie.
71  * argv Argument a afficher.
72  * Si argv est nul, tous les parametres sont affiches.
73  */
74 void
75 fprintf_vwstack (FILE *fp, char *argv)
76 {
77  static char proc_name[] = "fprintf_vwstack";
78 
79  if (argv == NULL || strcmp (argv, "type") == 0) {
80  const char *typetoa;
81 
82  switch (sp->type) {
83  case PARALLEL :
84  typetoa = "parallel";
85  break;
86  case PERSPECTIVE :
87  typetoa = "perspective";
88  break;
89  default :
90  typetoa = "unknown";
91  break;
92  }
93  fprintf (fp, "(type\t%s)\n", typetoa);
94  if (argv != NULL) return;
95  }
96  if (argv == NULL || strcmp (argv, "cop") == 0) {
97  fprintf (fp, "(cop\t%.3f\t%.3f\t%.3f)\n",
98  sp->cop.x, sp->cop.y, sp->cop.z);
99  if (argv != NULL) return;
100  }
101  if (argv == NULL || strcmp (argv, "vrp") == 0) {
102  fprintf (fp, "(vrp\t%.3f\t%.3f\t%.3f)\n",
103  sp->vrp.x, sp->vrp.y, sp->vrp.z);
104  if (argv != NULL) return;
105  }
106  if (argv == NULL || strcmp (argv, "vpn") == 0) {
107  fprintf (fp, "(vpn\t%.3f\t%.3f\t%.3f)\n",
108  sp->vpn.x, sp->vpn.y, sp->vpn.z);
109  if (argv != NULL) return;
110  }
111  if (argv == NULL || strcmp (argv, "vup") == 0) {
112  fprintf (fp, "(vup\t%.3f\t%.3f\t%.3f)\n",
113  sp->vup.x, sp->vup.y, sp->vup.z);
114  if (argv != NULL) return;
115  }
116  if (argv == NULL || strcmp (argv, "window") == 0) {
117  fprintf (fp, "(window\t%.3f\t%.3f\t%.3f\t%.3f)\n",
118  sp->vwd.umin,sp->vwd.umax,sp->vwd.vmin,sp->vwd.vmax);
119  if (argv != NULL) return;
120  }
121  if (argv == NULL || strcmp (argv, "depth") == 0) {
122  fprintf (fp, "(depth\t%.3f\t%.3f)\n",
123  sp->depth.front, sp->depth.back);
124  if (argv != NULL) return;
125  }
126  if (argv != NULL)
127  fprintf (stderr, "%s: argument unknown\n", proc_name);
128 }
129 
130 /*
131  * La procedure "get_vwstack" retourne le point de vue au sommet
132  * de la pile des points de vue.
133  * Sortie :
134  * Pointeur sur le point de vue du sommet de la pile.
135  */
136 View_parameters *
137 get_vwstack (void)
138 {
139  return (sp);
140 }
141 
142 /*
143  * La procedure "load_vwstack" charge un point de vue au sommet
144  * de la pile des points de vue.
145  * Entree :
146  * vp Point de vue a charger.
147  */
148 void
149 load_vwstack (View_parameters *vp)
150 {
151  *sp = *vp;
152 }
153 
154 /*
155  * La procedure "pop_vwstack" depile le point de vue au sommet
156  * de la pile des points de vue.
157  */
158 void
159 pop_vwstack (void)
160 {
161  static char proc_name[] = "pop_vwstack";
162 
163  if (sp == stack) {
164  fprintf (stderr, "%s: stack underflow\n", proc_name);
165  return;
166  }
167  else sp--;
168 }
169 
170 /*
171  * La procedure "push_vwstack" empile et duplique le point de vue au sommet
172  * de la pile des points de vue.
173  */
174 void
175 push_vwstack (void)
176 {
177  static char proc_name[] = "push_vwstack";
178 
179  if (sp == stack + STACKSIZE - 1) {
180  fprintf (stderr, "%s: stack overflow\n", proc_name);
181  return;
182  }
183  sp++;
184  *sp = *(sp - 1);
185 }
186 
187 /*
188  * La procedure "swap_vwstack" echange les deux premiers elements
189  * de la pile des points de vue.
190  */
191 void
192 swap_vwstack (void)
193 {
194  View_parameters *vp, tmp;
195 
196  vp = (sp == stack) ? sp + 1 : sp - 1;
197  SWAP(*sp, *vp, tmp);
198 }
199 
200 /*
201  * La procedure "add_vwstack" modifie un agrument du point de vue au sommet
202  * de la pile des points de vue.
203  * Entree :
204  * va_alist Nom de l'argument a modifier suivi de ses parametres.
205  */
206 
207 void
208 add_vwstack (const char* path, ... )
209 //add_vwstack (va_alist)
210 // va_dcl
211 {
212  static char proc_name[] = "add_vwstack";
213 
214  va_list ap;
215  char *argv;
216 
217  va_start (ap,path);
218  argv = va_arg (ap, char *);
219  if (strcmp (argv, "cop") == 0) {
220  /* initialise le centre de projection */
221  SET_COORD3(sp->cop,
222  (float) va_arg (ap, double),
223  (float) va_arg (ap, double),
224  (float) va_arg (ap, double));
225  }
226  else if (strcmp (argv, "depth") == 0) {
227  /* initialise les distances des plans de decoupage */
228  sp->depth.front = (float) va_arg (ap, double);
229  sp->depth.back = (float) va_arg (ap, double);
230  }
231  else if (strcmp (argv, "type") == 0) {
232  /* initialise le type de projection */
233  sp->type = (Type) va_arg (ap, int);
234  }
235  else if (strcmp (argv, "vpn") == 0) {
236  /* initialise le vecteur normal au plan */
237  float x = (float) va_arg (ap, double);
238  float y = (float) va_arg (ap, double);
239  float z = (float) va_arg (ap, double);
240 
241  //if (x == 0 && y == 0 && z == 0)
242  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())
243  fprintf (stderr, "%s: bad vpn\n", proc_name);
244  else {
245  SET_COORD3(sp->vpn,x,y,z);
246  }
247  }
248  else if (strcmp (argv, "vrp") == 0) {
249  /* initialise le vecteur de reference */
250  SET_COORD3(sp->vrp,
251  (float) va_arg (ap, double),
252  (float) va_arg (ap, double),
253  (float) va_arg (ap, double));
254  }
255  else if (strcmp (argv, "vup") == 0) {
256  /* initialise le vecteur haut du plan */
257  float x = (float) va_arg (ap, double);
258  float y = (float) va_arg (ap, double);
259  float z = (float) va_arg (ap, double);
260 
261  //if (x == 0 && y == 0 && z == 0)
262  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())
263  fprintf (stderr, "%s: bad vup\n", proc_name);
264  else {
265  SET_COORD3(sp->vup,x,y,z);
266  }
267  }
268  else if (strcmp (argv, "window") == 0) {
269  /* initialise la fenetre de projection */
270  sp->vwd.umin = (float) va_arg (ap, double);
271  sp->vwd.umax = (float) va_arg (ap, double);
272  sp->vwd.vmin = (float) va_arg (ap, double);
273  sp->vwd.vmax = (float) va_arg (ap, double);
274  }
275  else
276  fprintf (stderr, "%s: bad argument\n", proc_name);
277  va_end (ap);
278 }
279 
280 #endif
281