qTransformsPlugin.cpp 13.8 KB
Newer Older
Sören Schwertfeger's avatar
Sören Schwertfeger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//##########################################################################
//#                                                                        #
//#                       CLOUDCOMPARE PLUGIN: qDummy                      #
//#                                                                        #
//#  This program is free software; you can redistribute it and/or modify  #
//#  it under the terms of the GNU General Public License as published by  #
//#  the Free Software Foundation; version 2 of the License.               #
//#                                                                        #
//#  This program is distributed in the hope that it will be useful,       #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
//#  GNU General Public License for more details.                          #
//#                                                                        #
//#                             COPYRIGHT: Sören Schwertfeg                #
//#                                                                        #
//##########################################################################

//First: replace all occurrences of 'qDummyPlugin' by your own plugin class name in this file!
#include "qTransformsPlugin.h"

//Qt
#include <QtGui>
Sören Schwertfeger's avatar
Sören Schwertfeger committed
23
#include <QFileDialog>
Sören Schwertfeger's avatar
Sören Schwertfeger committed
24
#include <QInputDialog>
Sören Schwertfeger's avatar
Sören Schwertfeger committed
25

Sören Schwertfeger's avatar
Sören Schwertfeger committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include "sttl.h"

//Default constructor: should mainly be used to initialize
//actions (pointers) and other members
qTransformsPlugin::qTransformsPlugin(QObject* parent/*=0*/)
	: QObject(parent)
	, m_action(0)
{
}

//This method should enable or disable each plugin action
//depending on the currently selected entities ('selectedEntities').
//For example: if none of the selected entities is a cloud, and your
//plugin deals only with clouds, call 'm_action->setEnabled(false)'
void qTransformsPlugin::onNewSelection(const ccHObject::Container& selectedEntities)
{
	//if (m_action)
	//	m_action->setEnabled(!selectedEntities.empty());
}

//This method returns all 'actions' of your plugin.
//It will be called only once, when plugin is loaded.
void qTransformsPlugin::getActions(QActionGroup& group)
{
	//default action (if it has not been already created, it's the moment to do it)
	if (!m_action)
	{
		//here we use the default plugin name, description and icon,
		//but each action can have its own!
		m_action = new QAction(getName(),this);
		m_action->setToolTip(getDescription());
		m_action->setIcon(getIcon());
		//connect appropriate signal
		connect(m_action, SIGNAL(triggered()), this, SLOT(doAction()));
Sören Schwertfeger's avatar
Sören Schwertfeger committed
61
62
63
64
65
66
67
68
69
70
71
72
73

		m_actionLoadTransform = new QAction("Load Transforms...",this);
		m_actionLoadTransform->setToolTip("Load transforms from a JSON file and apply to the visible point clouds.");
		m_actionLoadTransform->setIcon(getIcon());
		//connect appropriate signal
		connect(m_actionLoadTransform, SIGNAL(triggered()), this, SLOT(loadTransforms()));

		m_actionSaveTransform = new QAction("Save Transforms...",this);
		m_actionSaveTransform->setToolTip("Save transforms to a JSON file.");
		m_actionSaveTransform->setIcon(getIcon());
		//connect appropriate signal
		connect(m_actionSaveTransform, SIGNAL(triggered()), this, SLOT(saveTransforms()));

Sören Schwertfeger's avatar
Sören Schwertfeger committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
		m_changeUserAction = new QAction("Change User Name: ",this);
		m_changeUserAction->setToolTip("Change the user name...");
		m_changeUserAction->setIcon(getIcon());
		connect(m_changeUserAction, SIGNAL(triggered()), this, SLOT(changeUser()));
	
		m_changeMethodAction = new QAction("Change Registration Method: ",this);
		m_changeMethodAction->setToolTip("Change the method used for registration...");
		m_changeMethodAction->setIcon(getIcon());
		connect(m_changeMethodAction, SIGNAL(triggered()), this, SLOT(changeMethod()));

		m_changeDetailsAction = new QAction("Change Method Details",this);
		m_changeDetailsAction->setToolTip("Change some details about the registration...");
		m_changeDetailsAction->setIcon(getIcon());
		connect(m_changeDetailsAction, SIGNAL(triggered()), this, SLOT(changeDetails()));

		m_frameNameLength = 8;
		QString txt("Change Frame Name Length: %1");
		txt = txt.arg(m_frameNameLength);

		m_changeFrameNameLengthAction = new QAction(txt,this);
		m_changeFrameNameLengthAction->setToolTip("Change some details about the registration...");
		m_changeFrameNameLengthAction->setIcon(getIcon());
		connect(m_changeFrameNameLengthAction, SIGNAL(triggered()), this, SLOT(changeFrameNameLength()));

Sören Schwertfeger's avatar
Sören Schwertfeger committed
98
99
	}

Sören Schwertfeger's avatar
Sören Schwertfeger committed
100

Sören Schwertfeger's avatar
Sören Schwertfeger committed
101
102
103
104
105
106
107
108
	//group.addAction(m_action);
	group.addAction(m_changeUserAction);
	group.addAction(m_changeMethodAction);
	group.addAction(m_changeDetailsAction);
	group.addAction(m_changeFrameNameLengthAction);
	QAction * sep = new QAction(this);
	sep->setSeparator(true);
	group.addAction(sep);
Sören Schwertfeger's avatar
Sören Schwertfeger committed
109
110
	group.addAction(m_actionLoadTransform);
	group.addAction(m_actionSaveTransform);
Sören Schwertfeger's avatar
Sören Schwertfeger committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
}



