ellipsoid_visualizer.cpp
Go to the documentation of this file.
00001 #include <hrl_phri_2011/ellipsoid_space.h>
00002 #include <ros/ros.h>
00003 #include <boost/random/uniform_real.hpp>
00004 #include <boost/random/mersenne_twister.hpp>
00005 #include <boost/random/variate_generator.hpp>
00006 #include <sensor_msgs/PointCloud2.h>
00007 #include <pcl_ros/point_cloud.h>
00008 #include <pcl/point_cloud.h>
00009 #include <pcl/point_types.h>
00010 #include <hrl_phri_2011/EllipsoidParams.h>
00011 #include <hrl_phri_2011/utils.h>
00012 
00013 typedef pcl::PointXYZRGB PRGB;
00014 typedef pcl::PointCloud<PRGB> PCRGB;
00015 
00016 void pubLoop(PCRGB &pc, const std::string& topic) 
00017 {
00018     ros::NodeHandle nh;
00019     ros::Publisher pub_pc = nh.advertise<sensor_msgs::PointCloud2>(topic, 1);
00020     ros::Rate r(1);
00021     sensor_msgs::PointCloud2 pc_msg;
00022     pcl::toROSMsg(pc, pc_msg);
00023     while(ros::ok()) {
00024         pc_msg.header.stamp = ros::Time::now();
00025         pub_pc.publish(pc_msg);
00026         r.sleep();
00027     }
00028 }
00029 
00030 class EllispoidVisualizer 
00031 {
00032 private:
00033     ros::Publisher pub_pc;
00034     ros::Subscriber sub_e_params;
00035     PCRGB head_pc;
00036     bool is_head;
00037 
00038 public:
00039     EllispoidVisualizer(const PCRGB& pc_head, const std::string& topic, bool use_head=true) : 
00040         head_pc(pc_head), is_head(use_head)
00041     {
00042         ros::NodeHandle nh;
00043         pub_pc = nh.advertise<sensor_msgs::PointCloud2>(topic, 1);
00044         sub_e_params = nh.subscribe("/ellipsoid_params", 1, &EllispoidVisualizer::publishEllipoid, this);
00045     }
00046     void sampleEllipse(double A, double B, double height, PCRGB& pc);
00047     void projectEllipsoid(double A, double B, double height, const PCRGB& pc, PCRGB& pc_ell);
00048     void publishEllipoid(hrl_phri_2011::EllipsoidParams::ConstPtr e_params);
00049 };
00050 
00051 void EllispoidVisualizer::sampleEllipse(double A, double B, double height, PCRGB& pc) 
00052 {
00053     Ellipsoid e(A, B);
00054     double lat = 0, lon = 0;
00055     int numlat = 8, numlon = 16;
00056     for(int i=0;i<numlat;i++) {
00057         lat += PI / numlat;
00058         lon = 0;
00059         for(int j=0;j<600;j++) {
00060             lon += 2 * PI / 600;
00061             PRGB pt;
00062             ((uint32_t*) &pt.rgb)[0] = 0xffffffff;
00063             double x, y, z;
00064             e.ellipsoidalToCart(lat, lon, height, x, y, z);
00065             pt.x = x; pt.y = y; pt.z = z;
00066             pc.points.push_back(pt);
00067         }
00068     }
00069     lat = 0; lon = 0;
00070     for(int i=0;i<numlon;i++) {
00071         lat = 0;
00072         lon += 2 * PI / numlon;
00073         for(int j=0;j<600;j++) {
00074             lat += PI / 600;
00075             PRGB pt;
00076             ((uint32_t*) &pt.rgb)[0] = 0xffffffff;
00077             double x, y, z;
00078             e.ellipsoidalToCart(lat, lon, height, x, y, z);
00079             pt.x = x; pt.y = y; pt.z = z;
00080             pc.points.push_back(pt);
00081         }
00082     }
00083 }
00084 
00085 void EllispoidVisualizer::projectEllipsoid(double A, double B, double height, const PCRGB& pc, PCRGB& pc_ell) 
00086 {
00087     Ellipsoid e(A, B);
00088     double lat, lon, hei, x, y, z; //, h, s, l;
00089     BOOST_FOREACH(PRGB const pt, pc.points) {
00090         PRGB npt;
00091         //extractHSL(pt.rgb, h, s, l);
00092         e.cartToEllipsoidal(pt.x, pt.y, pt.z, lat, lon, hei);
00093         e.ellipsoidalToCart(lat, lon, height, x, y, z);
00094         npt.x = x; npt.y = y; npt.z = z;
00095         npt.rgb = pt.rgb;
00096         //writeHSL(0, 0, l, npt.rgb);
00097         pc_ell.points.push_back(npt);
00098     }
00099     pc_ell.header.frame_id = "base_link";
00100     //pubLoop(pc_ell, "/proj_head");
00101 }
00102 
00103 void EllispoidVisualizer::publishEllipoid(hrl_phri_2011::EllipsoidParams::ConstPtr e_params) 
00104 {
00105     PCRGB pc, proj_head;
00106     double A = 1;
00107     double E = e_params->E;
00108     double B = sqrt(1 - SQ(E));
00109     pc.header.frame_id = "/ellipse_frame";
00110     sampleEllipse(A, B, e_params->height, pc);
00111     if(is_head) {
00112         projectEllipsoid(A, B, e_params->height, head_pc, proj_head);
00113         proj_head.header.frame_id = "/ellipse_frame";
00114         pc += proj_head;
00115     }
00116     pc.header.stamp = ros::Time::now();
00117     pub_pc.publish(pc);
00118 }
00119 
00120 int main(int argc, char **argv)
00121 {
00122     ros::init(argc, argv, "ellipsoid_visualizer");
00123     PCRGB pc_head;
00124     Ellipsoid ell;
00125     loadRegisteredHead(argv[1], argv[2], pc_head, ell);
00126     EllispoidVisualizer ev(pc_head, "/ellipsoid", false);
00127     ros::spin();
00128     /*
00129     ros::NodeHandle nh;
00130     double lat, lon, height, x, y, z, newx, newy, newz, newlat, newlon, newheight;
00131     boost::mt19937 rand_gen;
00132     rand_gen.seed(static_cast<uint32_t>(std::time(0)));
00133     boost::uniform_real<> rand_lat_dist(-PI/2, PI/2);
00134     boost::uniform_real<> rand_lon_dist(0, 2 * PI);
00135     boost::uniform_real<> rand_height_dist(0, 4);
00136     boost::uniform_real<> rand_x_dist(-1, 1);
00137     boost::variate_generator<boost::mt19937&, boost::uniform_real<> > rand_x(rand_gen, rand_x_dist);
00138     boost::variate_generator<boost::mt19937&, boost::uniform_real<> > rand_lat(rand_gen, rand_lat_dist);
00139     boost::variate_generator<boost::mt19937&, boost::uniform_real<> > rand_lon(rand_gen, rand_lon_dist);
00140     boost::variate_generator<boost::mt19937&, boost::uniform_real<> > rand_height(rand_gen, rand_height_dist);
00141     double maxlon = -100, minlon = 100, maxlat = -100, minlat = 100;
00142     height = 1.0;
00143     for(int i=0;i<100;i++) {
00144         x = rand_x(); y = rand_x(); z = rand_x();
00145         lat = rand_lat(); lon = rand_lon(); height = rand_height();
00146         e.ellipsoidalToCart(lat, lon, height, x, y, z);
00147         e.cartToEllipsoidal(x, y, z, newlat, newlon, newheight);
00148         printf("%f %f %f\n", lat-newlat, lon-newlon, height-newheight);
00149         //cout << x << ", " << y << ", " << z << endl;
00150         e.mollweideProjection(lat, lon, x, y);
00151         maxlon = max(lon, maxlon);
00152         minlon = min(lon, minlon);
00153         maxlat = max(lat, maxlat);
00154         minlat = min(lat, minlat);
00155     }
00156     printf("lon %f, %f; lat %f, %f\n", minlon, maxlon, minlat, maxlat);
00157     sampleEllipse(e, 0.2);
00158     */
00159     return 0;
00160 }


hrl_phri_2011
Author(s): Kelsey Hawkins
autogenerated on Wed Nov 27 2013 12:22:39