Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpScene.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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
31#include <visp3/core/vpConfig.h>
32
33#ifndef DOXYGEN_SHOULD_SKIP_THIS
34
35#include <cmath>
36#include <limits>
37
38#include "vpKeyword.h"
39#include "vpLex.h"
40#include "vpParser.h"
41#include "vpScene.h"
42
43#include <visp3/core/vpException.h>
44#include <visp3/core/vpPoint.h>
45
47/*
48 Get the extension of the file and return it
49*/
50Model_3D getExtension(const char *file)
51{
52 std::string sfilename(file);
53
54 size_t bnd = sfilename.find("bnd");
55 size_t BND = sfilename.find("BND");
56 size_t wrl = sfilename.find("wrl");
57 size_t WRL = sfilename.find("WRL");
58
59 size_t size = sfilename.size();
60
61 if ((bnd > 0 && bnd < size) || (BND > 0 && BND < size))
62 return BND_MODEL;
63 else if ((wrl > 0 && wrl < size) || (WRL > 0 && WRL < size)) {
64#if defined(VISP_HAVE_COIN3D)
65 return WRL_MODEL;
66#else
67 std::cout << "Coin not installed, cannot read VRML files" << std::endl;
68 throw std::string("Coin not installed, cannot read VRML files");
69#endif
70 }
71 return UNKNOWN_MODEL;
72}
73
74/*
75 Enable to initialize the scene
76*/
77void set_scene(const char *str, Bound_scene *sc, float factor)
78{
79 FILE *fd;
80
81 // if ((fd = fopen (str, 0)) == -1)
82 if ((fd = fopen(str, "r")) == NULL) {
83 std::string error = "The file " + std::string(str) + " can not be opened";
84
85 throw(vpException(vpException::ioError, error.c_str()));
86 }
87 open_keyword(keyword_tbl);
88 open_lex();
89 open_source(fd, str);
90 malloc_Bound_scene(sc, str, (Index)BOUND_NBR);
91 parser(sc);
92
93 // if (factor != 1)
94 if (std::fabs(factor) > std::numeric_limits<double>::epsilon()) {
95 for (int i = 0; i < sc->bound.nbr; i++) {
96 for (int j = 0; j < sc->bound.ptr[i].point.nbr; j++) {
97 sc->bound.ptr[i].point.ptr[j].x = sc->bound.ptr[i].point.ptr[j].x * factor;
98 sc->bound.ptr[i].point.ptr[j].y = sc->bound.ptr[i].point.ptr[j].y * factor;
99 sc->bound.ptr[i].point.ptr[j].z = sc->bound.ptr[i].point.ptr[j].z * factor;
100 }
101 }
102 }
103
104 close_source();
105 close_lex();
106 close_keyword();
107 fclose(fd);
108}
109
110#if defined(VISP_HAVE_COIN3D)
111
112void set_scene_wrl(const char *str, Bound_scene *sc, float factor)
113{
114 // Load the sceneGraph
115 SoDB::init();
116 SoInput in;
117 SbBool ok = in.openFile(str);
118 SoVRMLGroup *sceneGraphVRML2;
119
120 if (!ok) {
121 throw(vpException(vpException::fatalError, "Can't open file \"%s\". Please check the Marker_Less.ini file", str));
122 }
123
124 if (!in.isFileVRML2()) {
125 SoSeparator *sceneGraph = SoDB::readAll(&in);
126 if (sceneGraph == NULL) { /*return -1;*/
127 }
128 sceneGraph->ref();
129
130 SoToVRML2Action tovrml2;
131 tovrml2.apply(sceneGraph);
132 sceneGraphVRML2 = tovrml2.getVRML2SceneGraph();
133 sceneGraphVRML2->ref();
134 sceneGraph->unref();
135 }
136 else {
137 sceneGraphVRML2 = SoDB::readAllVRML(&in);
138 if (sceneGraphVRML2 == NULL) {
139 /*return -1;*/
140 throw(vpException(vpException::notInitialized, "Cannot read VRML file"));
141 }
142 sceneGraphVRML2->ref();
143 }
144
145 in.closeFile();
146
147 int nbShapes = sceneGraphVRML2->getNumChildren();
148
149 SoNode *child;
150
151 malloc_Bound_scene(sc, str, (Index)BOUND_NBR);
152
153 int iterShapes = 0;
154 for (int i = 0; i < nbShapes; i++) {
155 child = sceneGraphVRML2->getChild(i);
156 if (child->getTypeId() == SoVRMLShape::getClassTypeId()) {
157 std::list<indexFaceSet *> ifs_list;
158 SoChildList *child2list = child->getChildren();
159 for (int j = 0; j < child2list->getLength(); j++) {
160 if (((SoNode *)child2list->get(j))->getTypeId() == SoVRMLIndexedFaceSet::getClassTypeId()) {
161 indexFaceSet *ifs = new indexFaceSet;
162 SoVRMLIndexedFaceSet *face_set;
163 face_set = (SoVRMLIndexedFaceSet *)child2list->get(j);
164 extractFaces(face_set, ifs);
165 ifs_list.push_back(ifs);
166 }
167 // if (((SoNode*)child2list->get(j))->getTypeId() ==
168 // SoVRMLIndexedLineSet::getClassTypeId())
169 // {
170 // std::cout << "> We found a line" << std::endl;
171 // SoVRMLIndexedLineSet * line_set;
172 // line_set = (SoVRMLIndexedLineSet*)child2list->get(j);
173 // extractLines(line_set);
174 // }
175 }
176 sc->bound.nbr++;
177 ifsToBound(&(sc->bound.ptr[iterShapes]), ifs_list);
178 destroyIfs(ifs_list);
179 iterShapes++;
180 }
181 }
182
183 // if (factor != 1)
184 if (std::fabs(factor) > std::numeric_limits<double>::epsilon()) {
185 for (int i = 0; i < sc->bound.nbr; i++) {
186 for (int j = 0; j < sc->bound.ptr[i].point.nbr; j++) {
187 sc->bound.ptr[i].point.ptr[j].x = sc->bound.ptr[i].point.ptr[j].x * factor;
188 sc->bound.ptr[i].point.ptr[j].y = sc->bound.ptr[i].point.ptr[j].y * factor;
189 sc->bound.ptr[i].point.ptr[j].z = sc->bound.ptr[i].point.ptr[j].z * factor;
190 }
191 }
192 }
193}
194
195void extractFaces(SoVRMLIndexedFaceSet *face_set, indexFaceSet *ifs)
196{
197 // vpList<vpPoint> pointList;
198 // pointList.kill();
199 SoVRMLCoordinate *coord = (SoVRMLCoordinate *)(face_set->coord.getValue());
200 int coordSize = coord->point.getNum();
201
202 ifs->nbPt = coordSize;
203 for (int i = 0; i < coordSize; i++) {
204 SbVec3f point(0, 0, 0);
205 point[0] = coord->point[i].getValue()[0];
206 point[1] = coord->point[i].getValue()[1];
207 point[2] = coord->point[i].getValue()[2];
208 vpPoint pt(point[0], point[1], point[2]);
209 ifs->pt.push_back(pt);
210 }
211
212 SoMFInt32 indexList = face_set->coordIndex;
213 int indexListSize = indexList.getNum();
214
215 ifs->nbIndex = indexListSize;
216 for (int i = 0; i < indexListSize; i++) {
217 int index = face_set->coordIndex[i];
218 ifs->index.push_back(index);
219 }
220}
221
222void ifsToBound(Bound *bptr, std::list<indexFaceSet *> &ifs_list)
223{
224 int nbPt = 0;
225 for (std::list<indexFaceSet *>::const_iterator it = ifs_list.begin(); it != ifs_list.end(); ++it) {
226 nbPt += (*it)->nbPt;
227 }
228 bptr->point.nbr = (Index)nbPt;
229 bptr->point.ptr = (Point3f *)malloc(static_cast<unsigned int>(nbPt) * sizeof(Point3f));
230
231 unsigned int iter = 0;
232 for (std::list<indexFaceSet *>::const_iterator it = ifs_list.begin(); it != ifs_list.end(); ++it) {
233 indexFaceSet *ifs = *it;
234 for (unsigned int j = 0; j < static_cast<unsigned int>(ifs->nbPt); j++) {
235 bptr->point.ptr[iter].x = static_cast<float>(ifs->pt[j].get_oX());
236 bptr->point.ptr[iter].y = static_cast<float>(ifs->pt[j].get_oY());
237 bptr->point.ptr[iter].z = static_cast<float>(ifs->pt[j].get_oZ());
238 iter++;
239 }
240 }
241
242 unsigned int nbFace = 0;
243 std::list<int> indSize;
244 int indice = 0;
245 for (std::list<indexFaceSet *>::const_iterator it = ifs_list.begin(); it != ifs_list.end(); ++it) {
246 indexFaceSet *ifs = *it;
247 for (unsigned int j = 0; j < static_cast<unsigned int>(ifs->nbIndex); j++) {
248 if (ifs->index[j] == -1) {
249 nbFace++;
250 indSize.push_back(indice);
251 indice = 0;
252 }
253 else
254 indice++;
255 }
256 }
257
258 bptr->face.nbr = (Index)nbFace;
259 bptr->face.ptr = (Face *)malloc(nbFace * sizeof(Face));
260
261 std::list<int>::const_iterator iter_indSize = indSize.begin();
262 for (unsigned int i = 0; i < indSize.size(); i++) {
263 bptr->face.ptr[i].vertex.nbr = (Index)*iter_indSize;
264 bptr->face.ptr[i].vertex.ptr = (Index *)malloc(static_cast<unsigned int>(*iter_indSize) * sizeof(Index));
265 ++iter_indSize;
266 }
267
268 int offset = 0;
269 indice = 0;
270 for (std::list<indexFaceSet *>::const_iterator it = ifs_list.begin(); it != ifs_list.end(); ++it) {
271 indexFaceSet *ifs = *it;
272 iter = 0;
273 for (unsigned int j = 0; j <static_cast<unsigned int>(ifs->nbIndex); j++) {
274 if (ifs->index[j] != -1) {
275 bptr->face.ptr[indice].vertex.ptr[iter] = (Index)(ifs->index[j] + offset);
276 iter++;
277 }
278 else {
279 iter = 0;
280 indice++;
281 }
282 }
283 offset += ifs->nbPt;
284 }
285}
286
287void destroyIfs(std::list<indexFaceSet *> &ifs_list)
288{
289 for (std::list<indexFaceSet *>::const_iterator it = ifs_list.begin(); it != ifs_list.end(); ++it) {
290 delete *it;
291 }
292 ifs_list.clear();
293}
294#else
295void set_scene_wrl(const char * /*str*/, Bound_scene * /*sc*/, float /*factor*/) { }
296#endif
297
298/*
299 Convert the matrix format to deal with the one in the simulator
300*/
301void vp2jlc_matrix(const vpHomogeneousMatrix &vpM, Matrix &jlcM)
302{
303 for (unsigned int i = 0; i < 4; i++) {
304 for (unsigned int j = 0; j < 4; j++)
305 jlcM[j][i] = static_cast<float>(vpM[i][j]);
306 }
307}
308END_VISP_NAMESPACE
309#endif
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ ioError
I/O error.
Definition vpException.h:67
@ notInitialized
Used to indicate that a parameter is not initialized.
Definition vpException.h:74
@ fatalError
Fatal error.
Definition vpException.h:72
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition vpPoint.h:79