Visual Servoing Platform  version 3.4.0
vpBound.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 fichier "bound.c" contient les procedures de gestion des scenes de
33  *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 "vpArit.h"
44 #include "vpBound.h"
45 #include "vpMy.h"
46 #include <errno.h>
47 #include <math.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 /*
53  * La procedure "free_Bound" libere la memoire d'une surface.
54  * Les champs "bound.face.edge" ne sont pas utilises.
55  * Entree :
56  * bp Surface a liberer.
57  */
58 void free_Bound(Bound *bp)
59 {
60  Face *fp = bp->face.ptr;
61  Face *fend = fp + bp->face.nbr;
62 
63  for (; fp < fend; fp++) { /* libere les polygones */
64  if (fp->vertex.ptr != fp->vertex.tbl)
65  free((char *)fp->vertex.ptr);
66  }
67  if (bp->face.ptr != NULL) { /* libere les faces */
68  free((char *)bp->face.ptr);
69  bp->face.ptr = NULL;
70  }
71  if (bp->point.ptr != NULL) { /* libere les points */
72  free((char *)bp->point.ptr);
73  bp->point.ptr = NULL;
74  }
75 #ifdef face_normal
76  if (bp->normal.ptr != NULL) { /* libere les vecteurs */
77  free((char *)bp->normal.ptr);
78  bp->normal.ptr = NULL;
79  }
80 #endif /* face_normal */
81  bp->is_display = FALSE;
82 }
83 
84 /*
85  * La procedure "free_huge_Bound" libere une surface de taille maximale.
86  * La particularite de cette surface est le tableau unique des sommets.
87  * Entree :
88  * bp Surface a liberer.
89  */
90 void free_huge_Bound(Bound *bp)
91 {
92  bp->face.nbr = 1; /* pour la liberation en une fois */
93  free_Bound(bp);
94 }
95 
96 /*
97  * La procedure "free_Bound_scene" libere une scene de surfaces.
98  * Entree :
99  * bsp Scene a liberer.
100  */
101 void free_Bound_scene(Bound_scene *bsp)
102 {
103  Bound *bp = bsp->bound.ptr;
104  Bound *bend = bp + bsp->bound.nbr;
105 
106  for (; bp < bend; bp++) { /* libere les surfaces */
107  free_Bound(bp);
108  }
109  if (bsp->name != NULL) { /* libere le nom */
110  free((char *)bsp->name);
111  bsp->name = NULL;
112  }
113  if (bsp->bound.ptr != NULL) { /* libere le tableau */
114  free((char *)bsp->bound.ptr);
115  bsp->bound.ptr = NULL;
116  }
117 }
118 
119 /*
120  * La procedure "malloc_Bound" alloue une surface.
121  * Les champs "bound.face.edge" ne sont pas utilises.
122  * Entree :
123  * bp Surface a allouer.
124  * type Type de la surface.
125  * polygonal Booleen indiquant si la surface est polygonale.
126  * fn Nombre de faces de la surface.
127  * pn Nombre de points de la surface.
128  */
129 void malloc_Bound(Bound *bp, Type type, int polygonal, Index fn, Index pn)
130 {
131  static char proc_name[] = "malloc_Bound";
132 
133  if ((bp->face.nbr = fn) == 0) /* faces */
134  bp->face.ptr = NULL;
135  else if ((bp->face.ptr = (Face *)malloc(fn * sizeof(Face))) == NULL) {
136  perror(proc_name);
137  exit(1);
138  }
139 
140  if ((bp->point.nbr = pn) == 0) /* points */
141  bp->point.ptr = NULL;
142  else if ((bp->point.ptr = (Point3f *)malloc(pn * sizeof(Point3f))) == NULL) {
143  perror(proc_name);
144  exit(1);
145  }
146 
147 #ifdef face_normal
148  /* normales aux sommets */
149  if ((bp->normal.nbr = (bp->is_polygonal ? 0 : pn)) == 0)
150  bp->normal.ptr = NULL;
151  else if ((bp->normal.ptr = (Vector *)malloc(pn * sizeof(Vector))) == NULL) {
152  perror(proc_name);
153  exit(1);
154  }
155 #endif /* face_normal */
156 
157  bp->type = type;
158  bp->is_display = TRUE;
159  bp->is_polygonal = (unsigned)polygonal;
160 }
161 
162 /*
163  * La procedure "malloc_huge_Bound" alloue une surface de taille maximale.
164  * La surface est adaptee pour la reception de tout type de surface.
165  * La surface allouee peut etre utilisee comme une surface de travail.
166  * Sa taille est definie par les macros "..._NBR" de "world.h".
167  * FACE_NBR : son nombre de faces
168  * POINT_NBR : son nombre de points
169  * VECTOR_NBR : son monbre de vecteurs
170  * VERTEX_NBR : son nombre de sommets par face.
171  * La particularite de la surface vient de l'allocation en une seule fois
172  * d'un tableau de sommets. Les polygones des faces ne sont pas initialiser,
173  * exepte celui de la premiere face qui est la base du tableau des sommets.
174  * Les champs "bound.face.edge" ne sont pas utilises.
175  * Entree :
176  * bp Surface maximale a allouer.
177  */
178 void malloc_huge_Bound(Bound *bp)
179 {
180 
181 #ifdef face_normal
182  malloc_Bound(bp, (Type)BND_NULL, FALSE, FACE_NBR, POINT_NBR);
183 #else
184  malloc_Bound(bp, (Type)BND_NULL, TRUE, FACE_NBR, POINT_NBR);
185 #endif /* face_normal */
186  if ((bp->face.ptr->vertex.ptr = (Index *)malloc(FACE_NBR * VERTEX_NBR * sizeof(Index))) == NULL) {
187  static char proc_name[] = "malloc_Huge_Bound";
188  perror(proc_name);
189  exit(1);
190  }
191 }
192 
193 /*
194  * La procedure "malloc_Bound_scene" alloue une scene de surfaces.
195  * Stocke le nom de la scene et alloue l'espace memoire necessaire.
196  * Les champs "bound.face.edge" ne sont pas utilises.
197  * Entree :
198  * bsp Scene a allouer.
199  * name Nom de la scene.
200  * bn Nombre de surfaces de la scene.
201  */
202 void malloc_Bound_scene(Bound_scene *bsp, const char *name, Index bn)
203 {
204  static char proc_name[] = "malloc_Bound_scene";
205 
206  if ((bsp->name = (char *)malloc((strlen(name) + 1) * sizeof(char))) == NULL) {
207  perror(proc_name);
208  exit(1);
209  }
210  if ((bsp->bound.nbr = bn) == 0)
211  bsp->bound.ptr = NULL;
212  else if ((bsp->bound.ptr = (Bound *)malloc(bn * sizeof(Bound))) == NULL) {
213  perror(proc_name);
214  exit(1);
215  }
216  strcpy(bsp->name, name);
217  bsp->bound.nbr = 0;
218 }
219 
220 #endif