Visual Servoing Platform version 3.5.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#include <visp3/core/vpException.h>
42
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44#include "vpArit.h"
45#include "vpBound.h"
46#include "vpMy.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 */
59void 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 */
91void 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 */
102void 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 */
130void 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))) == NULL) {
137 perror(proc_name);
138 throw vpException(vpException::fatalError, "Error in malloc_Bound");
139 }
140
141 if ((bp->point.nbr = pn) == 0) /* points */
142 bp->point.ptr = NULL;
143 else if ((bp->point.ptr = (Point3f *)malloc(pn * sizeof(Point3f))) == NULL) {
144 perror(proc_name);
145 throw vpException(vpException::fatalError, "Error in malloc_Bound");
146 }
147
148#ifdef face_normal
149 /* normales aux sommets */
150 if ((bp->normal.nbr = (bp->is_polygonal ? 0 : pn)) == 0)
151 bp->normal.ptr = NULL;
152 else if ((bp->normal.ptr = (Vector *)malloc(pn * sizeof(Vector))) == NULL) {
153 perror(proc_name);
154 throw vpException(vpException::fatalError, "Error in malloc_Bound");
155 }
156#endif /* face_normal */
157
158 bp->type = type;
159 bp->is_display = TRUE;
160 bp->is_polygonal = (unsigned)polygonal;
161}
162
163/*
164 * La procedure "malloc_huge_Bound" alloue une surface de taille maximale.
165 * La surface est adaptee pour la reception de tout type de surface.
166 * La surface allouee peut etre utilisee comme une surface de travail.
167 * Sa taille est definie par les macros "..._NBR" de "world.h".
168 * FACE_NBR : son nombre de faces
169 * POINT_NBR : son nombre de points
170 * VECTOR_NBR : son monbre de vecteurs
171 * VERTEX_NBR : son nombre de sommets par face.
172 * La particularite de la surface vient de l'allocation en une seule fois
173 * d'un tableau de sommets. Les polygones des faces ne sont pas initialiser,
174 * exepte celui de la premiere face qui est la base du tableau des sommets.
175 * Les champs "bound.face.edge" ne sont pas utilises.
176 * Entree :
177 * bp Surface maximale a allouer.
178 */
179void malloc_huge_Bound(Bound *bp)
180{
181
182#ifdef face_normal
183 malloc_Bound(bp, (Type)BND_NULL, FALSE, FACE_NBR, POINT_NBR);
184#else
185 malloc_Bound(bp, (Type)BND_NULL, TRUE, FACE_NBR, POINT_NBR);
186#endif /* face_normal */
187 if ((bp->face.ptr->vertex.ptr = (Index *)malloc(FACE_NBR * VERTEX_NBR * sizeof(Index))) == NULL) {
188 static char proc_name[] = "malloc_Huge_Bound";
189 perror(proc_name);
190 throw vpException(vpException::fatalError, "Error in malloc_huge_Bound");
191 }
192}
193
194/*
195 * La procedure "malloc_Bound_scene" alloue une scene de surfaces.
196 * Stocke le nom de la scene et alloue l'espace memoire necessaire.
197 * Les champs "bound.face.edge" ne sont pas utilises.
198 * Entree :
199 * bsp Scene a allouer.
200 * name Nom de la scene.
201 * bn Nombre de surfaces de la scene.
202 */
203void malloc_Bound_scene(Bound_scene *bsp, const char *name, Index bn)
204{
205 static char proc_name[] = "malloc_Bound_scene";
206
207 if ((bsp->name = (char *)malloc((strlen(name) + 1) * sizeof(char))) == NULL) {
208 perror(proc_name);
209 exit(1);
210 }
211 if ((bsp->bound.nbr = bn) == 0)
212 bsp->bound.ptr = NULL;
213 else if ((bsp->bound.ptr = (Bound *)malloc(bn * sizeof(Bound))) == NULL) {
214 perror(proc_name);
215 throw vpException(vpException::fatalError, "Error in malloc_Bound_scene");
216 }
217 strcpy(bsp->name, name);
218 bsp->bound.nbr = 0;
219}
220
221#endif
error that can be emited by ViSP classes.
Definition: vpException.h:72
@ fatalError
Fatal error.
Definition: vpException.h:96