// Test: make a sinusoidal moving sheet. // Date: 20DEC2015 // Author: Erik Zorzin #include "LinkDef.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include const unsigned int N = 20; // Number of x-points in the graph [#]. const unsigned int M = 20; // Number of y-points in the graph [#]. const double FS = 20; // Sampling frequency [Hz]. const double F_x = 1; // "x" signal frequency [Hz]. const double F_y = 1.5; // "y" signal frequency [Hz]. const double PI = TMath::Pi(); // PI = 3.141.... const double PHI = 2*PI/360*5; // Phase shift [rad]. class TGWindow; class TGMainFrame; class TRootEmbeddedCanvas; class GUI // GUI: Graphic User Interface. { private: TGMainFrame* fMain; // Main frame. TRootEmbeddedCanvas* fEcanvas; // Embedded Canvas. TCanvas* fCanvas; // Canvas. TGHorizontalFrame* hframe; // Horizontal frame. TGHSlider* A_sld; // Amplitude slider [m]. TGraph2D* graph; // Graph. TPad* pad; // Pad. double A = 1; // Signal amplitude [m]. int i; // Integer counter. int j; // Integer counter. int k; // Integer counter. double omega_x = 2*PI*F_x; // "omega_x" pulsation [rad/s]. double omega_y = 2*PI*F_y; // "omega_y" pulsation [rad/s]. double ds = 1.0/FS; // "dt" [s]. double x; // "x" variable: displacement [m]. double y; // "y" variable: displacement [m]. double z; // "z" variable: displacement [m]. double phi_x = 0; // "phi_x" variable: phase shift [rad]. double phi_y = 0; // "phi_y" variable: phase shift [rad]. TStopwatch* watch; public: GUI(const TGWindow* p, UInt_t w, UInt_t h); virtual ~GUI(); void DoSlider(Int_t pos); ///< Manages "Amplitude" [m] slider. void CloseApplication(); ///< Manages window's close event. void Plot(); }; enum SlidersIDs // Slider ID enum. { A_sld_ID // "Amplitude" [m] slider. }; GUI::GUI(const TGWindow* p, UInt_t w, UInt_t h) { fMain = new TGMainFrame(p, w, h); // Creating main frame... fEcanvas = new TRootEmbeddedCanvas("Ecanvas", fMain, w, h); // Creating canvas... fMain->AddFrame(fEcanvas, new TGLayoutHints(kLHintsExpandX|kLHintsExpandY, 10, 10, 10, 1)); // Adding canvas to main frame... fMain->SetWindowName("TestGraph"); // Setting main frame name... hframe = new TGHorizontalFrame(fMain, 600, 40); // Creating hframe... A_sld = new TGHSlider(hframe, 150, kSlider1|kScaleDownRight, A_sld_ID); // Creating "A_sld" slider... A_sld->Connect("PositionChanged(Int_t)", "GUI", this, "DoSlider(Int_t)"); // Connecting "A_sld" silder with "Do_A_sld" method... A_sld->SetRange(0, 100); // Setting the range of the slider... hframe->AddFrame(A_sld, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4)); // Adding slider to hframe... fMain->AddFrame(hframe, new TGLayoutHints(kLHintsCenterX, 2, 2, 2, 2)); // Adding hframe to main frame... fMain->Connect("CloseWindow()", "GUI", this, "CloseApplication()"); // Closing the window will terminate the application... pad = new TPad("p1", "p1", 0.05, 0.05, 0.95, 0.95); // Creating a pad... watch = new TStopwatch(); /*-------------------------------------------------------------------------------------------------------------------------*/ /*----------------------------------------------------- CREATING A GRAPH --------------------------------------------------*/ /*-------------------------------------------------------------------------------------------------------------------------*/ graph = new TGraph2D(); // Creating new scatter plot graph... graph->SetLineColor(2); // Setting line color... graph->SetLineWidth(1); // Setting line width... graph->SetTitle("sin(x)"); // Setting graph title... graph->GetXaxis()->SetTitle("x"); // Setting x-axis title... graph->GetYaxis()->SetTitle("sin(x)"); // Setting y-axis title... graph->SetMarkerColor(4); // Setting marker color... graph->SetMarkerStyle(20); // Setting marker style... graph->GetXaxis()->SetLimits(0, 1); // Setting "X" limits... graph->SetMinimum(-1.5); // Setting "Y" minimum... graph->SetMaximum(1.5); // Setting "Y" maximum... /*-------------------------------------------------------------------------------------------------------------------------*/ /*----------------------------------------------------- DRAWING OBJECTS ---------------------------------------------------*/ /*-------------------------------------------------------------------------------------------------------------------------*/ fCanvas = fEcanvas->GetCanvas(); // Getting the canvas... fCanvas->Clear(); // Clearing the canvas... fCanvas->cd(); // Going into canvas... pad->Draw(); // Drawing pad... pad->cd(); // Going into pad... /*-------------------------------------------------------------------------------------------------------------------------*/ /*----------------------------------------------------- UPDATING OBJECTS --------------------------------------------------*/ /*-------------------------------------------------------------------------------------------------------------------------*/ fMain->MapSubwindows(); // Mapping all subwindows of the main frame... fMain->Resize(fMain->GetDefaultSize()); // Initializing the layout algorithm... fMain->MapWindow(); // Mapping main frame... } GUI::~GUI() { fMain->Cleanup(); // Cleaning up used widgets: frames, buttons, layout hints... delete fMain; // Deleting fMain... gApplication->Terminate(0); // Sending "Terminate" signal to the application... } void GUI::DoSlider(Int_t pos) { A = pos/100.0; // Calculating "Amplitude" [m]... } void GUI::Plot() { watch->Start(); phi_x += PHI; phi_y += PHI; if (phi_x > (2*PI - PHI)) { phi_x = 0; } if (phi_y > (2*PI - PHI)) { phi_y = 0; } graph->Clear(); // Clearing the graph... for (i = 0; i < N; i++) { for (j = 0; j < M; j++) { k = j + i*N; // Calulating index [#]... x = i; // Calculating "x" values... y = j; // Calculating "y" values... z = A*sin(6.28*i/M + phi_x)*cos(6.28*j/N + phi_y); // Calculating "z" values... graph->SetPoint(k, x, y, z); // Updating graph points... } } printf("Elapsed time (computation) [s] = %lf\n", watch->CpuTime()); watch->Start(); //fCanvas->SetEditable(kTRUE); // Enabling canvas editing... --> DON'T KNOW IF I HAVE TO PUT IT OR NOT... graph->Draw("surf1"); // Drawing the graph... fCanvas->Modified(); // Setting "Modified" bit... fCanvas->Update(); // Updating the canvas... --> DON'T KNOW IF I HAVE TO PUT IT OR NOT... //fCanvas->SetEditable(kFALSE); // Disabling canvas editing... printf("Elasped time (visualization) [s] = %lf\n", watch->CpuTime()); } void GUI::CloseApplication() { gApplication->Terminate(0); // Sending "Terminate" signal to the application... } const unsigned WIDTH = 640; // Window width [px]. const unsigned int HEIGHT = 480; // Window height [px]. int Graph() { TTimer *PlotTimer; // Timer for plotting graphs. //TApplication *Application; // Application. --> COMMENTED IN THIS MACRO GUI *GUI; // GUI. //Application = new class TApplication("TestGraph", &argc, argv); // Instantiating Application... --> COMMENTED IN THIS MACRO GUI = new class GUI(gClient->GetRoot(), WIDTH, HEIGHT); // Instantiating GUI... PlotTimer = new class TTimer(20); // Instantiating PlotTimer [ms]... PlotTimer->Connect("Timeout()", "GUI", GUI, "Plot()"); // Connecting PlotTimer->Timeout() to GUI->Plot()... PlotTimer->TurnOn(); // Starting PlotTimer... //Application->Run(); // Running the application... --> COMMENTED IN THIS MACRO return 0; // Returning SUCCESS... }