├── examples ├── compile.sh ├── ex101_creating_a_plot.cc ├── Makefile ├── ex102_mouse_and_keyboard_events.cc ├── ex106_surface_and_contour.cc ├── ex108_animation.cc ├── ex107_3d_line_and_surface.cc ├── ex105_axes.cc ├── ex103_multiple_plots.cc └── ex104_style_and_color.cc ├── Makefile ├── README ├── README.md ├── gl2ps.h ├── matplotpp.h └── matplotpp.cc /examples/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | g++ -g -lglut -lGLU -lGL -I../ $1 ../matplotpp.a 3 | exit 4 | 5 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | #Makefile 2 | matplotpp:matplotpp.cc gl2ps.c 3 | g++ -g -O0 -c ./matplotpp.cc ./gl2ps.c 4 | ar r matplotpp.a matplotpp.o gl2ps.o 5 | rm matplotpp.o gl2ps.o 6 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | MATPLOT++ (MATLAB-like plotting tool in C++) 2 | Copyright (c) 2011 Yuichi Katori All Rights Reserved 3 | Author: Yuichi Katori (yuichi.katori@gmail.com) 4 | 5 | Getting Started 6 | ------------------------------------------ 7 | 8 | To install a required library on ubuntu 9 | 10 | $ sudo apt-get install freeglut3-dev 11 | 12 | To compile MATPLOT++ library, type as following: 13 | 14 | $ tar xvf matplotpp-0.3.x.tar 15 | $ cd matplotpp 16 | $ make 17 | 18 | To compile examples: 19 | 20 | $ cd examples 21 | $ make 22 | -------------------------------------------------------------------------------- /examples/ex101_creating_a_plot.cc: -------------------------------------------------------------------------------- 1 | //Copyright (c) 2011 Yuichi Katori All Rights Reserved 2 | //Author: Yuichi Katori (yuichi.katori@gmail.com) 3 | using namespace std; 4 | #include "matplotpp.h" 5 | class MP :public MatPlot{ 6 | void DISPLAY(){ 7 | vector x(100),y(100); 8 | for(int i=0;i<100;++i){ 9 | x[i]=0.1*i; 10 | y[i]=sin(x[i]); 11 | } 12 | plot(x,y); 13 | } 14 | }mp; 15 | void display(){ mp.display(); } 16 | void reshape(int w,int h){ mp.reshape(w,h); } 17 | int main(int argc,char* argv[]){ 18 | glutInit(&argc, argv); 19 | glutCreateWindow(100,100,400,300); 20 | glutDisplayFunc( display ); 21 | glutReshapeFunc( reshape ); 22 | glutMainLoop(); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /examples/Makefile: -------------------------------------------------------------------------------- 1 | #Makefile 2 | CC=g++ 3 | LIB=-lglut -lGLU -lGL 4 | INCDIR=-I../ 5 | ALIB=../matplotpp.a 6 | matplotpp_example: ex101_creating_a_plot.cc ex102_mouse_and_keyboard_events.cc ex103_multiple_plots.cc ex104_style_and_color.cc ex105_axes.cc ex106_surface_and_contour.cc ex107_3d_line_and_surface.cc ex108_animation.cc 7 | if [ ! -d bin] ;\ 8 | then mkdir bin ;\ 9 | fi 10 | $(CC) $(LIB) $(INCDIR) -o bin/ex101_creating_a_plot ex101_creating_a_plot.cc $(ALIB) 11 | $(CC) $(LIB) $(INCDIR) -o bin/ex102_mouse_and_keyboard_events ex102_mouse_and_keyboard_events.cc $(ALIB) 12 | $(CC) $(LIB) $(INCDIR) -o bin/ex103_multiple_plots ex103_multiple_plots.cc $(ALIB) 13 | $(CC) $(LIB) $(INCDIR) -o bin/ex104_style_and_color ex104_style_and_color.cc $(ALIB) 14 | $(CC) $(LIB) $(INCDIR) -o bin/ex105_axes ex105_axes.cc $(ALIB) 15 | $(CC) $(LIB) $(INCDIR) -o bin/ex106_surface_and_contour ex106_surface_and_contour.cc $(ALIB) 16 | $(CC) $(LIB) $(INCDIR) -o bin/ex107_3d_line_and_surface ex107_3d_line_and_surface.cc $(ALIB) 17 | $(CC) $(LIB) $(INCDIR) -o bin/ex108_animation ex108_animation.cc $(ALIB) 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /examples/ex102_mouse_and_keyboard_events.cc: -------------------------------------------------------------------------------- 1 | //Copyright (c) 2011 Yuichi Katori (yuichi.katori@gmail.com) All Rights Reserved 2 | using namespace std; 3 | #include "matplotpp.h" 4 | class MP :public MatPlot{ 5 | void DISPLAY(){ 6 | vector x(100),y(100); 7 | for(int i=0;i<100;++i){ 8 | x[i]=0.1*i; 9 | y[i]=sin(x[i]); 10 | } 11 | plot(x,y); 12 | } 13 | }mp; 14 | void display(){mp.display(); } 15 | void reshape(int w,int h){ mp.reshape(w,h); } 16 | void idle( void ){glutPostRedisplay(); usleep(10000);} 17 | void mouse(int button, int state, int x, int y ){ mp.mouse(button,state,x,y); } 18 | void motion(int x, int y ){mp.motion(x,y); } 19 | void passive(int x, int y ){mp.passivemotion(x,y); } 20 | void keyboard(unsigned char key, int x, int y){mp.keyboard(key, x, y); } 21 | int main(int argc, char* argv[]){ 22 | glutInit(&argc, argv); 23 | glutCreateWindow(100,100,400,300); 24 | glutDisplayFunc( display ); 25 | glutReshapeFunc( reshape ); 26 | glutIdleFunc( idle ); 27 | glutMotionFunc( motion ); 28 | glutMouseFunc( mouse ); 29 | glutPassiveMotionFunc(passive); 30 | glutKeyboardFunc( keyboard ); 31 | glutMainLoop(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /examples/ex106_surface_and_contour.cc: -------------------------------------------------------------------------------- 1 | //Copyright (c) 2011 Yuichi Katori (yuichi.katori@gmail.com) All Rights Reserved 2 | using namespace std; 3 | #include "matplotpp.h" 4 | class MP :public MatPlot{ 5 | void DISPLAY(){ 6 | // Prepare test data 7 | int n=100; 8 | vector x,y,z; 9 | x=linspace(-2,2,n); 10 | y=linspace(-2,2,n); 11 | vector< vector< double > > Z(n,vector(n)),C(n,vector(n)); 12 | for(int i=0;i x,y,z; 10 | x=linspace(-2,2,n); 11 | y=linspace(-2,2,n); 12 | vector< vector< double > > Z(n,vector(n)),C(n,vector(n)); 13 | for(int i=0;i x(n),y1(n),y2(n),y3(n),y4(n); 11 | for(int i=0;i x(n),y1(n),y2(n),y3(n),y4(n); 8 | 9 | // Create test data 10 | for(int i=0;i x(n),y(n); 10 | x=linspace(0,10,n); 11 | vector > Y(m,vector(n)); 12 | for(int j=0;j. 34 | */ 35 | 36 | #ifndef __GL2PS_H__ 37 | #define __GL2PS_H__ 38 | 39 | #include 40 | #include 41 | 42 | /* Define GL2PSDLL at compile time to build a Windows DLL */ 43 | 44 | #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) 45 | # if defined(_MSC_VER) 46 | # pragma warning(disable:4115) 47 | # pragma warning(disable:4996) 48 | # endif 49 | # include 50 | # if defined(GL2PSDLL) 51 | # if defined(GL2PSDLL_EXPORTS) 52 | # define GL2PSDLL_API __declspec(dllexport) 53 | # else 54 | # define GL2PSDLL_API __declspec(dllimport) 55 | # endif 56 | # else 57 | # define GL2PSDLL_API 58 | # endif 59 | #else 60 | # define GL2PSDLL_API 61 | #endif 62 | 63 | #if defined(__APPLE__) || defined(HAVE_OPENGL_GL_H) 64 | # include 65 | #else 66 | # include 67 | #endif 68 | 69 | /* Support for compressed PostScript/PDF/SVG and for embedded PNG 70 | images in SVG */ 71 | 72 | #if defined(HAVE_ZLIB) || defined(HAVE_LIBZ) 73 | # define GL2PS_HAVE_ZLIB 74 | # if defined(HAVE_LIBPNG) || defined(HAVE_PNG) 75 | # define GL2PS_HAVE_LIBPNG 76 | # endif 77 | #endif 78 | 79 | /* Version number */ 80 | 81 | #define GL2PS_MAJOR_VERSION 1 82 | #define GL2PS_MINOR_VERSION 3 83 | #define GL2PS_PATCH_VERSION 5 84 | #define GL2PS_EXTRA_VERSION "" 85 | 86 | #define GL2PS_VERSION (GL2PS_MAJOR_VERSION + \ 87 | 0.01 * GL2PS_MINOR_VERSION + \ 88 | 0.0001 * GL2PS_PATCH_VERSION) 89 | 90 | #define GL2PS_COPYRIGHT "(C) 1999-2009 C. Geuzaine" 91 | 92 | /* Output file formats (the values and the ordering are important!) */ 93 | 94 | #define GL2PS_PS 0 95 | #define GL2PS_EPS 1 96 | #define GL2PS_TEX 2 97 | #define GL2PS_PDF 3 98 | #define GL2PS_SVG 4 99 | #define GL2PS_PGF 5 100 | 101 | /* Sorting algorithms */ 102 | 103 | #define GL2PS_NO_SORT 1 104 | #define GL2PS_SIMPLE_SORT 2 105 | #define GL2PS_BSP_SORT 3 106 | 107 | /* Message levels and error codes */ 108 | 109 | #define GL2PS_SUCCESS 0 110 | #define GL2PS_INFO 1 111 | #define GL2PS_WARNING 2 112 | #define GL2PS_ERROR 3 113 | #define GL2PS_NO_FEEDBACK 4 114 | #define GL2PS_OVERFLOW 5 115 | #define GL2PS_UNINITIALIZED 6 116 | 117 | /* Options for gl2psBeginPage */ 118 | 119 | #define GL2PS_NONE 0 120 | #define GL2PS_DRAW_BACKGROUND (1<<0) 121 | #define GL2PS_SIMPLE_LINE_OFFSET (1<<1) 122 | #define GL2PS_SILENT (1<<2) 123 | #define GL2PS_BEST_ROOT (1<<3) 124 | #define GL2PS_OCCLUSION_CULL (1<<4) 125 | #define GL2PS_NO_TEXT (1<<5) 126 | #define GL2PS_LANDSCAPE (1<<6) 127 | #define GL2PS_NO_PS3_SHADING (1<<7) 128 | #define GL2PS_NO_PIXMAP (1<<8) 129 | #define GL2PS_USE_CURRENT_VIEWPORT (1<<9) 130 | #define GL2PS_COMPRESS (1<<10) 131 | #define GL2PS_NO_BLENDING (1<<11) 132 | #define GL2PS_TIGHT_BOUNDING_BOX (1<<12) 133 | 134 | /* Arguments for gl2psEnable/gl2psDisable */ 135 | 136 | #define GL2PS_POLYGON_OFFSET_FILL 1 137 | #define GL2PS_POLYGON_BOUNDARY 2 138 | #define GL2PS_LINE_STIPPLE 3 139 | #define GL2PS_BLEND 4 140 | 141 | /* Text alignment (o=raster position; default mode is BL): 142 | +---+ +---+ +---+ +---+ +---+ +---+ +-o-+ o---+ +---o 143 | | o | o | | o | | | | | | | | | | | | 144 | +---+ +---+ +---+ +-o-+ o---+ +---o +---+ +---+ +---+ 145 | C CL CR B BL BR T TL TR */ 146 | 147 | #define GL2PS_TEXT_C 1 148 | #define GL2PS_TEXT_CL 2 149 | #define GL2PS_TEXT_CR 3 150 | #define GL2PS_TEXT_B 4 151 | #define GL2PS_TEXT_BL 5 152 | #define GL2PS_TEXT_BR 6 153 | #define GL2PS_TEXT_T 7 154 | #define GL2PS_TEXT_TL 8 155 | #define GL2PS_TEXT_TR 9 156 | 157 | typedef GLfloat GL2PSrgba[4]; 158 | 159 | #if defined(__cplusplus) 160 | extern "C" { 161 | #endif 162 | 163 | GL2PSDLL_API GLint gl2psBeginPage(const char *title, const char *producer, 164 | GLint viewport[4], GLint format, GLint sort, 165 | GLint options, GLint colormode, 166 | GLint colorsize, GL2PSrgba *colormap, 167 | GLint nr, GLint ng, GLint nb, GLint buffersize, 168 | FILE *stream, const char *filename); 169 | GL2PSDLL_API GLint gl2psEndPage(void); 170 | GL2PSDLL_API GLint gl2psSetOptions(GLint options); 171 | GL2PSDLL_API GLint gl2psGetOptions(GLint *options); 172 | GL2PSDLL_API GLint gl2psBeginViewport(GLint viewport[4]); 173 | GL2PSDLL_API GLint gl2psEndViewport(void); 174 | GL2PSDLL_API GLint gl2psText(const char *str, const char *fontname, 175 | GLshort fontsize); 176 | GL2PSDLL_API GLint gl2psTextOpt(const char *str, const char *fontname, 177 | GLshort fontsize, GLint align, GLfloat angle); 178 | GL2PSDLL_API GLint gl2psSpecial(GLint format, const char *str); 179 | GL2PSDLL_API GLint gl2psDrawPixels(GLsizei width, GLsizei height, 180 | GLint xorig, GLint yorig, 181 | GLenum format, GLenum type, const void *pixels); 182 | GL2PSDLL_API GLint gl2psEnable(GLint mode); 183 | GL2PSDLL_API GLint gl2psDisable(GLint mode); 184 | GL2PSDLL_API GLint gl2psPointSize(GLfloat value); 185 | GL2PSDLL_API GLint gl2psLineWidth(GLfloat value); 186 | GL2PSDLL_API GLint gl2psBlendFunc(GLenum sfactor, GLenum dfactor); 187 | 188 | /* undocumented */ 189 | GL2PSDLL_API GLint gl2psDrawImageMap(GLsizei width, GLsizei height, 190 | const GLfloat position[3], 191 | const unsigned char *imagemap); 192 | GL2PSDLL_API const char *gl2psGetFileExtension(GLint format); 193 | GL2PSDLL_API const char *gl2psGetFormatDescription(GLint format); 194 | 195 | #if defined(__cplusplus) 196 | } 197 | #endif 198 | 199 | #endif /* __GL2PS_H__ */ 200 | -------------------------------------------------------------------------------- /matplotpp.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | Copyright (c) 2011-2016 Yuichi Katori All Rights Reserved 3 | License: Gnu Public license (GPL) v3 4 | Author: Yuichi Katori (yuichi.katori@gmail.com) 5 | Project:MATPLOT++ (MATLAB-like plotting tool in C++). 6 | Version:0.3.15 7 | ****************************************************************************/ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include "gl2ps.h" 18 | 19 | #define PI 3.14159265358979323846264 20 | 21 | typedef vector dvec; 22 | typedef vector< vector > dmat; 23 | typedef vector< vector > tcvec; // true color vector 24 | typedef vector< vector< vector > > tcmat;//true color matrix 25 | typedef vector tc;//true color 26 | typedef vector tcolor;//true color 27 | 28 | /// glutCreate Window 29 | inline int glutCreateWindow(int left,int top,int width,int height,char c[]){ 30 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 31 | glutInitWindowPosition( left, top ); 32 | glutInitWindowSize( width, height ); 33 | return glutCreateWindow(c); 34 | }; 35 | inline int glutCreateWindow(int left,int top,int width,int height){ 36 | char c[]="MATPLOT++"; 37 | return glutCreateWindow(left,top,width,height,c); 38 | }; 39 | /// Primitives // 40 | class Figure{/// 41 | public: 42 | int id; 43 | //int Status;// 0:minimized, 1:default position, 2:maximized 44 | int Position[4];//left top width height 45 | int Visible; 46 | vector Children; 47 | 48 | void add_child(int i); 49 | Figure(int id_){ 50 | id=id_; 51 | //Status=1; 52 | //Position[0]=100; 53 | //Position[1]=100; 54 | //Position[2]=800; 55 | //Position[3]=800; 56 | Visible=1; 57 | }; 58 | }; 59 | 60 | 61 | class Layer{/// 62 | public: 63 | int id; 64 | int Visible; 65 | string layername; 66 | vector Children; 67 | Layer(int id_); 68 | void add_child(int i); 69 | }; 70 | 71 | class Axes{/// 72 | private: 73 | 74 | protected: 75 | 76 | public: 77 | int id; 78 | 79 | float cta,phi; // controled by mouse 80 | float cta0,phi0;// default value or specified by command line 81 | // Mouse 82 | double XMouse,YMouse; 83 | int Mouse; 84 | 85 | double xmin,xmax,ymin,ymax,zmin,zmax; 86 | int num_child; 87 | 88 | void reset(); 89 | void config(); 90 | int ID(); 91 | int selected(); 92 | void selected(int i); 93 | void add_child(int i); 94 | dvec make_tick(double min,double max); 95 | double flog10(double x); 96 | 97 | int View;// 0:2D, 1:3D 98 | 99 | vector > ColorMap;// for colorbar 100 | 101 | // Matlab variables // 102 | // styles 103 | int Box;//0:Off, 1:On 104 | string GridLineStyle; 105 | float LineWidth; 106 | string TickDir;// {in} | out 107 | //string TickDirMode; 108 | //TickLength 109 | int Visible;//0:Off, 1:On 110 | int XGrid,YGrid,ZGrid;// {0:Off}, 1:On 111 | 112 | // General Information 113 | int Parent; 114 | vector Children; 115 | int Selected; 116 | float Position[4];//left bottom width height 117 | float Viewport3d[4];//left bottom width height 118 | 119 | //Scale 120 | string XAxisLocation; 121 | string YAxisLocation; 122 | 123 | //string XDir,YDir,ZDir; 124 | 125 | double XLim[2],YLim[2],ZLim[2];//plot range 126 | int XLimMode,YLimMode,ZLimMode;//0:Auto 1:Manual 127 | 128 | int XScale,YScale,ZScale;// 0:linear | 1:log 129 | 130 | dvec XTick,YTick,ZTick; 131 | string XTickMode,YTickMode,ZTickMode; 132 | int TickLabel;// 0:Off, {1:On} 133 | //View 134 | float CameraPosition[3]; 135 | float CameraTarget[3]; 136 | float CameraUpVector[3]; 137 | 138 | // Label 139 | string Title; 140 | string XLabel,YLabel,ZLabel; 141 | 142 | 143 | // Mapping of color 144 | double CLim[2]; 145 | string CLimMode;// {auto} | manual 146 | dvec CTick; //not in matlab 147 | 148 | Axes(int id_){ 149 | id=id_; 150 | Selected=0; 151 | Position[0]=0.13; 152 | Position[1]=0.11; 153 | Position[2]=0.775; 154 | Position[3]=0.815; 155 | 156 | Viewport3d[0]=0.0; 157 | Viewport3d[1]=0.0; 158 | Viewport3d[2]=1.0; 159 | Viewport3d[3]=1.0; 160 | 161 | Mouse=1; 162 | View=0; 163 | Visible=1; 164 | Box=1; 165 | Children.clear(); 166 | 167 | cta0=30; 168 | phi0=30; 169 | cta=cta0; 170 | phi=cta0; 171 | 172 | CameraPosition[0]=1; CameraPosition[1]=1; CameraPosition[2]=1; 173 | CameraTarget[0]=0.; CameraTarget[1]=0; CameraTarget[2]=0; 174 | CameraUpVector[0]=0; CameraUpVector[1]=0; CameraUpVector[2]=1; 175 | 176 | LineWidth=1; 177 | 178 | GridLineStyle=":"; 179 | XGrid=0; 180 | YGrid=0; 181 | ZGrid=0; 182 | 183 | XLim[0]=0; XLim[1]=10; 184 | YLim[0]=0; YLim[1]=10; 185 | ZLim[0]=0; ZLim[1]=10; 186 | 187 | XLimMode=0; YLimMode=0; ZLimMode=0; 188 | XAxisLocation="bottom";//top | bottom 189 | YAxisLocation="left";// left | right 190 | XScale=0;// {0:linear} | 1:log 191 | YScale=0; 192 | ZScale=0; 193 | 194 | TickLabel=1; 195 | TickDir="in"; 196 | 197 | xmin= 1e99; xmax=-1e99; 198 | ymin= 1e99; ymax=-1e99; 199 | zmin= 1e99; zmax=-1e99; 200 | 201 | CLim[0]=0;CLim[1]=1; 202 | CLimMode="auto"; 203 | 204 | num_child=0; 205 | //MakeTick(); 206 | //Parent=i_figure; 207 | }; 208 | }; 209 | 210 | class Colorbar{/// 211 | public: 212 | // Appearence 213 | tc color; 214 | string box; 215 | float LineWidth; 216 | string Label; 217 | // location and size 218 | string Location; 219 | float Position[4];//left bottom width height 220 | string Units; 221 | 222 | // Ticks and label 223 | dvec Ticks; 224 | string TickMode; 225 | string TickLabels; 226 | string TickLabelsMode; 227 | string TickLabelsInterpreter; 228 | string Direction; 229 | string AxisLocation; 230 | string AxisLocationMode; 231 | string TickDirection; 232 | float TickLength; 233 | double Limits[2]; 234 | string LimitsMode; 235 | 236 | }; 237 | 238 | class Legend{/// 239 | public: 240 | string TextColor; 241 | string Color;//background color 242 | int Box; 243 | string EdgeColor; 244 | float LineWidth; 245 | string Location; 246 | string Orientation; 247 | float Position[4]; 248 | string Units; 249 | string String; 250 | //string Interpreter; 251 | 252 | }; 253 | class Line{/// 254 | public: 255 | int id; 256 | int Errorbar; 257 | 258 | void reset(); 259 | void color(float r,float g,float b); 260 | 261 | // Matlab oriented variables // 262 | 263 | dvec XData,YData,ZData; 264 | dvec YPData,YMData; 265 | //dmat XData,YData,ZData; 266 | //dmat EData,UData,LData; 267 | //dmat VData,WData; 268 | 269 | string Color; 270 | string LineStyle;// {-} | - - | : | -. | none 271 | float LineWidth; 272 | string Marker;// {none} 273 | float MarkerSize; 274 | string MarkerEdgeColor; 275 | string MarkerFaceColor; 276 | 277 | int Clipping; 278 | //string EraseMode; 279 | int SelectionHighlight; 280 | int Visible; 281 | 282 | // General Information 283 | int Parent; 284 | int Selected; 285 | 286 | Line(int id_){ 287 | id=id_; 288 | 289 | Color="b"; 290 | LineStyle="-"; 291 | LineWidth=0.5; 292 | 293 | Marker="none"; 294 | MarkerSize=6; 295 | MarkerEdgeColor="k"; 296 | MarkerFaceColor="w"; 297 | 298 | Errorbar=0; 299 | //PlotStyle=0; 300 | } 301 | }; 302 | class Surface{/// 303 | public: 304 | int type; 305 | int id; 306 | string ColorMap; 307 | 308 | //dvec XData,YData; 309 | dvec V;// for contour plot 310 | int NContour; 311 | 312 | // Face // 313 | string FaceColor;//ColorSpec | none | {flat} 314 | 315 | // Edge // 316 | string EdgeColor;//ColorSpec{k} | none | flat 317 | // string EdgeAlpha 318 | // string EdgeLighting 319 | string LineStyle;// {-} | - - | : | -. | none 320 | float LineWidth; 321 | // string AlignVertexCenters 322 | // string MeshStyle 323 | 324 | // Maker // 325 | string Marker;// {none} 326 | float MarkerSize; 327 | string MarkerEdgeColor; 328 | string MarkerFaceColor; 329 | 330 | // Color mapping // 331 | dmat CDataIndex; 332 | tcmat CData; // true color 333 | string CDataMapping;//{scaled} | direct 334 | string CDataMode; // {auto} | manual 335 | 336 | // Data // 337 | dmat XData,YData,ZData; 338 | dvec UserData; 339 | 340 | double CLim[2];// non in matlab 341 | 342 | // Visivility // 343 | 344 | // Others // 345 | int Parent; 346 | 347 | Surface(int id_){ 348 | id=id_; 349 | 350 | NContour=10; 351 | V.clear(); 352 | 353 | ColorMap="Gray"; 354 | //Shading="faceted"; 355 | FaceColor="flat"; 356 | EdgeColor="b"; 357 | LineStyle="-"; 358 | LineWidth=0.5; 359 | 360 | CDataMapping="scaled"; 361 | CDataMode="auto"; 362 | } 363 | void get(){ 364 | cout <<"FaceColor: "<< FaceColor < > Faces; 378 | dmat Vertices; 379 | dmat FaceVertexCData; 380 | dmat XData,YData,ZData; 381 | 382 | //dvec ICVec; 383 | //dmat ICMat; 384 | //tcmat CData; 385 | tcvec CData; 386 | 387 | string EdgeColor,FaceColor;//{ColorSpec}|none|flat|interp 388 | 389 | string LineStyle; // {-} | - - | : | -. | none 390 | float LineWidth; 391 | 392 | Patch(int id_){ 393 | id=id_; 394 | type=0; 395 | LineWidth=1; 396 | FaceColor="r"; 397 | EdgeColor="k"; 398 | LineStyle="-"; 399 | } 400 | }; 401 | //Note: XData[iv][if] 402 | 403 | class Text{/// 404 | public: 405 | int id; 406 | string String; 407 | float Position[3]; 408 | int Parent; 409 | int type;//0:axis 1:figure 410 | Text(int id_); 411 | }; 412 | 413 | 414 | 415 | const int tFigure=1; 416 | const int tAxes=2; 417 | const int tLine=3; 418 | const int tSurface=4; 419 | const int tText=5; 420 | const int tLayer=6; 421 | const int tPatch=7; 422 | const int tColorbar=8; 423 | const int tLegend=9; 424 | 425 | /// contour 426 | struct ContourPoint{ 427 | double x,y; 428 | int xj,yi; 429 | int xy; 430 | int done; 431 | }; 432 | dmat contourc(dvec x, dvec y, dmat Z, dvec v); 433 | 434 | 435 | class MatPlot{/// 436 | private: 437 | int is_debug1; 438 | int is_debug2; 439 | 440 | vector > cmap;//TODO: move to class Figure 441 | 442 | int mode;//0:initialization 1:configuration 443 | int init_level;// initialization level of objects 444 | int hObj;// handle number of current object 445 | 446 | int time_layer_clicked,time_layer_clicked_last; 447 | 448 | // Events // 449 | int window_w,window_h; 450 | float xButtonDown,yButtonDown;// last clicked mouse position 451 | float ctaButtonDown,phiButtonDown; 452 | int xPassive,yPassive; 453 | 454 | // pointer to current objects // 455 | Figure *cf; 456 | Layer *cfr; 457 | Axes *ca; 458 | Line *cl; 459 | Surface *cs; 460 | Patch *cp; 461 | Text *ct; 462 | Colorbar *ccb; 463 | Legend *clg; 464 | 465 | // objects containers // 466 | vector< Figure > vFigure; 467 | vector< Layer > vLayer; 468 | vector< Axes > vAxes; 469 | vector< Line > vLine; 470 | vector< Surface > vSurface; 471 | vector< Patch > vPatch; 472 | vector< Text > vText; 473 | vector< Colorbar > vColorbar; 474 | vector< Legend> vLegend; 475 | 476 | // objects counter // 477 | int iFigure; 478 | int iLayer; 479 | int iAxes; 480 | int iLine; 481 | int iSurface; 482 | int iPatch; 483 | int iText; 484 | int iColorbar; 485 | int iLegend; 486 | 487 | // Selected object // 488 | int iAxesSelected; 489 | 490 | // coordinate transform // 491 | // figure coordination 492 | float ctx2(double x); 493 | float cty2(double y); 494 | // axes coordination 495 | float ctx(double x); 496 | float cty(double y); 497 | float ct3x(double x); 498 | float ct3y(double y); 499 | float ct3z(double z); 500 | 501 | void SPrintx(char *s,double x); 502 | 503 | int figure(); 504 | 505 | // display // 506 | void display_figure(); 507 | void display_layer(); 508 | void display_layer2(); 509 | 510 | void display_axes(); 511 | void display_axes_2d(); 512 | void display_axes_3d(); 513 | void display_axes_colorbar(); 514 | 515 | void display_line(); 516 | 517 | void display_surface(); 518 | void display_surface_2d(); 519 | void display_surface_3d(); 520 | void display_pcolor(); 521 | void display_contour(); 522 | 523 | void display_patch(); 524 | void display_patch_2d(); 525 | void display_patch_3d(); 526 | 527 | 528 | void display_bar(); 529 | 530 | void display_text(); 531 | 532 | // mouse // 533 | void Layer_mouse(int button, int state, int x, int y ); 534 | void Axes_mouse(int button, int state, int x, int y ); 535 | void Axes_motion(int x, int y ); 536 | 537 | 538 | void surface_config(); 539 | void line_config(); 540 | void patch_config(); 541 | tcvec Index2TrueColor(dvec IC); 542 | 543 | public: 544 | 545 | MatPlot(); 546 | ~MatPlot(); 547 | 548 | void virtual DISPLAY(){}; 549 | 550 | void inline debug1(){is_debug1=1;} 551 | void inline debug2(){is_debug2=1;} 552 | 553 | vector linspace(double min,double max,int n){ 554 | vector a; 555 | if(n<1){n=1;} 556 | a.resize(n); 557 | for(int i=0;i valinspace(double min,double max,int n){ 562 | valarray a; 563 | a.resize(n); 564 | for(int i=0;i x,valarray y); 653 | 654 | int plot3(dvec x,dvec y,dvec z); 655 | //int plot3(dvec X,dvec Y,dvec Z); 656 | 657 | int semilogx(dvec x,dvec y); 658 | int semilogy(dvec x,dvec y); 659 | //int loglog(dvec y); 660 | int loglog(dvec x,dvec y); 661 | 662 | //int polar(dvec theta,dvec rho); 663 | 664 | void vertex(double x,double y,double ep,double em); 665 | int errorbar(dvec x,dvec y,dvec e); 666 | int errorbar(dvec x,dvec y,dvec ep,dvec em); 667 | 668 | //int quiver(U,V); 669 | //int quiver(X,Y,U,V); 670 | 671 | //int scatter(X,Y,S,C) 672 | //int scatter(X,Y,S) 673 | //int scatter(X,Y) 674 | 675 | // Surface, Contour /// 676 | dmat peaks(int n); 677 | //dmat peaks(int m,int n); 678 | //dmat peaks(int m,int n,string type); 679 | 680 | int surface(); 681 | int surface(dmat Z); 682 | int surface(dvec x,dvec y,dmat Z); 683 | int surface(dmat X,dmat Y,dmat Z); 684 | int surface(dmat Z,tcmat C); //!! 685 | int surface(dvec x,dvec y,dmat Z,tcmat C);//!! 686 | int surface(dmat X,dmat Y,dmat Z,tcmat C);//!! 687 | int surface(dmat Z,dmat C);//(not in matlab) 688 | int surface(dvec x,dvec y,dmat Z,dmat C); 689 | int surface(dmat X,dmat Y,dmat Z,dmat C); 690 | 691 | int pcolor(dmat C); 692 | int pcolor(dvec x,dvec y,dmat C); 693 | int pcolor(dmat X,dmat Y,dmat C); 694 | 695 | int pcolor(tcmat C); 696 | int pcolor(dvec x,dvec y,tcmat C); 697 | int pcolor(dmat X,dmat Y,tcmat C); 698 | 699 | int contour(dmat Z); 700 | int contour(dmat Z,int n); 701 | int contour(dmat Z,dvec v); 702 | int contour(dvec x, dvec y, dmat Z); 703 | int contour(dvec x, dvec y, dmat Z,int n); 704 | int contour(dvec x, dvec y, dmat Z,dvec v); 705 | //int contour(dmat X, dmat Y, dmat Z); 706 | //int contour(dmat X, dmat Y, dmat Z,int n); 707 | //int contour(dmat X, dmat Y, dmat Z,dvec v); 708 | 709 | //int mesh(dmat Z); 710 | //int mesh(dmat Z,dmat C); 711 | //int mesh(dmat Z,tcmat C); 712 | int mesh(dvec x, dvec y, dmat Z); 713 | //int mesh(dvec x, dvec y, dmat Z,dmat C); 714 | //int mesh(dvec x, dvec y, dmat Z,tcmat C); 715 | //int mesh(dmat X,dmat Y,dmat Z); 716 | //int mesh(dmat X,dmat Y,dmat Z,dmat C); 717 | //int mesh(dmat X,dmat Y,dmat Z,tcmat C); 718 | // meshc() 719 | // meshz() 720 | 721 | int surf(dvec x, dvec y, dmat Z); 722 | 723 | // Patch /// 724 | 725 | int patch(); 726 | int patch(dmat X,dmat Y); 727 | int patch(dmat X,dmat Y,dvec C); 728 | int patch(dmat X,dmat Y,tcvec C); 729 | int patch(dmat X,dmat Y,dmat Z);//!! 730 | int patch(dmat X,dmat Y,dmat Z,dvec C);//!! 731 | int patch(dmat X,dmat Y,dmat Z,tcvec C);//!! 732 | //int patch(dmat X,dmat Y,tcmat C); 733 | //int patch(dmat X,dmat Y,dmat Z,tcmat C); 734 | 735 | int bar(dvec y); 736 | int bar(dvec y,float width); 737 | int bar(dvec x,dvec y); 738 | int bar(dvec x,dvec y,float width); 739 | 740 | //int bar(Y) 741 | //int bar(Y,float width); 742 | //int bar(Y,string style); 743 | //int bar(Y,float width,string style); 744 | 745 | //int bar(x,Y) 746 | //int bar(x,Y,float width); 747 | //int bar(x,Y,string style); 748 | //int bar(x,Y,float width,string style); 749 | 750 | //int hist(y); 751 | //int hist(y,x); 752 | //int hist(y,nbins); 753 | 754 | //int pie(dvec x); 755 | //int pie(dvec x, vector Explode); 756 | 757 | // Text /// 758 | //TODO: more fonts 759 | int text(); 760 | int text(double x,double y,string s); 761 | void set_font(char font_[],int size); 762 | void ptext(float x,float y,string s); 763 | void ptext3(float x,float y,float z,string s); 764 | void ptext3c(float x,float y,float z,string s); 765 | 766 | // Colors /// 767 | void color(float r,float g,float b); 768 | vector colormap(string c,float t); 769 | void colormap(string c); 770 | void colormap(vector > c); 771 | 772 | void gray(); 773 | void jet(); 774 | void hsv(); 775 | void hot(); 776 | void cool(); 777 | void spring(); 778 | void summer(); 779 | void autumn(); 780 | void winter(); 781 | 782 | vector map2color(double x,double xmin,double xmax); 783 | 784 | void Shading(string c); 785 | void shading(string c); 786 | vector ColorSpec2RGB(string c); 787 | string rgb2colorspec(vector rgb); 788 | tcolor colorspec2rgb(string c); 789 | // print /// 790 | void print(); 791 | 792 | }; 793 | /// EOF 794 | -------------------------------------------------------------------------------- /matplotpp.cc: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | Copyright (c) 2011-2016 Yuichi Katori All Rights Reserved 3 | License: Gnu Public license (GPL) v3 4 | Author: Yuichi Katori (yuichi.katori@gmail.com) 5 | Project:MATPLOT++ (MATLAB-like plotting tool in C++). 6 | Version:0.3.15 7 | ****************************************************************************/ 8 | using namespace std; 9 | #include "matplotpp.h" 10 | 11 | /// Figure 12 | void Figure::add_child(int i){Children.push_back(i);} 13 | /// Axes 14 | void Axes::reset(){ 15 | num_child=0; 16 | xmin=1e99; xmax=-1e99; 17 | ymin=1e99; ymax=-1e99; 18 | zmin=1e99; zmax=-1e99; 19 | } 20 | void Axes::config(){ 21 | float extent=0,extent_linear=0.03; 22 | if((XLimMode==0)&&(xmax>xmin)){ 23 | if(XScale==0){extent=extent_linear;} 24 | if(XScale==1){extent=0;} 25 | XLim[0]=xmin-extent*(xmax-xmin); 26 | XLim[1]=xmax+extent*(xmax-xmin); 27 | } 28 | if((YLimMode==0)&&(ymax>ymin)){ 29 | if(YScale==0){extent=extent_linear;} 30 | if(YScale==1){extent=0;} 31 | YLim[0]=ymin-extent*(ymax-ymin); 32 | YLim[1]=ymax+extent*(ymax-ymin); 33 | } 34 | if((ZLimMode==0)&&(zmax>zmin)){ 35 | ZLim[0]=zmin-extent*(zmax-zmin); 36 | ZLim[1]=zmax+extent*(zmax-zmin); 37 | } 38 | //printf("Z: %d,%f,%f\n",ZLimMode,ZLim[0],ZLim[1]); 39 | //if(num_child){Visible=1;}else{Visible=0;} 40 | 41 | XTick=make_tick(XLim[0],XLim[1]); 42 | YTick=make_tick(YLim[0],YLim[1]); 43 | ZTick=make_tick(ZLim[0],ZLim[1]); 44 | CTick=make_tick(CLim[0],CLim[1]); 45 | } 46 | 47 | int Axes::ID(){return id;} 48 | int Axes::selected(){return Selected;} 49 | void Axes::selected(int i){Selected=i;} 50 | void Axes::add_child(int i){Children.push_back(i);} 51 | double Axes::flog10(double x){ 52 | int z=(int)log10(x); 53 | if(log10(x)<0){z=z-1;} 54 | return pow(10,z); 55 | } 56 | dvec Axes::make_tick(double min,double max){ 57 | int i,j; 58 | double dg; 59 | double x,y; 60 | int z; 61 | x=fabs(max-min); 62 | //z=(int)log10(x); 63 | //y=x/pow((double)10,(double)z); 64 | //dg=1*pow((double)10,(double)z); 65 | //if(y<5){dg=0.5*pow((double)10,(double)z);} 66 | //if(y<2){dg=0.2*pow((double)10,(double)z);} 67 | 68 | double d1,d2,d5; 69 | d1=flog10(x); 70 | d2=2*flog10(x/2); 71 | d5=5*flog10(x/5); 72 | dg=d1; 73 | if(dg>d2){dg=d2;} 74 | if(dg>d5){dg=d5;} 75 | 76 | double min0,t; 77 | min0=min-fmod(min,dg);j=0; 78 | 79 | dvec tick; 80 | tick.clear(); 81 | if(max>min){ 82 | i=-2; 83 | while(max>=min0+dg*i){ 84 | t=min0+dg*i; 85 | //if(min+0.5*dg=min0-dg*i){ tick.push_back(min0-dg*i); j++; } 93 | i+=1; 94 | } 95 | } 96 | return tick; 97 | } 98 | 99 | 100 | /// Line 101 | 102 | void Line::reset(){ 103 | XData.clear(); 104 | YData.clear(); 105 | ZData.clear(); 106 | YPData.clear(); 107 | YMData.clear(); 108 | } 109 | void Line::color(float r,float g,float b){ 110 | //Color[0]=r; 111 | //Color[1]=g; 112 | //Color[2]=b; 113 | } 114 | /// Surface 115 | 116 | /// Text 117 | Text::Text(int id_){ 118 | id=id_; 119 | type=0; 120 | } 121 | 122 | 123 | /// Layer 124 | Layer::Layer(int id_){ 125 | id=id_; 126 | Children.clear(); 127 | } 128 | /// Patch 129 | 130 | void Layer::add_child(int i){Children.push_back(i);} 131 | 132 | ///// MatPlot // 133 | MatPlot::MatPlot(){ 134 | is_debug1=0; 135 | is_debug2=0; 136 | 137 | if(is_debug1){cout<<"init()..."<Color[0]=r; 251 | //cl->Color[1]=g; 252 | //cl->Color[2]=b; 253 | } 254 | } 255 | /// coordinate transform 256 | 257 | // figure coordination 258 | float MatPlot::ctx2(double x){ 259 | double t; 260 | if(ca->XScale==0){//linear 261 | return ca->Position[0] +ca->Position[2]*( (x-ca->XLim[0])/(ca->XLim[1]-ca->XLim[0]) ); 262 | } 263 | if(ca->XScale==1){//log 264 | t=( log10(x) - log10(ca->XLim[0]) )/( log10(ca->XLim[1])-log10(ca->XLim[0]) ); 265 | if(x<=0){t=-1;} 266 | return ca->Position[0] +ca->Position[2]*t; 267 | } 268 | 269 | } 270 | float MatPlot::cty2(double y){ 271 | if(ca->YScale==0){//linear 272 | return ca->Position[1] +ca->Position[3]*( (y-ca->YLim[0])/(ca->YLim[1]-ca->YLim[0]) ); 273 | } 274 | if(ca->YScale==1){//log 275 | return ca->Position[1] +ca->Position[3]*( log10(y) - log10(ca->YLim[0]) )/( log10(ca->YLim[1])-log10(ca->YLim[0]) ); 276 | } 277 | } 278 | // axes coordination 279 | float MatPlot::ctx(double x){ 280 | if(ca->XScale==0){//linear 281 | return (x-ca->XLim[0])/(ca->XLim[1]-ca->XLim[0]); 282 | } 283 | if(ca->XScale==1){//log 284 | return ( log10(x) - log10(ca->XLim[0]) )/( log10(ca->XLim[1])-log10(ca->XLim[0]) ); 285 | } 286 | } 287 | float MatPlot::cty(double y){ 288 | if(ca->YScale==0){//linear 289 | return (y-ca->YLim[0])/(ca->YLim[1]-ca->YLim[0]); 290 | } 291 | if(ca->YScale==1){//log 292 | return ( log10(y) - log10(ca->YLim[0]) )/( log10(ca->YLim[1])-log10(ca->YLim[0]) ); 293 | } 294 | } 295 | float MatPlot::ct3x(double x){ 296 | return -1+2*(x-ca->XLim[0])/(ca->XLim[1]-ca->XLim[0]); 297 | } 298 | float MatPlot::ct3y(double y){ 299 | return -1+2*(y-ca->YLim[0])/(ca->YLim[1]-ca->YLim[0]); 300 | } 301 | float MatPlot::ct3z(double z){ 302 | return -1+2*(z-ca->ZLim[0])/(ca->ZLim[1]-ca->ZLim[0]); 303 | } 304 | void MatPlot::SPrintx(char *s,double x){ 305 | int H=1+(int)log10(x); 306 | double r=fmod(fabs(x),1.0); 307 | int X=(int)fabs(x*1000000); 308 | int Y=1000000; 309 | int L=0; 310 | for(int i=1;i<=6;i++){ 311 | //printf("X%Y %d\n",X/Y); 312 | if(X%Y!=0){L=i;} Y/=10; 313 | } 314 | 315 | //printf("%15.8f H=%2d L=%d ",x,H,L); 316 | if(H>= 3){ 317 | sprintf(s,"%.0f ",x); 318 | }else{ 319 | if(L==0){sprintf(s,"%.1f ",x);} 320 | if(L==1){sprintf(s,"%.2f ",x);} 321 | if(L==2){sprintf(s,"%.3f ",x);} 322 | if(L==3){sprintf(s,"%.4f ",x);} 323 | if(L==4){sprintf(s,"%.5f ",x);} 324 | if(L==5){sprintf(s,"%.6f ",x);} 325 | if(L>=6){sprintf(s,"%.7f ",x);} 326 | } 327 | } 328 | 329 | /// set 330 | /// set(v) 331 | void MatPlot::set(string v){ 332 | int h=gco(); 333 | int tObj=h%100; 334 | int iObj=h/100; 335 | //if( (tObj==tLine) && (iObj" ){ set(h,"Marker",">"); set(h,"LineStyle","none"); } 384 | if( v=="<" ){ set(h,"Marker","<"); set(h,"LineStyle","none"); } 385 | if( v=="p" ){ set(h,"Marker","p"); set(h,"LineStyle","none"); } 386 | if( v=="h" ){ set(h,"Marker","h"); set(h,"LineStyle","none"); } 387 | if( v=="|" ){ set(h,"Marker","|"); set(h,"LineStyle","none"); } 388 | 389 | } 390 | void MatPlot::set(float v){ 391 | int h=gco(); 392 | int tObj=h%100; 393 | int iObj=h/100; 394 | if( (tObj==tLine) && (iObjLineWidth=linewidth; 473 | //} 474 | 475 | /// Events // 476 | void MatPlot::reshape(int w, int h){ 477 | window_w=w; 478 | window_h=h; 479 | if(mode==2){ 480 | glViewport(0,0,w,h); 481 | } 482 | //cout <<"window size: "<< w <<" "<id);} 547 | 548 | int tObj;// type of child object 549 | int iObj;// index of child object 550 | if(cf->Visible){ 551 | glEnable(GL_DEPTH_TEST); 552 | //glDepthFunc(GL_LESS); 553 | //glDepthFunc(GL_EQUAL); 554 | glDepthFunc(GL_LEQUAL); 555 | 556 | glClearColor(1, 1, 1, 0.); 557 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 558 | 559 | glViewport(0,0, (int)(window_w),(int)(window_h)); 560 | glLoadIdentity(); 561 | //gluOrtho2D( 0.0, 1.0, 0.0, 1.0 ); 562 | glOrtho(0, 1, 0, 1, -1, 3); 563 | 564 | for(int i=0;iChildren.size();++i){ 565 | tObj=cf->Children[i]%100; 566 | iObj=cf->Children[i]/100; 567 | //printf("Obj t:%3d i:%3d \n",tObj,iObj); 568 | 569 | if(tObj==tLayer){ 570 | cfr=&vLayer[iObj]; 571 | if(cfr->Visible){ display_layer();} 572 | } 573 | } 574 | /* 575 | for(int i1=0;i1Children.size();++i1){ 576 | tObj1=cf->Children[i1]%100; 577 | iObj1=cf->Children[i1]/100; 578 | if(tObj1==tLayer){ 579 | cfr=&vLayer[iObj1]; 580 | if(cfr->Visible){ display_layer();} 581 | 582 | for(int i2=0;i2Children.size();++i2){ 583 | tObj2=cfr->Children[i2]%100; 584 | iObj2=cfr->Children[i2]/100; 585 | if(tObj2==tAxes){ 586 | ca=&vAxes[iObj2]; 587 | if(ca->Children.size()){display_axes();} 588 | } 589 | } 590 | } 591 | } 592 | */ 593 | 594 | //Visible(1); 595 | 596 | display_layer2(); 597 | 598 | glFlush(); 599 | glViewport(0,0,window_w,window_h); 600 | 601 | } 602 | glutSwapBuffers(); 603 | } 604 | 605 | 606 | /// Layer // 607 | int MatPlot::layer(){ 608 | int h=iLayer*100 + tLayer;hObj=h; 609 | if(is_debug1){printf("mode: %d handle: %4d Layer\n",mode,h);} 610 | 611 | if(mode==0){ 612 | vLayer.push_back(Layer(h)); 613 | cf->add_child(h); 614 | } 615 | if(mode==1){ 616 | 617 | } 618 | if(iLayerid,cfr->layername.c_str());} 629 | 630 | int tObj;// type of child object 631 | int iObj;// index of child object 632 | for(int i=0;iChildren.size();++i){ 633 | tObj=cfr->Children[i]%100; 634 | iObj=cfr->Children[i]/100; 635 | if(tObj==tAxes){ 636 | ca=&vAxes[iObj]; 637 | display_axes(); 638 | } 639 | }//i 640 | } 641 | void MatPlot::display_layer2(){ 642 | int l,t,w,h,r; 643 | string s; 644 | l=1; 645 | t=1; 646 | w=20;//button_width; 647 | h=20;//button_height; 648 | r=3; 649 | 650 | if(xPassive<25){ 651 | 652 | 653 | glViewport(0,0, (int)(window_w),(int)(window_h)); 654 | glLoadIdentity(); 655 | gluOrtho2D( 0.0, (int)(window_w), (int)(window_h),0 ); 656 | 657 | glDisable(GL_LINE_STIPPLE); 658 | gl2psDisable(GL2PS_LINE_STIPPLE); 659 | 660 | glLineWidth(2); 661 | glColor3d(0,0,1); 662 | 663 | for(int j=0;jVisible=Visible; 752 | cfr->layername=s; 753 | } 754 | return h; 755 | } 756 | int MatPlot::frame(string s,int Visible){ 757 | int h=layer(); 758 | if(mode==0){ 759 | cfr->Visible=Visible; 760 | cfr->layername=s; 761 | } 762 | return h; 763 | } 764 | 765 | /// Axes // 766 | int MatPlot::gca(){ 767 | return ca->id; 768 | } 769 | /// create axes 770 | int MatPlot::axes(){ 771 | int h=iAxes*100 + tAxes; hObj=h; 772 | //int h=handle(iAxes,tAxes); 773 | if(is_debug1){printf("mode: %d handle: %4d Axes \n",mode,h);} 774 | 775 | if(mode==0){ 776 | cfr->add_child(h); 777 | vAxes.push_back(Axes(h)); 778 | } 779 | if(mode==1){ 780 | if(iAxesVisible=cfr->Visible; 784 | iAxes++; 785 | return h; 786 | } 787 | /// display 788 | void MatPlot::display_axes(){ 789 | if(is_debug1){printf("mode: %d handle: %4d Axes #Children %d\n", 790 | mode,ca->id,ca->Children.size());} 791 | 792 | if(ca->Children.size()){ 793 | if(ca->View==0){//2D 794 | display_axes_2d(); 795 | } 796 | if(ca->View==1){//2D 797 | display_axes_3d(); 798 | } 799 | } 800 | if(ca->View==2){//colorbar 801 | display_axes_colorbar(); 802 | } 803 | 804 | // childlen // 805 | int tObj;// type of child object 806 | int iObj;// index of child object 807 | for(int i=0;iChildren.size();++i){ 808 | tObj=ca->Children[i]%100; 809 | iObj=ca->Children[i]/100; 810 | if(tObj==tLine){ 811 | cl=&vLine[iObj]; 812 | display_line(); 813 | } 814 | if(tObj==tSurface){ 815 | cs=&vSurface[iObj]; 816 | display_surface(); 817 | } 818 | if(tObj==tText){ 819 | ct=&vText[iObj]; 820 | display_text(); 821 | } 822 | if(tObj==tPatch){ 823 | cp=&vPatch[iObj]; 824 | display_patch(); 825 | } 826 | }//i 827 | } 828 | void MatPlot::display_axes_2d(){ 829 | 830 | char ctmp[100]; 831 | float l,b,w,h;//left,bottom,width,height 832 | float r=0.01; 833 | 834 | l=ca->Position[0]; 835 | b=ca->Position[1]; 836 | w=ca->Position[2]; 837 | h=ca->Position[3]; 838 | 839 | // Viewport Figure (VpF) for drawing axes 840 | glViewport(0,0, (int)(window_w),(int)(window_h)); 841 | glLoadIdentity(); 842 | gluOrtho2D( 0.0, 1.0, 0.0, 1.0 ); 843 | 844 | glDisable(GL_LINE_STIPPLE); 845 | gl2psDisable(GL2PS_LINE_STIPPLE); 846 | 847 | float x_axis_location=1,y_axis_location=1; 848 | if(ca->XAxisLocation=="top" ){x_axis_location=1;}else{x_axis_location=-1;} 849 | if(ca->YAxisLocation=="right"){y_axis_location=1;}else{y_axis_location=-1;} 850 | 851 | int char_w=6,char_h=12; 852 | float offset=0.01; 853 | int num_char=4; 854 | 855 | int gridlinestyle; 856 | 857 | int tickdir=1;//1:in, -1:out 858 | 859 | if(ca->Box){ 860 | // box // 861 | glLineWidth(ca->LineWidth); 862 | gl2psLineWidth(ca->LineWidth); 863 | if(ca->Selected){ 864 | glLineWidth( 2* ca->LineWidth); 865 | gl2psLineWidth(ca->LineWidth); 866 | } 867 | glColor3f(0,0,0); 868 | glBegin(GL_LINE_LOOP); 869 | glVertex2d(l, b); 870 | glVertex2d(l+w,b); 871 | glVertex2d(l+w,b+h); 872 | glVertex2d(l, b+h); 873 | glEnd(); 874 | 875 | // mouse capture // 876 | if(ca->Selected){ 877 | sprintf(ctmp,"Mouse:(%f,%f)",ca->XMouse,ca->YMouse); 878 | ptext( l,b+h+r,ctmp ); 879 | } 880 | 881 | // Grid // 882 | gridlinestyle=3; 883 | if(ca->GridLineStyle=="-" ){gridlinestyle=1;} 884 | if(ca->GridLineStyle=="- -"){gridlinestyle=2;} 885 | if(ca->GridLineStyle==":" ){gridlinestyle=3;} 886 | if(ca->GridLineStyle=="-." ){gridlinestyle=4;} 887 | 888 | if(ca->XGrid){ 889 | glLineWidth(ca->LineWidth); 890 | gl2psLineWidth(ca->LineWidth); 891 | for(int i=0;iXTick.size();++i){ 892 | 893 | //cout <<"grid "<XTick[i]<XTick[i]),b ); 916 | glVertex2d( ctx2(ca->XTick[i]),b+h );//!! TODO 917 | glEnd(); 918 | } 919 | } 920 | if(ca->YGrid){ 921 | for(int i=0;iXTick.size();++i){ 922 | if(gridlinestyle==1){// - 923 | glDisable(GL_LINE_STIPPLE); 924 | gl2psDisable(GL2PS_LINE_STIPPLE); 925 | } 926 | if(gridlinestyle==2){//- - 927 | glEnable(GL_LINE_STIPPLE); 928 | glLineStipple(1, 0xF0F0); 929 | gl2psEnable(GL2PS_LINE_STIPPLE); 930 | } 931 | if(gridlinestyle==3){//: 932 | glEnable(GL_LINE_STIPPLE); 933 | glLineStipple(1, 0xCCCC); 934 | gl2psEnable(GL2PS_LINE_STIPPLE); 935 | } 936 | if(gridlinestyle==4){//-. 937 | glEnable(GL_LINE_STIPPLE); 938 | glLineStipple(1, 0x087F); 939 | gl2psEnable(GL2PS_LINE_STIPPLE); 940 | } 941 | glBegin(GL_LINE_STRIP); 942 | glVertex2d( l, cty2(ca->YTick[i]) ); 943 | glVertex2d( l+w,cty2(ca->YTick[i]) ); 944 | glEnd(); 945 | } 946 | } 947 | 948 | // Ticks // 949 | if(ca->TickDir=="in"){tickdir=1;} 950 | if(ca->TickDir=="out"){tickdir=-1;} 951 | 952 | glDisable(GL_LINE_STIPPLE); 953 | gl2psDisable(GL2PS_LINE_STIPPLE); 954 | //TODO precise adjustment of tick location 955 | // x tick 956 | for(int i=0;iXTick.size();++i){ 957 | glBegin(GL_LINE_STRIP); 958 | glVertex2d( ctx2(ca->XTick[i]),b ); 959 | glVertex2d( ctx2(ca->XTick[i]),b+tickdir*0.01 );//b-0.02*h 960 | glEnd(); 961 | } 962 | // x tick label 963 | if(ca->TickLabel){ 964 | for(int i=0;iXTick.size();++i){ 965 | SPrintx(ctmp,ca->XTick[i]); 966 | //ptext( ctx2(ca->XTick[i])-0.02, b-0.025,ctmp );//b-0.05*h 967 | ptext( ctx2(ca->XTick[i])-(float)num_char*char_w/window_w/2.0, 968 | b-offset-1.0*char_h/window_h,ctmp );//b-0.05*h 969 | } 970 | } 971 | // y tick 972 | for(int i=0;iYTick.size();++i){ 973 | glBegin(GL_LINE_STRIP); 974 | glVertex2d( l, cty2(ca->YTick[i]) ); 975 | glVertex2d( l+tickdir*0.01,cty2(ca->YTick[i]) ); 976 | glEnd(); 977 | } 978 | // y tick label 979 | if(ca->TickLabel){ 980 | for(int i=0;iYTick.size();++i){ 981 | SPrintx(ctmp,ca->YTick[i]); 982 | //ptext( l-0.05,cty2(ca->YTick[i])-0.0,ctmp ); 983 | ptext( l-(float)num_char*char_w/window_w-offset, 984 | cty2(ca->YTick[i])-0.5*char_h/window_h,ctmp ); 985 | } 986 | } 987 | }//Box 988 | 989 | //Title 990 | num_char=ca->Title.length(); 991 | ptext( l+w/2.0-(float)num_char*char_w/window_w/2.0, 992 | b+h+offset, 993 | ca->Title ); 994 | 995 | //XLabel 996 | num_char=ca->XLabel.length(); 997 | ptext( l+w/2.0-(float)num_char*char_w/window_w/2.0, 998 | b-offset-2.0*char_h/window_h, 999 | ca->XLabel ); 1000 | 1001 | //YLabel 1002 | num_char=ca->YLabel.length(); 1003 | ptext( l, b+h+offset, 1004 | ca->YLabel ); 1005 | 1006 | // Viewport Axes (VpA) for drawing lines and surfaces 1007 | glViewport((int)(ca->Position[0]*window_w ), 1008 | (int)(ca->Position[1]*window_h ), 1009 | (int)(ca->Position[2]*window_w ), 1010 | (int)(ca->Position[3]*window_h )); 1011 | glLoadIdentity(); 1012 | gluOrtho2D( 0.0, 1.0, 0.0, 1.0 ); 1013 | } 1014 | void MatPlot::display_axes_3d(){ 1015 | 1016 | char ctmp[100]; 1017 | float l,b,w,h;//left,bottom,width,height 1018 | float r=0.01; 1019 | 1020 | l=ca->Position[0]; 1021 | b=ca->Position[1]; 1022 | w=ca->Position[2]; 1023 | h=ca->Position[3]; 1024 | 1025 | // Viewport Axes 1026 | /* 1027 | glViewport((int)(ca->Position[0]*window_w ), 1028 | (int)(ca->Position[1]*window_h ), 1029 | (int)(ca->Position[2]*window_w ), 1030 | (int)(ca->Position[3]*window_h )); 1031 | */ 1032 | 1033 | glViewport((int)(ca->Viewport3d[0]*window_w ), 1034 | (int)(ca->Viewport3d[1]*window_h ), 1035 | (int)(ca->Viewport3d[2]*window_w ), 1036 | (int)(ca->Viewport3d[3]*window_h )); 1037 | 1038 | glLoadIdentity(); 1039 | //glOrtho(-1.7, 1.7, -1.7, 1.7, -1.5, 3); 1040 | glOrtho(-1.8, 1.8, -1.8, 1.8, -1.5, 3); 1041 | 1042 | gluLookAt(cos(ca->cta*PI/180)*cos(ca->phi*PI/180), 1043 | sin(ca->cta*PI/180)*cos(ca->phi*PI/180), 1044 | sin(ca->phi*PI/180), 1045 | //gluLookAt(CameraPosition[0],CameraPosition[1],CameraPosition[2], 1046 | ca->CameraTarget[0], ca->CameraTarget[1], ca->CameraTarget[2], 1047 | ca->CameraUpVector[0],ca->CameraUpVector[1],ca->CameraUpVector[2]); 1048 | 1049 | /* 1050 | // x,y,z axes for test 1051 | glColor3f(1,0,0); 1052 | glBegin(GL_LINE_STRIP); 1053 | glVertex3d(0,0,0); glVertex3d(1,0,0); 1054 | glEnd(); 1055 | 1056 | glColor3f(0,1,0); 1057 | glBegin(GL_LINE_STRIP); 1058 | glVertex3d(0,0,0); glVertex3d(0,1,0); 1059 | glEnd(); 1060 | 1061 | glColor3f(0,0,1); 1062 | glBegin(GL_LINE_STRIP); 1063 | glVertex3d(0,0,0); glVertex3d(0,0,1); 1064 | glEnd(); 1065 | 1066 | glColor3f(0,0,0); 1067 | glBegin(GL_LINE_LOOP); 1068 | glVertex3d(-1,-1,0); glVertex3d(1,-1,0); glVertex3d(1,1,0); glVertex3d(-1,1,0); 1069 | glEnd(); 1070 | */ 1071 | 1072 | int char_w=6,char_h=12; 1073 | float offset=0.01; 1074 | int num_char=4; 1075 | 1076 | if(ca->Box){ 1077 | // tick 1078 | float cta0; 1079 | float r1=1.05;//tick width 1080 | float r2=1.2; 1081 | float r3=1.4; 1082 | int signx,signy; 1083 | cta0=ca->cta;cta0=fmod(ca->cta,360); 1084 | if(( 0<=cta0)&&(cta0< 90)){signx= 1;signy= 1;} 1085 | if(( 90<=cta0)&&(cta0<190)){signx=-1;signy= 1;} 1086 | if((180<=cta0)&&(cta0<270)){signx=-1;signy=-1;} 1087 | if((270<=cta0)&&(cta0<360)){signx= 1;signy=-1;} 1088 | 1089 | glColor3f(0,0,0); 1090 | 1091 | // axes // 1092 | // x 1093 | glBegin(GL_LINE_STRIP); 1094 | glVertex3d(-1,signy,-1); 1095 | glVertex3d( 1,signy,-1); 1096 | glEnd(); 1097 | // y 1098 | glBegin(GL_LINE_STRIP); 1099 | glVertex3d(signx,-1,-1); 1100 | glVertex3d(signx, 1,-1); 1101 | glEnd(); 1102 | // z 1103 | glBegin(GL_LINE_STRIP); 1104 | glVertex3d(signy,-signx,-1); 1105 | glVertex3d(signy,-signx, 1); 1106 | glEnd(); 1107 | 1108 | // Tick // 1109 | //x 1110 | for(int i=0;iXTick.size();++i){ 1111 | glBegin(GL_LINE_STRIP); 1112 | glVertex3d( ct3x(ca->XTick[i]),signy ,-1 ); 1113 | glVertex3d( ct3x(ca->XTick[i]),signy*r1,-1 ); 1114 | glEnd(); 1115 | } 1116 | // y 1117 | for(int i=0;iYTick.size();++i){ 1118 | glBegin(GL_LINE_STRIP); 1119 | glVertex3d( signx ,ct3y(ca->YTick[i]),-1 ); 1120 | glVertex3d( signx*r1,ct3y(ca->YTick[i]),-1 ); 1121 | glEnd(); 1122 | } 1123 | // z 1124 | for(int i=0;iZTick.size();++i){ 1125 | glBegin(GL_LINE_STRIP); 1126 | glVertex3d( signy ,-signx,ct3z(ca->ZTick[i]) ); 1127 | glVertex3d( signy*r1,-signx,ct3z(ca->ZTick[i]) ); 1128 | glEnd(); 1129 | } 1130 | // Tick Label // 1131 | if(ca->TickLabel){ 1132 | //x 1133 | for(int i=0;iXTick.size();++i){ 1134 | SPrintx(ctmp,ca->XTick[i]); 1135 | ptext3c( ct3x(ca->XTick[i]),signy*r2 ,-1,ctmp ); 1136 | } 1137 | // y 1138 | for(int i=0;iYTick.size();++i){ 1139 | SPrintx(ctmp,ca->YTick[i]); 1140 | ptext3c( signx*r2,ct3y(ca->YTick[i]),-1,ctmp ); 1141 | } 1142 | // z 1143 | for(int i=0;iZTick.size();++i){ 1144 | SPrintx(ctmp,ca->ZTick[i]); 1145 | ptext3c( signy*r2,-signx,ct3z(ca->ZTick[i]),ctmp ); 1146 | } 1147 | } 1148 | // xyz Label // 1149 | ptext3c(0,signy*r3,-1,"x"); 1150 | ptext3c(signx*r3,0,-1,"y"); 1151 | ptext3c(signy*r3,-signx,0,"z"); 1152 | 1153 | }//box 1154 | } 1155 | 1156 | 1157 | /// events (mouse, motion) 1158 | void MatPlot::Axes_mouse(int button, int state, int x, int y ){ 1159 | 1160 | float X,Y; 1161 | double rx,ry,mx,my;//mouse 1162 | X=(float) x /window_w; 1163 | Y=(float)(window_h-y)/window_h; 1164 | float l,b,w,h; 1165 | 1166 | if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) {// Left Click 1167 | xButtonDown=x; 1168 | yButtonDown=y; 1169 | 1170 | //cout <<"window w h"<=0){ 1175 | 1176 | ca=&vAxes[iAxesSelected]; 1177 | if(ca->Visible){ 1178 | l=ca->Position[0]; 1179 | b=ca->Position[1]; 1180 | w=ca->Position[2]; 1181 | h=ca->Position[3]; 1182 | 1183 | if( (l<=X)&&(X<=l+w)&&(b<=Y)&&(Y<=b+h)&&(ca->Mouse==1) ){ 1184 | rx=(X-l)/w; 1185 | ry=(Y-b)/h; 1186 | mx=rx*(ca->XLim[1]-ca->XLim[0])+ca->XLim[0]; 1187 | my=ry*(ca->YLim[1]-ca->YLim[0])+ca->YLim[0]; 1188 | ca->XMouse = mx; 1189 | ca->YMouse = my; 1190 | if(is_debug2){cout <<"mouse capture: "<< mx <<" "<< my <Selected=0; 1201 | if(ca->Visible){ 1202 | l=ca->Position[0]; 1203 | b=ca->Position[1]; 1204 | w=ca->Position[2]; 1205 | h=ca->Position[3]; 1206 | 1207 | if( (l<=X)&&(X<=l+w)&&(b<=Y)&&(Y<=b+h) ){ 1208 | iAxesSelected=i; 1209 | ca->Selected=1; 1210 | if(ca->View==1){//3D 1211 | ctaButtonDown = ca->cta; 1212 | phiButtonDown = ca->phi; 1213 | } 1214 | //cout << "axes "<< i << " is selected "<< vAxes[i].selected() << endl; 1215 | //cout <<"(cta,phi) = "<< ctaButtonDown <<" "<< phiButtonDown << endl; 1216 | } 1217 | } 1218 | } 1219 | 1220 | if(is_debug2){ cout <<"selected axes"<< iAxesSelected<Selected)&&(ca->View==1)){ 1230 | cta = ctaButtonDown - (float) (x - xButtonDown)*1; 1231 | phi = phiButtonDown + (float) (y - yButtonDown)*1; 1232 | if(phi>= 90){phi= 90;} 1233 | if(phi<=-90){phi=-90;} 1234 | if(cta>360){cta+=-360;} 1235 | if(cta< 0){cta+= 360;} 1236 | 1237 | ca->phi = phi; 1238 | ca->cta = cta; 1239 | //cout <<"( phi,cta ) = ( "<< vAxes[i].phi <<","<< vAxes[i].cta <<" )"<Position[0]=(ix+0.13)/n; 1260 | ca->Position[1]=(iy+0.11)/m; 1261 | ca->Position[2]=0.775/n; 1262 | ca->Position[3]=0.815/m; 1263 | 1264 | ca->Viewport3d[0]=1.0*ix/n; 1265 | ca->Viewport3d[1]=1.0*iy/m; 1266 | ca->Viewport3d[2]=1.0/n; 1267 | ca->Viewport3d[3]=1.0/m; 1268 | 1269 | return h; 1270 | } 1271 | 1272 | /// axis 1273 | void MatPlot::axis(double xMin,double xMax,double yMin,double yMax){ 1274 | if(xMin!=xMax){ 1275 | ca->XLim[0]=xMin; 1276 | ca->XLim[1]=xMax; 1277 | ca->XLimMode=1; 1278 | } 1279 | if(yMin!=yMax){ 1280 | ca->YLim[0]=yMin; 1281 | ca->YLim[1]=yMax; 1282 | ca->YLimMode=1; 1283 | } 1284 | ca->View=0;//2D 1285 | } 1286 | void MatPlot::xaxis(double xMin,double xMax){//not in Matlab 1287 | if(xMin!=xMax){ 1288 | ca->XLim[0]=xMin; 1289 | ca->XLim[1]=xMax; 1290 | ca->XLimMode=1; 1291 | } 1292 | //ca->View=0;//2D 1293 | } 1294 | void MatPlot::yaxis(double yMin,double yMax){//not in Matlab 1295 | if(yMin!=yMax){ 1296 | ca->YLim[0]=yMin; 1297 | ca->YLim[1]=yMax; 1298 | ca->YLimMode=1; 1299 | } 1300 | //ca->View=0;//2D 1301 | } 1302 | void MatPlot::axis(double xMin,double xMax,double yMin,double yMax,double zMin,double zMax){ 1303 | ca->XLim[0]=xMin; ca->XLim[1]=xMax; 1304 | ca->YLim[0]=yMin; ca->YLim[1]=yMax; 1305 | ca->ZLim[0]=zMin; ca->ZLim[1]=zMax; 1306 | ca->XLimMode=1; 1307 | ca->YLimMode=1; 1308 | ca->ZLimMode=1; 1309 | ca->View=1;//3D 1310 | } 1311 | void MatPlot::caxis(double cMin,double cMax){ 1312 | ca->CLim[0]=cMin; 1313 | ca->CLim[1]=cMax; 1314 | //ca->XLimMode=1; 1315 | } 1316 | 1317 | void MatPlot::axis(string s){ 1318 | if( s=="on" ){ca->Box=1;} 1319 | if( s=="off" ){ca->Box=0;} 1320 | } 1321 | void MatPlot::axis(int s){ 1322 | if(s){ca->Box=1;} 1323 | else{ ca->Box=0;} 1324 | } 1325 | void MatPlot::grid(string s){ 1326 | if( s=="on" ){ca->XGrid=1; ca->YGrid=1; ca->ZGrid=1;} 1327 | if( s=="off"){ca->XGrid=0; ca->YGrid=0; ca->ZGrid=0;} 1328 | } 1329 | void MatPlot::grid(int s){ 1330 | if(s){ca->XGrid=1; ca->YGrid=1; ca->ZGrid=1;} 1331 | else{ca->XGrid=0; ca->YGrid=0; ca->ZGrid=0;} 1332 | } 1333 | void MatPlot::ticklabel(int s){ 1334 | if(s){ca->TickLabel=1;} 1335 | else{ ca->TickLabel=0;} 1336 | } 1337 | void MatPlot::title(string s){ 1338 | ca->Title=s; 1339 | } 1340 | void MatPlot::xlabel(string s){ 1341 | ca->XLabel=s; 1342 | } 1343 | void MatPlot::ylabel(string s){ 1344 | ca->YLabel=s; 1345 | } 1346 | void MatPlot::mouse_capture(double *xmouse,double *ymouse){ 1347 | ca->Mouse=1; 1348 | *xmouse=ca->XMouse; 1349 | *ymouse=ca->YMouse; 1350 | } 1351 | void MatPlot::mouse_capture(float *xmouse,float *ymouse){ 1352 | ca->Mouse=1; 1353 | *xmouse=(float)ca->XMouse; 1354 | *ymouse=(float)ca->YMouse; 1355 | } 1356 | 1357 | /// Fmax Fmin 1358 | double Fmax(dvec x){ 1359 | double max=-1e99; 1360 | for(int i=0;imax){max=x[i];} 1362 | } 1363 | return max; 1364 | } 1365 | double Fmin(dvec x){ 1366 | double min=1e99; 1367 | for(int i=0;i max){max=x[i][j];} 1377 | } 1378 | } 1379 | return max; 1380 | } 1381 | double Fmin(dmat x){ 1382 | double min=1e99; 1383 | for(int i=0;iPosition[0]; 1394 | b=ca->Position[1]; 1395 | w=ca->Position[2]; 1396 | h=ca->Position[3]; 1397 | 1398 | float cmin,cmax; 1399 | cmin=ca->CLim[0]; 1400 | cmax=ca->CLim[1]; 1401 | 1402 | // TODO use in 3D 1403 | 1404 | int hh=axes(); 1405 | ca->ColorMap=cmap; 1406 | ca->View=2; 1407 | ca->Position[0]=l+w+w*0.01; 1408 | ca->Position[1]=b; 1409 | ca->Position[2]=w*0.05; 1410 | ca->Position[3]=h; 1411 | 1412 | //ca->CLim[0]=cs->CLim[0]; 1413 | //ca->CLim[1]=cs->CLim[1]; 1414 | ca->CLim[0]=cmin; 1415 | ca->CLim[1]=cmax; 1416 | 1417 | return hh; 1418 | } 1419 | void MatPlot::display_axes_colorbar(){ 1420 | char ctmp[100]; 1421 | float l,b,w,h;//left,bottom,width,height 1422 | float r=0.01; 1423 | 1424 | l=ca->Position[0]; 1425 | b=ca->Position[1]; 1426 | w=ca->Position[2]; 1427 | h=ca->Position[3]; 1428 | 1429 | // Viewport Figure (VpF) for drawing axes 1430 | glViewport(0,0, (int)(window_w),(int)(window_h)); 1431 | glLoadIdentity(); 1432 | gluOrtho2D( 0.0, 1.0, 0.0, 1.0 ); 1433 | 1434 | glDisable(GL_LINE_STIPPLE); 1435 | gl2psDisable(GL2PS_LINE_STIPPLE); 1436 | 1437 | //printf("%f %f\n",ca->CLim[0],ca->CLim[1]); 1438 | 1439 | if(ca->Box){ 1440 | // box 1441 | glLineWidth(ca->LineWidth); 1442 | gl2psLineWidth(ca->LineWidth); 1443 | glColor3f(0,0,0); 1444 | glBegin(GL_LINE_LOOP); 1445 | glVertex2d(l, b); 1446 | glVertex2d(l+w,b); 1447 | glVertex2d(l+w,b+h); 1448 | glVertex2d(l, b+h); 1449 | glEnd(); 1450 | 1451 | // z tick 1452 | //return ca->Position[1] +ca->Position[3]*( (y-ca->YLim[0])/(ca->YLim[1]-ca->YLim[0]) ); 1453 | for(int i=0;iCTick.size();++i){ 1454 | glBegin(GL_LINE_STRIP); 1455 | //glVertex2d( l+w, cty2(ca->CTick[i]) ); 1456 | //glVertex2d( l+w+0.01, cty2(ca->CTick[i]) ); 1457 | glVertex2d( l+w, b+h*((ca->CTick[i] - ca->CLim[0])/(ca->CLim[1] - ca->CLim[0])) ); 1458 | glVertex2d( l+w+0.01, b+h*((ca->CTick[i] - ca->CLim[0])/(ca->CLim[1] - ca->CLim[0])) ); 1459 | glEnd(); 1460 | } 1461 | // z tick number 1462 | for(int i=0;iCTick.size();++i){ 1463 | //sprintf(ctmp,"%4.1f",ca->CTick[i]); 1464 | SPrintx(ctmp,ca->CTick[i]); 1465 | ptext( l+w+0.01,b+h*((ca->CTick[i] - ca->CLim[0])/(ca->CLim[1] - ca->CLim[0])),ctmp ); 1466 | } 1467 | }//Box 1468 | 1469 | vector rgb; 1470 | int n=cmap.size(); 1471 | for(int i=0;iColorMap[i]; 1473 | glColor3f(rgb[0],rgb[1],rgb[2]); 1474 | 1475 | glBegin(GL_QUADS); 1476 | glVertex2d(l ,b+h*i/n); 1477 | glVertex2d(l+w,b+h*i/n); 1478 | glVertex2d(l+w,b+h*(i+1)/n); 1479 | glVertex2d(l ,b+h*(i+1)/n); 1480 | glEnd(); 1481 | } 1482 | } 1483 | /// Line // 1484 | /// line 1485 | int MatPlot::line(){ 1486 | int h=iLine*100 + tLine; hObj=h; 1487 | if(is_debug1){printf("mode: %d handle: %4d Line\n",mode,h);} 1488 | 1489 | if(mode==0){ 1490 | ca->add_child(h); 1491 | vLine.push_back(Line(h)); 1492 | //al.Parent=gca(); 1493 | } 1494 | if(mode==1){ 1495 | if(iLineXData.size(); 1505 | if(ca->XScale==0){//linear 1506 | for(int i=0;iXData[i]; 1508 | if(ca->xmin>t){ca->xmin=t;} 1509 | if(ca->xmaxxmax=t;} 1510 | } 1511 | } 1512 | if(ca->XScale==1){//log 1513 | for(int i=0;iXData[i]; 1515 | if((ca->xmin>t)&&(t>0)){ca->xmin=t;} 1516 | if(ca->xmaxxmax=t;} 1517 | } 1518 | } 1519 | double y; 1520 | n=cl->YData.size(); 1521 | for(int i=0;iYData[i]; 1523 | if(ca->ymin>y){ca->ymin=y;} 1524 | if(ca->ymaxymax=y;} 1525 | } 1526 | double z; 1527 | n=cl->ZData.size(); 1528 | for(int i=0;iZData[i]; 1530 | if(ca->zmin>z){ca->zmin=z;} 1531 | if(ca->zmaxzmax=z;} 1532 | } 1533 | } 1534 | int MatPlot::line(dvec x,dvec y){ 1535 | int h=line(); 1536 | if(cfr->Visible){ 1537 | cl->XData=x; 1538 | cl->YData=y; 1539 | line_config(); 1540 | } 1541 | return h; 1542 | } 1543 | int MatPlot::line(dvec x,dvec y,dvec z){ 1544 | int h=line(); 1545 | if(cfr->Visible){ 1546 | cl->XData=x; 1547 | cl->YData=y; 1548 | cl->ZData=z; 1549 | line_config(); 1550 | } 1551 | return h; 1552 | } 1553 | /// create vertex 1554 | int MatPlot::begin(){ return line(); } 1555 | void MatPlot::end(){} 1556 | void MatPlot::vertex(double x,double y){ 1557 | if(cfr->Visible){ 1558 | if(ca->xmin>x){ca->xmin=x;} 1559 | if(ca->xmaxxmax=x;} 1560 | if(ca->ymin>y){ca->ymin=y;} 1561 | if(ca->ymaxymax=y;} 1562 | cl->XData.push_back(x); 1563 | cl->YData.push_back(y); 1564 | } 1565 | } 1566 | /// create plot 1567 | int MatPlot::plot(dvec y){ 1568 | int n=y.size(); 1569 | dvec x; 1570 | x.resize(n); 1571 | for(int i=0;i x,valarray y){ 1578 | dvec xx,yy; 1579 | for(int i=0;iXScale=1; 1586 | int h=line(); 1587 | if(cfr->Visible){ 1588 | cl->XData=x; 1589 | cl->YData=y; 1590 | line_config(); 1591 | } 1592 | return h; 1593 | } 1594 | int MatPlot::semilogy(dvec x,dvec y){ 1595 | ca->YScale=1; 1596 | int h=line(); 1597 | if(cfr->Visible){ 1598 | cl->XData=x; 1599 | cl->YData=y; 1600 | line_config(); 1601 | } 1602 | return h; 1603 | } 1604 | int MatPlot::loglog(dvec x,dvec y){ 1605 | ca->XScale=1; 1606 | ca->YScale=1; 1607 | int h=line(); 1608 | if(cfr->Visible){ 1609 | cl->XData=x; 1610 | cl->YData=y; 1611 | line_config(); 1612 | } 1613 | return h; 1614 | } 1615 | /// create errorbar 1616 | void MatPlot::vertex(double x,double y,double ep,double em){//for errorbar 1617 | if(ca->xmin>x){ca->xmin=x;} 1618 | if(ca->xmaxxmax=x;} 1619 | if(ca->ymin>y+ep){ca->ymin=y+ep;} 1620 | if(ca->ymaxymax=y-em;} 1621 | cl->XData.push_back(x); 1622 | cl->YData.push_back(y); 1623 | cl->YPData.push_back(ep); 1624 | cl->YMData.push_back(em); 1625 | } 1626 | int MatPlot::errorbar(dvec x,dvec y,dvec e){ 1627 | begin(); 1628 | for(int i=0;iErrorbar=1; 1631 | return 0; 1632 | } 1633 | int MatPlot::errorbar(dvec x,dvec y,dvec ep,dvec em){ 1634 | begin(); 1635 | for(int i=0;iErrorbar=1; 1638 | return 0; 1639 | } 1640 | /// create 3D line 1641 | void MatPlot::vertex(double x,double y,double z){ 1642 | if(cfr->Visible){ 1643 | if(ca->xmin>x){ca->xmin=x;} 1644 | if(ca->xmaxxmax=x;} 1645 | if(ca->ymin>y){ca->ymin=y;} 1646 | if(ca->ymaxymax=y;} 1647 | if(ca->zmin>z){ca->zmin=z;} 1648 | if(ca->zmaxzmax=z;} 1649 | cl->XData.push_back(x); 1650 | cl->YData.push_back(y); 1651 | cl->ZData.push_back(z); 1652 | } 1653 | //if(cl->LineStyle){//Line 1654 | //glVertex3d(ct3x(x),ct3y(y),ct3z(z)); 1655 | //} 1656 | } 1657 | int MatPlot::plot3(dvec x,dvec y,dvec z){ 1658 | ca->View=1; 1659 | begin(); 1660 | for(int i=0;iid);} 1670 | 1671 | float xx,yy,zz;// transformed coordination 1672 | float r;//marker size 1673 | float rx,ry; 1674 | vector rgb=ColorSpec2RGB(cl->Color); 1675 | glColor3f(rgb[0],rgb[1],rgb[2]); 1676 | 1677 | glLineWidth(cl->LineWidth); 1678 | glPointSize(cl->LineWidth); 1679 | gl2psLineWidth(cl->LineWidth); 1680 | gl2psPointSize(cl->LineWidth); 1681 | 1682 | if(ca->View==0){// 2D // 1683 | if(cl->LineStyle !="none" ){// Line // 1684 | 1685 | if(cl->LineStyle=="-"){ 1686 | glDisable(GL_LINE_STIPPLE); 1687 | gl2psDisable(GL2PS_LINE_STIPPLE); 1688 | } 1689 | if(cl->LineStyle=="- -"){ 1690 | glEnable(GL_LINE_STIPPLE); 1691 | glLineStipple(1, 0xF0F0); 1692 | gl2psEnable(GL2PS_LINE_STIPPLE); 1693 | } 1694 | if(cl->LineStyle==":"){ 1695 | glEnable(GL_LINE_STIPPLE); 1696 | glLineStipple(1, 0xCCCC); 1697 | gl2psEnable(GL2PS_LINE_STIPPLE); 1698 | } 1699 | if(cl->LineStyle=="-."){ 1700 | glEnable(GL_LINE_STIPPLE); 1701 | glLineStipple(1, 0x087F); 1702 | gl2psEnable(GL2PS_LINE_STIPPLE); 1703 | } 1704 | if(cl->XData.size()){ 1705 | glBegin(GL_LINE_STRIP); 1706 | for(int i=0;iXData.size();++i){ 1707 | //printf("i:%d %f %f\n",i,xx,yy); 1708 | xx=ctx(cl->XData[i]); 1709 | yy=cty(cl->YData[i]); 1710 | glVertex2d(xx,yy); 1711 | } 1712 | glEnd(); 1713 | } 1714 | } 1715 | 1716 | if(cl->Marker != "none"){// Marker // 1717 | 1718 | r=cl->MarkerSize/500.0; 1719 | rx=cl->MarkerSize/window_w; 1720 | ry=cl->MarkerSize/window_h; 1721 | 1722 | glDisable(GL_LINE_STIPPLE); 1723 | gl2psDisable(GL2PS_LINE_STIPPLE); 1724 | 1725 | for(int i=0;iXData.size();++i){ 1726 | xx=ctx(cl->XData[i]); 1727 | yy=cty(cl->YData[i]); 1728 | 1729 | if(cl->Marker=="."){//. 1730 | glPointSize(cl->LineWidth); 1731 | glBegin(GL_POINTS); 1732 | glVertex2d(xx,yy); 1733 | glEnd(); 1734 | } 1735 | if(cl->Marker=="+"){//+ 1736 | glBegin(GL_LINE_STRIP); 1737 | glVertex2d(xx-rx,yy); 1738 | glVertex2d(xx+rx,yy); 1739 | glEnd(); 1740 | glBegin(GL_LINE_STRIP); 1741 | glVertex2d(xx,yy-ry); 1742 | glVertex2d(xx,yy+ry); 1743 | glEnd(); 1744 | } 1745 | if(cl->Marker=="x"){//x 1746 | glBegin(GL_LINE_STRIP); 1747 | glVertex2d(xx-rx,yy-ry); 1748 | glVertex2d(xx+rx,yy+ry); 1749 | glEnd(); 1750 | glBegin(GL_LINE_STRIP); 1751 | glVertex2d(xx+rx,yy-ry); 1752 | glVertex2d(xx-rx,yy+ry); 1753 | glEnd(); 1754 | } 1755 | if(cl->Marker=="d"){//d diamond 1756 | glBegin(GL_LINE_LOOP); 1757 | glVertex2d(xx, yy+ry); 1758 | glVertex2d(xx+rx,yy); 1759 | glVertex2d(xx, yy-ry); 1760 | glVertex2d(xx-rx,yy); 1761 | glEnd(); 1762 | } 1763 | if(cl->Marker=="^"){//^ 1764 | glBegin(GL_LINE_LOOP); 1765 | glVertex2d(xx, yy+ry); 1766 | glVertex2d(xx+rx,yy-ry); 1767 | glVertex2d(xx-rx,yy-ry); 1768 | glEnd(); 1769 | } 1770 | if(cl->Marker=="v"){//v 1771 | glBegin(GL_LINE_LOOP); 1772 | glVertex2d(xx, yy-ry); 1773 | glVertex2d(xx+rx,yy+ry); 1774 | glVertex2d(xx-rx,yy+ry); 1775 | glEnd(); 1776 | } 1777 | if(cl->Marker=="o"){//o 1778 | glBegin(GL_LINE_LOOP); 1779 | for(int i=0;i<20;i++){ 1780 | glVertex2d(xx+rx*cos(2*PI*(double)i/(double)(20)), 1781 | yy+ry*sin(2*PI*(double)i/(double)(20))); 1782 | } 1783 | glEnd(); 1784 | } 1785 | if(cl->Marker=="*"){//* 1786 | glBegin(GL_LINE_STRIP); 1787 | glVertex2d(xx-rx,yy); 1788 | glVertex2d(xx+rx,yy); 1789 | glEnd(); 1790 | glBegin(GL_LINE_STRIP); 1791 | glVertex2d(xx,yy-ry); 1792 | glVertex2d(xx,yy+ry); 1793 | glEnd(); 1794 | glBegin(GL_LINE_STRIP); 1795 | glVertex2d(xx-rx,yy-ry); 1796 | glVertex2d(xx+rx,yy+ry); 1797 | glEnd(); 1798 | glBegin(GL_LINE_STRIP); 1799 | glVertex2d(xx+rx,yy-ry); 1800 | glVertex2d(xx-rx,yy+ry); 1801 | glEnd(); 1802 | 1803 | } 1804 | if(cl->Marker=="s"){//s :squire 1805 | glBegin(GL_LINE_LOOP); 1806 | glVertex2d(xx-rx,yy-ry); 1807 | glVertex2d(xx-rx,yy+ry); 1808 | glVertex2d(xx+rx,yy+ry); 1809 | glVertex2d(xx+rx,yy-ry); 1810 | glEnd(); 1811 | } 1812 | if(cl->Marker=="|"){// | 1813 | glBegin(GL_LINE_STRIP); 1814 | glVertex2d(xx,yy-2*ry); 1815 | glVertex2d(xx,yy+2*ry); 1816 | glEnd(); 1817 | } 1818 | //TODO use switch 1819 | }//i 1820 | }// Marker 1821 | 1822 | if(cl->Errorbar){// Errorbar // 1823 | float xx,yy,yyp,yym;// transformed coordination 1824 | 1825 | glDisable(GL_LINE_STIPPLE); 1826 | gl2psDisable(GL2PS_LINE_STIPPLE); 1827 | //r=cl->MarkerSize/500; 1828 | 1829 | rx=cl->MarkerSize/window_w; 1830 | ry=cl->MarkerSize/window_h; 1831 | 1832 | 1833 | for(int i=0;iXData.size();++i){ 1834 | xx=ctx(cl->XData[i]); 1835 | yy=cty(cl->YData[i]); 1836 | yyp=cty(cl->YData[i] + cl->YPData[i]); 1837 | yym=cty(cl->YData[i] - cl->YMData[i]); 1838 | 1839 | glBegin(GL_LINE_STRIP); 1840 | glVertex2d(xx,yyp); 1841 | glVertex2d(xx,yym); 1842 | glEnd(); 1843 | glBegin(GL_LINE_STRIP); 1844 | glVertex2d(xx-rx,yy); 1845 | glVertex2d(xx+rx,yy); 1846 | glEnd(); 1847 | glBegin(GL_LINE_STRIP); 1848 | glVertex2d(xx-rx,yyp); 1849 | glVertex2d(xx+rx,yyp); 1850 | glEnd(); 1851 | glBegin(GL_LINE_STRIP); 1852 | glVertex2d(xx-rx,yym); 1853 | glVertex2d(xx+rx,yym); 1854 | glEnd(); 1855 | }//i 1856 | }//Errorbar 1857 | //TODO:selection of error bar type 1858 | }//2D 1859 | 1860 | // 3D //!!! 1861 | if(ca->View==1){ 1862 | 1863 | if(cl->LineStyle !="none" ){// Line // 1864 | 1865 | if(cl->LineStyle=="-"){ 1866 | glDisable(GL_LINE_STIPPLE); 1867 | gl2psDisable(GL2PS_LINE_STIPPLE); 1868 | } 1869 | if(cl->LineStyle=="- -"){ 1870 | glEnable(GL_LINE_STIPPLE); 1871 | glLineStipple(1, 0xF0F0); 1872 | gl2psEnable(GL2PS_LINE_STIPPLE); 1873 | } 1874 | if(cl->LineStyle==":"){ 1875 | glEnable(GL_LINE_STIPPLE); 1876 | glLineStipple(1, 0xCCCC); 1877 | gl2psEnable(GL2PS_LINE_STIPPLE); 1878 | } 1879 | if(cl->LineStyle=="-."){ 1880 | glEnable(GL_LINE_STIPPLE); 1881 | glLineStipple(1, 0x087F); 1882 | gl2psEnable(GL2PS_LINE_STIPPLE); 1883 | } 1884 | if(cl->XData.size()){ 1885 | glBegin(GL_LINE_STRIP); 1886 | for(int i=0;iXData.size();++i){ 1887 | //printf("i:%d %f %f\n",i,xx,yy); 1888 | //xx=ctx(cl->XData[i]); 1889 | //yy=cty(cl->YData[i]); 1890 | //glVertex2d(xx,yy); 1891 | glVertex3d(ct3x(cl->XData[i]), 1892 | ct3y(cl->YData[i]), 1893 | ct3z(cl->ZData[i])); 1894 | } 1895 | glEnd(); 1896 | } 1897 | } 1898 | 1899 | //glBegin(GL_LINE_STRIP); 1900 | //for(int i=0;iXData.size();++i){ 1901 | //glVertex3d(ct3x(cl->XData[i]), 1902 | //ct3y(cl->YData[i]), 1903 | // ct3z(cl->ZData[i])); 1904 | //} 1905 | //glEnd(); 1906 | if(cl->Marker != "none"){// Marker // 1907 | 1908 | r =cl->MarkerSize/500.0; 1909 | rx=cl->MarkerSize/window_w; 1910 | ry=cl->MarkerSize/window_h; 1911 | 1912 | glDisable(GL_LINE_STIPPLE); 1913 | gl2psDisable(GL2PS_LINE_STIPPLE); 1914 | 1915 | for(int i=0;iXData.size();++i){ 1916 | xx=ct3x(cl->XData[i]); 1917 | yy=ct3y(cl->YData[i]); 1918 | zz=ct3z(cl->ZData[i]); 1919 | 1920 | if(cl->Marker=="."){//. 1921 | glPointSize(cl->LineWidth); 1922 | glBegin(GL_POINTS); 1923 | glVertex3d(xx,yy,zz); 1924 | glEnd(); 1925 | } 1926 | if(cl->Marker=="+"){//+ 1927 | glBegin(GL_LINE_STRIP); 1928 | glVertex3d(xx-rx,yy,zz); 1929 | glVertex3d(xx+rx,yy,zz); 1930 | glEnd(); 1931 | glBegin(GL_LINE_STRIP); 1932 | glVertex3d(xx,yy-ry,zz); 1933 | glVertex3d(xx,yy+ry,zz); 1934 | glEnd(); 1935 | } 1936 | } 1937 | }//marker 1938 | }//3D 1939 | } 1940 | 1941 | /// Surface // 1942 | int MatPlot::surface(){ 1943 | int h=iSurface*100 + tSurface; hObj=h; 1944 | if(is_debug1){printf("mode: %d handle: %4d Surface\n",mode,h);} 1945 | 1946 | if(mode==0){ 1947 | ca->add_child(h); 1948 | vSurface.push_back(Surface(h)); 1949 | //as.Parent=gca(); 1950 | } 1951 | if(mode==1){ 1952 | 1953 | } 1954 | if(iSurfaceZData.size(); 1964 | if(nzi){nzj=cs->ZData[0].size(); } 1965 | 1966 | int nci,ncj; 1967 | nci=cs->CDataIndex.size(); 1968 | if(nci){ncj=cs->CDataIndex[0].size();} 1969 | 1970 | // generate x and y data 1971 | int nx=0,ny=0; 1972 | if(nzi){ny=nzi; nx=nzj;} 1973 | if(nci){ny=nci; nx=ncj;} 1974 | 1975 | //printf("%s %s:%d: %d %d %d %d \n", __func__, __FILE__, __LINE__,nzi,nci,nx,ny); 1976 | if(cs->XData.size()==0){ 1977 | cs->XData.resize(1); 1978 | cs->XData[0]=linspace(1.0,(double)nx,nx); 1979 | } 1980 | if(cs->YData.size()==0){ 1981 | cs->YData.resize(1); 1982 | cs->YData[0]=linspace(1.0,(double)ny,ny); 1983 | } 1984 | 1985 | // config data range 1986 | ca->xmax=fmax(Fmax(cs->XData),ca->xmax); 1987 | ca->xmin=fmin(Fmin(cs->XData),ca->xmin); 1988 | ca->ymax=fmax(Fmax(cs->YData),ca->ymax); 1989 | ca->ymin=fmin(Fmin(cs->YData),ca->ymin); 1990 | ca->zmax=fmax(Fmax(cs->ZData),ca->zmax); 1991 | ca->zmin=fmin(Fmin(cs->ZData),ca->zmin); 1992 | 1993 | // set CLim 1994 | //note: first called surface effects CLim 1995 | cs->CLim[0]=Fmin(cs->CDataIndex); 1996 | cs->CLim[1]=Fmax(cs->CDataIndex); 1997 | //if(ca->CLim[0]==ca->CLim[1]){ 1998 | if(ca->CLimMode=="auto"){ 1999 | ca->CLim[0]=cs->CLim[0]; 2000 | ca->CLim[1]=cs->CLim[1]; 2001 | //if(ca->CLim[0]==ca->CLim[1]){ ca->CLim[0]=0; ca->CLim[1]=1; } 2002 | } 2003 | 2004 | // CData TODO !! 2005 | if( (cs->CData.size()==0) && (cs->CDataIndex.size()) ){ 2006 | vector rgb; 2007 | //tcmat cdata(ny,nx); 2008 | tcmat cdata(ny); 2009 | 2010 | for(int i=0;iCDataIndex[i][j],ca->CLim[0],ca->CLim[1]); 2014 | cdata[i][j]=rgb; 2015 | } 2016 | } 2017 | cs->CData=cdata; 2018 | } 2019 | 2020 | // contour plot 2021 | if(cs->V.size()==0){ 2022 | if(cs->NContour<1){ 2023 | cs->NContour=10; 2024 | } 2025 | cs->V=linspace(Fmin(cs->ZData),Fmax(cs->ZData),cs->NContour); 2026 | } 2027 | } 2028 | 2029 | /// create surface 2030 | int MatPlot::surface(dmat Z){ 2031 | int h=surface(); 2032 | ca->View=1; 2033 | cs->type=1; 2034 | cs->ZData=Z; 2035 | cs->CDataIndex=Z; 2036 | cs->CData.clear(); 2037 | surface_config(); 2038 | return h; 2039 | } 2040 | int MatPlot::surface(dvec x,dvec y,dmat Z){ 2041 | int h=surface(); 2042 | ca->View=1; 2043 | cs->type=1; 2044 | cs->XData.resize(1); cs->XData[0]=x; 2045 | cs->YData.resize(1); cs->YData[0]=y; 2046 | cs->ZData=Z; 2047 | cs->CDataIndex=Z; 2048 | cs->CData.clear(); 2049 | surface_config(); 2050 | return h; 2051 | } 2052 | int MatPlot::surface(dmat X,dmat Y,dmat Z){ 2053 | int h=surface(); 2054 | ca->View=1; 2055 | cs->type=1; 2056 | cs->XData=X; 2057 | cs->YData=Y; 2058 | cs->ZData=Z; 2059 | cs->CDataIndex=Z; 2060 | cs->CData.clear(); 2061 | surface_config(); 2062 | return h; 2063 | } 2064 | int MatPlot::surface(dmat Z,dmat C){ 2065 | int h=surface(); 2066 | ca->View=1; 2067 | cs->type=1; 2068 | cs->ZData=Z; 2069 | cs->CDataIndex=C; 2070 | cs->CData.clear(); 2071 | surface_config(); 2072 | return h; 2073 | } 2074 | int MatPlot::surface(dvec x,dvec y,dmat Z,dmat C){ 2075 | int h=surface(); 2076 | ca->View=1; 2077 | cs->type=1; 2078 | cs->XData.resize(1); cs->XData[0]=x; 2079 | cs->YData.resize(1); cs->YData[0]=y; 2080 | cs->ZData=Z; 2081 | cs->CDataIndex=C; 2082 | cs->CData.clear(); 2083 | surface_config(); 2084 | return h; 2085 | } 2086 | int MatPlot::surface(dmat X,dmat Y,dmat Z,dmat C){ 2087 | int h=surface(); 2088 | ca->View=1; 2089 | cs->type=1; 2090 | cs->XData=X; 2091 | cs->YData=Y; 2092 | cs->ZData=Z; 2093 | cs->CDataIndex=C; 2094 | cs->CData.clear(); 2095 | 2096 | surface_config(); 2097 | return h; 2098 | } 2099 | 2100 | int MatPlot::surface(dmat Z,tcmat C){ 2101 | int h=surface(); 2102 | ca->View=1; 2103 | cs->type=1; 2104 | cs->ZData=Z; 2105 | cs->CDataIndex.clear(); 2106 | cs->CData=C; 2107 | surface_config(); 2108 | return h; 2109 | } 2110 | int MatPlot::surface(dvec x,dvec y,dmat Z,tcmat C){ 2111 | int h=surface(); 2112 | ca->View=1; 2113 | cs->type=1; 2114 | cs->XData.resize(1); cs->XData[0]=x; 2115 | cs->YData.resize(1); cs->YData[0]=y; 2116 | cs->ZData=Z; 2117 | cs->CDataIndex.clear(); 2118 | cs->CData=C; 2119 | surface_config(); 2120 | return h; 2121 | } 2122 | int MatPlot::surface(dmat X,dmat Y,dmat Z,tcmat C){ 2123 | int h=surface(); 2124 | ca->View=1; 2125 | cs->type=1; 2126 | cs->XData=X; 2127 | cs->YData=Y; 2128 | cs->ZData=Z; 2129 | cs->CDataIndex.clear(); 2130 | cs->CData=C; 2131 | 2132 | surface_config(); 2133 | return h; 2134 | } 2135 | /// create surf 2136 | int MatPlot::surf(dvec x, dvec y, dmat Z){ 2137 | int h=surface(); 2138 | ca->View=1; 2139 | cs->type=1; 2140 | cs->XData.resize(1); cs->XData[0]=x; 2141 | cs->YData.resize(1); cs->YData[0]=y; 2142 | cs->ZData=Z; 2143 | cs->CDataIndex=Z; 2144 | cs->CData.clear(); 2145 | cs->EdgeColor="k"; 2146 | cs->FaceColor="flat"; 2147 | surface_config(); 2148 | return h; 2149 | } 2150 | /// create pcolor 2151 | int MatPlot::pcolor(dmat C){ 2152 | int h; h=surface(); 2153 | cs->type=0; 2154 | cs->XData.clear(); 2155 | cs->YData.clear(); 2156 | cs->ZData.clear(); 2157 | cs->CDataIndex=C; 2158 | cs->CData.clear(); 2159 | surface_config(); 2160 | return h; 2161 | } 2162 | int MatPlot::pcolor(dvec x, dvec y, dmat C){ 2163 | int h=surface(); 2164 | cs->type=0; 2165 | cs->XData.resize(1); cs->XData[0]=x; 2166 | cs->YData.resize(1); cs->YData[0]=y; 2167 | cs->ZData.clear(); 2168 | cs->CDataIndex=C; 2169 | cs->CData.clear(); 2170 | surface_config(); 2171 | return h; 2172 | } 2173 | 2174 | int MatPlot::pcolor(dmat X,dmat Y,dmat C){ 2175 | int h=surface(); 2176 | cs->type=0; 2177 | cs->XData=X; 2178 | cs->YData=Y; 2179 | cs->ZData.clear(); 2180 | cs->CDataIndex=C; 2181 | cs->CData.clear(); 2182 | surface_config(); 2183 | return h; 2184 | } 2185 | int MatPlot::pcolor(tcmat C){ 2186 | int h=surface(); 2187 | cs->type=0; 2188 | cs->XData.clear(); 2189 | cs->YData.clear(); 2190 | cs->ZData.clear(); 2191 | cs->CDataIndex.clear(); 2192 | cs->CData=C; 2193 | surface_config(); 2194 | return h; 2195 | } 2196 | int MatPlot::pcolor(dvec x, dvec y, tcmat C){ 2197 | int h=surface(); 2198 | cs->type=0; 2199 | cs->XData.resize(1); cs->XData[0]=x; 2200 | cs->YData.resize(1); cs->YData[0]=y; 2201 | cs->ZData.clear(); 2202 | cs->CDataIndex.clear(); 2203 | cs->CData=C; 2204 | surface_config(); 2205 | return h; 2206 | } 2207 | int MatPlot::pcolor(dmat X,dmat Y,tcmat C){ 2208 | int h=surface(); 2209 | cs->type=0; 2210 | cs->XData=X; 2211 | cs->YData=Y; 2212 | cs->ZData.clear(); 2213 | cs->CDataIndex.clear(); 2214 | cs->CData=C; 2215 | surface_config(); 2216 | return h; 2217 | } 2218 | 2219 | /// create mesh 2220 | int MatPlot::mesh(dvec x, dvec y, dmat Z){ 2221 | int h=surface(); 2222 | ca->View=1; 2223 | cs->type=1; 2224 | cs->XData.resize(1); cs->XData[0]=x; 2225 | cs->YData.resize(1); cs->YData[0]=y; 2226 | cs->ZData=Z; 2227 | cs->CDataIndex=Z; 2228 | cs->CData.clear(); 2229 | cs->EdgeColor="k"; 2230 | cs->FaceColor="w"; 2231 | surface_config(); 2232 | return h; 2233 | } 2234 | 2235 | /// create contour 2236 | int MatPlot::contour(dmat Z){ 2237 | int h=surface(); 2238 | cs->type=3; 2239 | cs->XData.clear(); 2240 | cs->YData.clear(); 2241 | cs->ZData=Z; 2242 | cs->NContour=0; 2243 | cs->V.clear(); 2244 | 2245 | surface_config(); 2246 | return h; 2247 | } 2248 | int MatPlot::contour(dmat Z,int n){ 2249 | int h=surface(); 2250 | cs->type=3; 2251 | cs->XData.clear(); 2252 | cs->YData.clear(); 2253 | cs->ZData=Z; 2254 | cs->NContour=n; 2255 | cs->V.clear(); 2256 | 2257 | surface_config(); 2258 | return h; 2259 | } 2260 | int MatPlot::contour(dmat Z, dvec v){ 2261 | int h=surface(); 2262 | cs->type=3; 2263 | cs->XData.clear(); 2264 | cs->YData.clear(); 2265 | cs->ZData=Z; 2266 | cs->NContour=v.size(); 2267 | cs->V=v; 2268 | 2269 | surface_config(); 2270 | return h; 2271 | } 2272 | int MatPlot::contour(dvec x,dvec y,dmat Z){ 2273 | int h=surface(); 2274 | cs->type=3; 2275 | cs->XData.resize(1); cs->XData[0]=x; 2276 | cs->YData.resize(1); cs->YData[0]=y; 2277 | cs->ZData=Z; 2278 | cs->NContour=0; 2279 | cs->V.clear(); 2280 | 2281 | surface_config(); 2282 | return h; 2283 | } 2284 | int MatPlot::contour(dvec x,dvec y,dmat Z,int n){ 2285 | int h=surface(); 2286 | cs->type=3; 2287 | cs->XData.resize(1); cs->XData[0]=x; 2288 | cs->YData.resize(1); cs->YData[0]=y; 2289 | cs->ZData=Z; 2290 | cs->NContour=n; 2291 | cs->V.clear(); 2292 | 2293 | surface_config(); 2294 | return h; 2295 | } 2296 | int MatPlot::contour(dvec x, dvec y, dmat Z, dvec v){ 2297 | int h=surface(); 2298 | cs->type=3; 2299 | cs->XData.resize(1); cs->XData[0]=x; 2300 | cs->YData.resize(1); cs->YData[0]=y; 2301 | cs->ZData=Z; 2302 | cs->NContour=v.size(); 2303 | cs->V=v; 2304 | 2305 | surface_config(); 2306 | return h; 2307 | } 2308 | /// display surface 2d 2309 | void MatPlot::display_surface(){ 2310 | if(is_debug1){printf("mode: %d handle: %4d Surface type %d, Z.size %d\n", 2311 | mode,cs->id,cs->type,cs->ZData.size());} 2312 | 2313 | //if(cs->type==0){ display_pcolor(); } 2314 | if(cs->type==0){ display_surface_2d(); } 2315 | if(cs->type==1){ display_surface_3d(); } 2316 | if(cs->type==2){ display_surface_3d(); } 2317 | if(cs->type==3){ display_contour(); } 2318 | 2319 | } 2320 | void MatPlot::display_surface_2d(){ 2321 | int nxi,nxj,nyi,nyj,nzi,nzj; 2322 | vector rgb; 2323 | nzi=cs->ZData.size(); if(nzi){nzj=cs->ZData[0].size();} 2324 | nxi=cs->XData.size(); if(nxi){nxj=cs->XData[0].size();} 2325 | nyi=cs->YData.size(); if(nyi){nyj=cs->YData[0].size();} 2326 | 2327 | //printf("%s %s:%d (%d %d) (%d %d) (%d %d) \n", __func__, __FILE__, __LINE__,nxi,nxj,nyi,nyj,nzi,nzj); 2328 | 2329 | // (Z) // do not use 2330 | if(nxi==0){ 2331 | printf("%s %s:%d\n", __func__, __FILE__, __LINE__); 2332 | // Edge 2333 | if(cs->EdgeColor != "none"){ 2334 | glLineWidth(cs->LineWidth); 2335 | rgb=ColorSpec2RGB(cs->EdgeColor); 2336 | glColor3d(rgb[0],rgb[1],rgb[2]); 2337 | 2338 | for(int i=0;iXLim[0]+(ca->XLim[1]-ca->XLim[0])*(float)(j)/(nzj-1)), 2342 | cty(ca->YLim[0]+(ca->YLim[1]-ca->YLim[0])*(float)(i)/(nzi-1))); 2343 | } 2344 | glEnd(); 2345 | } 2346 | /* 2347 | for(int j=0;jXLim[0]+(ca->XLim[1]-ca->XLim[0])*(float)(j)/(nzj-1)), 2351 | cty(ca->YLim[0]+(ca->YLim[1]-ca->YLim[0])*(float)(i)/(nzi-1))); 2352 | } 2353 | glEnd(); 2354 | } 2355 | */ 2356 | } 2357 | // Face 2358 | if(cs->FaceColor != "none"){ 2359 | for(int i=0;iFaceColor); 2363 | if(cs->FaceColor=="flat"){ rgb=cs->CData[i][j]; } 2364 | glColor3f(rgb[0],rgb[1],rgb[2]); 2365 | 2366 | glBegin(GL_QUADS); 2367 | glVertex2d(ctx(ca->XLim[0]+(ca->XLim[1]-ca->XLim[0])*(float)(j )/(float)(nzj-1)), 2368 | cty(ca->YLim[0]+(ca->YLim[1]-ca->YLim[0])*(float)(i )/(float)(nzi-1))); 2369 | glVertex2d(ctx(ca->XLim[0]+(ca->XLim[1]-ca->XLim[0])*(float)(j+1)/(float)(nzj-1)), 2370 | cty(ca->YLim[0]+(ca->YLim[1]-ca->YLim[0])*(float)(i )/(float)(nzi-1))); 2371 | glVertex2d(ctx(ca->XLim[0]+(ca->XLim[1]-ca->XLim[0])*(float)(j+1)/(float)(nzj-1)), 2372 | cty(ca->YLim[0]+(ca->YLim[1]-ca->YLim[0])*(float)(i+1)/(float)(nzi-1))); 2373 | glVertex2d(ctx(ca->XLim[0]+(ca->XLim[1]-ca->XLim[0])*(float)(j )/(float)(nzj-1)), 2374 | cty(ca->YLim[0]+(ca->YLim[1]-ca->YLim[0])*(float)(i+1)/(float)(nzi-1))); 2375 | glEnd(); 2376 | } 2377 | } 2378 | }//Face 2379 | } 2380 | 2381 | // (x,y,Z) // 2382 | if(nxi==1){ 2383 | //printf("%s %s:%d %d %d \n", __func__, __FILE__, __LINE__,nxj,nyj); 2384 | // Edge 2385 | if(cs->EdgeColor != "none"){ 2386 | 2387 | glLineWidth(cs->LineWidth); 2388 | rgb=ColorSpec2RGB(cs->EdgeColor); 2389 | glColor3d(rgb[0],rgb[1],rgb[2]); 2390 | 2391 | for(int i=0;iXData[0][j]),cty(cs->YData[0][i])); 2395 | //} 2396 | glVertex2d( ctx(cs->XData[0][ 0]),cty(cs->YData[0][i]) ); 2397 | glVertex2d( ctx(cs->XData[0][nxj-1]),cty(cs->YData[0][i]) ); 2398 | 2399 | glEnd(); 2400 | } 2401 | for(int j=0;jXData[0][j]), cty(cs->YData[0][i])); 2405 | //} 2406 | glVertex2d(ctx(cs->XData[0][j]),cty(cs->YData[0][0])); 2407 | glVertex2d(ctx(cs->XData[0][j]),cty(cs->YData[0][nyj-1])); 2408 | glEnd(); 2409 | } 2410 | } 2411 | // Face 2412 | if(cs->FaceColor != "none"){ 2413 | for(int i=0;iFaceColor); 2417 | if(cs->FaceColor=="flat"){ rgb=cs->CData[i][j]; } 2418 | glColor3f(rgb[0],rgb[1],rgb[2]); 2419 | 2420 | glBegin(GL_QUADS); 2421 | glVertex2d(ctx(cs->XData[0][j]), 2422 | cty(cs->YData[0][i]) ); 2423 | glVertex2d(ctx(cs->XData[0][j]), 2424 | cty(cs->YData[0][i+1]) ); 2425 | glVertex2d(ctx(cs->XData[0][j+1]), 2426 | cty(cs->YData[0][i+1]) ); 2427 | glVertex2d(ctx(cs->XData[0][j+1]), 2428 | cty(cs->YData[0][i]) ); 2429 | glEnd(); 2430 | } 2431 | } 2432 | } 2433 | }//nxi==1 2434 | 2435 | // (X,Y,C) // 2436 | if(nxi>1){ 2437 | // Edge 2438 | //printf("%s %s:%d\n", __func__, __FILE__, __LINE__); 2439 | if(cs->EdgeColor != "none"){ 2440 | glLineWidth(cs->LineWidth); 2441 | rgb=ColorSpec2RGB(cs->EdgeColor); 2442 | glColor3d(rgb[0],rgb[1],rgb[2]); 2443 | 2444 | for(int i=0;iXData[i][j]), 2448 | cty(cs->YData[i][j])); 2449 | } 2450 | glEnd(); 2451 | } 2452 | for(int j=0;jXData[i][j]), 2456 | cty(cs->YData[i][j])); 2457 | } 2458 | glEnd(); 2459 | } 2460 | } 2461 | // Face 2462 | if(cs->FaceColor != "none"){ 2463 | for(int i=0;iFaceColor); 2467 | if(cs->FaceColor=="flat"){rgb=cs->CData[i][j]; } 2468 | glColor3f(rgb[0],rgb[1],rgb[2]); 2469 | 2470 | glBegin(GL_QUADS); 2471 | glVertex2d(ctx(cs->XData[i ][j]), 2472 | cty(cs->YData[i ][j]) ); 2473 | glVertex2d(ctx(cs->XData[i ][j+1]), 2474 | cty(cs->YData[i ][j+1]) ); 2475 | glVertex2d(ctx(cs->XData[i+1][j+1]), 2476 | cty(cs->YData[i+1][j+1]) ); 2477 | glVertex2d(ctx(cs->XData[i+1][j]), 2478 | cty(cs->YData[i+1][j]) ); 2479 | glEnd(); 2480 | } 2481 | } 2482 | } 2483 | } 2484 | } 2485 | 2486 | /// display surface 3d 2487 | void MatPlot::display_surface_3d(){ 2488 | vector rgb; 2489 | int ny=cs->ZData.size(); 2490 | int nx=cs->ZData[0].size(); 2491 | 2492 | if(cs->XData.size()==1){ 2493 | //(x,y,Z); 2494 | //Face 2495 | if(cs->FaceColor != "none"){ 2496 | for(int i=0;iFaceColor); 2499 | glColor3d(rgb[0], rgb[1], rgb[2]); 2500 | if(cs->FaceColor=="flat"){ 2501 | rgb=cs->CData[i][j]; 2502 | glColor3f(rgb[0],rgb[1],rgb[2]); 2503 | } 2504 | glBegin(GL_TRIANGLES); 2505 | glVertex3d(ct3x(cs->XData[0][j]), 2506 | ct3y(cs->YData[0][i]), 2507 | ct3z(cs->ZData[i][j]) ); 2508 | glVertex3d(ct3x(cs->XData[0][j]), 2509 | ct3y(cs->YData[0][i+1]), 2510 | ct3z(cs->ZData[i+1][j]) ); 2511 | glVertex3d(ct3x(cs->XData[0][j+1]), 2512 | ct3y(cs->YData[0][i+1]), 2513 | ct3z(cs->ZData[i+1][j+1]) ); 2514 | glEnd(); 2515 | glBegin(GL_TRIANGLES); 2516 | glVertex3d(ct3x(cs->XData[0][j]), 2517 | ct3y(cs->YData[0][i]), 2518 | ct3z(cs->ZData[i][j]) ); 2519 | glVertex3d(ct3x(cs->XData[0][j+1]), 2520 | ct3y(cs->YData[0][i]), 2521 | ct3z(cs->ZData[i][j+1]) ); 2522 | glVertex3d(ct3x(cs->XData[0][j+1]), 2523 | ct3y(cs->YData[0][i+1]), 2524 | ct3z(cs->ZData[i+1][j+1]) ); 2525 | glEnd(); 2526 | } 2527 | } 2528 | } 2529 | // Edge 2530 | if(cs->EdgeColor != "none"){ 2531 | glLineWidth(cs->LineWidth); 2532 | rgb=ColorSpec2RGB(cs->EdgeColor); 2533 | glColor3d(rgb[0], rgb[1], rgb[2]); 2534 | 2535 | for(int i=0;iXData[0][j]), 2539 | ct3y(cs->YData[0][i]), 2540 | ct3z(cs->ZData[i][j]) ); 2541 | } 2542 | glEnd(); 2543 | } 2544 | for(int j=0;jXData[0][j]), 2548 | ct3y(cs->YData[0][i]), 2549 | ct3z(cs->ZData[i][j]) ); 2550 | } 2551 | glEnd(); 2552 | } 2553 | } 2554 | }// (x,y,Z) 2555 | // (X,Y,Z) // 2556 | if(cs->XData.size()>1){ 2557 | 2558 | //Face 2559 | if(cs->FaceColor != "none"){ 2560 | for(int i=0;iFaceColor); 2563 | glColor3d(rgb[0], rgb[1], rgb[2]); 2564 | if(cs->FaceColor=="flat"){ 2565 | rgb=cs->CData[i][j]; 2566 | glColor3f(rgb[0],rgb[1],rgb[2]); 2567 | } 2568 | 2569 | glBegin(GL_TRIANGLES); 2570 | glVertex3d(ct3x(cs->XData[i][j]), 2571 | ct3y(cs->YData[i][j]), 2572 | ct3z(cs->ZData[i][j]) ); 2573 | glVertex3d(ct3x(cs->XData[i][j+1]), 2574 | ct3y(cs->YData[i][j+1]), 2575 | ct3z(cs->ZData[i][j+1]) ); 2576 | glVertex3d(ct3x(cs->XData[i+1][j+1]), 2577 | ct3y(cs->YData[i+1][j+1]), 2578 | ct3z(cs->ZData[i+1][j+1]) ); 2579 | glEnd(); 2580 | 2581 | glBegin(GL_TRIANGLES); 2582 | glVertex3d(ct3x(cs->XData[i][j]), 2583 | ct3y(cs->YData[i][j]), 2584 | ct3z(cs->ZData[i][j]) ); 2585 | glVertex3d(ct3x(cs->XData[i+1][j]), 2586 | ct3y(cs->YData[i+1][j]), 2587 | ct3z(cs->ZData[i+1][j]) ); 2588 | glVertex3d(ct3x(cs->XData[i+1][j+1]), 2589 | ct3y(cs->YData[i+1][j+1]), 2590 | ct3z(cs->ZData[i+1][j+1]) ); 2591 | glEnd(); 2592 | } 2593 | } 2594 | } 2595 | // Edge 2596 | if(cs->EdgeColor != "none"){ 2597 | glLineWidth(cs->LineWidth); 2598 | rgb=ColorSpec2RGB(cs->EdgeColor); 2599 | glColor3d(rgb[0], rgb[1], rgb[2]); 2600 | 2601 | for(int i=0;iXData[i][j]), 2605 | ct3y(cs->YData[i][j]), 2606 | ct3z(cs->ZData[i][j]) ); 2607 | } 2608 | glEnd(); 2609 | } 2610 | for(int j=0;jXData[i][j]), 2614 | ct3y(cs->YData[i][j]), 2615 | ct3z(cs->ZData[i][j]) ); 2616 | } 2617 | glEnd(); 2618 | } 2619 | } 2620 | }//(X,Y,Z) 2621 | } 2622 | /// dispaly contour 2623 | dmat contourc(dvec x, dvec y, dmat Z, dvec v){ 2624 | //Z(i,j), x(j),y(i) 2625 | double x0,y0,z0; 2626 | int ny=Z.size(); 2627 | int nx=Z[0].size(); 2628 | dmat C; 2629 | ContourPoint c; 2630 | vector vc; 2631 | deque ac; 2632 | C.resize(2); 2633 | //z0=0.1; 2634 | //v.clear();v.push_back(0.2);v.push_back(0.1); 2635 | int is_print=0; 2636 | 2637 | 2638 | 2639 | for(int iv=0;iv2)) ){ 2698 | if( (c.xy==0) && (vc[m].xy==0) && (vc[m].xj==c.xj ) && (vc[m].yi==c.yi-1) ){ is=1; } 2699 | if( (c.xy==0) && (vc[m].xy==0) && (vc[m].xj==c.xj ) && (vc[m].yi==c.yi+1) ){ is=2; } 2700 | if( (c.xy==0) && (vc[m].xy==1) && (vc[m].xj==c.xj ) && (vc[m].yi==c.yi ) ){ is=3; } 2701 | if( (c.xy==0) && (vc[m].xy==1) && (vc[m].xj==c.xj+1) && (vc[m].yi==c.yi ) ){ is=4; } 2702 | if( (c.xy==0) && (vc[m].xy==1) && (vc[m].xj==c.xj ) && (vc[m].yi==c.yi-1) ){ is=5; } 2703 | if( (c.xy==0) && (vc[m].xy==1) && (vc[m].xj==c.xj+1) && (vc[m].yi==c.yi-1) ){ is=6; } 2704 | if( (c.xy==1) && (vc[m].xy==1) && (vc[m].xj==c.xj+1) && (vc[m].yi==c.yi ) ){ is=7; } 2705 | if( (c.xy==1) && (vc[m].xy==1) && (vc[m].xj==c.xj-1) && (vc[m].yi==c.yi ) ){ is=8; } 2706 | if( (c.xy==1) && (vc[m].xy==0) && (vc[m].xj==c.xj ) && (vc[m].yi==c.yi ) ){ is=9; } 2707 | if( (c.xy==1) && (vc[m].xy==0) && (vc[m].xj==c.xj ) && (vc[m].yi==c.yi+1) ){ is=10; } 2708 | if( (c.xy==1) && (vc[m].xy==0) && (vc[m].xj==c.xj-1) && (vc[m].yi==c.yi ) ){ is=11; } 2709 | if( (c.xy==1) && (vc[m].xy==0) && (vc[m].xj==c.xj-1) && (vc[m].yi==c.yi+1) ){ is=12; } 2710 | } 2711 | if(is){kk=m;} 2712 | m++; 2713 | } 2714 | if(is){ 2715 | vc[kk].done=1; 2716 | c=vc[kk]; 2717 | } 2718 | if(mode==1){ 2719 | if(is){ 2720 | ac.push_back(vc[kk]); 2721 | mode_next=1; 2722 | }else{ 2723 | mode_next=2; 2724 | } 2725 | } 2726 | if(mode==3){ 2727 | if(is){ 2728 | ac.push_front(vc[kk]); 2729 | mode_next=3; 2730 | }else{ 2731 | mode_next=4; 2732 | } 2733 | } 2734 | } 2735 | if(mode==2){// set first terminal 2736 | c=ac[0]; 2737 | mode_next=3; 2738 | } 2739 | if(mode==4){// move the accumlated points 2740 | if(ac.size()){ 2741 | C[0].push_back(z0); 2742 | C[1].push_back(ac.size()); 2743 | for(int i=0;i rgb; 2768 | //int ny=cs->ZData.size(); 2769 | //int nx=cs->ZData[0].size(); 2770 | vector< vector< double > > C; 2771 | double xx,yy; 2772 | 2773 | C=contourc(cs->XData[0],cs->YData[0],cs->ZData,cs->V); 2774 | 2775 | glDisable(GL_LINE_STIPPLE); 2776 | gl2psDisable(GL2PS_LINE_STIPPLE); 2777 | 2778 | //glLineWidth(cl->LineWidth); 2779 | glLineWidth(2); 2780 | gl2psLineWidth(2); 2781 | 2782 | //rgb=cs->CData[i][j]; 2783 | //glColor3f(rgb[0],rgb[1],rgb[2]); 2784 | glColor3f(0,0,0); 2785 | 2786 | //TODO: adjust line color and properties 2787 | int k=0,nk; 2788 | for(int i=0;ink){ 2801 | k=0; 2802 | glEnd(); 2803 | } 2804 | } 2805 | } 2806 | 2807 | dmat MatPlot::peaks(int n){ 2808 | 2809 | float x1=1,y1=0; 2810 | float x2=-1,y2=1; 2811 | float x3=-0.5,y3=-1; 2812 | float sr1,sr2,sr3; 2813 | float sigma=0.4; 2814 | float a1=1,a2=0.5,a3=0.3; 2815 | double x,y; 2816 | //vector< vector< double > > Z(n,n); 2817 | dmat Z(n,dvec(n)); 2818 | for(int i=0;iadd_child(h); 2839 | vPatch.push_back(Patch(h)); 2840 | //as.Parent=gca(); 2841 | } 2842 | if(mode==1){ 2843 | 2844 | } 2845 | if(iPatchxmax=fmax( Fmax(cp->XData), ca->xmax); 2851 | ca->xmin=fmin( Fmin(cp->XData), ca->xmin); 2852 | ca->ymax=fmax( Fmax(cp->YData), ca->ymax); 2853 | ca->ymin=fmin( Fmin(cp->YData), ca->ymin); 2854 | ca->zmax=fmax( Fmax(cp->ZData), ca->zmax ); 2855 | ca->zmin=fmin( Fmin(cp->ZData), ca->zmin ); 2856 | } 2857 | tcvec MatPlot::Index2TrueColor(dvec IC){ 2858 | if(ca->CLim[0]==ca->CLim[1]){ 2859 | ca->CLim[0]=fmin( Fmin(IC), Fmin(IC) ); 2860 | ca->CLim[1]=fmax( Fmax(IC), Fmax(IC) ); 2861 | } 2862 | vector rgb; 2863 | tcvec tc; 2864 | for(int j=0;jCLim[0],ca->CLim[1]); 2866 | tc.push_back(rgb); 2867 | } 2868 | return tc; 2869 | } 2870 | /// patch 2871 | int MatPlot::patch(dmat X,dmat Y){ 2872 | // Single color 2873 | int h=patch(); 2874 | cp->type=0; 2875 | 2876 | cp->XData=X; 2877 | cp->YData=Y; 2878 | cp->ZData.clear(); 2879 | cp->CData.clear(); 2880 | 2881 | patch_config(); 2882 | return h; 2883 | } 2884 | int MatPlot::patch(dmat X,dmat Y,dvec C){ 2885 | // One color per face with index color 2886 | int h=patch(); 2887 | cp->type=0; 2888 | cp->XData=X; 2889 | cp->YData=Y; 2890 | cp->ZData.clear(); 2891 | cp->CData=Index2TrueColor(C); 2892 | 2893 | patch_config(); 2894 | return h; 2895 | } 2896 | int MatPlot::patch(dmat X,dmat Y,tcvec C){ 2897 | // One color per face with true color 2898 | int h=patch(); 2899 | cp->type=0; 2900 | cp->XData=X; 2901 | cp->YData=Y; 2902 | cp->ZData.clear(); 2903 | cp->CData=C; 2904 | 2905 | patch_config(); 2906 | return h; 2907 | } 2908 | int MatPlot::patch(dmat X,dmat Y,dmat Z){ 2909 | // Single color 2910 | int h=patch(); 2911 | ca->View=1; 2912 | cp->type=1; 2913 | cp->XData=X; 2914 | cp->YData=Y; 2915 | cp->ZData=Z; 2916 | cp->CData.clear(); 2917 | 2918 | patch_config(); 2919 | return h; 2920 | } 2921 | int MatPlot::patch(dmat X,dmat Y,dmat Z,dvec C){ 2922 | // One color per face 2923 | int h=patch(); 2924 | ca->View=1; 2925 | cp->type=1; 2926 | 2927 | cp->XData=X; 2928 | cp->YData=Y; 2929 | cp->ZData=Z; 2930 | cp->CData=Index2TrueColor(C); 2931 | 2932 | patch_config(); 2933 | return h; 2934 | } 2935 | int MatPlot::patch(dmat X,dmat Y,dmat Z,tcvec C){ 2936 | // One color per face 2937 | int h=patch(); 2938 | ca->View=1; 2939 | cp->type=1; 2940 | 2941 | cp->XData=X; 2942 | cp->YData=Y; 2943 | cp->ZData=Z; 2944 | cp->CData=C; 2945 | 2946 | patch_config(); 2947 | return h; 2948 | } 2949 | /// bar 2950 | 2951 | int MatPlot::bar(dvec y){ 2952 | dvec x; 2953 | x.resize(y.size()); 2954 | for(int i=0;itype=0; 2973 | cp->XData.clear(); 2974 | cp->YData.clear(); 2975 | cp->ZData.clear(); 2976 | //cp->CData.clear(); 2977 | 2978 | double wx=width*(Fmax(x)-Fmin(x))/x.size(); 2979 | 2980 | dvec X(4),Y(4); 2981 | for(int i=0;iXData.push_back(X); 2987 | cp->YData.push_back(Y); 2988 | } 2989 | patch_config(); 2990 | return h; 2991 | } 2992 | 2993 | /// display 2994 | 2995 | void MatPlot::display_patch(){ 2996 | if(is_debug1){printf("display:%4d Patch :type %d, Faces.size %d\n", 2997 | cp->id,cp->type,cp->Faces.size());} 2998 | 2999 | if(cp->type==0){ display_patch_2d(); } 3000 | if(cp->type==1){ display_patch_3d(); } 3001 | } 3002 | 3003 | void MatPlot::display_patch_2d(){ 3004 | // FaceVertex & CData // 3005 | 3006 | vector v(3); 3007 | vector f(3); 3008 | float x,y; 3009 | for(int i=0;iFaces.size();++i){ 3010 | f=cp->Faces[i]; 3011 | glBegin(GL_TRIANGLES); 3012 | x=ctx(cp->Vertices[f[0]][0]); 3013 | y=cty(cp->Vertices[f[0]][1]); 3014 | glVertex2d(x,y); 3015 | x=ctx(cp->Vertices[f[1]][0]); 3016 | y=cty(cp->Vertices[f[1]][1]); 3017 | glVertex2d(x,y); 3018 | x=ctx(cp->Vertices[f[2]][0]); 3019 | y=cty(cp->Vertices[f[2]][1]); 3020 | glVertex2d(x,y); 3021 | glEnd(); 3022 | } 3023 | 3024 | 3025 | // XYZ Data // 3026 | int nf,nv;//number of faces and vertex 3027 | nf=cp->XData.size(); 3028 | vector rgb; 3029 | 3030 | for(int i=0;iXData[i].size(); 3032 | 3033 | // Edge 3034 | if(cp->EdgeColor!="none"){ 3035 | 3036 | glLineWidth(cp->LineWidth); 3037 | gl2psLineWidth(cp->LineWidth); 3038 | 3039 | rgb=ColorSpec2RGB(cp->EdgeColor); 3040 | glColor3f(rgb[0],rgb[1],rgb[2]); 3041 | 3042 | glBegin(GL_LINE_LOOP); 3043 | for(int iv=0;ivXData[i][iv]), 3045 | cty(cp->YData[i][iv]) ); 3046 | } 3047 | glEnd(); 3048 | 3049 | } 3050 | // Face 3051 | if(cp->FaceColor!="none"){ 3052 | 3053 | rgb=ColorSpec2RGB(cp->FaceColor); 3054 | glColor3f(rgb[0],rgb[1],rgb[2]); 3055 | 3056 | if(cp->CData.size()){ 3057 | rgb=cp->CData[i]; 3058 | glColor3d(rgb[0],rgb[1],rgb[2]); 3059 | } 3060 | 3061 | glBegin(GL_POLYGON); 3062 | for(int iv=0;ivXData[i][iv]), 3064 | cty(cp->YData[i][iv]) ); 3065 | } 3066 | glEnd(); 3067 | 3068 | } 3069 | } 3070 | 3071 | 3072 | } 3073 | void MatPlot::display_patch_3d(){ 3074 | // FaceVertex & CData // 3075 | // XYZ Data // 3076 | int nf,nv;//number of faces and vertex 3077 | nf=cp->XData.size(); 3078 | vector rgb; 3079 | 3080 | for(int i=0;iXData[i].size(); 3082 | 3083 | // Edge 3084 | if(cp->EdgeColor!="none"){ 3085 | 3086 | glLineWidth(cp->LineWidth); 3087 | gl2psLineWidth(cp->LineWidth); 3088 | 3089 | rgb=ColorSpec2RGB(cp->EdgeColor); 3090 | glColor3f(rgb[0],rgb[1],rgb[2]); 3091 | 3092 | glBegin(GL_LINE_LOOP); 3093 | for(int iv=0;ivXData[i][iv]), 3095 | ct3y(cp->YData[i][iv]), 3096 | ct3z(cp->ZData[i][iv]) ); 3097 | } 3098 | glEnd(); 3099 | 3100 | } 3101 | // Face 3102 | if(cp->FaceColor!="none"){ 3103 | 3104 | rgb=ColorSpec2RGB(cp->FaceColor); 3105 | glColor3f(rgb[0],rgb[1],rgb[2]); 3106 | 3107 | if(cp->CData.size()){ 3108 | rgb=cp->CData[i]; 3109 | glColor3d(rgb[0],rgb[1],rgb[2]); 3110 | } 3111 | 3112 | glBegin(GL_POLYGON); 3113 | for(int iv=0;ivXData[i][iv]), 3115 | ct3y(cp->YData[i][iv]), 3116 | ct3z(cp->ZData[i][iv]) 3117 | ); 3118 | } 3119 | glEnd(); 3120 | 3121 | } 3122 | } 3123 | } 3124 | /// Text // 3125 | //TODO more fonts 3126 | /// text 3127 | int MatPlot::text(){ 3128 | int h=iText*100 + tText; hObj=h; 3129 | if(is_debug1){printf("mode: %d handle: %4d Text\n",mode,h);} 3130 | 3131 | if(mode==0){ 3132 | vText.push_back(Text(h)); 3133 | ca->add_child(h); 3134 | //at.Parent=gca(); 3135 | } 3136 | if(mode==1){ 3137 | } 3138 | if(iTextid);} 3144 | glColor3f(0,0,0); 3145 | glRasterPos2f( ctx(ct->Position[0]), cty(ct->Position[1]) ); 3146 | gl2psText(ct->String.c_str(), "Arial", 12); 3147 | for(int i=0; i<(int)ct->String.size(); ++i ){ 3148 | glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, ct->String[i] ); 3149 | } 3150 | } 3151 | int MatPlot::text(double x,double y,string s){ 3152 | // text on current axes 3153 | //if(is_debug1){cout<<"mode:"<Position[0]=x; 3156 | ct->Position[1]=y; 3157 | ct->String=s; 3158 | return h; 3159 | } 3160 | void MatPlot::set_font(char font_[],int size){ 3161 | //font=font_; 3162 | //font_size=size; 3163 | } 3164 | /// ptext 3165 | void MatPlot::ptext(float x,float y,string s){ 3166 | // Viewport Figure 3167 | glViewport(0,0, (int)(window_w),(int)(window_h)); 3168 | glLoadIdentity(); 3169 | gluOrtho2D( 0.0, 1.0, 0.0, 1.0 ); 3170 | 3171 | glColor3f(0,0,0); 3172 | glRasterPos2f( x, y ); 3173 | gl2psText(s.c_str(), "Arial", 12); 3174 | //gl2psText(test, "Times-Roman", 24); 3175 | 3176 | for(int i=0; i<(int)s.size(); i++ ){ 3177 | glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, s[i] ); 3178 | } 3179 | } 3180 | void MatPlot::ptext3(float x,float y,float z,string s){ 3181 | glColor3f(0,0,0); 3182 | glRasterPos3d(x,y,z); 3183 | gl2psText(s.c_str(), "Arial", 12); 3184 | for(int i=0; i<(int)s.size(); i++ ){ 3185 | glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, s[i] ); 3186 | } 3187 | } 3188 | void MatPlot::ptext3c(float x,float y,float z,string s){ 3189 | int char_w=6,char_h=12; 3190 | int num_char=s.length(); 3191 | glColor3f(0,0,0); 3192 | glRasterPos3d(x,y,z); 3193 | glBitmap(0,0,0,0,-char_w*num_char/2,-char_h/2,NULL); 3194 | gl2psText(s.c_str(), "Arial", 12); 3195 | 3196 | for(int i=0; i<(int)s.size(); i++ ){ 3197 | glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, s[i] ); 3198 | } 3199 | } 3200 | 3201 | 3202 | /// Color // 3203 | 3204 | void MatPlot::Shading(string c){ 3205 | shading(c); 3206 | } 3207 | void MatPlot::shading(string c){ 3208 | 3209 | int tObj=hObj%100; 3210 | int iObj=hObj/100; 3211 | if( (tObj==tSurface) && (iObj MatPlot::colormap(string c,float t){ 3224 | 3225 | vector rgb(3); 3226 | if(t>1){t=1;} 3227 | if(t<0){t=0;} 3228 | 3229 | if( c=="Gray" ){ 3230 | rgb[0]=t; 3231 | rgb[1]=t; 3232 | rgb[2]=t; 3233 | return rgb; 3234 | } 3235 | if( c=="HSV" ){ 3236 | t*=6; 3237 | if( 0<=t && t<=1.0){rgb[0] = 1; rgb[1] = t; rgb[2] = 0;} 3238 | if(1.0<=t && t<=2.0){rgb[0] = 1-(t-1); rgb[1] = 1; rgb[2] = 0;} 3239 | if(2.0<=t && t<=3.0){rgb[0] = 0; rgb[1] = 1; rgb[2] = t-2;} 3240 | if(3.0<=t && t<=4.0){rgb[0] = 0; rgb[1] = 1-(t-3); rgb[2] = 1;} 3241 | if(4.0<=t && t<=5.0){rgb[0] = t-4; rgb[1] = 0; rgb[2] = 1;} 3242 | if(5.0<=t && t<=6.0){rgb[0] = 1; rgb[1] = 0; rgb[2] = 1-(t-5);} 3243 | 3244 | return rgb; 3245 | } 3246 | if( c=="Jet" ){ 3247 | t*=8; 3248 | if( 0<=t && t<=1.0){rgb[0] = 0; rgb[1] = 0; rgb[2] = 0.5+0.5*t;} 3249 | if(1.0<=t && t<=3.0){rgb[0] = 0; rgb[1] = 0.5*(t-1); rgb[2] = 1;} 3250 | if(3.0<=t && t<=5.0){rgb[0] = 0.5*(t-3);rgb[1] = 1; rgb[2] = 1-0.5*(t-3);} 3251 | if(5.0<=t && t<=7.0){rgb[0] = 1; rgb[1] = 1-0.5*(t-5);rgb[2] = 0;} 3252 | if(7.0<=t && t<=8.0){rgb[0] = 1-0.5*(t-7);rgb[1] = 0; rgb[2] = 0;} 3253 | return rgb; 3254 | } 3255 | if( c=="Hot" ){ 3256 | t*=3; 3257 | if( 0<=t && t<=1.0){rgb[0] = t; rgb[1] = 0; rgb[2] = 0;} 3258 | if(1.0<=t && t<=2.0){rgb[0] = 1; rgb[1] = t-1; rgb[2] = 0;} 3259 | if(2.0<=t && t<=3.0){rgb[0] = 1; rgb[1] = 1; rgb[2] = t-2;} 3260 | return rgb; 3261 | } 3262 | 3263 | if( c=="Cool" ){ 3264 | rgb[0]=t; 3265 | rgb[1]=1-t; 3266 | rgb[2]=1; 3267 | return rgb; 3268 | } 3269 | if( c=="Spring" ){// Magenta - Yellow 3270 | rgb[0]=1; 3271 | rgb[1]=t; 3272 | rgb[2]=1-t; 3273 | return rgb; 3274 | } 3275 | if( c=="Summer" ){// Green Yellow 3276 | rgb[0]=t; 3277 | rgb[1]=1; 3278 | rgb[2]=0; 3279 | return rgb; 3280 | } 3281 | if( c=="Autumn" ){ 3282 | rgb[0]=1; 3283 | rgb[1]=t; 3284 | rgb[2]=0; 3285 | return rgb; 3286 | } 3287 | if( c=="Winter" ){ 3288 | rgb[0]=0; 3289 | rgb[1]=t; 3290 | rgb[2]=1-t; 3291 | return rgb; 3292 | } 3293 | if( c=="Bone" ){ 3294 | rgb[0]=t; 3295 | //rgb[1]=t;if(t<0.8){rgb[1]=t;} 3296 | rgb[2]=t; 3297 | return rgb; 3298 | } 3299 | } 3300 | void MatPlot::colormap(string c){ 3301 | //if(is_debug1){printf("colormap %s \n",c.c_str());} 3302 | int n; 3303 | float t; 3304 | vector rgb(3); 3305 | 3306 | cmap.clear(); 3307 | n=64; 3308 | for(int i=0;iiColorMap=iColorMap; 3314 | cs->ColorMap=c; 3315 | } 3316 | } 3317 | void MatPlot::colormap(vector > c){ 3318 | cmap=c; 3319 | } 3320 | void MatPlot::gray(){colormap("Gray");}; 3321 | void MatPlot::jet(){ colormap("Jet"); } 3322 | void MatPlot::hsv(){ colormap("HSV"); } 3323 | void MatPlot::hot(){ colormap("Hot"); } 3324 | void MatPlot::cool(){colormap("Cool");} 3325 | void MatPlot::spring(){colormap("Spring");} 3326 | void MatPlot::summer(){colormap("Summer");} 3327 | void MatPlot::autumn(){colormap("Autumn");} 3328 | void MatPlot::winter(){colormap("Winter");} 3329 | 3330 | vector MatPlot::map2color(double x,double xmin,double xmax){ 3331 | int n=cmap.size(); 3332 | float normx; 3333 | vector rgb(3); 3334 | 3335 | normx=(x-xmin)/(xmax-xmin); 3336 | if(x>xmax){normx=1;} 3337 | if(x MatPlot::ColorSpec2RGB(string c){ 3345 | float r,g,b; 3346 | //line 3347 | if( c=="k" ){r=0;g=0;b=0;}// black 3348 | if( c=="r" ){r=1;g=0;b=0;}// red 3349 | if( c=="b" ){r=0;g=0;b=1;}// blue 3350 | if( c=="g" ){r=0;g=1;b=0;}// green 3351 | if( c=="c" ){r=0;g=1;b=1;}// cyan 3352 | if( c=="m" ){r=1;g=0;b=1;}// magenta 3353 | if( c=="y" ){r=1;g=1;b=0;}// yellow 3354 | if( c=="w" ){r=1;g=1;b=1;}// white 3355 | 3356 | //dark color 3357 | float h=0.6; 3358 | if( c=="dr" ){r=h;g=0;b=0;}// red 3359 | if( c=="db" ){r=0;g=0;b=h;}// blue 3360 | if( c=="dg" ){r=0;g=h;b=0;}// green 3361 | if( c=="dc" ){r=0;g=h;b=h;}// cyan 3362 | if( c=="dm" ){r=h;g=0;b=h;}// magenta 3363 | if( c=="dy" ){r=h;g=h;b=0;}// yellow 3364 | 3365 | //light color 3366 | h=0.5; 3367 | if( c=="lr" ){r=1;g=h;b=h;}// red 3368 | if( c=="lb" ){r=h;g=h;b=1;}// blue 3369 | if( c=="lg" ){r=h;g=1;b=h;}// green 3370 | if( c=="lc" ){r=h;g=1;b=1;}// cyan 3371 | if( c=="lm" ){r=1;g=h;b=1;}// magenta 3372 | if( c=="ly" ){r=1;g=1;b=h;}// yellow 3373 | 3374 | //universal color 3375 | if( c=="ur" ){r=1; g=0.2; b=0; }//red 3376 | if( c=="ub" ){r=0; g=0.25;b=1; }//blue 3377 | if( c=="ug" ){r=0.2; g=0.6; b=0.4;}//green 3378 | if( c=="uy" ){r=1; g=1; b=1; }//yellow 3379 | if( c=="uc" ){r=0.4; g=0.8; b=1; }//sky blue 3380 | if( c=="up" ){r=1; g=0.6; b=0.6;}//pink 3381 | if( c=="uo" ){r=1; g=0.6; b=0; }//orange 3382 | if( c=="um" ){r=0.6; g=0; b=0.4;}//perple 3383 | if( c=="ubr"){r=0.4; g=0.2; b=0; }//brown 3384 | 3385 | char line[1000],*tp; 3386 | char d[]=" ,:\t\n[]";//delimiter 3387 | /* 3388 | if(c.size()){ 3389 | if( (c[0]=='[') && (c[c.size()-1]==']') ){ 3390 | sprintf(line,c.c_str()); 3391 | tp=strtok(line,d); if(tp){r=atof(tp);} 3392 | tp=strtok(NULL,d); if(tp){g=atof(tp);} 3393 | tp=strtok(NULL,d); if(tp){b=atof(tp);} 3394 | } 3395 | } 3396 | */ 3397 | //cout <<"c,r,g,b: "<< c <<" "< out(3); 3399 | out[0]=r; 3400 | out[1]=g; 3401 | out[2]=b; 3402 | return out; 3403 | 3404 | } 3405 | string MatPlot::rgb2colorspec(vector rgb){ 3406 | char c[100]; 3407 | sprintf(c,"[%f %f %f]",rgb[0],rgb[1],rgb[2]); 3408 | string s=c; 3409 | return s; 3410 | } 3411 | tcolor MatPlot::colorspec2rgb(string c){ 3412 | tcolor tc; tc.clear(); 3413 | float r,g,b; 3414 | sscanf(c.c_str(),"[%lf %lf %lf]",&r,&g,&b); 3415 | tc.push_back(r); 3416 | tc.push_back(g); 3417 | tc.push_back(b); 3418 | return tc; 3419 | } 3420 | /// EOF 3421 | 3422 | 3423 | --------------------------------------------------------------------------------