ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
qAnimation.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "qAnimation.h"
9 
10 // Local
11 #include "qAnimationDlg.h"
12 
13 // CORE_LIB
14 #include <CVTools.h>
15 
16 // CV_DB_LIB
17 #include <ecv2DViewportObject.h>
18 #include <ecvDisplayTools.h>
19 #include <ecvMaterialSet.h>
20 #include <ecvMesh.h>
21 #include <ecvPointCloud.h>
22 #include <ecvPolyline.h>
23 
24 // Qt
25 #include <QMainWindow>
26 #include <QtGui>
27 
28 typedef std::vector<cc2DViewportObject *> ViewPortList;
29 typedef std::vector<ccMesh *> MeshList;
30 
31 static void GetSelectedObjects(const ccHObject::Container &selectedEntities,
33  ccHObject::Container &targetObj) {
34  targetObj.clear();
35  for (ccHObject *object : selectedEntities) {
36  if (object->isKindOf(type)) {
37  targetObj.push_back(object);
38  continue;
39  }
40 
41  ccHObject::Container internalContainer;
42  object->filterChildren(internalContainer, true, type);
43  if (!internalContainer.empty()) {
44  targetObj.insert(targetObj.end(), internalContainer.begin(),
45  internalContainer.end());
46  }
47  }
48 }
49 
51  const ccHObject::Container &selectedEntities) {
52  ccHObject::Container targetObjContainer;
54  targetObjContainer);
55 
56  ViewPortList viewports;
57  for (auto obj : targetObjContainer) {
58  auto *viewport = dynamic_cast<cc2DViewportObject *>(obj);
59  if (viewport) {
60  viewports.push_back(viewport);
61  }
62  }
63  return viewports;
64 }
65 
67  const ccHObject::Container &selectedEntities) {
68  ccHObject::Container targetObjContainer;
69  GetSelectedObjects(selectedEntities, CV_TYPES::MESH, targetObjContainer);
70 
71  MeshList meshes;
72  for (auto obj : targetObjContainer) {
73  auto *mesh = dynamic_cast<ccMesh *>(obj);
74  if (mesh && mesh->hasTextures()) {
75  meshes.push_back(mesh);
76  }
77  }
78  return meshes;
79 }
80 
81 qAnimation::qAnimation(QObject *parent)
82  : QObject(parent),
83  ccStdPluginInterface(":/CC/plugin/qAnimation/info.json"),
84  m_action(nullptr) {}
85 
86 void qAnimation::onNewSelection(const ccHObject::Container &selectedEntities) {
87  if (m_action == nullptr) {
88  return;
89  }
90 
91  ViewPortList viewports = GetSelectedViewPorts(selectedEntities);
93 
94  if (viewports.size() >= 2 || !meshes.empty()) {
95  m_action->setEnabled(true);
96  m_action->setToolTip(getDescription());
97  } else {
98  m_action->setEnabled(false);
99  m_action->setToolTip(tr("%1\nAt least 2 viewports must be selected.")
100  .arg(getDescription()));
101  }
102 }
103 
104 QList<QAction *> qAnimation::getActions() {
105  // default action (if it has not been already created, it's the moment to do
106  // it)
107  if (!m_action) {
108  m_action = new QAction(getName(), this);
109  m_action->setToolTip(getDescription());
110  m_action->setIcon(getIcon());
111 
112  connect(m_action, &QAction::triggered, this, &qAnimation::doAction);
113  }
114 
115  return QList<QAction *>{m_action};
116 }
117 
118 // what to do when clicked.
119 void qAnimation::doAction() {
120  // m_app should have already been initialized by CC when plugin is loaded!
121  //(--> pure internal check)
122  assert(m_app);
123  if (!m_app) return;
124 
125  // get active GL window
126  QWidget *glWindow = m_app->getActiveWindow();
127  if (!glWindow) {
128  m_app->dispToConsole("No active 3D view!",
130  return;
131  }
132 
134 
136 
137  // store history texture files
138  std::vector<std::vector<std::string>> textureFileList;
139  for (auto &mesh : meshes) {
140  auto *materials = const_cast<ccMaterialSet *>(mesh->getMaterialSet());
141  std::vector<std::string> textureFiles;
142  if (materials) {
143  for (int i = 0; i < materials->size(); ++i) {
144  textureFiles.push_back(CVTools::FromQString(
145  materials->at(i)->getTextureFilename()));
146  }
147  }
148  textureFileList.push_back(textureFiles);
149  }
150 
151  Q_ASSERT((viewports.size() >= 2 ||
152  !meshes.empty())); // action will not be active unless we
153  // have at least 2 viewports or have meshes
154 
155  m_app->dispToConsole(QString("[qAnimation] Selected viewports: %1")
156  .arg(viewports.size()));
158  QString("[qAnimation] Selected meshes: %1").arg(meshes.size()));
159 
160  qAnimationDlg videoDlg(glWindow, m_app->getMainWindow());
161 
162  if (!videoDlg.init(viewports, meshes)) {
164  "Failed to initialize the plugin dialog (not enough memory?)",
166  return;
167  }
168 
169  videoDlg.exec();
170 
171  // restore
172  for (std::size_t i = 0; i < meshes.size(); ++i) {
173  auto &mesh = meshes[i];
174  if (mesh && mesh->getMaterialSet()) {
175  if (!mesh->updateTextures(textureFileList[i])) {
176  CVLog::Warning(QString("Restore texture for %1 failed!")
177  .arg(mesh->getName()));
178  };
179  }
180  }
182 
183  // Export trajectory (for debug)
184  if (videoDlg.exportTrajectoryOnExit() && videoDlg.getTrajectory()) {
185  ccPolyline *trajectory = new ccPolyline(*videoDlg.getTrajectory());
186  if (!trajectory) {
187  CVLog::Error("Not enough memory");
188  } else {
189  trajectory->setColor(ecvColor::yellow);
190  trajectory->showColors(true);
191  trajectory->setWidth(2);
192 
193  getMainAppInterface()->addToDB(trajectory);
194  }
195  }
196 }
int64_t CV_CLASS_ENUM
Type of object type flags (64 bits)
Definition: CVTypes.h:97
char type
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
static bool Error(const char *format,...)
Display an error dialog with formatted message.
Definition: CVLog.cpp:143
static std::string FromQString(const QString &qs)
Definition: CVTools.cpp:100
2D viewport object
virtual QString getName() const override
Returns (short) name (for menu entry, etc.)
virtual QString getDescription() const override
Returns long name/description (for tooltip, etc.)
virtual QIcon getIcon() const override
Returns icon.
virtual void showColors(bool state)
Sets colors visibility.
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
std::vector< ccHObject * > Container
Standard instances container (for children, etc.)
Definition: ecvHObject.h:337
Mesh (triangle) material.
Triangular mesh.
Definition: ecvMesh.h:35
Colored polyline.
Definition: ecvPolyline.h:24
void setColor(const ecvColor::Rgb &col)
Sets the polyline color.
Definition: ecvPolyline.h:81
void setWidth(PointCoordinateType width)
Sets the width of the line.
Standard ECV plugin interface.
virtual ecvMainAppInterface * getMainAppInterface()
A callback pointer to the main app interface for use by plugins.
ecvMainAppInterface * m_app
Main application interface.
static void UpdateScreen()
virtual QMainWindow * getMainWindow()=0
Returns main window.
virtual QWidget * getActiveWindow()=0
virtual const ccHObject::Container & getSelectedEntities() const =0
Returns currently selected entities ("read only")
virtual void addToDB(ccHObject *obj, bool updateZoom=false, bool autoExpandDBTree=true, bool checkDimensions=false, bool autoRedraw=true)=0
virtual void dispToConsole(QString message, ConsoleMessageLevel level=STD_CONSOLE_MESSAGE)=0
Dialog for qAnimation plugin.
Definition: qAnimationDlg.h:48
@ VIEWPORT_2D_OBJECT
Definition: CVTypes.h:141
@ MESH
Definition: CVTypes.h:105
constexpr Rgb yellow(MAX, MAX, 0)
static ViewPortList GetSelectedViewPorts(const ccHObject::Container &selectedEntities)
Definition: qAnimation.cpp:50
std::vector< cc2DViewportObject * > ViewPortList
Definition: qAnimation.cpp:28
std::vector< ccMesh * > MeshList
Definition: qAnimation.cpp:29
static void GetSelectedObjects(const ccHObject::Container &selectedEntities, CV_CLASS_ENUM type, ccHObject::Container &targetObj)
Definition: qAnimation.cpp:31
static MeshList GetSelectedMeshes(const ccHObject::Container &selectedEntities)
Definition: qAnimation.cpp:66