p_quad.cpp

Go to the documentation of this file.
00001 /*
00002  * p_quad.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 quad 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 "bezier/quad.h"
00040 #include "common/wave_ex.h"
00041 #include "perf/perf.h"
00042 #include "util/parsing.h"
00043 
00044 
00045 
00046 namespace vgfx {
00047 
00048 ////////////////////////////////////////////////////////////////////////////////
00049 //
00050 //      static helper methods
00051 //
00052 ////////////////////////////////////////////////////////////////////////////////
00053 
00054 class Quad : public Primitive {
00055 public:
00056         ~Quad(void) throw() { }
00057 
00058         // public class methods ------------------------------------------------
00059         void initialize(IN const init_quad_t& init);
00060 
00061         // vgfx::Primitive class interface methods -----------------------------
00062         virtual const char * getType(void) const throw() { return "quad"; }
00063         virtual void persist(OUT std::ostream& stream) const;
00064         virtual void getContainerDictionary(IN const VecString& path,
00065                                 OUT dictionary_t& data) const;
00066         virtual void recalcBoundingRect(IN const char * tag_path,
00067                                 IN Drawer * drawer,
00068                                 IN const xform_2d_t& T) { }
00069         virtual bool getBoundingRect(OUT rect_t& r) const throw();
00070         virtual void draw(IN Drawer * drawer,
00071                                 IN const rect_t& r_cm,
00072                                 IN const xform_2d_t& T);
00073 
00074 private:
00075         bezier::quad_bezier_t   m_quad;
00076         rect_t                  m_rect;         // bounding rect
00077 };
00078 
00079 
00080 
00081 ////////////////////////////////////////////////////////////////////////////////
00082 //
00083 //      Quad -- public class methods
00084 //
00085 ////////////////////////////////////////////////////////////////////////////////
00086 
00087 void
00088 Quad::initialize
00089 (
00090 IN const init_quad_t& init
00091 )
00092 {
00093         this->setID(init.id.c_str());
00094         m_quad = init.quad;
00095 }
00096 
00097 
00098 
00099 bool
00100 Quad::getBoundingRect
00101 (
00102 OUT rect_t& r
00103 )
00104 const throw()
00105 {
00106         m_quad.getBoundingRect(r);
00107         r.expand(0.05);         // pen width etc
00108         return true;
00109 }
00110 
00111 
00112 
00113 #define WRITE_L_FIELD(tag, q) { if (q) { stream << " " << #tag << " " << q; } }
00114 
00115 void
00116 Quad::persist
00117 (
00118 OUT std::ostream& stream
00119 )
00120 const
00121 {
00122         stream << "quad id " << this->getID();
00123         WRITE_L_FIELD(x0, m_quad.p0.x)
00124         WRITE_L_FIELD(y0, m_quad.p0.y)
00125         WRITE_L_FIELD(x1, m_quad.p1.x)
00126         WRITE_L_FIELD(y1, m_quad.p1.y)
00127         WRITE_L_FIELD(x2, m_quad.p2.x)
00128         WRITE_L_FIELD(y2, m_quad.p2.y)
00129 }
00130 
00131 
00132 
00133 void
00134 Quad::getContainerDictionary
00135 (
00136 IN const VecString& path,
00137 OUT dictionary_t& data
00138 )
00139 const
00140 {
00141         ASSERT(0 == path.size(),
00142             "quads only have zero-size dictionaries!");
00143 
00144         // list beginning + ending control points
00145         data.clear();
00146         data["x0"] = getStringValue(m_quad.p0.x);
00147         data["y0"] = getStringValue(m_quad.p0.y);
00148         data["x2"] = getStringValue(m_quad.p2.x);
00149         data["y2"] = getStringValue(m_quad.p2.y);
00150 }
00151 
00152 
00153 
00154 void
00155 Quad::draw
00156 (
00157 IN Drawer * drawer,
00158 IN const rect_t& r_cm,
00159 IN const xform_2d_t& T
00160 )
00161 {
00162         ASSERT(drawer, "null");
00163 
00164         drawer->setTransform(T);
00165         drawer->drawQuadBezier(m_quad);
00166 }
00167 
00168 
00169 
00170 ////////////////////////////////////////////////////////////////////////////////
00171 //
00172 //      public API
00173 //
00174 ////////////////////////////////////////////////////////////////////////////////
00175 
00176 #define SET_L_FIELD(x, tag)   init.quad.x = atof(getOptionalValue(data, #tag , "0.0"));
00177 
00178 void
00179 init_quad_from_data
00180 (
00181 IN const dictionary_t& data,
00182 OUT init_quad_t& init
00183 )
00184 {
00185         init.id = getRequiredValue(data, "id");
00186 
00187         SET_L_FIELD(p0.x, x0)
00188         SET_L_FIELD(p0.y, y0)
00189         SET_L_FIELD(p1.x, x1)
00190         SET_L_FIELD(p1.y, y1)
00191         SET_L_FIELD(p2.x, x2)
00192         SET_L_FIELD(p2.y, y2)
00193 }
00194 
00195 
00196 
00197 smart_ptr<Primitive>
00198 create_quad
00199 (
00200 IN const init_quad_t& init
00201 )
00202 {
00203         smart_ptr<Quad> local = new Quad;
00204         ASSERT(local, "out of memory");
00205 
00206         local->initialize(init);
00207 
00208         return local;
00209 }
00210 
00211 
00212 };      // vgfx namespace
00213