$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2011, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 #include <ros/ros.h> 00037 #include <pr2_dremel_server/Segment.h> 00038 #include <geometry_msgs/Point.h> 00039 00040 using vlr::TagLine; 00041 namespace dremel { 00042 00043 class DrawObject{ 00044 public: 00045 DrawObject(std::vector<std::vector<vlr::TagLine> > setlines, double initscale, double initx, double inity) 00046 { 00047 lines_=setlines; 00048 is_selected_=false; 00049 font_advance_=0.0; 00050 is_text_=true; 00051 updateCentroid(); 00052 } 00053 00054 DrawObject(std::vector<std::vector<vlr::TagLine> > setlines_) 00055 { 00056 lines_=setlines_; 00057 is_selected_=false; 00058 font_advance_=0.0; 00059 is_text_=true; 00060 updateCentroid(); 00061 } 00062 00063 ~DrawObject(){}; 00064 00065 void updateCentroid() { 00066 centroid_x_ = centroid_y_ = 0; 00067 int num_points = 0; 00068 std::vector<std::vector<vlr::TagLine> >& lines = getLines(); 00069 for(std::vector<std::vector<vlr::TagLine> >::iterator itL=lines.begin();itL!=lines.end();++itL) { 00070 std::vector<vlr::TagLine>& tag_lines = *itL; 00071 for(std::vector<vlr::TagLine>::iterator itT=tag_lines.begin();itT!=tag_lines.end();++itT) { 00072 vlr::TagLine& tl = *itT; 00073 centroid_x_ += tl.x0(); 00074 centroid_x_ += tl.x1(); 00075 centroid_y_ += tl.y0(); 00076 centroid_y_ += tl.y1(); 00077 num_points+=2; 00078 } 00079 } 00080 centroid_x_ /= num_points; 00081 centroid_y_ /= num_points; 00082 } 00083 00084 void move(float dx, float dy) { 00085 std::vector<std::vector<vlr::TagLine> >& lines = getLines(); 00086 for(std::vector<std::vector<vlr::TagLine> >::iterator itL=lines.begin();itL!=lines.end();++itL) { 00087 std::vector<vlr::TagLine>& tag_lines = *itL; 00088 for(std::vector<vlr::TagLine>::iterator itT=tag_lines.begin();itT!=tag_lines.end();++itT) { 00089 vlr::TagLine& tl = *itT; 00090 tl.setX0(tl.x0()+dx); 00091 tl.setX1(tl.x1()+dx); 00092 tl.setY0(tl.y0()+dy); 00093 tl.setY1(tl.y1()+dy); 00094 } 00095 } 00096 updateCentroid(); 00097 } 00098 00099 void scale(float scale) { 00100 std::vector<std::vector<vlr::TagLine> >& lines = getLines(); 00101 for(std::vector<std::vector<vlr::TagLine> >::iterator itL=lines.begin();itL!=lines.end();++itL) { 00102 std::vector<vlr::TagLine>& tag_lines = *itL; 00103 for(std::vector<vlr::TagLine>::iterator itT=tag_lines.begin();itT!=tag_lines.end();++itT) { 00104 vlr::TagLine& tl = *itT; 00105 tl.setX0((tl.x0()-centroid_x_)*scale + centroid_x_); 00106 tl.setX1((tl.x1()-centroid_x_)*scale + centroid_x_); 00107 tl.setY0((tl.y0()-centroid_y_)*scale + centroid_y_); 00108 tl.setY1((tl.y1()-centroid_y_)*scale + centroid_y_); 00109 } 00110 } 00111 updateCentroid(); 00112 } 00113 00114 bool isSelected(){ 00115 return is_selected_; 00116 } 00117 00118 void changeSelected(bool selected) { 00119 is_selected_ = selected; 00120 } 00121 00122 void addToFontAdvance(float value){ 00123 font_advance_+=value; 00124 } 00125 00126 float getFontAdvance(){ 00127 return font_advance_; 00128 } 00129 00130 void setFontAdvance(float value){ 00131 font_advance_=value; 00132 } 00133 00134 void setAsText(){ 00135 is_text_=true; 00136 } 00137 00138 void setAsNotText(){ 00139 is_text_=false; 00140 } 00141 00142 bool isText(){ 00143 return is_text_; 00144 } 00145 00146 std::vector<std::vector<vlr::TagLine> >& getLines() { 00147 return lines_; 00148 } 00149 00150 void insertNewLines(std::vector<std::vector<vlr::TagLine> > newlines){ 00151 lines_.insert(lines_.end(), newlines.begin(), newlines.end()); 00152 updateCentroid(); 00153 } 00154 00155 std::vector<pr2_dremel_server::Segment> convertToSegments(){ 00156 std::vector<pr2_dremel_server::Segment> segments; 00157 00158 for(std::vector<std::vector<TagLine> >::iterator it=lines_.begin();it!=lines_.end();++it) { 00159 std::vector<TagLine>& line_segment = *it; 00160 std::vector<geometry_msgs::Point> points; 00161 pr2_dremel_server::Segment segment; 00162 00163 for(std::vector<TagLine>::iterator itT=line_segment.begin();itT!=line_segment.end();++itT) { 00164 TagLine& line = *itT; 00165 geometry_msgs::Point p0,p1; 00166 p0.x = line.x0()*0.0005; 00167 p0.y = line.y0()*0.0005; 00168 p0.z=0; 00169 points.push_back(p0); 00170 p1.x = line.x1()*0.0005; 00171 p1.y = line.y1()*0.0005; 00172 p1.z=0; 00173 points.push_back(p1); 00174 } 00175 segment.points=points; 00176 segments.push_back(segment); 00177 } 00178 return segments; 00179 } 00180 00181 private: 00182 std::vector<std::vector<vlr::TagLine> > lines_; 00183 bool is_selected_; 00184 float font_advance_; 00185 bool is_text_; 00186 float centroid_x_,centroid_y_; 00187 }; 00188 00189 } 00190