📄 sample4_11.c
字号:
#include "toolbox.h"#include "movectrl.h"#include <ansi_c.h>#include "cviogl.h"#include "scroll.h"#include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */#include <userint.h>#include "sample4_11.h"#define XPOINTS 21#define YPOINTS 21 #define PI 3.1415926535897932384626433832795028841971#define RAD(x) ((x)*PI/180.0)#define SINC(x) ((x) != 0.0 ? sin(PI*(x))/(PI*(x)): 1.0)static int panelHandle,panelTemp;int PlotCtrl;char msgBuf[256]; int CreatePlot(void) ;int SetControlAttributes(void) ;void initScroll(void);int error;int CVICALLBACK Picture1 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) ;int main (int argc, char *argv[]){ if (InitCVIRTE (0, argv, 0) == 0) /* Needed if linking in external compiler; harmless otherwise */ return -1; /* out of memory */ SetSleepPolicy (VAL_SLEEP_MORE); if ((panelHandle = LoadPanel (0, "sample4_11.uir", PANEL)) < 0) return -1; if ((panelTemp = LoadPanel (panelHandle, "sample4_11.uir", PANEL2)) < 0) return -1; SetPanelPos (panelTemp, 30, 300); MakeMovableCtrl (panelHandle,-1, "", 1, 1, 1, 1); EnableExtendedMouseEvents (panelHandle, PANEL_PICTURE, 0.1); error=ScrollBar_ConvertFromGraph (panelTemp, PANEL2_GRAPH, 0, 0); if(error<0) goto Error; initScroll(); PlotCtrl=OGLConvertCtrl(panelHandle,PANEL_PICTURE); if (PlotCtrl<0) { OGLGetErrorString (panelHandle, msgBuf, 255); MessagePopup("OGLConvertCtrl Error", msgBuf); goto Error; } SetControlAttributes(); CreatePlot(); ChainCtrlCallback (panelHandle, PANEL_PICTURE,Picture1 , 0, "chained 1"); DisplayPanel (panelTemp); DisplayPanel (panelHandle); RunUserInterface ();Error: return 0;}int CVICALLBACK Picture (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case OGLEVENT_REFRESH : break; case EVENT_MOUSE_MOVE: case EVENT_LEFT_MOUSE_UP: case EVENT_RIGHT_MOUSE_UP: SetCtrlVal (panelHandle, PANEL_X1,eventData1); SetCtrlVal (panelHandle, PANEL_Y1, eventData2); ConvertMouseCoordinates (panelHandle,control , 1, 1,&eventData1 ,&eventData2 ); SetCtrlVal (panelHandle, PANEL_X2,eventData1 ); SetCtrlVal (panelHandle, PANEL_Y2, eventData2); break; case EVENT_LEFT_CLICK : SetCtrlVal (panelHandle, PANEL_TEXTMSG_CLICL3D,"Left Click 3D"); break; } return 0;}int CVICALLBACK Picture1 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_LEFT_DOUBLE_CLICK: SetCtrlVal (panelHandle, PANEL_TEXTMSG_CLICL3D,"Double Click 3D"); break; } return 0;}int CVICALLBACK Demition (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_COMMIT: CreatePlot(); break; } return 0;}int CVICALLBACK Quit (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_COMMIT: QuitUserInterface (0); break; } return 0;}int CVICALLBACK Graph (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_SB_UP_BUTTON: case EVENT_SB_DOWN_BUTTON: case EVENT_SB_PAGE_UP: case EVENT_SB_PAGE_DOWN: case EVENT_SB_THUMB_CHANGE: case EVENT_SB_COMMIT: CreatePlot(); break; } return 0;}int CVICALLBACK Save (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_COMMIT: SaveMovableCtrlSettings (panelHandle, -1, "Default"); break; } return 0;}int CVICALLBACK Load (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ char configName[256]; switch (event) { case EVENT_COMMIT: LoadMovableCtrlSettings (panelHandle, -1, configName); break; } return 0;}int SetControlAttributes(){ OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_LIGHTING_ENABLE, 1); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_LIGHT_SELECT, 1); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_LIGHT_ENABLE, 1); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_LIGHT_DISTANCE, 3.0); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_VIEW_DISTANCE,2.5); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_PROJECTION_TYPE,OGLVAL_PERSPECTIVE); // Setup Axis Labels OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_XNAME_VISIBLE,1); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_YNAME_VISIBLE,1); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_ZNAME_VISIBLE,1); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_XNAME,"x 轴"); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_YNAME,"y 轴"); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_ZNAME,"z 轴"); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_XLABEL_VISIBLE,1); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_YLABEL_VISIBLE,1); OGLSetCtrlAttribute(panelHandle,PlotCtrl,OGLATTR_ZLABEL_VISIBLE,1); OGLSetCtrlAttribute (panelHandle,PlotCtrl , OGLATTR_ZAXIS_SCALING, OGLVAL_MANUAL); OGLSetCtrlAttribute (panelHandle,PlotCtrl, OGLATTR_ZMIN, 0.0); OGLSetCtrlAttribute (panelHandle,PlotCtrl, OGLATTR_ZMAX, 100.0); // OGLSetCtrlAttribute (panelHandle,PlotCtrl , OGLATTR_ENABLE_PROPERTY_POPUP, 1); return 0; }int CreatePlot(void){ int error = 0; int PlotDemition = 1; int MaxofPlot; int numPlots; double radxStep; double radyStep; int plot; register int i,j; double x,y; double xStep,yStep; void * pts; double *pts3DPtr; OGLVertexD *pts2DPtr; /* Get the plottype of either 3D or 2D lines(no surface) */ GetCtrlVal(panelHandle, PANEL_DEMITION, &PlotDemition); error=ScrollBar_GetAttribute (panelTemp, PANEL2_GRAPH, ATTR_SB_VALUE, &MaxofPlot); xStep = 360.0/(XPOINTS-1); yStep = 360.0/(YPOINTS-1); radxStep = RAD(xStep); radyStep = RAD(yStep); // Malloc data for plot if (PlotDemition) //if 1,3D { pts = malloc((XPOINTS)*(YPOINTS)*sizeof(double)); if (!pts) return -1; else pts3DPtr = (double *)pts; } else { pts = malloc((XPOINTS)*(YPOINTS)*sizeof(OGLVertexD)); if (!pts) return -1; else pts2DPtr = (OGLVertexD *)pts; } // Generate plot data for(j=0,y=-PI;j<YPOINTS;j++,y+=radyStep) { for(i=0,x=-PI;i<XPOINTS;i++,x+=radxStep) if (PlotDemition) { *pts3DPtr = sin(x)*MaxofPlot; pts3DPtr++; } else { pts2DPtr->x = x; pts2DPtr->y = y; pts2DPtr->z = sin(y)*MaxofPlot; pts2DPtr++; } } // Setup control OGLSetCtrlAttribute (panelHandle, PlotCtrl,OGLATTR_PLOTAREA_ZSTART, 0.0); OGLSetCtrlAttribute (panelHandle, PlotCtrl,OGLATTR_PLOTAREA_ZSIZE, 0.6); OGLSetCtrlAttribute (panelHandle, PlotCtrl,OGLATTR_VIEW_AUTO_DISTANCE, 0); OGLSetCtrlAttribute (panelHandle, PlotCtrl,OGLATTR_VIEW_DISTANCE, 2.5); OGLGetCtrlAttribute (panelHandle, PlotCtrl,OGLATTR_NUM_PLOTHANDLES,&numPlots); // Delete any existing plots for (i=0;i<numPlots;i++) { OGLGetCtrlAttribute(panelHandle,PlotCtrl, OGLATTR_FIRST_PLOTHANDLE, &plot); OGLDeletePlot(panelHandle,PlotCtrl,plot,0); } // Give plot data to control if (PlotDemition) { pts3DPtr = pts; plot = OGLPlot3DUniform (panelHandle, PlotCtrl, pts3DPtr, XPOINTS, YPOINTS, OGLVAL_DOUBLE, 2*PI/(XPOINTS-1), -PI, 2*PI/(YPOINTS-1), -PI); // Setup plot attributes OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_SURFACE_STYLE,OGLVAL_SMOOTH); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_SURFACE_COLOR,OGLVAL_RED); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_SURFACE_SPECULAR_FACTOR,1.0); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_SURFACE_SHININESS,50); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_WIRE_STYLE,OGLVAL_SOLID); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_WIRE_COLOR,OGLVAL_RED); } else { for (i=0;i<XPOINTS;i++) { pts2DPtr = pts; plot = OGLPlot3DScatter (panelHandle, PlotCtrl, &pts2DPtr[i*(XPOINTS)], XPOINTS, 1, OGLVAL_DOUBLE); // Setup plot attributes OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_SURFACE_STYLE,OGLVAL_SMOOTH); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_SURFACE_COLOR,OGLVAL_RED); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_SURFACE_SPECULAR_FACTOR,1.0); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_SURFACE_SHININESS,50); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_WIRE_STYLE,OGLVAL_SOLID); OGLSetPlotAttribute(panelHandle,PlotCtrl,plot,OGLATTR_WIRE_COLOR,OGLVAL_RED); } } // Display plot OGLRefreshGraph(panelHandle, PlotCtrl); // free plot data memory because control copies it free(pts);Error : return error;}void initScroll(){ ScrollBar_SetAttribute (panelTemp, PANEL2_GRAPH, ATTR_SB_DOC_MIN, 0); ScrollBar_SetAttribute (panelTemp, PANEL2_GRAPH, ATTR_SB_DOC_MAX, 100); ScrollBar_SetAttribute (panelTemp, PANEL2_GRAPH, ATTR_SB_VIEW_SIZE, 5); ScrollBar_SetAttribute (panelTemp, PANEL2_GRAPH, ATTR_SB_PROP_THUMB, 1); ScrollBar_SetAttribute (panelTemp, PANEL2_GRAPH, ATTR_SB_VALUE, 50); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -