Fraxinus  17.12-rc3
An IGT application
cxViewContainerItem.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxViewContainerItem.h"
34 
35 #include <QApplication>
36 #include <QDesktopWidget>
37 #include "vtkRenderer.h"
38 #include "cxBoundingBox3D.h"
40 
41 namespace cx
42 {
43 
44 ViewItem::ViewItem(QString uid, QString name, QWidget *parent, vtkRenderWindowPtr renderWindow, QRect rect) :
45  QObject(parent),
46  mGeometry(rect),
47  mParent(parent)
48 {
49  mZoomFactor = -1.0;
50  mView = ViewLinkingViewContainerItem::create(this, renderWindow);
51  mView->clear();
52 }
53 
55 {
56 }
57 
58 void ViewItem::setZoomFactor(double factor)
59 {
60  if (similar(factor, mZoomFactor))
61  {
62  return;
63  }
64  mZoomFactor = factor;
65  emit resized(this->size());
66 }
67 
68 void ViewItem::setGeometry(const QRect &r)
69 {
70  mGeometry = r;
71  QSize parentSize = mParent->size();
72  double xMin = r.left()/(double)parentSize.width();
73  double xMax = (r.right() + 1)/(double)parentSize.width();
74  double yMin = (parentSize.height() - r.bottom() - 1) / (double)parentSize.height();
75  double yMax = (parentSize.height() - r.top()) / (double)parentSize.height();
76  this->getView()->getRenderer()->SetViewport(xMin, yMin, xMax, yMax);
77  emit resized(r.size());
78 }
79 
81 {
82  return transform(this->get_vpMs().inv(), this->getViewport());
83 }
84 
86 {
87  Vector3D center_vp = this->getViewport().center();
88  double scale = mZoomFactor / this->mmPerPix(); // double zoomFactor = 0.3; // real magnification
89  Transform3D S = createTransformScale(Vector3D(scale, scale, scale));
90  Transform3D T = createTransformTranslate(center_vp);// center of viewport in viewport coordinates
91  Transform3D M_vp_w = T * S; // first scale , then translate to center.
92  return M_vp_w;
93 }
94 
98 {
99  return DoubleBoundingBox3D(0, size().width(), 0, size().height(), 0, 0);
100 }
101 
102 double ViewItem::mmPerPix() const
103 {
104  // use mean mm/pix over entire screen. DONT use the height of the widget in mm,
105  // this is truncated to the nearest integer.
106  QDesktopWidget* desktop = dynamic_cast<QApplication*>(QApplication::instance())->desktop();
107  QWidget* screen = desktop->screen(desktop->screenNumber(mParent));
108  double r_h = (double) screen->heightMM() / (double) screen->geometry().height();
109  double r_w = (double) screen->widthMM() / (double) screen->geometry().width();
110  double retval = (r_h + r_w) / 2.0;
111  return retval;
112 }
113 
114 
115 } /* namespace cx */
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
virtual DoubleBoundingBox3D getViewport_s() const
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
virtual void setGeometry(const QRect &r)
vtkSmartPointer< class vtkRenderWindow > vtkRenderWindowPtr
virtual void setZoomFactor(double factor)
static ViewRepCollectionPtr create(ViewItem *base, vtkRenderWindowPtr renderWindow)
ViewItem(QString uid, QString name, QWidget *parent, vtkRenderWindowPtr renderWindow, QRect rect)
Transform3D createTransformTranslate(const Vector3D &translation)
virtual DoubleBoundingBox3D getViewport() const
virtual Transform3D get_vpMs() const
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
void resized(QSize size)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Vector3D center() const
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
ViewRepCollectionPtr getView()
virtual QSize size() const
Namespace for all CustusX production code.