gxaligner.cc

Go to the documentation of this file.
00001 
00003 
00004 //
00005 // Copyright (c) 2002-2004 California Institute of Technology
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Wed Nov 13 12:59:04 2002
00010 // commit: $Id: gxaligner.cc 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/gfx/gxaligner.cc $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_GFX_GXALIGNER_CC_UTC20050626084023_DEFINED
00035 #define GROOVX_GFX_GXALIGNER_CC_UTC20050626084023_DEFINED
00036 
00037 #include "gxaligner.h"
00038 
00039 #include "gfx/bbox.h"
00040 #include "gfx/canvas.h"
00041 
00042 #include "geom/box.h"
00043 #include "geom/rect.h"
00044 #include "geom/txform.h"
00045 #include "geom/vec3.h"
00046 
00047 #include "io/reader.h"
00048 #include "io/writer.h"
00049 
00050 #include "rutz/debug.h"
00051 GVX_DBG_REGISTER
00052 #include "rutz/trace.h"
00053 
00054 using geom::rectd;
00055 using geom::vec2d;
00056 using geom::vec3d;
00057 
00058 GxAligner::GxAligner(nub::soft_ref<GxNode> child) :
00059   GxBin(child),
00060   itsMode(NATIVE_ALIGNMENT),
00061   itsCenter(0.0, 0.0)
00062 {}
00063 
00064 GxAligner::~GxAligner() throw() {}
00065 
00066 vec2d GxAligner::getCenter(const rectd& b) const
00067 {
00068 GVX_TRACE("GxAligner::getCenter");
00069   switch (itsMode)
00070     {
00071     case CENTER_ON_CENTER:    return vec2d::zeros();
00072     case NW_ON_CENTER:        return vec2d( b.width()/2.0, -b.height()/2.0);
00073     case NE_ON_CENTER:        return vec2d(-b.width()/2.0, -b.height()/2.0);
00074     case SW_ON_CENTER:        return vec2d( b.width()/2.0,  b.height()/2.0);
00075     case SE_ON_CENTER:        return vec2d(-b.width()/2.0,  b.height()/2.0);
00076     case ARBITRARY_ON_CENTER: return itsCenter;
00077     case NATIVE_ALIGNMENT:    return b.center();
00078     }
00079 
00080   GVX_ASSERT(0);
00081   return vec2d();
00082 }
00083 
00084 void GxAligner::doAlignment(Gfx::Canvas& canvas, const rectd& native) const
00085 {
00086 GVX_TRACE("GxAligner::doAlignment");
00087 
00088   if (NATIVE_ALIGNMENT == itsMode) return;
00089 
00090   // This indicates where the center of the object will go
00091   const vec2d center = getCenter(native) - native.center();
00092 
00093   const vec3d vec(center.x(), center.y(), 0.0);
00094 
00095   canvas.translate(vec);
00096 }
00097 
00098 void GxAligner::read_from(io::reader& reader)
00099 {
00100 GVX_TRACE("GxAligner::read_from");
00101   reader.read_value("mode", itsMode);
00102   reader.read_value_obj("center", itsCenter);
00103 }
00104 
00105 void GxAligner::write_to(io::writer& writer) const
00106 {
00107 GVX_TRACE("GxAligner::write_to");
00108   writer.write_value("mode", itsMode);
00109   writer.write_value_obj("center", itsCenter);
00110 }
00111 
00112 void GxAligner::draw(Gfx::Canvas& canvas) const
00113 {
00114 GVX_TRACE("GxAligner::draw");
00115   Gfx::MatrixSaver state(canvas);
00116 
00117   doAlignment(canvas, child()->getBoundingBox(canvas));
00118 
00119   child()->draw(canvas);
00120 }
00121 
00122 void GxAligner::getBoundingCube(Gfx::Bbox& bbox) const
00123 {
00124 GVX_TRACE("GxAligner::getBoundingCube");
00125 
00126   Gfx::Bbox mybox = bbox.peer();
00127 
00128   child()->getBoundingCube(mybox);
00129 
00130   rectd bounds = mybox.rect();
00131 
00132   const vec2d center = getCenter(bounds);
00133 
00134   bounds.set_center(center);
00135 
00136   bbox.drawRect(bounds);
00137 
00138   dbg_dump(2, bbox.cube());
00139 }
00140 
00141 static const char __attribute__((used)) vcid_groovx_gfx_gxaligner_cc_utc20050626084023[] = "$Id: gxaligner.cc 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00142 #endif // !GROOVX_GFX_GXALIGNER_CC_UTC20050626084023_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:38 2008 by Doxygen version 1.5.5.