contrib/brl/bbas/bxml/bsvg/bsvg_element.cxx
Go to the documentation of this file.
00001 #include "bsvg_element.h"
00002 //:
00003 // \file
00004 // \author Ozge C. Ozcanli (Brown)
00005 // \date   April 21, 2009
00006 
00007 #include <vcl_cmath.h>
00008 #include <vcl_sstream.h>
00009 
00010 void bsvg_element::set_transformation(float trans_x, float trans_y, float rot)
00011 {
00012   vcl_stringstream trans;
00013   trans << "translate(" << trans_x << ',' << trans_y << ") rotate(" << rot << ')';
00014   this->set_attribute("transform", trans.str());
00015 }
00016 
00017 void bsvg_element::set_location(float trans_x, float trans_y)
00018 {
00019   vcl_stringstream trans;
00020   trans << "translate(" << trans_x << ',' << trans_y << ") ";
00021   this->set_attribute("transform", trans.str());
00022 }
00023 
00024 //: adds rotation to an existing translation if any
00025 void bsvg_element::set_rotation(float rot)
00026 {
00027   vcl_stringstream trans;
00028   trans << "rotate(" << rot << ')';
00029 
00030   vcl_string val;
00031   if (this->get_attribute("transform", val))
00032     val = val + " " + trans.str();
00033   else
00034     val = trans.str();
00035 
00036   this->set_attribute("transform", val);
00037 }
00038 
00039 void bsvg_element::set_fill_color(const vcl_string& c)
00040 {
00041   this->set_attribute("fill", c);
00042 }
00043 
00044 vcl_string hex_value(unsigned red, unsigned green, unsigned blue)
00045 {
00046   vcl_stringstream out; out << '#';
00047   unsigned first = red%16;
00048   unsigned second = red/16;
00049   char fc;
00050   if (second > 9) fc = 65 + (second-10);
00051   else fc = 48 + second;
00052   out << fc;
00053   if (first > 9) fc = 65 + (first-10);
00054   else fc = 48 + first;
00055   out << fc;
00056   first = green%16;
00057   second = green/16;
00058   if (second > 9) fc = 65 + (second-10);
00059   else fc = 48 + second;
00060   out << fc;
00061   if (first > 9) fc = 65 + (first-10);
00062   else fc = 48 + first;
00063   out << fc;
00064   first = blue%16;
00065   second = blue/16;
00066   if (second > 9) fc = 65 + (second-10);
00067   else fc = 48 + second;
00068   out << fc;
00069   if (first > 9) fc = 65 + (first-10);
00070   else fc = 48 + first;
00071   out << fc;
00072   return out.str();
00073 }
00074 
00075 //: turns the given red, green, blue values in range [0,255] to #00 00 00 notation (Hex color) four bytes for each color
00076 void bsvg_element::set_fill_color(unsigned red, unsigned green, unsigned blue)
00077 {
00078   vcl_string hexval = hex_value(red, green, blue);
00079   this->set_attribute("fill", hexval);
00080 }
00081 
00082 void bsvg_element::set_stroke_color(const vcl_string& c)
00083 {
00084   this->set_attribute("stroke", c);
00085 }
00086 
00087 //: turns the given red, green, blue values in range [0,255] to #00 00 00 notation (Hex color) four bytes for each color
00088 void bsvg_element::set_stroke_color(unsigned red, unsigned green, unsigned blue)
00089 {
00090   vcl_string hexval = hex_value(red, green, blue);
00091   this->set_attribute("stroke", hexval);
00092 }
00093 
00094 void bsvg_element::set_stroke_width(float w)
00095 {
00096   vcl_stringstream sw; sw << w;
00097   this->set_attribute("stroke-width", sw.str());
00098 }
00099 
00100 //: 0 <= opacity <= 1
00101 void bsvg_element::set_fill_opacity(float o)
00102 {
00103   vcl_stringstream os; os << o;
00104   this->set_attribute("fill-opacity", os.str());
00105 }
00106 
00107 //: 0 <= opacity <= 1
00108 void bsvg_element::set_stroke_opacity(float o)
00109 {
00110   vcl_stringstream os; os << o;
00111   this->set_attribute("stroke-opacity", os.str());
00112 }
00113 
00114 void bsvg_text::set_font_size(int s)
00115 {
00116   vcl_stringstream fs; fs << s;
00117   this->set_attribute("font-size", fs.str());
00118 }
00119 
00120 bsvg_ellipse::bsvg_ellipse(float rx, float ry) : bsvg_element("ellipse")
00121 {
00122   vcl_stringstream rxs; rxs << rx; vcl_stringstream rys; rys << ry;
00123   this->set_attribute("rx", rxs.str());
00124   this->set_attribute("ry", rys.str());
00125 }
00126 
00127 bsvg_rectangle::bsvg_rectangle(float x, float y, float width, float height) : bsvg_element("rect")
00128 {
00129   vcl_stringstream xs; xs << x; vcl_stringstream ys; ys << y;
00130   vcl_stringstream ws; ws << width; vcl_stringstream hs; hs << height;
00131 
00132   this->set_attribute("x", xs.str());
00133   this->set_attribute("y", ys.str());
00134   this->set_attribute("width", ws.str());
00135   this->set_attribute("height", hs.str());
00136 }
00137 
00138 bsvg_line::bsvg_line(float x1, float y1, float x2, float y2) : bsvg_element("line")
00139 {
00140   vcl_stringstream x1s; x1s << x1; vcl_stringstream y1s; y1s << y1;
00141   vcl_stringstream x2s; x2s << x2; vcl_stringstream y2s; y2s << y2;
00142 
00143   this->set_attribute("x1", x1s.str());
00144   this->set_attribute("y1", y1s.str());
00145   this->set_attribute("x2", x2s.str());
00146   this->set_attribute("y2", y2s.str());
00147 }
00148 
00149 bsvg_arrow_head::bsvg_arrow_head(float x, float y, float l) : bsvg_group()
00150 {
00151   this->set_location(x,y);
00152 
00153   bsvg_line* l1 = new bsvg_line(0, 0, 0, l);
00154   l1->set_rotation(135);
00155   this->add_element(l1);
00156 
00157   bsvg_line* l2 = new bsvg_line(0, 0, 0, l);
00158   l2->set_rotation(45);
00159   this->add_element(l2);
00160 }
00161 
00162 bsvg_polyline::bsvg_polyline(const vcl_vector<float>& xs, const vcl_vector<float>& ys) : bsvg_element("polyline")
00163 {
00164   if (xs.size() == ys.size()) {
00165     vcl_stringstream ss;
00166     for (unsigned i = 0; i < xs.size(); i++) {
00167       ss << xs[i] << ',' << ys[i] << ' ';
00168     }
00169     this->set_attribute("points", ss.str());
00170   }
00171 }
00172 
00173 //: draw a splice e.g. for a "pie chart".
00174 //  A splice is an arc of a full circle given by start and end angles and the
00175 //  arc is closed at the ends by lines from and to the center of the circle pass
00176 //  the angles in radians in range [0,2pi]
00177 bsvg_splice::bsvg_splice(float center_x, float center_y, float radius, float start_angle, float end_angle, bool long_arc) : bsvg_group()
00178 {
00179   // compute the first and second points on the arc using the start_angle and end_angle
00180   float first_point_x = radius*vcl_cos(start_angle)+center_x;
00181   float first_point_y = radius*-vcl_sin(start_angle)+center_y; // invert the y value
00182   float second_point_x = radius*vcl_cos(end_angle)+center_x;
00183   float second_point_y = radius*-vcl_sin(end_angle)+center_y;
00184 
00185   bsvg_element* el = new bsvg_element("path");
00186   vcl_stringstream attr;
00187   attr << 'M' << center_x << ',' << center_y << ' ' << first_point_x << ',' << first_point_y << " A" << radius << ',' << radius << " 0 ";
00188   if (long_arc)
00189     attr << "1,0 ";
00190   else
00191     attr << "0,0 ";
00192   attr << second_point_x << ',' << second_point_y << " z";
00193   el->set_attribute("d", attr.str());
00194   this->add_element(el);
00195 }
00196