p_line.cpp

Go to the documentation of this file.
00001 /*
00002  * p_line.cpp
00003  *
00004  * Copyright (C) 2007  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Implementation of a line primitive.
00032  * (p_ stands for Primitive)
00033  */
00034 
00035 // includes --------------------------------------------------------------------
00036 #include "vgfx.h"             // all primitives should include this header first
00037 #include "drawer.h"
00038 
00039 #include "common/wave_ex.h"
00040 #include "perf/perf.h"
00041 #include "util/parsing.h"
00042 
00043 
00044 
00045 namespace vgfx {
00046 
00047 ////////////////////////////////////////////////////////////////////////////////
00048 //
00049 //      static helper methods
00050 //
00051 ////////////////////////////////////////////////////////////////////////////////
00052 
00053 class Line : public Primitive {
00054 public:
00055         ~Line(void) throw() { }
00056 
00057         // public class methods ------------------------------------------------
00058         void initialize(IN const init_line_t& init);
00059 
00060         // vgfx::Primitive class interface methods -----------------------------
00061         virtual const char * getType(void) const throw() { return "line"; }
00062         virtual void persist(OUT std::ostream& stream) const;
00063         virtual void recalcBoundingRect(IN const char * tag_path,
00064                                 IN Drawer * drawer,
00065                                 IN const xform_2d_t& T) { }
00066         virtual bool getBoundingRect(OUT rect_t& r) const throw();
00067         virtual void draw(IN Drawer * drawer,
00068                                 IN const rect_t& r_cm,
00069                                 IN const xform_2d_t& T);
00070 
00071 private:
00072         point_t         m_p0;   // starting point
00073         point_t         m_p1;   // ending point
00074 };
00075 
00076 
00077 
00078 ////////////////////////////////////////////////////////////////////////////////
00079 //
00080 //      Line -- public class methods
00081 //
00082 ////////////////////////////////////////////////////////////////////////////////
00083 
00084 void
00085 Line::initialize
00086 (
00087 IN const init_line_t& init
00088 )
00089 {
00090         this->setID(init.id.c_str());
00091         m_p0.set(init.x0, init.y0);
00092         m_p1.set(init.x1, init.y1);
00093 }
00094 
00095 
00096 
00097 bool
00098 Line::getBoundingRect
00099 (
00100 OUT rect_t& r
00101 )
00102 const throw()
00103 {
00104         r.left = (m_p0.x < m_p1.x) ? m_p0.x : m_p1.x;
00105         r.right =(m_p0.x > m_p1.x) ? m_p0.x : m_p1.x;
00106 
00107         r.top = (m_p0.y < m_p1.y) ? m_p0.y : m_p1.y;
00108         r.bottom = (m_p0.y > m_p1.y) ? m_p0.y : m_p1.y;
00109 
00110         r.expand(0.05); // pen width etc
00111         return true;
00112 }
00113 
00114 
00115 
00116 #define WRITE_L_FIELD(tag, q) { if (q) { stream << " " << #tag << " " << q; } }
00117 
00118 void
00119 Line::persist
00120 (
00121 OUT std::ostream& stream
00122 )
00123 const
00124 {
00125         stream << "line id " << this->getID();
00126         WRITE_L_FIELD(x0, m_p0.x)
00127         WRITE_L_FIELD(y0, m_p0.y)
00128         WRITE_L_FIELD(x1, m_p1.x)
00129         WRITE_L_FIELD(y1, m_p1.y)
00130 }
00131 
00132 
00133 
00134 void
00135 Line::draw
00136 (
00137 IN Drawer * drawer,
00138 IN const rect_t& r_cm,
00139 IN const xform_2d_t& T
00140 )
00141 {
00142         ASSERT(drawer, "null");
00143 
00144         drawer->setTransform(T);
00145         drawer->drawLine(m_p0, m_p1);
00146 }
00147 
00148 
00149 
00150 ////////////////////////////////////////////////////////////////////////////////
00151 //
00152 //      public API
00153 //
00154 ////////////////////////////////////////////////////////////////////////////////
00155 
00156 #define SET_L_FIELD(x)   init.x = atof(getOptionalValue(data, #x , "0.0"));
00157 
00158 void
00159 init_line_from_data
00160 (
00161 IN const dictionary_t& data,
00162 OUT init_line_t& init
00163 )
00164 {
00165         init.id = getRequiredValue(data, "id");
00166 
00167         SET_L_FIELD(x0)
00168         SET_L_FIELD(y0)
00169         SET_L_FIELD(x1)
00170         SET_L_FIELD(y1)
00171 }
00172 
00173 
00174 
00175 smart_ptr<Primitive>
00176 create_line
00177 (
00178 IN const init_line_t& init
00179 )
00180 {
00181         smart_ptr<Line> local = new Line;
00182         ASSERT(local, "out of memory");
00183 
00184         local->initialize(init);
00185 
00186         return local;
00187 }
00188 
00189 
00190 };      // vgfx namespace
00191