Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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 #include "BinToColorScale.h"
00020 
00021 #include "axes/AxisModelBase.h"
00022 #include "axes/AxisTick.h"
00023 #include "graphics/Color.h"
00024 #include "graphics/ViewBase.h"
00025 #include "transforms/BinaryTransform.h"
00026 
00027 #include <algorithm>
00028 #include <functional>
00029 
00030 #include <cmath>
00031 
00032 #include <cassert>
00033 
00034 using std::min;
00035 using std::string;
00036 using std::vector;
00037 
00038 using namespace hippodraw;
00039 
00040 AxisRepColor::AxisRepColor ()
00041   : AxisRepBase (),
00042     m_axis_z_origin( 0.0 ),
00043     m_axis_depth( 0.0 )
00044 {
00045 }
00046 
00047 AxisRepColor::AxisRepColor( const AxisRepColor & axis_rep )
00048   : AxisRepBase( axis_rep ),
00049     m_axis_z_origin( axis_rep.m_axis_z_origin ),
00050     m_axis_depth( axis_rep.m_axis_depth )
00051 {
00052 }
00053 
00054 AxisRepBase * AxisRepColor::clone()
00055 {
00056   return new AxisRepColor( *this );
00057 }
00058 
00059 
00060 inline float XPADDING( ViewBase & view )
00061 {
00062   return ( view.getDrawRect().getWidth() * 0.01 );
00063 }
00064 
00065 inline float YPADDING( ViewBase & view )
00066 {
00067   return ( view.getDrawRect().getHeight() * 0.01 );
00068 }
00069 
00070 inline float ZPADDING( ViewBase & view )
00071 {
00072   return ( view.getDrawRect().getWidth() * 0.01 );
00073 }
00074 
00075 void AxisRepColor::endPlot()
00076 {
00077 }
00078 
00079 void
00080 AxisRepColor::
00081 drawZLabels ( const AxisModelBase & axisModel,
00082               ViewBase & view, 
00083               const std::string & z_label )
00084 {
00085   float x, y;
00086 
00087   HippoRectangle draw_rect = view.getDrawRect ();
00088   float dh = draw_rect.getHeight ();
00089 
00090   HippoRectangle margin_rect = view.getMarginRect ();
00091   float mx = margin_rect.getX ();
00092   float mw = margin_rect.getWidth ();
00093   float mh = margin_rect.getHeight ();
00094 
00095   float y_font_size = ( dh - mh ) / 2 * 0.20;
00096   float x_font_size = ( mw * 2 ) / z_label.size() * 2 / 3;
00097 
00098   m_z_font_size = min ( x_font_size, y_font_size );
00099 
00100   x = mx + 0.5 * mw;
00101   y = YPADDING( view ) + ( dh - mh ) / 2 * 0.30 + YPADDING ( view ) 
00102     + m_z_font_size ;
00103 
00104   if ( m_zLabelFont != 0 ) {
00105     view.drawText ( z_label, x, y, 0.0, 0.0, 'c', 'b',
00106                     false, m_zLabelFont );
00107   } else {
00108     view.drawText ( z_label, x, y, m_z_font_size, 0.0, 'c', 'b', false );
00109   }
00110 }
00111 
00112 void AxisRepColor::drawAllZTicks ( const AxisModelBase & axisModel,
00113                                    const TransformBase & transform,
00114                                    ViewBase & view )
00115 {
00116   drawZTickLines ( axisModel, transform, view );
00117   drawZTickLabels ( axisModel, transform, view );
00118 }
00119 
00120 void AxisRepColor::drawZTickLines ( const AxisModelBase & axisModel,
00121                                     const TransformBase & transform,
00122                                     ViewBase & view )
00123 {
00124   AxisLoc loc = axisModel.getLabelLocation ();
00125   assert ( loc == PLOTBOTTOM || PLOTTOP );
00126 
00127   const vector< AxisTick > & ticks = axisModel.getTicks ();
00128   if ( ticks.empty() ) return;  
00129 
00130   vector< double > xv;
00131   vector< double > yv;
00132   
00133   unsigned int size = 2 * ticks.size ();
00134   xv.reserve ( size );
00135   yv.reserve ( size );
00136 
00137   const BinaryTransform & t 
00138     = dynamic_cast< const BinaryTransform & > ( transform );
00139   
00140   const HippoRectangle view_rect = view.getMarginRect ();
00141   double tick_length = 0.05 * view_rect.getHeight ();
00142 
00143   double y_base = view_rect.getY() -
00144     ( 1.0 / 9.0 ) * view_rect.getHeight() - tick_length;
00145 
00146   for ( unsigned int i = 0; i < ticks.size(); i++ ) {
00147     
00148     double user_z = ticks[i].value ();
00149     t.transformZ ( user_z );
00150     // Reinstate the following line when XYZ transform exists.
00151     //t.transform ( user_z, y ); // Some valid value on Y.
00152   
00153     double view_x = view.userToDrawColor ( user_z );
00154 
00155     xv.push_back ( view_x );
00156     yv.push_back ( y_base );
00157     xv.push_back ( view_x );
00158     yv.push_back ( y_base + tick_length );
00159   
00160   }
00161 
00162   view.drawViewLines ( xv, yv, Line::Solid, false, 0 );  
00163 
00164 }
00165 
00166 void AxisRepColor::drawZTickLabels ( const AxisModelBase & axisModel,
00167                                      const TransformBase & transform, 
00168                                      ViewBase & view )
00169 {
00170   vector < double > xv;  
00171   vector < double > yv;  
00172   
00173   const vector< AxisTick > & ticks = axisModel.getTicks ();
00174   unsigned int size = ticks.size ();
00175   if ( size == 0 ) return;
00176 
00177   xv.reserve ( size );
00178   yv.reserve ( size );
00179 
00180   const BinaryTransform & t 
00181     = dynamic_cast< const BinaryTransform & > ( transform );
00182 
00183   unsigned int i = 0; // For MS VC++.
00184   for ( ; i < size; i++ ) {
00185     double value = ticks[i].value ();
00186     t.transformZ ( value );
00187     xv.push_back ( value );
00188     yv.push_back ( 1.0 ); // Use a valid Y value.
00189   }
00190 
00191   // Calculate the Y position in view coordinates.
00192   
00193   const HippoRectangle & margin = view.getMarginRect ();
00194   double tick_length = 0.05 * margin.getHeight ();
00195   float y = margin.getY() - ( 1.0 / 9.0 ) * margin.getHeight() -
00196     2 * tick_length;
00197   char yalign = 't';
00198 
00199   i = 0;  // For MS VC++.
00200   for ( ; i < size; i++ ) {
00201     float x = view.userToDrawColor ( xv[i] );
00202     view.drawText( ticks[i].content(), x, y, m_font_size, 0.0, 'c', yalign );
00203   }       
00204   
00205   if ( axisModel.needPMag () ) {
00206     double pmag = axisModel.getPMag ();
00207     const HippoRectangle view_rect = view.getDrawRect ();
00208 
00209     float last_x = view.userToDrawColor ( xv[ size -1 ] );
00210     float x = margin.getX () + last_x;
00211     
00212     const AxisTick & last_tick = ticks.back ();
00213     const string & last_label = last_tick.content ();
00214     float label_size = last_label.size ();
00215     x += 0.5 * label_size * m_font_size;
00216     
00217     float margin_right = margin.getX () + margin.getWidth ();
00218     x = min ( x, margin_right + XPADDING ( view ) );
00219     
00220     if ( yalign == 't' ) {
00221       y -= m_font_size * 0.8; // used to be +0.8
00222     } else {
00223       y -= m_font_size * 1.8; // might need adjustment?
00224     }
00225 
00226     x += view_rect.getX();
00227     y += view_rect.getY();    
00228 
00229     // Logarithmic graphs do not need the magnitude written.
00230     if(!(axisModel.isLog()))
00231       view.drawMag ( x, y, static_cast< int >( pmag ), m_font_size * 0.75 );
00232   }
00233 
00234 }
00235 
00236 void
00237 AxisRepColor::
00238 drawColorScale ( const BinToColor & bin_to_color, ViewBase & view )
00239 {
00240   HippoRectangle margin = view.getMarginRect(); 
00241 
00242   double y_base = margin.getY() -
00243     ( 1.05 / 9.0 ) * margin.getHeight();
00244 
00245   const Range & range = bin_to_color.getRange ();
00246   double value = range.low ();
00247   double delta_v = range.length () / margin.getWidth ();
00248   for ( float i = 0; i <= margin.getWidth() ; i++ ) {
00249     
00250     Color color;
00251     bin_to_color.doubleToColor ( value, color );
00252     view.drawViewSquare ( margin.getX() + i,
00253                           y_base,
00254                           margin.getX() + i + 1.0,
00255                           y_base + 0.05 * margin.getHeight(),
00256                           color.getRed(),
00257                           color.getGreen(),
00258                           color.getBlue () );
00259     value  += delta_v;
00260   }
00261   
00262   view.drawViewSquare( margin.getX(),
00263                      y_base,
00264                      margin.getX(),
00265                      y_base + 0.05 * margin.getHeight(),
00266                      0, 0, 0 );
00267 }

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3