void qTransformsPlugin::recTravTree(ccHObject * obj, QString spaces){
	QString num("Name: %1 Children %2 PointCloud %3");
	num = num.arg(obj->getName());
	num = num.arg(obj->getChildrenNumber());
	num = num.arg(obj->getClassID() == CC_TYPES::POINT_CLOUD);
	m_app->dispToConsole(spaces+num,ccMainAppInterface::WRN_CONSOLE_MESSAGE); //an error message is displayed in the console AND an error box will pop-up!

	const QVariantMap& meta = obj->metaData();
    for(QVariantMap::const_iterator iter = meta.begin(); iter != meta.end(); ++iter) {
  		m_app->dispToConsole(spaces+iter.key()+"  "+iter.value().toString(),ccMainAppInterface::STD_CONSOLE_MESSAGE); //an error message is displayed in the console AND an error box will pop-up!
    }
126
127
    //ccGLMatrix transf;
    //obj->getAbsoluteGLTransformation(transf);
Sören Schwertfeger's avatar
Sören Schwertfeger committed
128
129
130
131
132
133
134
135
136
137
138
139
    const ccGLMatrix& transf = obj->getGLTransformationHistory();
	m_app->dispToConsole(spaces+"  "+transf.toString(),ccMainAppInterface::WRN_CONSOLE_MESSAGE); //an error message is displayed in the console AND an error box will pop-up!



	spaces = spaces + QString("  ");
	for(int i=0; i<obj->getChildrenNumber(); ++i){
		recTravTree(obj->getChild(i), spaces);
	}

}

Sören Schwertfeger's avatar
Sören Schwertfeger committed
140
141
void qTransformsPlugin::recTravTree(ccHObject * obj, sttl::Transform * parent){
	sttl::Transform * current = parent;
Sören Schwertfeger's avatar
Sören Schwertfeger committed
142
143
	if(obj->getClassID() == CC_TYPES::POINT_CLOUD){
		// this is a point cloud - will be a new parent...
Sören Schwertfeger's avatar
Sören Schwertfeger committed
144
		sttl::Transform transform;
Sören Schwertfeger's avatar
Sören Schwertfeger committed
145
146
147
148
149
150
151
152
153
154
155
156

		transform.program = "Cloudcompare";
		transform.byUser = m_user.toStdString();
		transform.method = m_method.toStdString();
		transform.details = m_detials.toStdString();

		transform.time = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss").toStdString();


		//std::list<Id> history; // ToDo: History filling...

		transform.frameName = obj->getName().toStdString().substr(0,m_frameNameLength);
157
158
159
160

	    //ccGLMatrix transf;
    	//obj->getAbsoluteGLTransformation(transf);

Sören Schwertfeger's avatar
Sören Schwertfeger committed
161
162
163
164
165
		const ccGLMatrix& transf = obj->getGLTransformationHistory();
		Eigen::Transform<double,3,Eigen::Affine> globalT;
		for(int i = 0; i<16; ++i){
			globalT.data()[i] = transf.data()[i];
		}
166

Sören Schwertfeger's avatar
Sören Schwertfeger committed
167
168
		// ToDo: properly compute the transform of this frame wrt the parent... - I think this is correct...
		Eigen::Transform<double,3,Eigen::Affine> result = globalT * parent->transform.inverse();
Sören Schwertfeger's avatar
Sören Schwertfeger committed
169
		transform.transform = result;
Sören Schwertfeger's avatar
Sören Schwertfeger committed
170

Sören Schwertfeger's avatar
Sören Schwertfeger committed
171
172
173
174
		transform.referenceFrameName = parent->frameName;
		transform.generateId();
		m_transforms.push_back(transform);
		current = &m_transforms.back();
Sören Schwertfeger's avatar
Sören Schwertfeger committed
175
176
177
178
179
180
181
	}

	// recursively call the children...
	for(int i=0; i<obj->getChildrenNumber(); ++i){
		recTravTree(obj->getChild(i), current);
	}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
}

