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