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