SkeletonObjectInfoScripting.cc 11.9 KB
Newer Older
Matthias Möller's avatar
Matthias Möller committed
1
2
3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Matthias Möller's avatar
Matthias Möller committed
39
40
*                                                                            *
\*===========================================================================*/
41

Matthias Möller's avatar
Matthias Möller committed
42
43
/*===========================================================================*\
*                                                                            *
Matthias Möller's avatar
Matthias Möller committed
44
45
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
Matthias Möller's avatar
Matthias Möller committed
46
*   $Date$                    *
Matthias Möller's avatar
Matthias Möller committed
47
48
*                                                                            *
\*===========================================================================*/
49
50
51
52
#include "SkeletonObjectInfoPlugin.hh"
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

//-----------------------------------------------------------------------------
Matthias Möller's avatar
- doc    
Matthias Möller committed
53
54
55
/** \brief set the descriptions for scripting slots
 *
 */
56
57
58
59
60
61
62
63
void InfoSkeletonObjectPlugin::setDescriptions()
{
	emit setSlotDescription("jointCount(int)",tr("get total number of joints for a given skeleton"),
			QStringList(tr("skeletontID")), QStringList(tr("id of a skeleton")));

	emit setSlotDescription("branchCount(int)",tr("get total number of branches for a given skeleton"),
			QStringList(tr("skeletonID")), QStringList(tr("id of a skeleton")));

Matthias Möller's avatar
Matthias Möller committed
64
65
66
67
68
	emit setSlotDescription("leafCount(int)",tr("get total number of leaves for a given skeleton"),
				QStringList(tr("skeletonID")), QStringList(tr("id of a skeleton")));



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
	emit setSlotDescription("boundingBoxMin(int)",tr("get minimum point of the axis-aligned bounding box"),
			QStringList(tr("skeletonID")), QStringList(tr("id of a skeleton")));

	emit setSlotDescription("boundingBoxMax(int)",tr("get maximum point of the axis-aligned bounding box"),
			QStringList(tr("skeletonID")), QStringList(tr("id of a skeleton")));

	emit setSlotDescription("boundingBoxSize(int)",tr("get the size of the axis-aligned bounding box"),
			QStringList(tr("skeletonID")), QStringList(tr("id of a skeleton")));


	emit setSlotDescription("boneLength(int,int)",tr("Get the length of a bone from given joint to his parent"),
			QString(tr("SkeletonID,jointID")).split(","),
			QString(tr("id of the skeleton, id of the joint")).split(","));

	emit setSlotDescription("minBoneLength(int)",tr("Get the minimal bone length of a skeleton"),
			QStringList(tr("SkeletonID")), QStringList(tr("id of the skeleton")));

	emit setSlotDescription("maxBoneLength(int)",tr("Get the maximal bone length of a skeleton"),
			QStringList(tr("SkeletonID")), QStringList(tr("id of the skeleton")));

	emit setSlotDescription("meanBoneLength(int)",tr("Get the mean bone length of a skeleton"),
			QStringList(tr("SkeletonID")), QStringList(tr("id of the skeleton")));
}

//-----------------------------------------------------------------------------

Matthias Möller's avatar
- doc    
Matthias Möller committed
95
96
97
98
99
/** \brief get total number of vertices for a given object
 *
 * @param _skeletonID skeleton id
 * @return number of joints or -1 if an error occured
 */
100
101
102
103
104
105
106
107
108
109
110
111
112
113
int InfoSkeletonObjectPlugin::jointCount(int _skeletonID)
{
	Skeleton* skeleton = PluginFunctions::skeleton(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
		return -1;
	}

	return skeleton->jointCount();
}

//-----------------------------------------------------------------------------

Matthias Möller's avatar
- doc    
Matthias Möller committed
114
115
116
117
118
/** \brief get total number of vertices for a given object
 *
 * @param _skeletonID skeleton id
 * @return number of branches (joints with multiple childs) or -1 if an error occured
 */
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
int InfoSkeletonObjectPlugin::branchCount(int _skeletonID)
{
	Skeleton* skeleton = PluginFunctions::skeleton(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
		return -1;
	}

	int result = 0;
	for (Skeleton::Iterator iter = skeleton->begin(); iter != skeleton->end(); ++iter)
		if (iter->size() > 1)
			++result;

	return result;
Matthias Möller's avatar
Matthias Möller committed
134
//safsdf
135
136
137
138
}

//-----------------------------------------------------------------------------

Matthias Möller's avatar
- doc    
Matthias Möller committed
139
140
141
142
143
/** \brief get total number of vertices for a given object
 *
 * @param _skeletonID skeleton id
 * @return number of leaves or -1 if an error occured
 */
Matthias Möller's avatar
Matthias Möller committed
144
int InfoSkeletonObjectPlugin::leafCount(int _skeletonID)
Matthias Möller's avatar
Matthias Möller committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{
	Skeleton* skeleton = PluginFunctions::skeleton(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
		return -1;
	}

	int result = 0;
	for (Skeleton::Iterator iter = skeleton->begin(); iter != skeleton->end(); ++iter)
		if (iter->size() == 0)
			++result;

	return result;
}

//-----------------------------------------------------------------------------

Matthias Möller's avatar
- doc    
Matthias Möller committed
163
164
165
166
167
/** \brief get total number of vertices for a given object
 *
 * @param _skeletonID skeleton id
 * @return minimum point of the bounding box
 */
