plot.C

00001 /*
00002  * Definition of elementary graphical functions
00003  */
00004 
00005 /*
00006  *   Copyright (c) 2005 Eric Gourgoulhon
00007  *
00008  *   This file is part of LORENE.
00009  *
00010  *   LORENE is free software; you can redistribute it and/or modify
00011  *   it under the terms of the GNU General Public License as published by
00012  *   the Free Software Foundation; either version 2 of the License, or
00013  *   (at your option) any later version.
00014  *
00015  *   LORENE is distributed in the hope that it will be useful,
00016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *   GNU General Public License for more details.
00019  *
00020  *   You should have received a copy of the GNU General Public License
00021  *   along with LORENE; if not, write to the Free Software
00022  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  */
00025 
00026 /*
00027  * $Id: plot.C,v 1.2 2005/11/14 14:12:10 e_gourgoulhon Exp $
00028  * $Log: plot.C,v $
00029  * Revision 1.2  2005/11/14 14:12:10  e_gourgoulhon
00030  * Added include <assert.h>
00031  *
00032  * Revision 1.1  2005/11/14 01:57:00  e_gourgoulhon
00033  * First version
00034  *
00035  *
00036  * $Header: /cvsroot/Lorene/School05/Monday/plot.C,v 1.2 2005/11/14 14:12:10 e_gourgoulhon Exp $
00037  *
00038  */
00039 
00040 #include <iostream>
00041 
00042 using namespace std ;
00043 
00044 #include <stdlib.h>
00045 #include <assert.h>
00046 
00047 #include <cpgplot.h>
00048 
00049 #include "plot.h"
00050 
00051 
00052 namespace Plotid {
00053     const int nfig_max = 100 ; 
00054     int fig_id[nfig_max] ;     
00055 }
00056 
00057 //===========================================================================
00058 
00059 void plot_init() {
00060     using namespace Plotid ; 
00061 
00062     static bool never_called = true ; 
00063     
00064     if (never_called) {
00065         for (int i=0; i<nfig_max; i++) {
00066             fig_id[i] = 0 ; 
00067         }     
00068         never_called = false ; 
00069     }
00070     
00071 }
00072 
00073 //===========================================================================
00074 
00075 void plot_open(int nfig, double ymin, double ymax, const char* title,    
00076     const char* label_y, const char* device) {
00077     using namespace Plotid ; 
00078 
00079     assert( fig_id[nfig] == 0 ) ; 
00080     
00081     if (device == 0x0) device = "?" ; 
00082    
00083     fig_id[nfig] = cpgopen(device) ; 
00084         
00085     if ( fig_id[nfig] <= 0 ) {
00086         cerr << "plot_profile: problem in opening PGPLOT display !\n" ;
00087         abort() ; 
00088     }
00089         
00090     cpgask(0) ;  // Disables the ``Type RETURN for next page:'' prompt
00091             
00092     cpgsch(1.3) ;  // Character size
00093     
00094     cpgslw(4) ;  // Line width
00095     
00096     cpgscf(2) ;  // Axis fonts = Roman
00097 
00098     cpgsls(1) ;  // Continous lines
00099 
00100     // Sets the window :         
00101     cpgenv(-1., 1., float(ymin), float(ymax), 0, 0 ) ; 
00102     
00103     // Title and labels
00104     if (label_y == 0x0) label_y = "y" ; 
00105     if (title == 0x0) title = " " ; 
00106     cpglab("x", label_y, title) ;
00107     
00108 }
00109 
00110 
00111 
00112 //===========================================================================
00113 
00114 void plot_close(int nfig) {
00115 
00116     using namespace Plotid ;     
00117     plot_init() ; 
00118 
00119     if ( (nfig < 0) || (nfig >= nfig_max) ) {
00120         cerr << "plot_close : figure index out of range ! \n" ;
00121         cerr << " nfig = " << nfig << "  while range = 0, " 
00122             << nfig_max-1 << endl ; 
00123         abort() ;
00124     }
00125     
00126     if (fig_id[nfig] != 0) { 
00127         cpgslct( fig_id[nfig] ) ; // selects the appropriate device        
00128         cpgclos() ; 
00129         fig_id[nfig] = 0 ; 
00130     }
00131 
00132 }
00133 
00134 //===========================================================================
00135 
00136 void plot_close_all() {
00137 
00138     using namespace Plotid ;     
00139     plot_init() ; 
00140 
00141     for (int nfig = 0; nfig < nfig_max; nfig++) {
00142         if (fig_id[nfig] != 0) { 
00143             cpgslct( fig_id[nfig] ) ; // selects the appropriate device        
00144             cpgclos() ; 
00145             fig_id[nfig] = 0 ; 
00146         }
00147     }
00148 }
00149 
00150 //===========================================================================
00151 
00152 void plot_profile(const double* yy, int nx, int color, int style,    
00153     int nfig, double ymin, double ymax, const char* title,    
00154     const char* label_y, const char* device) {
00155 
00156     using namespace Plotid ; 
00157     plot_init() ; 
00158                   
00159     // Conversion double -> float
00160     // --------------------------
00161     float* yyf = new float[nx] ; 
00162     for (int i=0; i<nx; i++) {
00163         yyf[i] = yy[i] ; 
00164     }
00165          
00166     // Points abscisses : 
00167     // ----------------
00168     float xmin = -1. ; 
00169     float xmax = 1. ; 
00170     float* xx = new float[nx] ; 
00171     float hx = (xmax-xmin)/float(nx-1) ;
00172     for(int i=0; i<nx; i++) {
00173         xx[i] = xmin + i * hx ; 
00174     }
00175          
00176     // Graphics display
00177     // ----------------
00178     
00179     // Opening of the device
00180     
00181     if ( (nfig < 0) || (nfig >= nfig_max) ) {
00182         cerr << "plot_profile : graph index out of range ! \n" ;
00183         cerr << " nfig = " << nfig << "  while range = 0, " 
00184             << nfig_max-1 << endl ; 
00185         abort() ;
00186     }
00187     
00188     if (fig_id[nfig] == 0) { // opening is required
00189                                
00190         plot_open(nfig, ymin, ymax, title, label_y, device ) ; 
00191 
00192     }
00193     else {   // the graphic device has been opened previously   
00194 
00195         cpgslct( fig_id[nfig] ) ; // selects the appropriate device
00196     }  
00197     
00198     // Drawing of the curve
00199     cpgsci(color) ; 
00200     cpgsls(style) ;  
00201     cpgline(nx, xx, yyf) ;  
00202 
00203     delete [] xx ; 
00204     delete [] yyf ; 
00205 
00206 }
00207 
00208 //===========================================================================
00209 
00210 void plot_point(double x, double y, int color, int nfig, double ymin, 
00211     double ymax, const char* title, const char* label_y, const char* device) {
00212 
00213     using namespace Plotid ; 
00214     plot_init() ; 
00215 
00216     if ( (nfig < 0) || (nfig >= nfig_max) ) {
00217         cerr << "plot_point : figure index out of range ! \n" ;
00218         cerr << " nfig = " << nfig << "  while range = 0, " 
00219             << nfig_max-1 << endl ; 
00220         abort() ;
00221     }
00222     
00223     if (fig_id[nfig] == 0) { // opening is required
00224                                
00225         plot_open(nfig, ymin, ymax, title, label_y, device ) ; 
00226 
00227     }
00228     else {   // the graphic device has been opened previously   
00229 
00230         cpgslct( fig_id[nfig] ) ; // selects the appropriate device
00231     }
00232 
00233     cpgsci(color) ; 
00234 
00235     cpgpt1(float(x), float(y), 24) ; 
00236     
00237 }
00238 
00239 //===========================================================================
00240 
00241 void plot_point_set(int np, const double* xx, const double* yy, int color, 
00242                     int nfig, double ymin, double ymax, const char* title, 
00243                     const char* label_y, const char* device) {
00244 
00245     using namespace Plotid ; 
00246     plot_init() ; 
00247 
00248     if ( (nfig < 0) || (nfig >= nfig_max) ) {
00249         cerr << "plot_point : figure index out of range ! \n" ;
00250         cerr << " nfig = " << nfig << "  while range = 0, " 
00251             << nfig_max-1 << endl ; 
00252         abort() ;
00253     }
00254     
00255     if (fig_id[nfig] == 0) { // opening is required
00256                                
00257         plot_open(nfig, ymin, ymax, title, label_y, device ) ; 
00258 
00259     }
00260     else {   // the graphic device has been opened previously   
00261 
00262         cpgslct( fig_id[nfig] ) ; // selects the appropriate device
00263     }
00264 
00265     cpgsci(color) ; 
00266     
00267     float* x1 = new float[np] ; 
00268     float* y1 = new float[np] ; 
00269     
00270     for (int i=0; i<np; i++) {
00271         x1[i] = xx[i] ;
00272         y1[i] = yy[i] ;
00273     }
00274     cpgpt(np, x1, y1, 24) ; 
00275 
00276     delete [] x1 ; 
00277     delete [] y1 ; 
00278 }

Generated on Tue Dec 6 14:48:44 2011 for POLYNOM by  doxygen 1.4.6