view.C

Go to the documentation of this file.
00001 /* Gxsm - Gnome X Scanning Microscopy
00002  * universal STM/AFM/SARLS/SPALEED/... controlling and
00003  * data analysis software
00004  * 
00005  * Copyright (C) 1999,2000,2001,2002,2003 Percy Zahl
00006  *
00007  * Authors: Percy Zahl <zahl@users.sf.net>
00008  * additional features: Andreas Klust <klust@users.sf.net>
00009  * WWW Home: http://gxsm.sf.net
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00024  */
00025 
00026 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 8 c-style: "K&R" -*- */
00027 
00028 #define XSHMIMG_MAXZOOM 10
00029 
00030 
00031 #include <locale.h>
00032 #include <libintl.h>
00033 
00034 #include "view.h"
00035 #include "mem2d.h"
00036 #include "xsmmasks.h"
00037 #include "glbvars.h"
00038 #include "scan_event.h"
00039 
00040 #include "bench.h"
00041 #include "util.h"
00042 
00043 #include "app_view.h"
00044 
00045 //==================================================
00046 // Posthandler
00047 //==================================================
00048 
00049 
00050 #define L_NULL    0
00051 #define L_START   1
00052 #define L_CHANGE  2
00053 #define L_MOVE    3
00054 #define L_END     4
00055 
00056 #define QSIZE   5  // Size of Line-Handles (Squares)
00057 
00058 //==================================================
00059 // View Class
00060 //==================================================
00061 
00062 View::View(){
00063   XSM_DEBUG (DBG_L2, "View::View");
00064   scan=NULL;
00065   data=NULL;
00066   mem2d=NULL;
00067 
00068   trace=NULL;
00069   reset_trace();
00070   abs_move_tip(0.,0.);
00071 }
00072 
00073 View::View(Scan *sc, int ChNo){
00074   XSM_DEBUG (DBG_L2, "View::View(m2d)");
00075   scan  = sc;
00076   data  = &scan->data;
00077   //-  data  = scan->vdata;
00078   mem2d = scan->mem2d;
00079   ChanNo=ChNo;
00080 
00081   trace=NULL;
00082   reset_trace();
00083   abs_move_tip(scan->data.s.x0, scan->data.s.y0);
00084 }
00085 
00086 View::~View(){
00087   XSM_DEBUG (DBG_L2, "View::~View");
00088   hide();
00089 }
00090 
00091 void View::hide(){
00092   XSM_DEBUG (DBG_L2, "View::hide");
00093 }
00094 
00095 void View::update_mxyz_from_points (){
00096         /* plane with  ax +by +cz +d = 0 given by 3 points */
00097         double a, b, c, d;
00098         double x1, y1, z1, z2, x2, y2, z3, x3, y3;
00099         long i;
00100         int n_obj = scan->number_of_object ();
00101         
00102         Point2D p[3];
00103         
00104         // at least three...
00105         if (n_obj < 3) {
00106                 scan->mem2d-> SetPlaneMxyz ();
00107                 return;
00108         }
00109 
00110         // look for the three point objects 
00111         i=0;
00112         while (n_obj-- && i < 3){
00113                 scan_object_data *obj_data = scan->get_object_data (n_obj);
00114                 
00115                 if (strncmp (obj_data->get_name (), "Point", 5) )
00116                         continue; // only points are used!
00117                 
00118                 if (obj_data->get_num_points () != 1) 
00119                         continue; // sth. is weired!
00120 
00121                 // get real world coordinates of point
00122                 double x,y;
00123                 obj_data->get_xy_pixel (0, x, y); // get point 0 coordinates in pixels!!
00124                 p[i].x = (int)x; p[i].y = (int)y;
00125                 ++i;
00126         }               
00127 
00128         if(i != 3){
00129                 scan->mem2d-> SetPlaneMxyz ();
00130                 return;
00131         }
00132 
00133         /* PRODUCE IN-PLANE VECTORS */
00134         x3 = p[1].x;
00135         y3 = p[1].y;
00136         if (x3 < 0 || y3 < 0 || x3 > scan->mem2d->GetNx() || y3 > scan->mem2d->GetNy()) 
00137                 return;
00138         z3 = scan->mem2d->GetDataPkt((int)(x3), (int)(y3));
00139         
00140         x1 = p[0].x-x3;
00141         y1 = p[0].y-y3;
00142         if (p[0].x < 0 || p[0].y < 0 || p[0].x > scan->mem2d->GetNx() || p[0].y > scan->mem2d->GetNy()) 
00143                 return;
00144         z1 = (double)scan->mem2d->GetDataPkt((int)p[0].x, (int)p[0].y) - z3;
00145         
00146         x2 = p[2].x-x3;
00147         y2 = p[2].y-y3;
00148         if (p[0].x < 0 || p[0].y < 0 || p[2].x > scan->mem2d->GetNx() || p[2].y > scan->mem2d->GetNy()) 
00149                 return;
00150         z2 = (double)scan->mem2d->GetDataPkt((int)p[2].x, (int)p[2].y) - z3;
00151         
00152         /* a,b,c follow from cross-produkt of 1 and 2 */
00153         a = y1*z2 - z1*y2;
00154         b = z1*x2 - x1*z2;
00155         c = x1*y2 - y1*x2;
00156         
00157         /* d follows from one special point, use POINT 3 */
00158         d = -(a*x3 + b*y3 + c*z3);
00159         
00160         d /= c;
00161         a /= c;
00162         b /= c;
00163 
00164         scan->mem2d-> SetPlaneMxyz (a,b,d);
00165 }
00166 
00167 int View::draw(int zoomoverride){
00168   XSM_DEBUG (DBG_L2, "View Base: draw !");
00169   return 0;
00170 }
00171 
00172 int View::update(int y1, int y2){
00173   XSM_DEBUG (DBG_L2, "View Base: update !");
00174   return 0;
00175 }
00176 
00177 void View::abs_move_tip(double xa, double ya, int mode){
00178         Trace_Data *td = new Trace_Data;
00179         current_td.x = xa;
00180         current_td.y = ya;
00181         current_td.mode = mode;
00182         current_td.t = clock () - trace_t0;
00183         memcpy (td, &current_td, sizeof (Trace_Data));
00184         add_to_trace (td);
00185         update_trace ();
00186 }
00187 
00188 void View::rel_move_tip(double dxa, double dya, int mode){
00189         current_td.x += dxa;
00190         current_td.y += dya;
00191         abs_move_tip(current_td.x, current_td.y, mode);
00192 }
00193 
00194 void View::update_position(Trace_Data *td){
00195         memcpy (&current_td, td, sizeof (Trace_Data));  
00196 }
00197 
00198 void View::save_trace(){
00199         g_slist_foreach((GSList*) trace, 
00200                         (GFunc) View::print_td, this);
00201 }
00202 
00203 void View::print_trace(){
00204         g_slist_foreach((GSList*) trace, 
00205                         (GFunc) View::print_td, this);
00206 }
00207 
00208 void View::add_to_trace(Trace_Data *td){
00209         g_slist_prepend (trace, td);
00210 }
00211 
00212 void View::print_td(Trace_Data *td, gpointer data){
00213         XSM_DEBUG(DBG_L1, "("
00214              << ((double)td->t/CLOCKS_PER_SEC) << ": " 
00215              << td->x << ", " 
00216              << td->y << ", " 
00217              << td->z << ", " 
00218              << td->v[0] << ", " 
00219              << td->mode
00220              << ")" );
00221 }
00222 
00223 void View::reset_trace(){
00224         if (trace){
00225                 g_slist_foreach((GSList*) trace, 
00226                                 (GFunc) View::delete_td, this);
00227                 g_slist_free(trace);
00228                 trace = NULL;
00229         }
00230         trace_t0 = clock ();
00231 }
00232 
00233 
00234 // ============================== Class Grey2D ==============================
00235 
00236 Grey2D::Grey2D(Scan *sc, int ChNo):View(sc, ChNo){
00237   XSM_DEBUG (DBG_L2, "Grey2D::Grey2D");
00238   XImg = NULL;
00239   oMC=oZ=oQ=oXPMx=oXPMy=oVm=0;
00240   viewcontrol=NULL;
00241   userzoom = FALSE;
00242 }
00243 
00244 Grey2D::Grey2D():View(){
00245   oMC=oZ=oQ=oXPMx=oXPMy=oVm=0;
00246   XImg = NULL;
00247   viewcontrol=NULL;
00248   userzoom = FALSE;
00249 }
00250 
00251 Grey2D::~Grey2D(){
00252   XSM_DEBUG (DBG_L2, "Grey2D::~");
00253   hide();
00254 }
00255 
00256 void Grey2D::hide(){
00257   XSM_DEBUG (DBG_L2, "Grey2D::hide");
00258   if(viewcontrol)
00259     delete viewcontrol;
00260   viewcontrol=NULL;
00261   XImg=NULL;
00262 }
00263 
00264 int Grey2D::update(int y1, int y2){
00265         if (!XImg) 
00266                 return 0;
00267 
00268         if (y2 < y1){
00269                 if (ChanNo == gapp->xsm->ActiveChannel)
00270                         viewcontrol->SetActive (TRUE);
00271                 else
00272                         viewcontrol->SetActive (FALSE);
00273                 
00274                 XImg->ShowPic (0,0);
00275                 return 0;
00276         }
00277         int j,k;
00278         int nx;
00279 
00280         if ((y1%QuenchFac) == 0){
00281                 nx=mem2d->GetNx ();
00282                 for (j=y1; j<y2; j+=QuenchFac)
00283                         for (k=0; k<nx; k+=QuenchFac)
00284                                 XImg->PutPixel ((unsigned long)(k/QuenchFac),(unsigned long)(j/QuenchFac),
00285                                                mem2d->GetDataVMode (k,j));
00286                 
00287                 XImg->ShowSubPic(0,y1/QuenchFac,nx/QuenchFac,(y2+QuenchFac-y1-1)/QuenchFac);
00288         }
00289         
00290         // Show Red Line ...
00291         --y2;
00292         if (y1 == y2){
00293                 scan->Pkt2dScanLine[0].x = 0;
00294                 scan->Pkt2dScanLine[0].y = y1;
00295                 scan->Pkt2dScanLine[1].x = mem2d->GetNx () - 1;
00296                 scan->Pkt2dScanLine[1].y = y1;
00297                 viewcontrol -> CheckRedLine ();
00298         }
00299         return 0;
00300 }
00301 
00302 void Grey2D::add_object(int type, gpointer data){
00303         double *xy = (double*) data;
00304         scan -> realloc_pkt2d ((int)xy[0]);
00305         viewcontrol -> AddObject(
00306                 new VObPolyLine (viewcontrol->GetCanvas(),
00307                                  xy, scan->Pkt2d,
00308                                  FALSE, VOBJ_COORD_ABSOLUT)
00309                 );
00310 }
00311 
00312 void Grey2D::update_events(){
00313         if (viewcontrol) 
00314                 viewcontrol->events_update (); 
00315 }
00316 
00317 void Grey2D::update_event_info(ScanEvent* se){
00318         viewcontrol->update_event_panel (se);
00319 }
00320 
00321 
00322 
00323 void Grey2D::ZoomIn(){
00324   if(QuenchFac>1)
00325     --QuenchFac;
00326   else
00327     ++ZoomFac;
00328   draw(TRUE);
00329 }
00330 
00331 void Grey2D::ZoomOut(){
00332   if(ZoomFac>1)
00333     --ZoomFac;
00334   else
00335     ++QuenchFac;
00336   draw(TRUE);
00337 }
00338 
00339 int Grey2D::SetZF(int zf, int qf, Grey2D *p){
00340   if(!zf){
00341     p->ZoomIn();
00342     return 0;
00343   }
00344   if(!qf){
00345     p->ZoomOut();
00346     return 0;
00347   }
00348   p->ZoomFac = zf;
00349   p->QuenchFac = qf;
00350   p->userzoom = TRUE;
00351   p->draw();
00352   return 0;
00353 }
00354 
00355 void Grey2D::setup_data_transformation(){
00356         if(gapp->xsm->ZoomFlg & VIEW_COLOR)
00357                 MaxColor = 1024;
00358         else
00359                 MaxColor = 64;
00360         
00361         update_mxyz_from_points ();
00362         mem2d->SetDataPktMode (data->display.ViewFlg);
00363         mem2d->SetDataRange (0, MaxColor-1);
00364 }
00365 
00366 
00367 int Grey2D::draw(int zoomoverride){
00368   int nx,ny;
00369   gchar *titel=NULL;
00370   
00371   int NewFlg=0;
00372 
00373   if(!mem2d) { 
00374     XSM_DEBUG (DBG_L2, "Grey2D: no mem2d !"); 
00375     return 1; 
00376   }
00377 
00378   nx=mem2d->GetNx();
00379   ny=mem2d->GetNy();
00380 
00381   XSM_DEBUG (DBG_L2, "NX: " << nx << " NY: " << ny);
00382   if(nx<1 || ny<1) return 1;
00383 
00384   if(!zoomoverride && !userzoom){
00385     ZoomFac = 1;
00386     QuenchFac = 1;
00387     if(gapp->xsm->ZoomFlg & VIEW_ZOOM){
00388       if(gapp->xsm->ZoomFlg & VIEW_Z400)
00389         ZoomFac   = 400/nx;
00390       if(gapp->xsm->ZoomFlg & VIEW_Z600)
00391         ZoomFac   = 600/nx;
00392       QuenchFac = nx/400;
00393       
00394       if(ZoomFac   < 1 )
00395         ZoomFac = 1;
00396       if(ZoomFac > XSHMIMG_MAXZOOM )
00397         ZoomFac = XSHMIMG_MAXZOOM;
00398       if(QuenchFac < 1 )
00399         QuenchFac = 1;
00400     }
00401   }
00402   XSM_DEBUG (DBG_L2, "Grey2D::draw ZoomFac: " << ZoomFac << " QuenchFac: " << QuenchFac);
00403   
00404   // hier war das Problem....
00405   XPM_x = nx*ZoomFac/QuenchFac + (QuenchFac > 1 ? 1:0);
00406   XPM_y = ny*ZoomFac/QuenchFac + (QuenchFac > 1 ? 1:0);
00407 
00408   if(oXPMx != XPM_x || oXPMy != XPM_y || oQ != QuenchFac || oZ != ZoomFac 
00409      || oMC != MaxColor || (oVm & VIEW_INFO) != (gapp->xsm->ZoomFlg & VIEW_INFO) ){
00410     if(!zoomoverride){
00411       oXPMx=XPM_x;
00412       oXPMy=XPM_y;
00413     }
00414     oZ=ZoomFac;
00415     oQ=QuenchFac;
00416     oMC=MaxColor;
00417     oVm=gapp->xsm->ZoomFlg;
00418     NewFlg  = 1;
00419   }
00420 
00421   titel = g_strdup_printf("Ch%d:%d%s %s Q%d/%d %s", 
00422                           ChanNo+1, mem2d->GetLayer()+1,
00423                           strcmp(data->ui.originalname, "unknown (not saved)") == 0 ? " *NOT-SAVED* :" : ":",
00424                           data->ui.name,ZoomFac,QuenchFac, mem2d->GetEname());
00425 
00426   if(NewFlg){ // neue Größe ?
00427     XSM_DEBUG (DBG_L2, "Grey2D::draw ResizeVC");
00428     if(viewcontrol)
00429       viewcontrol->Resize(titel, XPM_x , XPM_y, ChanNo, scan, ZoomFac, QuenchFac);
00430     NewFlg=0;
00431   }
00432 
00433   if(!viewcontrol){
00434     XSM_DEBUG (DBG_L2, "Grey2D::draw NewVC");
00435     viewcontrol =  new ViewControl(titel, XPM_x , XPM_y, ChanNo, scan, ZoomFac, QuenchFac);
00436     viewcontrol->SetZoomQFkt(SetZF, this);
00437     viewcontrol->show();
00438     XImg = viewcontrol->GetXImg();
00439   }
00440 
00441   if(ChanNo == gapp->xsm->ActiveChannel)
00442     viewcontrol->SetActive(TRUE);
00443   else
00444     viewcontrol->SetActive(FALSE);
00445 
00446   if(gapp->xsm->ZoomFlg & VIEW_PALETTE)
00447     XImg->MkPalette(xsmres.Palette);
00448   else
00449     XImg->MkPalette();
00450     
00451   MaxColor=XImg->GetMaxCol();
00452 
00453 //  XSM_DEBUG (DBG_L2, "Grey2D::draw update 0..ny=" << ny << " MaxCol=" << MaxColor);
00454   BenchStart(vbm,"View Update All",nx*ny);
00455   update(0, ny);
00456   BenchStop(vbm);
00457 
00458   viewcontrol->SetTitle(titel);  
00459   g_free(titel);
00460 
00461   XSM_DEBUG (DBG_L5,"Grey2D::draw ret");
00462 
00463   return 0;
00464 }
00465 
00466 void Grey2D::show_tip(){
00467 }
00468 
00469 void Grey2D::hide_tip(){
00470 }
00471 
00472 
00473 void Grey2D::show_trace(){
00474         update_trace();
00475 }
00476 
00477 void Grey2D::hide_trace(){
00478         viewcontrol->remove_trace ();
00479 }
00480 
00481 void Grey2D::update_trace(){
00482 //      viewcontrol->update_trace (double *xy, int len);
00483 }
00484 
00485 void Grey2D::remove_events(){
00486         if (viewcontrol){
00487                 viewcontrol->RemoveEventObjects ();
00488                 scan->mem2d->RemoveScanEvents ();
00489         }
00490 }

Generated on Sat Apr 1 09:04:05 2006 for GXSM by  doxygen 1.4.6