Visual Servoing Platform  version 3.6.1 under development (2024-04-19)
vpCoreDisplay.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Le module "display.c" contient les procedures de d'affichage
33  * des scenes de modele geometrique surfacique.
34  *
35  * Authors:
36  * Jean-Luc CORRE
37  *
38 *****************************************************************************/
39 
40 #include <visp3/core/vpConfig.h>
41 #include <visp3/core/vpException.h>
42 
43 #ifndef DOXYGEN_SHOULD_SKIP_THIS
44 #include <stdio.h>
45 #include <stdlib.h>
46 
47 #include "vpCoreDisplay.h"
48 #include "vpImstack.h"
49 #include "vpMy.h"
50 #include "vpRfstack.h"
51 #include "vpView.h"
52 #include "vpVwstack.h"
53 
54 /*
55  * POINT2I :
56  * Tableau de points 2D dans l'espace ecran servant a l'affichage fil-de-fer.
57  *
58  * RENAME :
59  * Tableau de renommage des sommets ou tableau de compteurs associes aux
60  * points.
61  */
62 Point2i *point2i = (Point2i *)NULL;
63 Point2i *listpoint2i = (Point2i *)NULL;
64 static int *rename_jlc = (int *)NULL;
65 
66 /*
67  * La procedure "open_display" alloue et initialise les variables utilisees
68  * par le mode "display".
69  */
70 void open_display(void)
71 {
72  if ((point2i = (Point2i *)malloc(POINT_NBR * sizeof(Point2i))) == NULL ||
73  (listpoint2i = (Point2i *)malloc(50 * sizeof(Point2i))) == NULL ||
74  (rename_jlc = (int *)malloc(POINT_NBR * sizeof(int))) == NULL) {
75  static char proc_name[] = "open_display";
76  perror(proc_name);
77  throw vpException(vpException::fatalError, "Error in open_display");
78  }
79 }
80 
81 /*
82  * La procedure "close_display" libere les variables utilisees par le mode
83  * "display".
84  */
85 void close_display(void)
86 {
87  free((char *)point2i);
88  free((char *)listpoint2i);
89  free((char *)rename_jlc);
90  point2i = (Point2i *)NULL;
91  listpoint2i = (Point2i *)NULL;
92  rename_jlc = (int *)NULL;
93 }
94 
95 /*
96  * La procedure "point_3D_2D" projette les points 3D du volume canonique
97  * dans l'espace image 2D.
98  *
99  * Volume canonique Espace image
100  * ________________ ____________
101  *
102  * - 1 < X < 1 0 < X < xsize
103  * - 1 < Y < 1 0 < Y < ysize
104  * 0 < Z < 1
105  *
106  * Z < 0 X = 0, Y = -1 non significatifs.
107  *
108  * Entree :
109  * p3 Tableau de points 3D a projeter.
110  * size Taille du tableau de points "p3".
111  * xsize, ysize Tailles de l'espace image.
112  * p2 Tableau de points 2D en sortie.
113  */
114 // static
115 void point_3D_2D(Point3f *p3, Index size, int xsize, int ysize, Point2i *p2)
116 {
117  Point3f *pend = p3 + size; /* borne de p3 */
118  float xdiv2 = ((float)xsize) / (float)2.0;
119  float ydiv2 = ((float)ysize) / (float)2.0;
120 
121  for (; p3 < pend; p3++, p2++) {
122  p2->x = (int)((1.0 + p3->x) * xdiv2);
123  p2->y = (int)((1.0 - p3->y) * ydiv2);
124  }
125 }
126 
127 /*
128  * La procedure "set_Bound_face_display" marque les faces affichables
129  * de la surface "bp".
130  * Soit la face comportant le contour oriente suivant : (...,P2,P0,P1...).
131  * La normale a la face au point P0 est obtenue par le produit vectoriel :
132  *
133  * | x1 - x0 x2 - x0 | | Nx |
134  * N = (P1 - P0) ^ (P2 - P0) = | y1 - y0 y2 - y0 | = | Ny |
135  * | z1 - z0 z2 - z0 | | Nz |
136  *
137  * La face est dans le volume canonique de vision et dans un repere gauche.
138  * L'observateur est situe a l'infini dans la direction [0, 0, -1].
139  * IS_ABOVE <=> Ny < 0, IS_BELOW <=> Ny > 0.
140  * IS_RIGHT <=> Nx < 0, IS_LEFT <=> Nx > 0.
141  * IS_BACK <=> Nz < 0, IS_FRONT <=> Nz > 0.
142  * Entree :
143  * bp Surface a initialiser.
144  * b Drapeaux indiquant les faces non affichables.
145  */
146 void set_Bound_face_display(Bound *bp, Byte b)
147 {
148  Face *fp = bp->face.ptr;
149  Face *fend = fp + bp->face.nbr;
150  Point3f *pp = bp->point.ptr;
151 
152  for (; fp < fend; fp++) {
153  Index *vp;
154  Point3f *p0; /* premier sommet */
155  Point3f *p1; /* second sommet */
156  Point3f *p2; /* dernier sommet */
157 
158  fp->is_visible = TRUE;
159  if (b == IS_INSIDE)
160  continue;
161  vp = fp->vertex.ptr;
162  p0 = pp + *vp;
163  p1 = pp + *(vp + 1);
164  p2 = pp + *(vp + fp->vertex.nbr - 1);
165  if (b & IS_ABOVE) {
166  fp->is_visible = ((p1->z - p0->z) * (p2->x - p0->x) >= (p1->x - p0->x) * (p2->z - p0->z));
167  }
168  if (!fp->is_visible)
169  continue;
170  if (b & IS_BELOW) {
171  fp->is_visible = ((p1->z - p0->z) * (p2->x - p0->x) <= (p1->x - p0->x) * (p2->z - p0->z));
172  }
173  if (!fp->is_visible)
174  continue;
175  if (b & IS_RIGHT) {
176  fp->is_visible = ((p1->y - p0->y) * (p2->z - p0->z) >= (p1->z - p0->z) * (p2->y - p0->y));
177  }
178  if (!fp->is_visible)
179  continue;
180  if (b & IS_LEFT) {
181  fp->is_visible = ((p1->y - p0->y) * (p2->z - p0->z) <= (p1->z - p0->z) * (p2->y - p0->y));
182  }
183  if (!fp->is_visible)
184  continue;
185  if (b & IS_BACK) {
186  fp->is_visible = ((p1->x - p0->x) * (p2->y - p0->y) >= (p1->y - p0->y) * (p2->x - p0->x));
187  }
188  if (!fp->is_visible)
189  continue;
190  if (b & IS_FRONT) {
191  fp->is_visible = ((p1->x - p0->x) * (p2->y - p0->y) <= (p1->y - p0->y) * (p2->x - p0->x));
192  }
193  }
194 }
195 
196 /*
197  * La procedure "wireframe_Face" affiche une face "fp" en "fil de fer".
198  * sur la fenetre graphique de "suncgi" sur "SUN".
199  * Les points des sommets de la face sont contenu dans les points "pp"
200  * de la surface contenant la face.
201  * Entree :
202  * fp face a afficher.
203  * pp Points de la surface contenant la face.
204  */
205 void wireframe_Face(Face *fp, Point2i *pp)
206 {
207  // extern Window id_window;
208 
209  Index *vp = fp->vertex.ptr;
210  Index *vend = vp + fp->vertex.nbr;
211  Point2i *cp = listpoint2i;
212 
213  if (fp->vertex.nbr < 2)
214  return;
215  if (fp->vertex.nbr > 50) {
216  printf("pb malloc listpoint2i (display.c)\n");
217  return;
218  }
219  for (; vp < vend; vp++, cp++) {
220  SET_COORD2(*cp, pp[*vp].x, pp[*vp].y);
221  }
222 }
223 
224 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84