marker_shapes_impl.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include <string>
00019 
00020 #include "cob_obstacle_distance/marker_shapes/marker_shapes.hpp"
00021 #include "cob_obstacle_distance/parsers/mesh_parser.hpp"
00022 
00023 /* BEGIN MarkerShape ********************************************************************************************/
00024 MarkerShape<BVH_RSS_t>::MarkerShape(const std::string& root_frame,
00025                                     const shape_msgs::Mesh& mesh,
00026                                     const geometry_msgs::Pose& pose,
00027                                     const std_msgs::ColorRGBA& col)
00028 {
00029     this->ptr_fcl_bvh_.reset(new BVH_RSS_t());
00030     this->ptr_fcl_bvh_->beginModel();
00031     for (shape_msgs::MeshTriangle tri : mesh.triangles)
00032     {
00033         uint32_t v_idx_1 = tri.vertex_indices.elems[0];
00034         uint32_t v_idx_2 = tri.vertex_indices.elems[1];
00035         uint32_t v_idx_3 = tri.vertex_indices.elems[2];
00036 
00037         fcl::Vec3f v1(mesh.vertices[v_idx_1].x, mesh.vertices[v_idx_1].y, mesh.vertices[v_idx_1].z);
00038         fcl::Vec3f v2(mesh.vertices[v_idx_2].x, mesh.vertices[v_idx_2].y, mesh.vertices[v_idx_2].z);
00039         fcl::Vec3f v3(mesh.vertices[v_idx_3].x, mesh.vertices[v_idx_3].y, mesh.vertices[v_idx_3].z);
00040 
00041         this->ptr_fcl_bvh_->addTriangle(v1, v2, v3);
00042     }
00043 
00044     this->ptr_fcl_bvh_->endModel();
00045     this->ptr_fcl_bvh_->computeLocalAABB();
00046 
00047     marker_.pose = pose;
00048     marker_.color = col;
00049 
00050     marker_.scale.x = 1.0;
00051     marker_.scale.y = 1.0;
00052     marker_.scale.z = 1.0;
00053     marker_.type = visualization_msgs::Marker::MESH_RESOURCE;
00054 
00055     marker_.header.frame_id = root_frame;
00056     marker_.header.stamp = ros::Time::now();
00057     marker_.ns = g_marker_namespace;
00058     marker_.action = visualization_msgs::Marker::ADD;
00059     marker_.id = IMarkerShape::class_ctr_;
00060     marker_.mesh_resource = "";  // TODO: Not given in this case: can happen e.g. when moveit_msgs/CollisionObject was given!
00061 
00062     marker_.lifetime = ros::Duration();
00063 }
00064 
00065 
00066 MarkerShape<BVH_RSS_t>::MarkerShape(const std::string& root_frame, const std::string& mesh_resource,
00067       double x, double y, double z,
00068       double quat_x, double quat_y, double quat_z, double quat_w,
00069       double color_r, double color_g, double color_b, double color_a)
00070 {
00071     this->init(mesh_resource, root_frame, x, y, z, quat_x, quat_y, quat_z, quat_w, color_r, color_g, color_b, color_a);
00072 }
00073 
00074 
00075 void MarkerShape<BVH_RSS_t>::init(const std::string& mesh_resource, const std::string& root_frame, double x, double y, double z,
00076           double quat_x, double quat_y, double quat_z, double quat_w,
00077           double color_r, double color_g, double color_b, double color_a)
00078 {
00079     MeshParser sp(mesh_resource);
00080     this->ptr_fcl_bvh_.reset(new BVH_RSS_t());
00081 
00082     if (0 != sp.createBVH(this->ptr_fcl_bvh_))
00083     {
00084         ROS_ERROR("Could not create BVH model!");
00085     }
00086 
00087     marker_.pose.position.x = origin_.position.x = x;
00088     marker_.pose.position.y = origin_.position.y = y;
00089     marker_.pose.position.z = origin_.position.z = z;
00090     marker_.pose.orientation.x = origin_.orientation.x = quat_x;
00091     marker_.pose.orientation.y = origin_.orientation.y = quat_y;
00092     marker_.pose.orientation.z = origin_.orientation.z = quat_z;
00093     marker_.pose.orientation.w = origin_.orientation.w = quat_w;
00094 
00095     marker_.color.r = color_r;
00096     marker_.color.g = color_g;
00097     marker_.color.b = color_b;
00098     marker_.color.a = color_a;
00099 
00100     marker_.scale.x = 1.0;
00101     marker_.scale.y = 1.0;
00102     marker_.scale.z = 1.0;
00103     marker_.type = visualization_msgs::Marker::MESH_RESOURCE;
00104 
00105     marker_.header.frame_id = root_frame;
00106     marker_.header.stamp = ros::Time::now();
00107     marker_.ns = g_marker_namespace;
00108     marker_.action = visualization_msgs::Marker::ADD;
00109     marker_.id = IMarkerShape::class_ctr_;
00110     marker_.mesh_resource = mesh_resource;
00111     marker_.mesh_use_embedded_materials = true;
00112 
00113     marker_.lifetime = ros::Duration();
00114 }
00115 
00116 
00117 inline geometry_msgs::Pose MarkerShape<BVH_RSS_t>::getMarkerPose() const
00118 {
00119     return this->marker_.pose;
00120 }
00121 
00122 
00123 inline geometry_msgs::Pose MarkerShape<BVH_RSS_t>::getOriginRelToFrame() const
00124 {
00125     return this->origin_;
00126 }
00127 
00128 
00129 inline uint32_t MarkerShape<BVH_RSS_t>::getId() const
00130 {
00131     return this->marker_.id;
00132 }
00133 
00134 
00135 inline void MarkerShape<BVH_RSS_t>::setColor(double color_r, double color_g, double color_b, double color_a)
00136 {
00137     marker_.color.r = color_r;
00138     marker_.color.g = color_g;
00139     marker_.color.b = color_b;
00140     marker_.color.a = color_a;
00141 }
00142 
00143 
00144 inline void MarkerShape<BVH_RSS_t>::updatePose(const geometry_msgs::Vector3& pos, const geometry_msgs::Quaternion& quat)
00145 {
00146     marker_.pose.position.x = pos.x;
00147     marker_.pose.position.y = pos.y;
00148     marker_.pose.position.z = pos.z;
00149     marker_.pose.orientation = quat;
00150 }
00151 
00152 
00153 inline void MarkerShape<BVH_RSS_t>::updatePose(const geometry_msgs::Pose& pose)
00154 {
00155     marker_.pose = pose;
00156 }
00157 
00158 
00159 inline visualization_msgs::Marker MarkerShape<BVH_RSS_t>::getMarker()
00160 {
00161     this->marker_.header.stamp = ros::Time::now();
00162     return this->marker_;
00163 }
00164 
00165 
00166 fcl::CollisionObject MarkerShape<BVH_RSS_t>::getCollisionObject() const
00167 {
00168     fcl::Transform3f x(fcl::Quaternion3f(this->marker_.pose.orientation.w,
00169                                          this->marker_.pose.orientation.x,
00170                                          this->marker_.pose.orientation.y,
00171                                          this->marker_.pose.orientation.z),
00172                        fcl::Vec3f(this->marker_.pose.position.x,
00173                                   this->marker_.pose.position.y,
00174                                   this->marker_.pose.position.z));
00175 
00176     fcl::CollisionObject cobj(this->ptr_fcl_bvh_, x);
00177     return cobj;
00178 }
00179 
00180 /* END MarkerShape **********************************************************************************************/


cob_obstacle_distance
Author(s): Marco Bezzon
autogenerated on Thu Jun 6 2019 21:19:14