void qTransformsPlugin::applyFrames(ccHObject * obj, std::list<sttl::Frame> &frames){

	if(obj->getClassID() == CC_TYPES::POINT_CLOUD){
		std::string name = obj->getName().toStdString();
		std::list<sttl::Frame>::iterator found = findFrameForData(frames, name);
		if(found == frames.end()){
			QString wrn("Warning: did not find frame with name %1 in our transforms!");
			wrn = wrn.arg(name.c_str());
			m_app->dispToConsole(wrn,ccMainAppInterface::WRN_CONSOLE_MESSAGE); 
		}else{
			if(!found->visited){
				QString wrn("Warning: found frame %1, but it has no valid pose!");
				wrn = wrn.arg(name.c_str());
				m_app->dispToConsole(wrn,ccMainAppInterface::WRN_CONSOLE_MESSAGE); 
			}else{
				// all good - apply the transform!
				ccGLMatrix trans;
				for(int i = 0; i<16; ++i){
					trans.data()[i] = found->rootTransform.data()[i];
				}
				m_app->dispToConsole("Transformation applied to "+QString(name.c_str())+" : "+trans.toString(),ccMainAppInterface::WRN_CONSOLE_MESSAGE); //an error message is displayed in the console AND an error box will pop-up!

//				ccGLMatrix backToOrigin;
//    			obj->getAbsoluteGLTransformation(backToOrigin);
				const ccGLMatrix& bb = obj->getGLTransformationHistory();
				ccGLMatrix backToOrigin = bb;
//    			obj->getAbsoluteGLTransformation(backToOrigin);
				m_app->dispToConsole(QString(name.c_str())+" back : "+backToOrigin.toString(),ccMainAppInterface::WRN_CONSOLE_MESSAGE);

    			backToOrigin = backToOrigin.inverse();

				m_app->dispToConsole(QString(name.c_str())+" back.inv : "+backToOrigin.toString(),ccMainAppInterface::WRN_CONSOLE_MESSAGE);

				m_app->dispToConsole(QString(name.c_str())+" back.inv * trans : "+(backToOrigin * trans).toString(),ccMainAppInterface::WRN_CONSOLE_MESSAGE);

//				obj->resetGLTransformation();
				obj->setGLTransformation(trans * backToOrigin);
//				obj->setGLTransformationHistory(backToOrigin * trans);
				obj->applyGLTransformation_recursive();
				obj->prepareDisplayForRefresh_recursive();
				obj->notifyGeometryUpdate();
			}
		}
	}

	for(int i=0; i<obj->getChildrenNumber(); ++i){
		applyFrames(obj->getChild(i), frames);
	}

	m_app->refreshAll();
		
Sören Schwertfeger's avatar
Sören Schwertfeger committed
235
236
}

Sören Schwertfeger's avatar
Sören Schwertfeger committed
237
238
239
240
241
242
243
244
245
246
247
248
void qTransformsPlugin::loadTransforms(){
	ccHObject * root = getMainAppInterface()->dbRootObject();

	QString file = QFileDialog::getOpenFileName(0, "SaveTransforms", "", "*.stt", 0 , 0); 

	m_transforms.clear();
	if(!file.isEmpty()){
		sttl::loadList(m_transforms, file.toStdString());
	}

	std::cerr<<"We now have "<<m_transforms.size()<<" transforms! "<<std::endl;

249
250
251
252
253
254
255

	std::list<sttl::Frame> frames;

	generateFrames(m_transforms, frames);

	applyFrames(getMainAppInterface()->dbRootObject(), frames);

Sören Schwertfeger's avatar
Sören Schwertfeger committed
256
257
}

Sören Schwertfeger's avatar
Sören Schwertfeger committed
258
259


Sören Schwertfeger's avatar
Sören Schwertfeger committed
260
261
void qTransformsPlugin::saveTransforms(){

Sören Schwertfeger's avatar
Sören Schwertfeger committed
262
263
264
	while(m_user.isEmpty()) changeUser();
	while(m_method.isEmpty()) changeMethod();

Sören Schwertfeger's avatar
Sören Schwertfeger committed
265
266
267
268
269
270
271
272
273
274
	ccHObject * root = getMainAppInterface()->dbRootObject();
	recTravTree(root, QString(""));

	m_transforms.clear();
	sttl::Transform rootFrame;
	rootFrame.frameName = "root";
	rootFrame.transform = Eigen::Transform<double,3,Eigen::Affine>::Identity();
	rootFrame.referenceFrameName = "";
	rootFrame.byUser = "Schwerti";

275
276
	//m_transforms.push_back(rootFrame);
	recTravTree(root, &rootFrame);
Sören Schwertfeger's avatar
Sören Schwertfeger committed
277
278
279
280
281
282
283
284
285

	QString file = QFileDialog::getSaveFileName(0, "SaveTransforms", "", "*.stt", 0 , 0); 

	if(!file.isEmpty()){
		sttl::saveList(m_transforms, file.toStdString());
	}
	
}

