Visual Servoing Platform  version 3.4.0
vpCoreDisplay.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 http://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 
42 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 #include <stdio.h>
44 #include <stdlib.h>
45 
46 #include "vpCoreDisplay.h"
47 #include "vpImstack.h"
48 #include "vpMy.h"
49 #include "vpRfstack.h"
50 #include "vpView.h"
51 #include "vpVwstack.h"
52 
53 /*
54  * POINT2I :
55  * Tableau de points 2D dans l'espace ecran servant a l'affichage fil-de-fer.
56  *
57  * RENAME :
58  * Tableau de renommage des sommets ou tableau de compteurs associes aux
59  * points.
60  */
61 Point2i *point2i = (Point2i *)NULL;
62 Point2i *listpoint2i = (Point2i *)NULL;
63 static int *rename_jlc = (int *)NULL;
64 
65 /*
66  * La procedure "open_display" alloue et initialise les variables utilisees
67  * par le mode "display".
68  */
69 void open_display(void)
70 {
71  if ((point2i = (Point2i *)malloc(POINT_NBR * sizeof(Point2i))) == NULL ||
72  (listpoint2i = (Point2i *)malloc(50 * sizeof(Point2i))) == NULL ||
73  (rename_jlc = (int *)malloc(POINT_NBR * sizeof(int))) == NULL) {
74  static char proc_name[] = "open_display";
75  perror(proc_name);
76  exit(1);
77  }
78 }
79 
80 /*
81  * La procedure "close_display" libere les variables utilisees par le mode
82  * "display".
83  */
84 void close_display(void)
85 {
86  free((char *)point2i);
87  free((char *)listpoint2i);
88  free((char *)rename_jlc);
89  point2i = (Point2i *)NULL;
90  listpoint2i = (Point2i *)NULL;
91  rename_jlc = (int *)NULL;
92 }
93 
94 /*
95  * La procedure "point_3D_2D" projette les points 3D du volume canonique
96  * dans l'espace image 2D.
97  *
98  * Volume canonique Espace image
99  * ________________ ____________
100  *
101  * - 1 < X < 1 0 < X < xsize
102  * - 1 < Y < 1 0 < Y < ysize
103  * 0 < Z < 1
104  *
105  * Z < 0 X = 0, Y = -1 non significatifs.
106  *
107  * Entree :
108  * p3 Tableau de points 3D a projeter.
109  * size Taille du tableau de points "p3".
110  * xsize, ysize Tailles de l'espace image.
111  * p2 Tableau de points 2D en sortie.
112  */
113 // static
114 void point_3D_2D(Point3f *p3, Index size, int xsize, int ysize, Point2i *p2)
115 {
116  Point3f *pend = p3 + size; /* borne de p3 */
117  float xdiv2 = ((float)xsize) / (float)2.0;
118  float ydiv2 = ((float)ysize) / (float)2.0;
119 
120  for (; p3 < pend; p3++, p2++) {
121  p2->x = (int)((1.0 + p3->x) * xdiv2);
122  p2->y = (int)((1.0 - p3->y) * ydiv2);
123  }
124 }
125 
126 /*
127  * La procedure "set_Bound_face_display" marque les faces affichables
128  * de la surface "bp".
129  * Soit la face comportant le contour oriente suivant : (...,P2,P0,P1...).
130  * La normale a la face au point P0 est obtenue par le produit vectoriel :
131  *
132  * | x1 - x0 x2 - x0 | | Nx |
133  * N = (P1 - P0) ^ (P2 - P0) = | y1 - y0 y2 - y0 | = | Ny |
134  * | z1 - z0 z2 - z0 | | Nz |
135  *
136  * La face est dans le volume canonique de vision et dans un repere gauche.
137  * L'observateur est situe a l'infini dans la direction [0, 0, -1].
138  * IS_ABOVE <=> Ny < 0, IS_BELOW <=> Ny > 0.
139  * IS_RIGHT <=> Nx < 0, IS_LEFT <=> Nx > 0.
140  * IS_BACK <=> Nz < 0, IS_FRONT <=> Nz > 0.
141  * Entree :
142  * bp Surface a initialiser.
143  * b Drapeaux indiquant les faces non affichables.
144  */
145 void set_Bound_face_display(Bound *bp, Byte b)
146 {
147  Face *fp = bp->face.ptr;
148  Face *fend = fp + bp->face.nbr;
149  Point3f *pp = bp->point.ptr;
150 
151  for (; fp < fend; fp++) {
152  Index *vp;
153  Point3f *p0; /* premier sommet */
154  Point3f *p1; /* second sommet */
155  Point3f *p2; /* dernier sommet */
156 
157  fp->is_visible = TRUE;
158  if (b == IS_INSIDE)
159  continue;
160  vp = fp->vertex.ptr;
161  p0 = pp + *vp;
162  p1 = pp + *(vp + 1);
163  p2 = pp + *(vp + fp->vertex.nbr - 1);
164  if (b & IS_ABOVE) {
165  fp->is_visible = ((p1->z - p0->z) * (p2->x - p0->x) >= (p1->x - p0->x) * (p2->z - p0->z));
166  }
167  if (!fp->is_visible)
168  continue;
169  if (b & IS_BELOW) {
170  fp->is_visible = ((p1->z - p0->z) * (p2->x - p0->x) <= (p1->x - p0->x) * (p2->z - p0->z));
171  }
172  if (!fp->is_visible)
173  continue;
174  if (b & IS_RIGHT) {
175  fp->is_visible = ((p1->y - p0->y) * (p2->z - p0->z) >= (p1->z - p0->z) * (p2->y - p0->y));
176  }
177  if (!fp->is_visible)
178  continue;
179  if (b & IS_LEFT) {
180  fp->is_visible = ((p1->y - p0->y) * (p2->z - p0->z) <= (p1->z - p0->z) * (p2->y - p0->y));
181  }
182  if (!fp->is_visible)
183  continue;
184  if (b & IS_BACK) {
185  fp->is_visible = ((p1->x - p0->x) * (p2->y - p0->y) >= (p1->y - p0->y) * (p2->x - p0->x));
186  }
187  if (!fp->is_visible)
188  continue;
189  if (b & IS_FRONT) {
190  fp->is_visible = ((p1->x - p0->x) * (p2->y - p0->y) <= (p1->y - p0->y) * (p2->x - p0->x));
191  }
192  }
193 }
194 
195 /*
196  * La procedure "wireframe_Face" affiche une face "fp" en "fil de fer".
197  * sur la fenetre graphique de "suncgi" sur "SUN".
198  * Les points des sommets de la face sont contenu dans les points "pp"
199  * de la surface contenant la face.
200  * Entree :
201  * fp face a afficher.
202  * pp Points de la surface contenant la face.
203  */
204 void wireframe_Face(Face *fp, Point2i *pp)
205 {
206  // extern Window id_window;
207 
208  Index *vp = fp->vertex.ptr;
209  Index *vend = vp + fp->vertex.nbr;
210  Point2i *cp = listpoint2i;
211 
212  if (fp->vertex.nbr < 2)
213  return;
214  if (fp->vertex.nbr > 50) {
215  printf("pb malloc listpoint2i (display.c)\n");
216  return;
217  }
218  for (; vp < vend; vp++, cp++) {
219  SET_COORD2(*cp, pp[*vp].x, pp[*vp].y);
220  }
221 }
222 
223 #endif