contrib/brl/bbas/bxml/bsvg/bsvg_element.cxx

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

Generated on Mon Mar 8 05:30:06 2010 for contrib/brl/bbas/bxml by  doxygen 1.5.1