Sören Schwertfeger's avatar
Sören Schwertfeger committed
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
void qTransformsPlugin::changeUser(){
	bool ok;
	QString name = QInputDialog::getText(0, "Provide the user Name (currently "+m_user+")", "New user name:",QLineEdit::Normal, m_user, &ok);
	if(ok && !name.isEmpty()) {
		m_user = name;
		m_changeUserAction->setText("Change User Name: "+m_user);
	}
}

void qTransformsPlugin::changeMethod(){
	QStringList list;
	list << sttl::METHOD_MANUAL.c_str() << sttl::METHOD_POINT_PAIRS.c_str() << sttl::METHOD_ICP.c_str();

	bool ok;
	QString method = QInputDialog::getItem(0, "Registration Method", "Method:", list, 0, true, &ok);
	if(ok && !method.isEmpty()) {
		m_method = method;
		m_changeMethodAction->setText("Change Registration Method: "+m_method);
	}
}

void qTransformsPlugin::changeDetails(){
	bool ok;
	QString details = QInputDialog::getText(0, "Change Details", "Details:", QLineEdit::Normal, m_detials, &ok);
	if(ok && !details.isEmpty()) {
		m_detials = details;
	}
}


void qTransformsPlugin::changeFrameNameLength(){
	bool ok;
	int len = QInputDialog::getInt(0, "Provide the frame name length", "New frame name length:", m_frameNameLength, 1, 255, 1, &ok);
	if(ok) {
		m_frameNameLength = len;
		QString txt("Change Frame Name Length: %1");
		txt = txt.arg(m_frameNameLength);
		m_changeFrameNameLengthAction->setText(txt);
	}
}

Sören Schwertfeger's avatar
Sören Schwertfeger committed
327

Sören Schwertfeger's avatar
Sören Schwertfeger committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//This is an example of an action's slot called when the corresponding action
//is triggered (i.e. the corresponding icon or menu entry is clicked in CC's
//main interface). You can access to most of CC components (database,
//3D views, console, etc.) via the 'm_app' attribute (ccMainAppInterface
//object).
void qTransformsPlugin::doAction()
{
	//m_app should have already been initialized by CC when plugin is loaded!
	//(--> pure internal check)
	assert(m_app);
	if (!m_app)
		return;


	//This is how you can output messages
	m_app->dispToConsole("[qTransformsPlugin] Hello world!",ccMainAppInterface::STD_CONSOLE_MESSAGE); //a standard message is displayed in the console
	m_app->dispToConsole("[qTransformsPlugin] Warning: dummy plugin shouldn't be used as is!",ccMainAppInterface::WRN_CONSOLE_MESSAGE); //a warning message is displayed in the console
	//	m_app->dispToConsole("Transforms plugin shouldn't be used as is!",ccMainAppInterface::ERR_CONSOLE_MESSAGE); //an error message is displayed in the console AND an error box will pop-up!


	ccHObject * root = getMainAppInterface()->dbRootObject();
	recTravTree(root, QString(""));

Sören Schwertfeger's avatar
Sören Schwertfeger committed
351
352
353
	m_transforms.clear();
	sttl::Transform rootFrame;
	rootFrame.frameName = "root";
Sören Schwertfeger's avatar
Sören Schwertfeger committed
354
355
356
357
	rootFrame.transform = Eigen::Transform<double,3,Eigen::Affine>::Identity();
	rootFrame.referenceFrameName = "";
	rootFrame.byUser = "Schwerti";

Sören Schwertfeger's avatar
Sören Schwertfeger committed
358
359
360
	m_transforms.push_back(rootFrame);
	recTravTree(root, &m_transforms.front());

Sören Schwertfeger's avatar
Sören Schwertfeger committed
361
362
363
364
365
366
367
368
369
370
371
372
373

}

//This method should return the plugin icon (it will be used mainly
//if your plugin as several actions in which case CC will create a
//dedicated sub-menu entry with this icon.
QIcon qTransformsPlugin::getIcon() const
{
	//open qDummyPlugin.qrc (text file), update the "prefix" and the
	//icon(s) filename(s). Then save it with the right name (yourPlugin.qrc).
	//(eventually, remove the original qDummyPlugin.qrc file!)
	return QIcon(":/CC/plugin/qTransforms/icon.png");
}