AxisRepColor.cxx

Go to the documentation of this file.
00001 
00012 #ifdef _MSC_VER
00013 // for min()
00014 #include "msdevstudio/MSconfig.h"
00015 #endif
00016 
00017 #include "AxisRepColor.h"
00018 
00019 
00020 #include "axes/AxisModelBase.h"
00021 #include "axes/AxisTick.h"
00022 #include "colorreps/BinToColor.h"
00023 #include "graphics/DataView.h"
00024 #include "pattern/string_convert.h"
00025 #include "transforms/BinaryTransform.h"
00026 #include "plotters/PlotterBase.h"
00027 
00028 
00029 #include <algorithm>
00030 #include <functional>
00031 
00032 #include <cmath>
00033 
00034 #include <cassert>
00035 
00036 #include <iostream>
00037 
00038 using std::min;
00039 using std::string;
00040 using std::vector;
00041 
00042 using namespace hippodraw;
00043 
00044 AxisRepColor::AxisRepColor ()
00045   : AxisRepBase (),
00046     m_axis_z_origin( 0.0 ),
00047     m_axis_depth( 0.0 )
00048 {
00049 }
00050 
00051 AxisRepColor::AxisRepColor( const AxisRepColor & axis_rep )
00052   : AxisRepBase( axis_rep ),
00053     m_axis_z_origin( axis_rep.m_axis_z_origin ),
00054     m_axis_depth( axis_rep.m_axis_depth )
00055 {
00056 }
00057 
00058 AxisRepBase * AxisRepColor::clone()
00059 {
00060   return new AxisRepColor( *this );
00061 }
00062 
00063 
00064 inline float XPADDING( ViewBase & view )
00065 {
00066   return ( view.getDrawRect().getWidth() * 0.01 );
00067 }
00068 
00069 inline float YPADDING( ViewBase & view )
00070 {
00071   return ( view.getDrawRect().getHeight() * 0.01 );
00072 }
00073 
00074 inline float ZPADDING( ViewBase & view )
00075 {
00076   return ( view.getDrawRect().getWidth() * 0.01 );
00077 }
00078 
00081 void
00082 AxisRepColor::
00083 drawZLabels ( const AxisModelBase &, // axisModel,
00084               ViewBase & base, 
00085               const std::string & z_label )
00086 {
00087 
00088   DataView & view = dynamic_cast < DataView & > ( base );
00089 
00090   // Draw Latex at the top of the plotter if in Latex format
00091   if (String::ci_find(z_label, "tex:")==0) {
00092     string tex_snippet = z_label.substr(4);
00093     view.drawLatex ( tex_snippet, 4 );
00094     return;
00095   }
00096 
00097   Rect draw_rect = view.getDrawRect ();
00098   float dh = draw_rect.getHeight ();
00099 
00100   const Rect & margin_rect = view.getMarginRect ();
00101   float mx = margin_rect.getX ();
00102   float mw = margin_rect.getWidth ();
00103   float my = margin_rect.getY ();
00104   float mh = margin_rect.getHeight ();
00105 
00106   float y_font_size = ( dh - mh ) / 2 * 0.20;
00107   float x_font_size = ( mw * 2 ) / z_label.size() * 2 / 3;
00108 
00109   m_z_font_size = min ( x_font_size, y_font_size );
00110 
00111   float x = mx + 0.5 * mw;
00112   float y = my - 37;
00113 
00114   if (m_titleFont != 0) {
00115     y = y + 1.2 * m_titleFont->pointSize() - 11.0; //experimental
00116   }
00117 
00118   if ( m_zLabelFont != 0 ) {
00119     y = y + 1.2 * m_zLabelFont->pointSize() - 8.0; // experimental
00120     view.drawText ( z_label, x, y, 0.0, 0.0, 'c', 'b',
00121                     false, m_zLabelFont );
00122   } else {
00123     view.drawText ( z_label, x, y, m_z_font_size, 0.0, 'c', 'b', false );
00124   }
00125 }
00126 
00127 void AxisRepColor::drawAllZTicks ( const AxisModelBase & axisModel,
00128                                    const TransformBase & transform,
00129                                    ViewBase & view )
00130 {
00131   drawZTickLines ( axisModel, transform, view );
00132   drawZTickLabels ( axisModel, transform, view );
00133 }
00134 
00135 void AxisRepColor::drawZTickLines ( const AxisModelBase & axisModel,
00136                                     const TransformBase & transform,
00137                                     ViewBase & base )
00138 {
00139   AxisLoc loc = axisModel.getLabelLocation ();
00140   assert ( loc == PLOTBOTTOM || PLOTTOP );
00141 
00142   const vector< AxisTick > & ticks = axisModel.getTicks ();
00143   if ( ticks.empty() ) return;  
00144 
00145   vector< double > xv;
00146   vector< double > yv;
00147   
00148   unsigned int size = 2 * ticks.size ();
00149   xv.reserve ( size );
00150   yv.reserve ( size );
00151 
00152   const BinaryTransform & t 
00153     = dynamic_cast< const BinaryTransform & > ( transform );
00154   DataView & view = dynamic_cast < DataView & > ( base );
00155   const Rect & view_rect = view.getMarginRect ();
00156   double tick_length = 5;
00157   double y_base = view_rect.getY() - 24;
00158 
00159   for ( unsigned int i = 0; i < ticks.size(); i++ ) {
00160     double user_z = ticks[i].value ();
00161     t.transformZ ( user_z );
00162     // Reinstate the following line when XYZ transform exists.
00163     //t.transform ( user_z, y ); // Some valid value on Y.
00164   
00165     double view_x = view.userToDrawColor ( user_z );
00166 
00167     xv.push_back ( view_x );
00168     yv.push_back ( y_base );
00169     xv.push_back ( view_x );
00170     yv.push_back ( y_base + tick_length );
00171   
00172   }
00173 
00174   view.drawViewLines ( xv, yv, Line::Solid, false, 0 );  
00175 
00176 }
00177 
00178 void AxisRepColor::drawZTickLabels ( const AxisModelBase & axisModel,
00179                                      const TransformBase & transform, 
00180                                      ViewBase & base )
00181 {
00182   vector < double > xv;  
00183   vector < double > yv;  
00184   
00185   const vector< AxisTick > & ticks = axisModel.getTicks ();
00186   unsigned int size = ticks.size ();
00187   if ( size == 0 ) return;
00188 
00189   xv.reserve ( size );
00190   yv.reserve ( size );
00191 
00192   const BinaryTransform & t 
00193     = dynamic_cast< const BinaryTransform & > ( transform );
00194 
00195   unsigned int i = 0; // For MS VC++.
00196   for ( ; i < size; i++ ) {
00197     double value = ticks[i].value ();
00198     t.transformZ ( value );
00199     xv.push_back ( value );
00200     yv.push_back ( 1.0 ); // Use a valid Y value.
00201   }
00202 
00203 
00204   // Calculate the Y position in view coordinates.
00205   DataView & view = dynamic_cast < DataView & > ( base );  
00206   const Rect & margin = view.getMarginRect ();
00207   float y = margin.getY() - 42;
00208 
00209   i = 0;  // For MS VC++.
00210   for ( ; i < size; i++ ) {
00211     float x = view.userToDrawColor ( xv[i] );
00212     drawXTickLabel ( ticks[i].content (), x, y, view );
00213   }       
00214   
00215   if ( axisModel.needPMag () ) {
00216     // Logarithmic graphs do not need the magnitude written.
00217     if ( !(axisModel.isLog() ) ) {
00218       const Rect & margin_rect = view.getMarginRect ();
00219       float xx = margin_rect.getX() 
00220         + margin_rect.getWidth ()
00221         - m_y_font_size;
00222       float yy = margin_rect.getY() - 30. - m_y_tick_font_size;
00223       view.drawText ( "x10",  xx, yy, m_y_tick_font_size, 0., 'l', 'b' );
00224       double mag = axisModel.getPMag ();
00225       int i = static_cast < int > ( mag );
00226       const string text = String::convert ( i );
00227       xx += 1.75* m_y_tick_font_size;
00228       yy -= 0.5 * m_y_tick_font_size;
00229       view.drawText ( text, xx, yy, m_y_tick_font_size, 0., 'l', 'b' );
00230     }
00231   }
00232 
00233 }
00234 
00235 void
00236 AxisRepColor::
00237 drawColorScale ( const BinToColor & bin_to_color, ViewBase & base )
00238 {
00239   DataView & view = dynamic_cast < DataView & > ( base );
00240   PlotterBase * plotter = view.getPlotter ();
00241 
00242   const Rect & margin = view.getMarginRect(); 
00243 
00244   double y_base = margin.getY() - 22;
00245 
00246   const Range range = bin_to_color.getRange ();
00247   double value = range.low ();
00248   double delta_v = range.length () / margin.getWidth ();
00249   if (delta_v == 0) {
00250      return;
00251   }
00252   for ( float i = 0; i <= margin.getWidth() ; i++ ) {
00253     
00254     const Color & rep_color = plotter->repColor();
00255         
00256         Color color = rep_color;
00257 
00258     bin_to_color.doubleToColor ( value, color );
00259     view.drawViewSquare ( margin.getX() + i,
00260                           y_base,
00261                           margin.getX() + i + 1.0,
00262                           y_base + 6,
00263                           color.getRed(),
00264                           color.getGreen(),
00265                           color.getBlue () );
00266     value  += delta_v;
00267   }
00268   
00269   view.drawViewSquare ( margin.getX(),
00270                         y_base,
00271                         margin.getX(),
00272                         y_base + 8, 
00273                         0, 0, 0 );
00274 }

Generated for HippoDraw Class Library by doxygen