Developer Documentation
TypeCamera.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 #include "TypeCamera.hh"
52 
55 
56 TypeCameraPlugin::TypeCameraPlugin() :
57  contextMenu_(0), showFrustumAction_(0)
58 {
59 
60 }
61 
63 
64  if ( OpenFlipper::Options::gui() ){
65  contextMenu_ = new QMenu(tr("CameraNode"));
66 
67  showFrustumAction_ = contextMenu_->addAction( tr("Show viewing frustum") );
68  showFrustumAction_->setCheckable(true);
69  showFrustumAction_->setChecked(false);
70  showFrustumAction_->setToolTip(tr("Visualize cameras viewing frustum."));
71  showFrustumAction_->setStatusTip( showFrustumAction_->toolTip() );
72 
73  QAction* flyAction = contextMenu_->addAction( tr("Fly to") );
74  flyAction->setToolTip(tr("Fly viewer to the camera position."));
75  flyAction->setStatusTip( flyAction->toolTip() );
76 
77  // Add context menu
78  emit addContextMenuItem(contextMenu_->menuAction(), DATA_CAMERA, CONTEXTOBJECTMENU);
79 
80  connect(contextMenu_, SIGNAL(triggered(QAction*)), this ,SLOT(contextMenuClicked(QAction*)));
81  }
82 }
83 
85 
86  if (_objectId < 0)
87  return;
88 
89  CameraObject* object;
90  if (!PluginFunctions::getObject(_objectId, object))
91  return;
92 
93  if (showFrustumAction_)
94  showFrustumAction_->setChecked(object->cameraNode()->showFrustum());
95 }
96 
97 void TypeCameraPlugin::contextMenuClicked(QAction* _contextAction) {
98 
99  QVariant contextObject = _contextAction->data();
100  int objectId = contextObject.toInt();
101 
102  if (objectId == -1)
103  return;
104 
105  CameraObject* object;
106  if (!PluginFunctions::getObject(objectId, object))
107  return;
108 
109  if (_contextAction->text() == tr("Show viewing frustum")) {
110 
111  // Set frustum flag to whether action is checked or not
112  object->cameraNode()->showFrustum(_contextAction->isChecked());
113 
114  emit updatedObject(objectId, UPDATE_VISIBILITY);
115  }
116  else if (_contextAction->text() == tr("Fly to")) {
117 
118  // calculate camera position and view direction in world space
119 
120  ACG::GLMatrixd m = object->cameraNode()->modelview();
121  ACG::GLMatrixd mInv = m;
122  mInv.invert();
123 
124  ACG::Vec3d camPosWS(mInv(0,3), mInv(1,3), mInv(2,3));
125  ACG::Vec3d camViewWS(mInv(0,2), mInv(1,2), mInv(2,2));
126 
127  ACG::Vec3d camCenterWS = camPosWS - camViewWS;
128 
129  // target up vector can't be specified unfortunately
130  PluginFunctions::flyTo(camPosWS, camCenterWS, 500.0);
131  }
132 
133 }
134 
135 bool TypeCameraPlugin::registerType() {
136 
137  addDataType ( "Camera",tr ( "Camera" ) );
138  setTypeIcon ( "Camera", "camera.png" );
139  return true;
140 }
141 
143 
144  // new object data struct
145  CameraObject* object = new CameraObject ();
146 
147  if ( OpenFlipperSettings().value("Core/File/AllTarget",false).toBool() )
148  object->target(true);
149  else {
150 
151  // Only the first object in the scene will be target
152  if ( PluginFunctions::objectCount() == 1 )
153  object->target(true);
154 
155  // If no target is available, we set the new object as target
156  if (PluginFunctions::targetCount() == 0 )
157  object->target(true);
158  }
159 
160  QString name = QString(tr("New Camera %1.cam").arg( object->id() ));
161 
162  // call the local function to update names
163  object->setName ( name );
164 
165  object->update();
166 
167  object->show();
168 
169  emit log ( LOGINFO, object->getObjectinfo() );
170 
171  emit emptyObjectAdded ( object->id() );
172 
173  return object->id();
174 }
175 
176 
#define DATA_CAMERA
Definition: Camera.hh:67
void pluginsInitialized()
Initialize Plugin step 2.
Definition: TypeCamera.cc:62
int objectCount()
Get the number of available objects.
bool getObject(const int _identifier, BaseObject *&_object)
Get the object which has the given identifier.
CameraNode * cameraNode()
Get the scenegraph Node.
int id() const
Definition: BaseObject.cc:190
DLLEXPORT DataType addDataType(QString _name, QString _readableName)
Adds a datatype and returns the id for the new type.
Definition: Types.cc:117
bool invert()
matrix inversion (returns true on success)
QString getObjectinfo()
Get all Info for the Object as a string.
DLLEXPORT void setTypeIcon(DataType _id, QString _icon)
Set an Icon for a given DataType.
Definition: Types.cc:223
const UpdateType UPDATE_VISIBILITY(UpdateTypeSet(1)<< 1)
This is the update identifier for global Object visibility ( show/hide )
int targetCount()
Get the number of target objects.
void showFrustum(bool _showFrustum)
Set if viewing frustum should be shown or not.
Definition: CameraNode.hh:148
void slotUpdateContextMenu(int _objectId)
Definition: TypeCamera.cc:84
DLLEXPORT OpenFlipperQSettings & OpenFlipperSettings()
QSettings object containing all program settings of OpenFlipper.
int addEmpty()
Create an empty object.
Definition: TypeCamera.cc:142
The Menu will be shown when an object was picked.
void flyTo(const ACG::Vec3d &_position, const ACG::Vec3d &_center, double _time)
Fly to point and viewing direction (animated).