168
169
170
171
172
173
Vector InfoSkeletonObjectPlugin::boundingBoxMin(int _skeletonID)
{
	SkeletonObject* skeleton = PluginFunctions::skeletonObject(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
Matthias Möller's avatar
- doc    
Matthias Möller committed
174
		return Vector();
175
176
177
178
179
180
181
182
183
	}

	Vector min,max;
	skeleton->skeletonNode()->boundingBox(min,max);
	return min;
}

//-----------------------------------------------------------------------------

Matthias Möller's avatar
- doc    
Matthias Möller committed
184
185
186
187
188
/** \brief get total number of vertices for a given object
 *
 * @param _skeletonID skeleton id
 * @return maximum point of the bounding box
 */
189
190
191
192
193
194
Vector InfoSkeletonObjectPlugin::boundingBoxMax(int _skeletonID)
{
	SkeletonObject* skeleton = PluginFunctions::skeletonObject(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
Matthias Möller's avatar
- doc    
Matthias Möller committed
195
		return Vector();
196
197
198
199
200
201
202
203
204
	}

	Vector min,max;
	skeleton->skeletonNode()->boundingBox(min,max);
	return max;
}

//-----------------------------------------------------------------------------

Matthias Möller's avatar
- doc    
Matthias Möller committed
205
206
207
208
209
/** \brief get total number of vertices for a given object
 *
 * @param _skeletonID skeleton id
 * @return size of the bounding box
 */
210
211
212
213
214
215
Vector InfoSkeletonObjectPlugin::boundingBoxSize(int _skeletonID)
{
	SkeletonObject* skeleton = PluginFunctions::skeletonObject(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
Matthias Möller's avatar
- doc    
Matthias Möller committed
216
		return Vector();
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
	}

	Vector min,max;
	skeleton->skeletonNode()->boundingBox(min,max);
	return (max - min);
}

//-----------------------------------------------------------------------------
/** \brief get the length of a bone from a given joint to his parent
 *
 * @param _skeletonID id of a skeleton
 * @param _jointID id of the joint
 * @return bone length (from _jointID to the parent of _jointID)
 */
double InfoSkeletonObjectPlugin::boneLength(int _skeletonID, int _jointID)
{
	Skeleton* skeleton = PluginFunctions::skeleton(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
	}

	Skeleton::Joint* parent = skeleton->joint(_jointID)->parent();
	if (!parent)
	{
		emit log(LOGERR, tr ("Unable to get parent joint"));
	}

	unsigned int parentID = parent->id();

	//length of bones is defined in the reference pose and cannot be modified in any animation
	Skeleton::Pose* pose = skeleton->referencePose();

	return (pose->globalTranslation(_jointID) - pose->globalTranslation(parentID)).length();
}

//-----------------------------------------------------------------------------
Matthias Möller's avatar
- doc    
Matthias Möller committed
254
255
256
257
258
259

/** \brief get total number of vertices for a given object
 *
 * @param _skeletonID skeleton id
 * @return minimal bone length or -1 if an error occured
 */
260
261
262
263
264
265
266
267
268
269
270
271
272
double InfoSkeletonObjectPlugin::minBoneLength(int _skeletonID)
{
	Skeleton* skeleton = PluginFunctions::skeleton(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
		return -1;
	}


	double min = std::numeric_limits<double>::max();
	for (Skeleton::Iterator iter = skeleton->begin(); iter != skeleton->end(); ++iter)
	{
273
274
275
276
277
278
		if (!iter->isRoot())
		{
			double length = boneLength(_skeletonID,iter->id());
			if (length < min)
				min = length;
		}
279
280
281
282
283
284
	}
	return min;
}

//-----------------------------------------------------------------------------

Matthias Möller's avatar
- doc    
Matthias Möller committed
285
286
287
288
289
/** \brief get total number of vertices for a given object
 *
 * @param _skeletonID skeleton id
 * @return maximal bone length or -1 if an error occured
 */
290
291
292
293
294
295
296
297
298
299
300
301
302
double InfoSkeletonObjectPlugin::maxBoneLength(int _skeletonID)
{
	Skeleton* skeleton = PluginFunctions::skeleton(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
		return -1;
	}


	double max = std::numeric_limits<double>::min();
	for (Skeleton::Iterator iter = skeleton->begin(); iter != skeleton->end(); ++iter)
	{
303
304
305
306
307
308
		if (!iter->isRoot())
		{
			double length = boneLength(_skeletonID,iter->id());
			if (length > max)
				max = length;
		}
309
310
311
312
313
314
	}
	return max;
}

//-----------------------------------------------------------------------------

Matthias Möller's avatar
- doc    
Matthias Möller committed
315
316
317
318
319
/** \brief get total number of vertices for a given object
 *
 * @param _skeletonID skeleton id
 * @return mean bone length or -1 if an error occured
 */
320
321
322
323
324
325
326
327
328
329
330
331
332
333
double InfoSkeletonObjectPlugin::meanBoneLength(int _skeletonID)
{
	Skeleton* skeleton = PluginFunctions::skeleton(_skeletonID);
	if ( !skeleton )
	{
		emit log(LOGERR, tr("Unable to get skeleton"));
		return -1;
	}


	double sum = 0;
	unsigned int count = 0;
	for (Skeleton::Iterator iter = skeleton->begin(); iter != skeleton->end(); ++iter)
	{
334
335
336
337
338
		if (!iter->isRoot())
		{
			sum += boneLength(_skeletonID,iter->id());
			++count;
		}
339
340
341
342
	}

	return (sum/static_cast<double>(count));
}