00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <uwsim/SceneBuilder.h>
00014
00015 #include <osg/Notify>
00016
00017 #include <string>
00018 #include <vector>
00019
00020 #include <uwsim/osgOceanScene.h>
00021 #include <uwsim/HUDCamera.h>
00022 #include <uwsim/VirtualRangeSensor.h>
00023 #include <uwsim/ROSInterface.h>
00024 #include <uwsim/UWSimUtils.h>
00025 #include <uwsim/TrajectoryVisualization.h>
00026
00027 using namespace std;
00028
00029 SceneBuilder::SceneBuilder()
00030 {
00031 int argc = 0;
00032 char **argv = NULL;
00033 arguments.reset(new osg::ArgumentParser(&argc, argv));
00034 }
00035
00036 SceneBuilder::SceneBuilder(int *argc, char **argv)
00037 {
00038 arguments.reset(new osg::ArgumentParser(argc, argv));
00039 }
00040
00041 SceneBuilder::SceneBuilder(boost::shared_ptr<osg::ArgumentParser> args)
00042 {
00043 arguments = args;
00044 }
00045
00046 bool SceneBuilder::loadScene(std::string xml_file)
00047 {
00048 ConfigFile config(xml_file);
00049
00050 return loadScene(config);
00051 }
00052
00055 bool SceneBuilder::loadScene(ConfigFile config)
00056 {
00057 float windx = config.windx, windy = config.windy;
00058 while (arguments->read("--windx", windx))
00059 ;
00060 while (arguments->read("--windy", windy))
00061 ;
00062 osg::Vec2f windDirection(windx, windy);
00063
00064 float windSpeed = config.windSpeed;
00065 while (arguments->read("--windSpeed", windSpeed))
00066 ;
00067
00068 float depth = config.depth;
00069
00070
00071 float reflectionDamping = config.reflectionDamping;
00072 while (arguments->read("--reflectionDamping", reflectionDamping))
00073 ;
00074
00075 float reswidth = config.resw, resheight = config.resh;
00076 while (arguments->read("--resw", reswidth))
00077 ;
00078 while (arguments->read("--resh", resheight))
00079 ;
00080
00081 float scale = config.waveScale;
00082 while (arguments->read("--waveScale", scale))
00083 ;
00084
00085 bool isChoppy = not config.isNotChoppy;
00086 while (arguments->read("--isNotChoppy"))
00087 isChoppy = false;
00088
00089 float choppyFactor = config.choppyFactor;
00090 while (arguments->read("--choppyFactor", choppyFactor))
00091 ;
00092 choppyFactor = -choppyFactor;
00093
00094 float crestFoamHeight = config.crestFoamHeight;
00095 while (arguments->read("--crestFoamHeight", crestFoamHeight))
00096 ;
00097
00098 double oceanSurfaceHeight = config.oceanSurfaceHeight;
00099 while (arguments->read("--oceanSurfaceHeight", oceanSurfaceHeight))
00100 ;
00101
00102 bool disableShaders = config.disableShaders;
00103 if (arguments->read("--disableShaders"))
00104 disableShaders = true;
00105
00106 bool disableTextures = false;
00107 if (arguments->read("--disableTextures"))
00108 disableTextures = true;
00109
00110 bool freeMotion = config.freeMotion;
00111 if (arguments->read("--freeMotion"))
00112 {
00113 freeMotion = true;
00114 }
00115
00116 osgOcean::ShaderManager::instance().enableShaders(!disableShaders);
00117
00118 root = new osg::Group;
00119
00120
00121 scene = new osgOceanScene(config.offsetp, config.offsetr, windDirection, windSpeed, depth, reflectionDamping, scale,
00122 isChoppy, choppyFactor, crestFoamHeight, false, "terrain");
00123
00124 if (disableShaders)
00125 {
00126
00127 scene->getOceanScene()->enableDistortion(false);
00128 scene->getOceanScene()->enableGlare(false);
00129 scene->getOceanScene()->enableUnderwaterDOF(false);
00130
00131
00132 scene->getOceanScene()->enableUnderwaterScattering(false);
00133
00134
00135 scene->getOceanScene()->enableReflections(false);
00136 scene->getOceanScene()->enableRefractions(false);
00137 scene->getOceanScene()->enableGodRays(false);
00138 scene->getOceanScene()->enableSilt(false);
00139 }
00140 else
00141 {
00142 static const char model_vertex[] = "default_scene.vert";
00143 static const char model_fragment[] = "default_scene.frag";
00144 osg::Program* program = osgOcean::ShaderManager::instance().createProgram("object_shader", model_vertex,model_fragment, "", "");
00145 scene->getOceanScene()->setDefaultSceneShader(program);
00146
00147 root->getOrCreateStateSet()->addUniform(new osg::Uniform("uOverlayMap", 1));
00148 root->getStateSet()->addUniform(new osg::Uniform("uNormalMap", 2));
00149 root->getStateSet()->addUniform(new osg::Uniform("SLStex", 3));
00150 root->getStateSet()->addUniform(new osg::Uniform("SLStex2", 4));
00151 root->getStateSet()->addUniform( new osg::Uniform("stddev", 0.0f ) );
00152 root->getStateSet()->addUniform( new osg::Uniform("mean", 0.0f ) );
00153 root->getStateSet()->addUniform( new osg::Uniform("light", (float)config.lightRate ) );
00154 }
00155
00156 scene->getOceanScene()->setOceanSurfaceHeight(oceanSurfaceHeight);
00157 scene->getOceanScene()->setUnderwaterFog(config.fogDensity,
00158 osg::Vec4f(config.fogColor[0], config.fogColor[1], config.fogColor[2], 1));
00159 scene->getOceanScene()->setUnderwaterDiffuse(osg::Vec4f(config.color[0], config.color[1], config.color[2], 1));
00160 scene->getOceanScene()->setUnderwaterAttenuation(
00161 osg::Vec3f(config.attenuation[0], config.attenuation[1], config.attenuation[2]));
00162
00163
00164 int nvehicle = config.vehicles.size();
00165 int slsProjectors=0;
00166 for (int i = 0; i < nvehicle; i++)
00167 {
00168 Vehicle vehicle = config.vehicles.front();
00169 boost::shared_ptr < SimulatedIAUV > siauv(new SimulatedIAUV(this, vehicle));
00170 iauvFile.push_back(siauv);
00171 config.vehicles.pop_front();
00172
00173 scene->addObject(iauvFile[i]->baseTransform);
00174
00175 siauv->setVehiclePosition(vehicle.position[0], vehicle.position[1], vehicle.position[2], vehicle.orientation[0],
00176 vehicle.orientation[1], vehicle.orientation[2]);
00177
00178 for (int j = 0; j < vehicle.nlinks; j++)
00179 {
00180 NodeDataType * data = new NodeDataType(0);
00181 siauv->urdf->link[j]->setUserData(data);
00182 }
00183
00184 if (vehicle.jointValues.size() && siauv->urdf != NULL)
00185 {
00186 siauv->urdf->setJointPosition(vehicle.jointValues);
00187 }
00188
00189 slsProjectors+=vehicle.sls_projectors.size();
00190 }
00191
00192
00193 if(slsProjectors==0)
00194 {
00195 root->getOrCreateStateSet()->addUniform(new osg::Uniform("sls_projector", false));
00196 }
00197 else
00198 {
00199 root->getOrCreateStateSet()->addUniform(new osg::Uniform("sls_projector", true));
00200 }
00201
00202
00203 while (config.objects.size() > 0)
00204 {
00205 Object auxObject = config.objects.front();
00206
00207 osg::Matrixd wMb_m;
00208 wMb_m.makeRotate(
00209 osg::Quat(auxObject.orientation[0], osg::Vec3d(1, 0, 0), auxObject.orientation[1], osg::Vec3d(0, 1, 0),
00210 auxObject.orientation[2], osg::Vec3d(0, 0, 1)));
00211 wMb_m.setTrans(auxObject.position[0], auxObject.position[1], auxObject.position[2]);
00212 wMb_m.preMultScale(osg::Vec3d(auxObject.scale[0],auxObject.scale[1],auxObject.scale[2]));
00213
00214 osg::ref_ptr < osg::MatrixTransform > wMb = new osg::MatrixTransform(wMb_m);
00215 osg::Node *object = scene->addObject(wMb, auxObject.file, &auxObject);
00216 object->setName(auxObject.name);
00217
00218
00219 if (auxObject.name != "terrain")
00220 {
00221 NodeDataType * data = new NodeDataType(1, auxObject.position, auxObject.orientation);
00222 object->setUserData(data);
00223 }
00224 else
00225 {
00226 NodeDataType * data = new NodeDataType(0);
00227 object->setUserData(data);
00228 }
00229
00230 if (auxObject.buried>0)
00231 {
00232 osg::Node* mud=createHeightField(object,"sand2.jpg",auxObject.buried, iauvFile);
00233 object->asGroup()->addChild(mud);
00234 }
00235
00236 objects.push_back(object);
00237 config.objects.pop_front();
00238 }
00239
00240
00241 root->addChild(scene->getScene());
00242
00243
00244
00245
00246 OSG_INFO << "Setting vehicle virtual cameras" << std::endl;
00247 for (int j = 0; j < nvehicle; j++)
00248 {
00249 for (unsigned int i = 0; i < iauvFile[j]->getNumCams(); i++)
00250 {
00251 iauvFile[j]->camview[i].textureCamera->addChild(scene->getScene());
00252 root->addChild(iauvFile[j]->camview[i].textureCamera);
00253
00254
00255 osg::Fog *fog = new osg::Fog();
00256 fog->setMode(osg::Fog::EXP2);
00257 fog->setFogCoordinateSource(osg::Fog::FRAGMENT_DEPTH);
00258 fog->setDensity(config.fogDensity);
00259 fog->setColor(osg::Vec4d(config.fogColor[0], config.fogColor[1], config.fogColor[2], 1));
00260 iauvFile[j]->camview[i].textureCamera->getOrCreateStateSet()->setAttributeAndModes(fog, osg::StateAttribute::ON);
00261 }
00262 }
00263
00264 OSG_INFO << "Setting interfaces with external software..." << std::endl;
00265 while (config.ROSInterfaces.size() > 0)
00266 {
00267 ROSInterfaceInfo rosInterface = config.ROSInterfaces.front();
00268
00269 boost::shared_ptr < ROSInterface > iface;
00270 if (rosInterface.type == ROSInterfaceInfo::ROSOdomToPAT)
00271 iface = boost::shared_ptr < ROSOdomToPAT
00272 > (new ROSOdomToPAT(root, rosInterface.topic, rosInterface.targetName));
00273
00274 if (rosInterface.type == ROSInterfaceInfo::ROSTwistToPAT)
00275 iface = boost::shared_ptr < ROSTwistToPAT
00276 > (new ROSTwistToPAT(root, rosInterface.topic, rosInterface.targetName));
00277
00278 if (rosInterface.type == ROSInterfaceInfo::PATToROSOdom)
00279 iface = boost::shared_ptr < PATToROSOdom
00280 > (new PATToROSOdom(root, rosInterface.targetName, rosInterface.topic, rosInterface.rate));
00281
00282 if (rosInterface.type == ROSInterfaceInfo::WorldToROSTF)
00283 {
00284 iface = boost::shared_ptr < WorldToROSTF
00285 > (new WorldToROSTF(root, iauvFile, objects, rosInterface.rootName, rosInterface.enableObjects, rosInterface.rate));
00286
00287 }
00288 if (rosInterface.type == ROSInterfaceInfo::ROSPointCloudLoader)
00289 {
00290 iface = boost::shared_ptr < ROSPointCloudLoader
00291 > (new ROSPointCloudLoader(rosInterface.topic,root,scene->getOceanScene()->getARMask(),rosInterface.del));
00292
00293 }
00294 if (rosInterface.type == ROSInterfaceInfo::ROSJointStateToArm
00295 || rosInterface.type == ROSInterfaceInfo::ArmToROSJointState)
00296 {
00297 int correspondences=0;
00298
00299 for (int j = 0; j < nvehicle; j++)
00300 {
00301 if (iauvFile[j]->name == rosInterface.targetName)
00302 {
00303 if (rosInterface.type == ROSInterfaceInfo::ROSJointStateToArm)
00304 iface = boost::shared_ptr < ROSJointStateToArm > (new ROSJointStateToArm(rosInterface.topic, iauvFile[j]));
00305 else
00306 iface = boost::shared_ptr < ArmToROSJointState
00307 > (new ArmToROSJointState(iauvFile[j].get(), rosInterface.topic, rosInterface.rate));
00308 correspondences++;
00309 }
00310 }
00311 if(correspondences==0)
00312 ROS_WARN ("Arm ROSInterface is not able to find %s vehicle.",rosInterface.targetName.c_str());
00313 else if (correspondences > 1)
00314 ROS_WARN ("Arm ROSInterface more than one %s vehicle.",rosInterface.targetName.c_str());
00315 }
00316
00317 if (rosInterface.type == ROSInterfaceInfo::VirtualCameraToROSImage)
00318 {
00319 int correspondences=0;
00320
00321 for (int j = 0; j < nvehicle; j++)
00322 {
00323 for (unsigned int c = 0; c < iauvFile[j]->getNumCams(); c++)
00324 if (iauvFile[j]->camview[c].name == rosInterface.targetName)
00325 {
00326 iface = boost::shared_ptr < VirtualCameraToROSImage
00327 > (new VirtualCameraToROSImage(&(iauvFile[j]->camview[c]), rosInterface.topic, rosInterface.infoTopic,
00328 rosInterface.rate, rosInterface.depth));
00329 correspondences++;
00330 }
00331 }
00332 if(correspondences==0)
00333 ROS_WARN ("VirtualCameraToROSInterface is not able to find %s camera.",rosInterface.targetName.c_str());
00334 else if (correspondences > 1)
00335 ROS_WARN ("VirtualCameraToROSInterface more than one %s cameras.",rosInterface.targetName.c_str());
00336 }
00337
00338 if (rosInterface.type == ROSInterfaceInfo::RangeImageSensorToROSImage)
00339 {
00340 int correspondences=0;
00341
00342 for (int j = 0; j < nvehicle; j++)
00343 {
00344 for (unsigned int c = 0; c < iauvFile[j]->getNumCams(); c++)
00345 if (iauvFile[j]->camview[c].name == rosInterface.targetName)
00346 {
00347 iface = boost::shared_ptr < VirtualCameraToROSImage
00348 > (new VirtualCameraToROSImage(&(iauvFile[j]->camview[c]), rosInterface.topic, rosInterface.infoTopic,
00349 rosInterface.rate, 1));
00350 correspondences++;
00351 }
00352 }
00353 if(correspondences==0)
00354 ROS_WARN ("RangeImageSensorToROSImage is not able to find %s sensor.",rosInterface.targetName.c_str());
00355 else if (correspondences > 1)
00356 ROS_WARN ("RangeImageSensorToROSImage more than one %s sensors.",rosInterface.targetName.c_str());
00357 }
00358
00359 if (rosInterface.type == ROSInterfaceInfo::ROSImageToHUD)
00360 {
00361 boost::shared_ptr < HUDCamera
00362 > realcam(
00363 new HUDCamera(rosInterface.w, rosInterface.h, rosInterface.posx, rosInterface.posy, rosInterface.scale,
00364 rosInterface.blackWhite));
00365 iface = boost::shared_ptr < ROSImageToHUDCamera
00366 > (new ROSImageToHUDCamera(rosInterface.topic, rosInterface.infoTopic, realcam));
00367 realcams.push_back(realcam);
00368 }
00369
00370 if (rosInterface.type == ROSInterfaceInfo::RangeSensorToROSRange)
00371 {
00372 int correspondences=0;
00373
00374 for (int j = 0; j < nvehicle; j++)
00375 {
00376 for (unsigned int c = 0; c < iauvFile[j]->getNumRangeSensors(); c++)
00377 if (iauvFile[j]->range_sensors[c].name == rosInterface.targetName)
00378 {
00379 iface = boost::shared_ptr < RangeSensorToROSRange
00380 > (new RangeSensorToROSRange(&(iauvFile[j]->range_sensors[c]), rosInterface.topic, rosInterface.rate));
00381 correspondences++;
00382 }
00383
00384 for (unsigned int c = 0; c < iauvFile[j]->getNumObjectPickers(); c++)
00385 if (iauvFile[j]->object_pickers[c].name == rosInterface.targetName)
00386 {
00387 iface = boost::shared_ptr < RangeSensorToROSRange
00388 > (new RangeSensorToROSRange(&(iauvFile[j]->object_pickers[c]), rosInterface.topic, rosInterface.rate));
00389 correspondences++;
00390 }
00391 }
00392 if(correspondences==0)
00393 ROS_WARN ("RangeSensorToROSRange is not able to find %s sensor.",rosInterface.targetName.c_str());
00394 else if (correspondences > 1)
00395 ROS_WARN ("RangeSensorToROSRange more than one %s sensors.",rosInterface.targetName.c_str());
00396 }
00397
00398 if (rosInterface.type == ROSInterfaceInfo::ImuToROSImu)
00399 {
00400 int correspondences=0;
00401
00402 for (int j = 0; j < nvehicle; j++)
00403 {
00404 for (unsigned int i = 0; i < iauvFile[j]->imus.size(); i++)
00405 if (iauvFile[j]->imus[i].name == rosInterface.targetName)
00406 {
00407 iface = boost::shared_ptr < ImuToROSImu
00408 > (new ImuToROSImu(&(iauvFile[j]->imus[i]), rosInterface.topic, rosInterface.rate));
00409 correspondences++;
00410 }
00411 }
00412 if(correspondences==0)
00413 ROS_WARN ("ImuToROSImu is not able to find %s sensor.",rosInterface.targetName.c_str());
00414 else if (correspondences > 1)
00415 ROS_WARN ("ImuToROSImu more than one %s sensors.",rosInterface.targetName.c_str());
00416 }
00417
00418 if (rosInterface.type == ROSInterfaceInfo::PressureSensorToROS)
00419 {
00420 int correspondences=0;
00421 for (int j = 0; j < nvehicle; j++)
00422 {
00423 for (unsigned int i = 0; i < iauvFile[j]->pressure_sensors.size(); i++)
00424 if (iauvFile[j]->pressure_sensors[i].name == rosInterface.targetName)
00425 {
00426 iface = boost::shared_ptr < PressureSensorToROS
00427 > (new PressureSensorToROS(&(iauvFile[j]->pressure_sensors[i]), rosInterface.topic, rosInterface.rate));
00428 correspondences++;
00429 }
00430 }
00431 if(correspondences==0)
00432 ROS_WARN ("PressureSensorToROS is not able to find %s sensor.",rosInterface.targetName.c_str());
00433 else if (correspondences > 1)
00434 ROS_WARN ("PressureSensorToROS more than one %s sensors.",rosInterface.targetName.c_str());
00435 }
00436
00437 if (rosInterface.type == ROSInterfaceInfo::GPSSensorToROS)
00438 {
00439 int correspondences=0;
00440 for (int j = 0; j < nvehicle; j++)
00441 {
00442 for (unsigned int i = 0; i < iauvFile[j]->gps_sensors.size(); i++)
00443 if (iauvFile[j]->gps_sensors[i].name == rosInterface.targetName)
00444 {
00445 iface = boost::shared_ptr < GPSSensorToROS
00446 > (new GPSSensorToROS(&(iauvFile[j]->gps_sensors[i]), rosInterface.topic, rosInterface.rate));
00447 correspondences++;
00448 }
00449 }
00450 if(correspondences==0)
00451 ROS_WARN ("GPSSensorToROS is not able to find %s sensor.",rosInterface.targetName.c_str());
00452 else if (correspondences > 1)
00453 ROS_WARN ("GPSSensorToROS more than one %s sensors.",rosInterface.targetName.c_str());
00454 }
00455
00456 if (rosInterface.type == ROSInterfaceInfo::DVLSensorToROS)
00457 {
00458 int correspondences=0;
00459 for (int j = 0; j < nvehicle; j++)
00460 {
00461 for (unsigned int i = 0; i < iauvFile[j]->dvl_sensors.size(); i++)
00462 if (iauvFile[j]->dvl_sensors[i].name == rosInterface.targetName)
00463 {
00464 iface = boost::shared_ptr < DVLSensorToROS
00465 > (new DVLSensorToROS(&(iauvFile[j]->dvl_sensors[i]), rosInterface.topic, rosInterface.rate));
00466 correspondences++;
00467 }
00468 }
00469 if(correspondences==0)
00470 ROS_WARN ("DVLSensorToROS is not able to find %s sensor.",rosInterface.targetName.c_str());
00471 else if (correspondences > 1)
00472 ROS_WARN ("DVLSensorToROS more than one %s sensors.",rosInterface.targetName.c_str());
00473 }
00474
00475 if (rosInterface.type == ROSInterfaceInfo::multibeamSensorToLaserScan)
00476 {
00477 int correspondences=0;
00478 for (int j = 0; j < nvehicle; j++)
00479 {
00480 for (unsigned int i = 0; i < iauvFile[j]->multibeam_sensors.size(); i++)
00481 if (iauvFile[j]->multibeam_sensors[i].name == rosInterface.targetName)
00482 {
00483 iface =
00484 boost::shared_ptr < MultibeamSensorToROS
00485 > (new MultibeamSensorToROS(&(iauvFile[j]->multibeam_sensors[i]), rosInterface.topic,
00486 rosInterface.rate));
00487 correspondences++;
00488 }
00489 }
00490 if(correspondences==0)
00491 ROS_WARN ("multibeamSensorToLaserScan is not able to find %s sensor.",rosInterface.targetName.c_str());
00492 else if (correspondences > 1)
00493 ROS_WARN ("multibeamSensorToLaserScan more than one %s sensors.",rosInterface.targetName.c_str());
00494 }
00495
00496 if (rosInterface.type == ROSInterfaceInfo::ROSPoseToPAT)
00497 iface = boost::shared_ptr < ROSPoseToPAT > (new ROSPoseToPAT(root, rosInterface.topic, rosInterface.targetName));
00498
00499 if (rosInterface.type == ROSInterfaceInfo::SimulatedDevice)
00500 {
00501 std::vector < boost::shared_ptr<ROSInterface> > ifaces = SimulatedDevices::getInterfaces(rosInterface, iauvFile);
00502 for (size_t i = 0; i < ifaces.size(); ++i)
00503 ROSInterfaces.push_back(ifaces[i]);
00504 }
00505
00506 if (iface)
00507 ROSInterfaces.push_back(iface);
00508 config.ROSInterfaces.pop_front();
00509 }
00510
00511
00512 while (config.trajectories.size() > 0)
00513 {
00514 ShowTrajectory trajectory = config.trajectories.front();
00515
00516 osg::ref_ptr<TrajectoryUpdateCallback> node_tracker = new TrajectoryUpdateCallback(trajectory.color, 0.02,trajectory.lineStyle,
00517 trajectory.timeWindow, root, scene->getOceanScene()->getARMask());
00518
00519 osg::Node * trackNode=findRN(trajectory.target,root);
00520 if(trackNode)
00521 {
00522 trackNode->setUpdateCallback(node_tracker);
00523 trajectories.push_back(trackNode);
00524 }
00525 else
00526 OSG_FATAL << "Scene Builder: "<<trajectory.target<<" trajectory target not found, please check your XML."<<std::endl;
00527
00528 config.trajectories.pop_front();
00529 }
00530
00531 return true;
00532 }
00533
00534 SceneBuilder::~SceneBuilder()
00535 {
00536
00537
00538
00539
00540
00541 }
00542