ToolHelper.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00026 #include "ToolHelper.h"
00027 #include "Tool.h"
00028 #include "GLPreviewFrame.h"
00029 #include "GLViewer.h"
00030 #include "MeshManager.h"
00031 
00032 ToolHelper::ToolHelper(PT::Panorama *pano_in,
00033                                      VisualizationState *visualization_state_in,
00034                                      GLPreviewFrame *frame_in)
00035 {
00036     pano = pano_in;
00037     visualization_state = visualization_state_in;
00038     frame = frame_in;
00039     images_under_mouse_current = false;
00040     mouse_over_pano = false;
00041     mouse_screen_x = 0;
00042     mouse_screen_y = 0;
00043     mouse_pano_x = 0;
00044     mouse_pano_y = 0;
00045 }
00046 
00047 ToolHelper::~ToolHelper()
00048 {
00049 }
00050 
00051 std::set<Tool *> ToolHelper::ActivateTool(Tool *tool)
00052 {
00053     tools_deactivated.clear();
00054     tool->Activate();
00055     return tools_deactivated;
00056 }
00057 
00058 void ToolHelper::DeactivateTool(Tool *tool)
00059 {
00060     tools_deactivated.insert(tool);
00061     // To deactivate it we need to give up all of its notifications.
00062     RemoveTool(tool, &mouse_button_notified_tools);
00063     RemoveTool(tool, &keypress_notified_tools);
00064     RemoveTool(tool, &mouse_move_notified_tools);
00065     RemoveTool(tool, &draw_under_notified_tools);
00066     RemoveTool(tool, &draw_over_notified_tools);
00067     RemoveTool(tool, &image_draw_begin_tools);
00068     RemoveTool(tool, &image_draw_end_tools);
00069     RemoveTool(tool, &images_under_mouse_notified_tools);
00070     RemoveTool(tool, &really_draw_over_notified_tools);
00071 }
00072 
00073 void ToolHelper::MouseMoved(int x, int y, wxMouseEvent & e)
00074 {
00075     mouse_screen_x = x;
00076     mouse_screen_y = y;
00077     // now tell tools that want notification.
00078     std::set<Tool *>::iterator iterator;
00079     for (iterator = mouse_move_notified_tools.begin();
00080          iterator != mouse_move_notified_tools.end(); iterator++)
00081     {
00082         (*iterator)->MouseMoveEvent(mouse_screen_x, mouse_screen_y, e);
00083     }
00084     // If the mouse has moved, then we don't know what is underneath it anoymore
00085     InvalidateImagesUnderMouse();
00086 }
00087 
00088 void ToolHelper::InvalidateImagesUnderMouse()
00089 {
00090     images_under_mouse_current = false;
00091     // if there are tools that want to know when the images under the mouse
00092     // pointer change, we better detect the images and notfiy them is applicable
00093     if (!images_under_mouse_notified_tools.empty())
00094     {
00095         // there are tools that want to know... so find out:
00096         std::set<unsigned int> old_images_under_mouse = images_under_mouse;
00097         UpdateImagesUnderMouse();
00098         if (old_images_under_mouse != images_under_mouse)
00099         {
00100             // The list has changed. Notifiy all tools that requested it.
00101             std::set<Tool *>::iterator iterator;
00102             for (iterator = images_under_mouse_notified_tools.begin();
00103                  iterator != images_under_mouse_notified_tools.end();
00104                  iterator++)
00105             {
00106                 (*iterator)->ImagesUnderMouseChangedEvent();
00107             }
00108         }
00109     }
00110 }
00111 
00112 void ToolHelper::MouseButtonEvent(wxMouseEvent &e)
00113 {
00114 //    // if there is a tool monitoring mouse button presses, notify it
00115 //    if (mouse_button_notified_tool)
00116 //    {
00117 //        mouse_button_notified_tool->MouseButtonEvent(e);
00118 //    }
00119     std::set<Tool *>::iterator iterator;
00120     for (iterator = mouse_button_notified_tools.begin();
00121          iterator != mouse_button_notified_tools.end(); iterator++)
00122     {
00123         (*iterator)->MouseButtonEvent(e);
00124     }
00125 
00126 }
00127 
00128 void ToolHelper::MouseWheelEvent(wxMouseEvent &e)
00129 {
00130     std::set<Tool *>::iterator iterator;
00131     for (iterator = mouse_wheel_notified_tools.begin();
00132          iterator != mouse_wheel_notified_tools.end(); iterator++)
00133     {
00134         (*iterator)->MouseWheelEvent(e);
00135     }
00136 
00137 }
00138 
00139 
00140 void ToolHelper::KeypressEvent(int keycode, int modifiers, bool pressed)
00141 {
00142 //    if (keypress_notified_tool)
00143 //    {
00144 //        keypress_notified_tool->KeypressEvent(keycode, modifiers, pressed);
00145 //    }
00146     std::set<Tool *>::iterator iterator;
00147     for (iterator = keypress_notified_tools.begin();
00148          iterator != keypress_notified_tools.end(); iterator++)
00149     {
00150         (*iterator)->KeypressEvent(keycode, modifiers, pressed);
00151     }
00152 
00153 }
00154 
00155 void ToolHelper::BeforeDrawImages()
00156 {
00157     // let all tools that want to draw under the images do so.
00158     std::set<Tool *>::iterator iterator;
00159     for (iterator = draw_under_notified_tools.begin();
00160          iterator != draw_under_notified_tools.end(); iterator++)
00161     {
00162         (*iterator)->BeforeDrawImagesEvent();
00163     }
00164     // Since we are drawing a new frame, lets assume something has changed,
00165     // however we want to keep with no images under the mouse if the mouse is
00166     // not on the panorama.
00167     if (mouse_over_pano)
00168     {
00169         InvalidateImagesUnderMouse();
00170     }
00171 }
00172 
00173 void ToolHelper::AfterDrawImages()
00174 {
00175 //    std::cerr << "begin" << std::endl;
00176     // let all tools that want to draw on top of the images do so.
00177     std::set<Tool *>::iterator iterator;
00178     for (iterator = draw_over_notified_tools.begin();
00179          iterator != draw_over_notified_tools.end(); iterator++)
00180     {
00181 //        std::cerr << "tool after draw" << std::endl;
00182         (*iterator)->AfterDrawImagesEvent();
00183     }
00184 //    std::cerr << "after draw images" << std::endl;
00185     // The overlays are done separetly to avoid errors with blending order.
00186     for (iterator = really_draw_over_notified_tools.begin();
00187          iterator != really_draw_over_notified_tools.end(); iterator++)
00188     {
00189         (*iterator)->ReallyAfterDrawImagesEvent();
00190     }
00191 //    std::cerr << "really after draw images" << std::endl;
00192 }
00193 
00194 bool ToolHelper::BeforeDrawImageNumber(unsigned int image)
00195 {
00196     if (image_draw_begin_tools.size() > image)
00197     {
00198         if (image_draw_begin_tools[image].size() > 0)
00199         {
00200             bool result = true;
00201             std::set<Tool *>::iterator it;
00202             for(it = image_draw_begin_tools[image].begin() ; it != image_draw_begin_tools[image].end() ; it++) {
00203                 result &= (*it)->BeforeDrawImageEvent(image);
00204             }
00205             return result;
00206 //            return image_draw_begin_tools[image]->BeforeDrawImageEvent(image);
00207         }
00208     }
00209     return true;
00210 }
00211 
00212 void ToolHelper::AfterDrawImageNumber(unsigned int image)
00213 {
00214     if (image_draw_end_tools.size() > image)
00215     {
00216         if (image_draw_end_tools[image].size() > 0)
00217         {
00218             std::set<Tool *>::iterator it;
00219             for(it = image_draw_end_tools[image].begin() ; it != image_draw_end_tools[image].end() ; it++) {
00220                 (*it)->BeforeDrawImageEvent(image);
00221             }
00222 //            image_draw_end_tools[image]->AfterDrawImageEvent(image);
00223         }
00224     }
00225 }
00226 
00227 void ToolHelper::MouseLeave()
00228 {
00229     // if the mouse leaves the preview, there are no images under the mouse
00230     // pointer anymore.
00231     mouse_over_pano = false;
00232     images_under_mouse.clear();
00233     images_under_mouse_current = true;
00234     if (!images_under_mouse_notified_tools.empty())
00235     {
00236         // notify tools that the set has changed.
00237         std::set<Tool *>::iterator iterator;
00238         for (iterator = images_under_mouse_notified_tools.begin();
00239              iterator != images_under_mouse_notified_tools.end();
00240              iterator++)
00241         {
00242             (*iterator)->ImagesUnderMouseChangedEvent();
00243         }        
00244     }
00245 }
00246 
00247 
00248 std::set<unsigned int> ToolHelper::GetImageNumbersUnderMouse()
00249 {
00250     if (!images_under_mouse_current)
00251     {
00252         UpdateImagesUnderMouse();
00253     }
00254     return images_under_mouse;
00255 }
00256 
00257 hugin_utils::FDiff2D ToolHelper::GetMouseScreenPosition()
00258 {
00259     return hugin_utils::FDiff2D(mouse_screen_x, mouse_screen_y);
00260 }
00261 
00262 hugin_utils::FDiff2D ToolHelper::GetMousePanoPosition()
00263 {
00264     return hugin_utils::FDiff2D(mouse_pano_x, mouse_pano_y);
00265 }
00266 
00267 VisualizationState *ToolHelper::GetVisualizationStatePtr()
00268 {
00269     return visualization_state;
00270 }
00271 
00272 ViewState *ToolHelper::GetViewStatePtr()
00273 {
00274     return visualization_state->getViewState();
00275 }
00276 
00277 PT::Panorama *ToolHelper::GetPanoramaPtr()
00278 {
00279     return pano;
00280 }
00281 
00282 void ToolHelper::NotifyMe(Event event, Tool *tool)
00283 {
00284     switch (event)
00285     {
00286         case MOUSE_MOVE:
00287             AddTool(tool, &mouse_move_notified_tools);
00288             break;
00289         case MOUSE_PRESS:
00290             AddTool(tool, &mouse_button_notified_tools);
00291             break;
00292         case MOUSE_WHEEL:
00293             AddTool(tool, &mouse_wheel_notified_tools);
00294             break;
00295         case KEY_PRESS:
00296             AddTool(tool, &keypress_notified_tools);
00297             break;
00298         case DRAW_UNDER_IMAGES:
00299             AddTool(tool, &draw_under_notified_tools);
00300             break;
00301         case DRAW_OVER_IMAGES:
00302             AddTool(tool, &draw_over_notified_tools);
00303             break;
00304         case IMAGES_UNDER_MOUSE_CHANGE:
00305             AddTool(tool, &images_under_mouse_notified_tools);
00306             break;
00307         case REALLY_DRAW_OVER_IMAGES:
00308             AddTool(tool, &really_draw_over_notified_tools);
00309             break;
00310     }   
00311 }
00312 
00313 void ToolHelper::NotifyMeBeforeDrawing(unsigned int image_nr,
00314                                               Tool *tool)
00315 {
00316     AddTool(tool, &image_draw_begin_tools, image_nr);
00317 }
00318 
00319 void ToolHelper::NotifyMeAfterDrawing(unsigned int image_nr,
00320                                              Tool *tool)
00321 {
00322     AddTool(tool, &image_draw_end_tools, image_nr);
00323 }
00324 
00325 void ToolHelper::DoNotNotifyMe(Event event, Tool *tool)
00326 {
00327     // TODO we should probably check that the tools have the notification they
00328     // are trying to give up, as misbehaving tools could break another tool.
00329     switch (event)
00330     {
00331         case MOUSE_MOVE:
00332             RemoveTool(tool, &mouse_move_notified_tools);
00333             break;
00334         case MOUSE_PRESS:
00335             RemoveTool(tool, &mouse_button_notified_tools);
00336             break;
00337         case MOUSE_WHEEL:
00338             RemoveTool(tool, &mouse_wheel_notified_tools);
00339             break;
00340         case KEY_PRESS:
00341             RemoveTool(tool, &keypress_notified_tools);
00342             break;
00343         case DRAW_UNDER_IMAGES:
00344             RemoveTool(tool, &draw_under_notified_tools);
00345             break;
00346         case DRAW_OVER_IMAGES:
00347             RemoveTool(tool, &draw_over_notified_tools);
00348             break;
00349         case IMAGES_UNDER_MOUSE_CHANGE:
00350             RemoveTool(tool, &images_under_mouse_notified_tools);
00351             break;
00352     }   
00353 }
00354 
00355 void ToolHelper::DoNotNotifyMeBeforeDrawing(unsigned int image_nr,
00356                                               Tool *tool)
00357 {
00358     RemoveTool(tool, &image_draw_begin_tools, image_nr);
00359 }
00360 
00361 void ToolHelper::DoNotNotifyMeAfterDrawing(unsigned int image_nr,
00362                                              Tool *tool)
00363 {
00364     RemoveTool(tool, &image_draw_end_tools, image_nr);
00365 }
00366 
00367 void ToolHelper::SetStatusMessage(wxString message)
00368 {
00369     // get the GLPreviewFrame to set its status bar's text.
00370     frame->SetStatusMessage(message);
00371 }
00372 
00373 
00374 // These functions remove tools from various things that keep pointers to them
00375 // so that they can be notified. They are called when we don't want to notify
00376 // them anymore.
00377 
00378 void ToolHelper::RemoveTool(Tool *tool, Tool **single)
00379 {
00380     if (*single == tool)
00381     {
00382         *single = 0;
00383     }
00384 }
00385 
00386 void ToolHelper::RemoveTool(Tool *tool,
00387                                    std::set<Tool *> *set)
00388 {
00389     std::set<Tool *>::iterator iterator = set->find(tool);
00390     if (iterator != set->end())
00391     {
00392         set->erase(iterator);
00393     }
00394 }
00395 
00396 void ToolHelper::RemoveTool(Tool *tool,
00397                                   std::vector<std::set<Tool *> > *vector)
00398 {
00399     // check every item for presence.
00400     for (unsigned int image = 0; image < vector->size(); image++)
00401     {
00402         (*vector)[image].erase(tool);
00403 //        if ((*vector)[image] == tool)
00404 //        {
00405 //            (*vector)[image] = 0;
00406 //        }
00407     }
00408 }
00409 
00410 void ToolHelper::RemoveTool(Tool *tool,
00411                                   std::vector<std::set<Tool *> > *vector,
00412                                   unsigned int index)
00413 {
00414     if ((*vector).size() > index)
00415     {
00416         (*vector)[index].erase(tool);
00417 //        if ((*vector)[index] == tool)
00418 //        {
00419 //            (*vector)[index] = 0;
00420 //        }
00421     }
00422 }
00423 
00424 void ToolHelper::AddTool(Tool *tool, Tool **single)
00425 {
00426     if (*single != 0 || *single == tool)
00427     {
00428         DeactivateTool(*single);
00429     }
00430     *single = tool;
00431 }
00432 
00433 void ToolHelper::AddTool(Tool *tool, std::set<Tool *> *set)
00434 {
00435     set->insert(tool);
00436 }
00437 
00438 void ToolHelper::AddTool(Tool *tool,
00439                                std::vector<std::set<Tool *> > *vector,
00440                                unsigned int index)
00441 {
00442     if (vector->size() < index + 1)
00443     {
00444         // increase the size of the vector to handle enough elements for index
00445         // to exist
00446         vector->resize(index + 1, std::set<Tool*>());
00447     }
00448 //    else if ((*vector)[index] && (*vector)[index] != tool)
00449 //    {
00450 //        // if a different tool already was doing this, deactivate it.
00451 //        DeactivateTool((*vector)[index]);
00452 //    };
00453 //    (*vector)[index] = tool;
00454     (*vector)[index].insert(tool);
00455 }
00456 
00457 
00458 void PreviewToolHelper::MouseMoved(int x, int y, wxMouseEvent &e)
00459 {
00460     mouse_over_pano = true;
00461     mouse_screen_x = x;
00462     mouse_screen_y = y;
00463     // work out where the pointer is in the panorama.
00464     vigra::Rect2D visible = visualization_state->GetVisibleArea();
00465     mouse_pano_x = (double) x / visualization_state->GetScale() + (double) visible.left();
00466     mouse_pano_y = (double) y / visualization_state->GetScale() + (double) visible.top();
00467     // now tell tools that want notification.
00468     std::set<Tool *>::iterator iterator;
00469     for (iterator = mouse_move_notified_tools.begin();
00470          iterator != mouse_move_notified_tools.end(); iterator++)
00471     {
00472         (*iterator)->MouseMoveEvent(mouse_pano_x, mouse_pano_y, e);
00473     }
00474     // If the mouse has moved, then we don't know what is underneath it anoymore
00475     InvalidateImagesUnderMouse();
00476 }
00477 
00478 void PreviewToolHelper::UpdateImagesUnderMouse()
00479 {
00480     // We want to find out which images cover the point underneath the mouse
00481     // pointer.
00482     images_under_mouse.clear();
00483     if (IsMouseOverPano()) {
00484         unsigned int num_images = pano->getNrOfImages();
00485         std::set<unsigned int> displayedImages = pano->getActiveImages();
00486         for (unsigned int image_index = 0; image_index < num_images; image_index++)
00487         {
00488             // don't try any images that are turned off
00489             if (displayedImages.count(image_index))
00490             {
00491                 // work out if the image covers the point under the mouse.
00492                 HuginBase::PTools::Transform transform;
00493                 transform.createTransform(*visualization_state->GetSrcImage(image_index),
00494                                           *visualization_state->GetOptions());
00495                 double image_x, image_y;
00496                 transform.transformImgCoord(image_x, image_y, mouse_pano_x, mouse_pano_y);
00497                 if (visualization_state->getViewState()->GetSrcImage(image_index)->isInside(vigra::Point2D(
00498                                                       int(image_x), int (image_y))))
00499                 {
00500                     // this image is under the mouse, add it to the set.
00501                     images_under_mouse.insert(image_index);
00502                 }
00503             }
00504         }
00505     }
00506     images_under_mouse_current = true;
00507 }
00508 
00509 void PanosphereOverviewToolHelper::UpdateImagesUnderMouse()
00510 {
00511     images_under_mouse.clear();
00512     if (IsMouseOverPano()) {
00513         unsigned int num_images = pano->getNrOfImages();
00514         std::set<unsigned int> displayedImages = pano->getActiveImages();
00515         for (unsigned int image_index = 0; image_index < num_images; image_index++)
00516         {
00517             // don't try any images that are turned off
00518             if (displayedImages.count(image_index))
00519             {
00520                 // work out if the image covers the point under the mouse.
00521                 HuginBase::PTools::Transform transform;
00522                 transform.createTransform(*visualization_state->GetSrcImage(image_index),
00523                                           *visualization_state->GetOptions());
00524                 double image_x, image_y;
00525                 transform.transformImgCoord(image_x, image_y, mouse_pano_x, mouse_pano_y);
00526                 if (visualization_state->getViewState()->GetSrcImage(image_index)->isInside(vigra::Point2D(
00527                                                       int(image_x), int (image_y))))
00528                 {
00529                     // this image is under the mouse, add it to the set.
00530                     images_under_mouse.insert(image_index);
00531                 }
00532             }
00533         }
00534     }
00535     images_under_mouse_current = true;
00536 }
00537 
00538 void PanosphereOverviewToolHelper::MouseMoved(int x, int y, wxMouseEvent & e)
00539 {
00540     PanosphereOverviewVisualizationState * panostate = (PanosphereOverviewVisualizationState*) visualization_state;
00541 
00542     double d = panostate->getR();
00543     double r = panostate->getSphereRadius();
00544 
00545     int tcanv_w, tcanv_h;
00546     panostate->GetViewer()->GetClientSize(&tcanv_w,&tcanv_h);
00547 
00548     double canv_w, canv_h;
00549     canv_w = tcanv_w;
00550     canv_h = tcanv_h;
00551     
00552     double fov = panostate->getFOV();
00553 
00554     double fovy, fovx;
00555     if (canv_w > canv_h) {
00556         fovy = DEG_TO_RAD(fov);
00557         fovx = 2 * atan( tan(fovy / 2.0) * canv_w / canv_h);
00558     } else {
00559         fovx = DEG_TO_RAD(fov);
00560         fovy = 2 * atan( tan(fovx / 2.0) * canv_h / canv_w);
00561     }
00562 
00563     double ax = tan(fovx / 2.0) * d * (x / (canv_w / 2.0) - 1);
00564     double ay = tan(fovy / 2.0) * d * (y / (canv_h / 2.0) - 1);
00565 
00566     double a_limit = r * d / sqrt(d*d - r*r);
00567 
00568     if (ax*ax + ay*ay < a_limit*a_limit) {
00569 
00570         mouse_over_pano = true;
00571 
00572         double ta,tb,tc;
00573         ta = (ax*ax + ay*ay) / (d*d) + 1;
00574         tb = - 2 * (ax*ax + ay*ay) / d;
00575         tc = ax*ax + ay*ay - r*r;
00576 
00577         double pz = ( -tb + sqrt(tb*tb - 4*ta*tc) ) / ( 2 * ta );
00578         double px = ax * (d - pz) / d;
00579         double py = ay * (d - pz) / d;
00580 
00581         double pl = sqrt(px*px + py*py + pz*pz);
00582 
00583         double pang_yaw = -atan(px / pz);
00584         double pang_pitch = asin(py / pl);
00585 
00586         double ang_yaw = panostate->getAngX();
00587         double ang_pitch = panostate->getAngY();
00588 
00589         double x,y,z;
00590         x = sin(ang_yaw)*cos(ang_pitch);
00591         z = cos(ang_yaw)*cos(ang_pitch);
00592         y = sin(ang_pitch);
00593 
00594         Vector3 vec(x,y,z);
00595 
00596         Vector3 top(0,1,0);
00597 
00598         Vector3 ptop = vec.Cross(top).Cross(vec).GetNormalized();
00599 
00600         Vector3 tres;
00601         tres.x = ptop.x*(ptop.x * vec.x + ptop.y*vec.y + ptop.z*vec.z) + cos(pang_yaw) * (ptop.x * (-ptop.y * vec.y - ptop.z * vec.z) + vec.x*(ptop.y*ptop.y + ptop.z*ptop.z)) + sin(pang_yaw) * (-ptop.z*vec.y + ptop.y*vec.z);
00602         tres.y = ptop.y*(ptop.x * vec.x + ptop.y*vec.y + ptop.z*vec.z) + cos(pang_yaw) * (ptop.y * (-ptop.x * vec.x - ptop.z * vec.z) + vec.y*(ptop.x*ptop.x + ptop.z*ptop.z)) + sin(pang_yaw) * (-ptop.z*vec.x + ptop.x*vec.z);
00603         tres.z = ptop.z*(ptop.x * vec.x + ptop.y*vec.y + ptop.z*vec.z) + cos(pang_yaw) * (ptop.z * (-ptop.x * vec.x - ptop.y * vec.y) + vec.z*(ptop.x*ptop.x + ptop.y*ptop.y)) + sin(pang_yaw) * (-ptop.y*vec.x + ptop.x*vec.y);
00604 
00605         Vector3 pside = ptop.Cross(tres).GetNormalized();
00606         Vector3 res;
00607 
00608         res.x = pside.x*(pside.x * tres.x + pside.y*tres.y + pside.z*tres.z) + cos(pang_pitch) * (pside.x * (-pside.y * tres.y - pside.z * tres.z) + tres.x*(pside.y*pside.y + pside.z*pside.z)) + sin(pang_pitch) * (-pside.z*tres.y + pside.y*tres.z);
00609         res.y = pside.y*(pside.x * tres.x + pside.y*tres.y + pside.z*tres.z) + cos(pang_pitch) * (pside.y * (-pside.x * tres.x - pside.z * tres.z) + tres.y*(pside.x*pside.x + pside.z*pside.z)) + sin(-pang_pitch) * (-pside.z*tres.x + pside.x*tres.z);
00610         res.z = pside.z*(pside.x * tres.x + pside.y*tres.y + pside.z*tres.z) + cos(pang_pitch) * (pside.z * (-pside.x * tres.x - pside.y * tres.y) + tres.z*(pside.x*pside.x + pside.y*pside.y)) + sin(pang_pitch) * (-pside.y*tres.x + pside.x*tres.y);
00611 
00612         double yaw, pitch;
00613         pitch = asin(res.y);
00614         yaw = atan2(res.x , res.z);
00615 
00616         yaw += M_PI / 2.0;
00617         if (yaw < 0) yaw += 2 * M_PI;
00618         if (yaw > 2 * M_PI) yaw -= 2 * M_PI;
00619 
00620         pitch += M_PI / 2.0;
00621         pitch = M_PI - pitch;
00622 
00623         mouse_pano_x = yaw / (2 * M_PI) * panostate->GetOptions()->getWidth();
00624         mouse_pano_y = pitch / (M_PI) * panostate->GetOptions()->getHeight();
00625 
00627 
00628 //        cerr << "mouse " << mouse_pano_x << " " << mouse_pano_y << " ; " << canv_w << " " << canv_h);
00629 
00630 //        double t_mouse_pano_x = yaw M
00631 
00632     } else {
00633 
00634         mouse_over_pano = false;
00635     }
00636     
00637     ToolHelper::MouseMoved(x,y,e);
00638 }
00639 
00640 PanosphereOverviewToolHelper::PanosphereOverviewToolHelper(PT::Panorama *pano,
00641                   VisualizationState *visualization_state,
00642                   GLPreviewFrame * frame) : OverviewToolHelper(pano, visualization_state, frame) {}
00643 
00644 PanosphereOverviewToolHelper::~PanosphereOverviewToolHelper() {}
00645 
00646 void PanosphereOverviewToolHelper::NotifyMe(PanosphereOverviewEvent event, PanosphereOverviewTool * tool) {
00647     switch (event) {
00648         case DRAW_OVER_IMAGES_BACK:
00649             AddTool(tool, &draw_over_notified_tools_back);
00650         break;
00651         case DRAW_OVER_IMAGES_FRONT:
00652             AddTool(tool, &draw_over_notified_tools_front);
00653         break;
00654         case DRAW_UNDER_IMAGES_BACK:
00655             AddTool(tool, &draw_under_notified_tools_back);
00656         break;
00657         case DRAW_UNDER_IMAGES_FRONT:
00658             AddTool(tool, &draw_under_notified_tools_front);
00659         break;
00660     }
00661 }
00662 
00663 void PanosphereOverviewToolHelper::DoNotNotifyMe(PanosphereOverviewEvent event, PanosphereOverviewTool * tool) {
00664     switch (event) {
00665         case DRAW_OVER_IMAGES_BACK:
00666             RemoveTool(tool, &draw_over_notified_tools_back);
00667         break;
00668         case DRAW_OVER_IMAGES_FRONT:
00669             RemoveTool(tool, &draw_over_notified_tools_front);
00670         break;
00671         case DRAW_UNDER_IMAGES_BACK:
00672             RemoveTool(tool, &draw_under_notified_tools_back);
00673         break;
00674         case DRAW_UNDER_IMAGES_FRONT:
00675             RemoveTool(tool, &draw_under_notified_tools_front);
00676         break;
00677     }
00678 }
00679 
00680 void PanosphereOverviewToolHelper::BeforeDrawImagesBack()
00681 {
00682     std::set<Tool *>::iterator iterator;
00683     for (iterator = draw_under_notified_tools_back.begin();
00684          iterator != draw_under_notified_tools_back.end(); iterator++)
00685     {
00686         ((PanosphereOverviewTool*)(*iterator))->BeforeDrawImagesBackEvent();
00687     }
00688 }
00689 
00690 void PanosphereOverviewToolHelper::BeforeDrawImagesFront()
00691 {
00692     std::set<Tool *>::iterator iterator;
00693     for (iterator = draw_under_notified_tools_front.begin();
00694          iterator != draw_under_notified_tools_front.end(); iterator++)
00695     {
00696         ((PanosphereOverviewTool*)(*iterator))->BeforeDrawImagesFrontEvent();
00697     }
00698 }
00699 
00700 void PanosphereOverviewToolHelper::AfterDrawImagesBack()
00701 {
00702     std::set<Tool *>::iterator iterator;
00703     for (iterator = draw_over_notified_tools_back.begin();
00704          iterator != draw_over_notified_tools_back.end(); iterator++)
00705     {
00706         ((PanosphereOverviewTool*)(*iterator))->AfterDrawImagesBackEvent();
00707     }
00708 }
00709 
00710 void PanosphereOverviewToolHelper::AfterDrawImagesFront()
00711 {
00712     std::set<Tool *>::iterator iterator;
00713     for (iterator = draw_over_notified_tools_front.begin();
00714          iterator != draw_over_notified_tools_front.end(); iterator++)
00715     {
00716         ((PanosphereOverviewTool*)(*iterator))->AfterDrawImagesFrontEvent();
00717     }
00718 }
00719 
00720 void PanosphereOverviewToolHelper::DeactivateTool(Tool *tool)
00721 {
00722     ToolHelper::DeactivateTool(tool);
00723 
00724     RemoveTool(tool, &draw_under_notified_tools_back);
00725     RemoveTool(tool, &draw_under_notified_tools_front);
00726     RemoveTool(tool, &draw_over_notified_tools_back);
00727     RemoveTool(tool, &draw_over_notified_tools_front);
00728 }
00729 
00730 
00731 PlaneOverviewToolHelper::PlaneOverviewToolHelper(PT::Panorama *pano,
00732                   VisualizationState *visualization_state,
00733                   GLPreviewFrame * frame) : OverviewToolHelper(pano, visualization_state, frame) {}
00734 
00735 PlaneOverviewToolHelper::~PlaneOverviewToolHelper() {}
00736 
00737 void PlaneOverviewToolHelper::MouseMoved(int x, int y, wxMouseEvent & e)
00738 {
00739 
00740     PlaneOverviewVisualizationState * panostate = (PlaneOverviewVisualizationState*) visualization_state;
00741 
00742     double d = panostate->getR();
00743 
00744     int tcanv_w, tcanv_h;
00745     panostate->GetViewer()->GetClientSize(&tcanv_w,&tcanv_h);
00746 
00747     double canv_w, canv_h;
00748     canv_w = tcanv_w;
00749     canv_h = tcanv_h;
00750     
00751     double fov = panostate->getFOV();
00752 
00753     double fovy, fovx;
00754     if (canv_w > canv_h) {
00755         fovy = DEG_TO_RAD(fov);
00756         fovx = 2 * atan( tan(fovy / 2.0) * canv_w / canv_h);
00757     } else {
00758         fovx = DEG_TO_RAD(fov);
00759         fovy = 2 * atan( tan(fovx / 2.0) * canv_h / canv_w);
00760     }
00761 
00762     double vis_w, vis_h;
00763     vis_w = 2.0 * tan ( fovx / 2.0 ) * d;
00764     vis_h = 2.0 * tan ( fovy / 2.0 ) * d;
00765 
00766     //position of the mouse on the z=0 plane
00767     double prim_x, prim_y;
00768     prim_x = (double) x / canv_w * vis_w - vis_w / 2.0 + panostate->getX();
00769     prim_y = ((double) y / canv_h * vis_h - vis_h / 2.0 - panostate->getY());
00770 
00771 //    std::cout << "mouse ov" << plane_x << " " << plane_y << std::endl;
00772     plane_x = prim_x;
00773     plane_y = prim_y;
00774 
00775     double width, height;
00776     HuginBase::PanoramaOptions * opts = panostate->GetOptions();
00777     width = opts->getWidth();
00778     height = opts->getHeight();
00779 
00780     mouse_pano_x = prim_x / MeshManager::PlaneOverviewMeshInfo::scale * width + width / 2.0;
00781     mouse_pano_y = prim_y / MeshManager::PlaneOverviewMeshInfo::scale * width + height / 2.0;
00782 
00783 //    std::cout << "plane mouse " << mouse_pano_x << " " << mouse_pano_y << " ; " << prim_x << " " << prim_y << " ; " << width << " " << height << std::endl;
00784 //    cerr << "plane mouse " << mouse_pano_x << " " << mouse_pano_y);
00785 
00786     mouse_over_pano = true;
00787     
00788     ToolHelper::MouseMoved(x,y,e);
00789 }
00790 
00791 void PlaneOverviewToolHelper::UpdateImagesUnderMouse()
00792 {
00793     images_under_mouse.clear();
00794     if (IsMouseOverPano()) {
00795         unsigned int num_images = pano->getNrOfImages();
00796         std::set<unsigned int> displayedImages = pano->getActiveImages();
00797         for (unsigned int image_index = 0; image_index < num_images; image_index++)
00798         {
00799             // don't try any images that are turned off
00800             if (displayedImages.count(image_index))
00801             {
00802                 // work out if the image covers the point under the mouse.
00803                 HuginBase::PTools::Transform transform;
00804                 transform.createTransform(*visualization_state->GetSrcImage(image_index),
00805                                           *visualization_state->GetOptions());
00806                 double image_x, image_y;
00807                 transform.transformImgCoord(image_x, image_y, mouse_pano_x, mouse_pano_y);
00808                 if (visualization_state->getViewState()->GetSrcImage(image_index)->isInside(vigra::Point2D(
00809                                                       int(image_x), int (image_y))))
00810                 {
00811                     // this image is under the mouse, add it to the set.
00812                     images_under_mouse.insert(image_index);
00813                 }
00814             }
00815         }
00816     }
00817     images_under_mouse_current = true;
00818 
00819 }
00820 
00821 

Generated on 23 Nov 2014 for Hugintrunk by  doxygen 1.4.7