1 2 3 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOpenNI.h>
#include <XnLog.h>
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define XN_MAX_CAPABILITIES_COUNT 100
#define XN_MAX_NEEDED_NODES 100
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
struct XnNodeQuery
{
XnChar strVendor[XN_MAX_NAME_LENGTH];
XnChar strName[XN_MAX_NAME_LENGTH];
XnVersion MinVersion;
XnVersion MaxVersion;
const XnChar* astrSupportedCapabilities[XN_MAX_CAPABILITIES_COUNT];
XnUInt32 nSupportedCapabilities;
XnMapOutputMode aSupportedMapOutputModes[XN_MAX_CAPABILITIES_COUNT];
XnUInt32 nSupportedMapOutputModes;
XnUInt32 nMinUserPositions;
XnBool bExistingNodeOnly;
XnBool bNonExistingNodeOnly;
const XnChar* astrNeededNodes[XN_MAX_NEEDED_NODES];
XnUInt32 nNeededNodes;
XnChar strCreationInfo[XN_MAX_CREATION_INFO_LENGTH];
};
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
XN_C_API XnStatus xnNodeQueryAllocate(XnNodeQuery** ppQuery)
{
XN_VALIDATE_OUTPUT_PTR(ppQuery);
XN_VALIDATE_CALLOC(*ppQuery, XnNodeQuery, 1);
(*ppQuery)->MaxVersion.nMajor = 0xFF;
return (XN_STATUS_OK);
}
XN_C_API void xnNodeQueryFree(XnNodeQuery* pQuery)
{
// free needed nodes list
for (XnUInt32 i = 0; i < pQuery->nNeededNodes; ++i)
{
xnOSFree(pQuery->astrNeededNodes[i]);
}
// free capabilities list
for (XnUInt32 i = 0; i < pQuery->nSupportedCapabilities; ++i)
{
xnOSFree(pQuery->astrSupportedCapabilities[i]);
}
xnOSFree(pQuery);
}
XN_C_API XnStatus xnNodeQuerySetVendor(XnNodeQuery* pQuery, const XnChar* strVendor)
{
XN_VALIDATE_INPUT_PTR(pQuery);
XN_VALIDATE_INPUT_PTR(strVendor);
strncpy(pQuery->strVendor, strVendor, XN_MAX_NAME_LENGTH);
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnNodeQuerySetName(XnNodeQuery* pQuery, const XnChar* strName)
{
XN_VALIDATE_INPUT_PTR(pQuery);
XN_VALIDATE_INPUT_PTR(strName);
strncpy(pQuery->strName, strName, XN_MAX_NAME_LENGTH);
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnNodeQuerySetMinVersion(XnNodeQuery* pQuery, const XnVersion* pMinVersion)
{
XN_VALIDATE_INPUT_PTR(pQuery);
XN_VALIDATE_INPUT_PTR(pMinVersion);
xnOSMemCopy(&pQuery->MinVersion, pMinVersion, sizeof(XnVersion));
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnNodeQuerySetMaxVersion(XnNodeQuery* pQuery, const XnVersion* pMaxVersion)
{
XN_VALIDATE_INPUT_PTR(pQuery);
XN_VALIDATE_INPUT_PTR(pMaxVersion);
xnOSMemCopy(&pQuery->MaxVersion, pMaxVersion, sizeof(XnVersion));
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnNodeQueryAddSupportedCapability(XnNodeQuery* pQuery, const XnChar* strNeededCapability)
{
XN_VALIDATE_INPUT_PTR(pQuery);
XN_VALIDATE_INPUT_PTR(strNeededCapability);
pQuery->astrSupportedCapabilities[pQuery->nSupportedCapabilities++] = xnOSStrDup(strNeededCapability);
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnNodeQueryAddSupportedMapOutputMode(XnNodeQuery* pQuery, const XnMapOutputMode* pMapOutputMode)
{
XN_VALIDATE_INPUT_PTR(pQuery);
XN_VALIDATE_INPUT_PTR(pMapOutputMode);
xnOSMemCopy(&pQuery->aSupportedMapOutputModes[pQuery->nSupportedMapOutputModes++], pMapOutputMode, sizeof(XnMapOutputMode));
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnNodeQuerySetSupportedMinUserPositions(XnNodeQuery* pQuery, const XnUInt32 nCount)
{
XN_VALIDATE_INPUT_PTR(pQuery);
pQuery->nMinUserPositions = nCount;
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnNodeQuerySetExistingNodeOnly(XnNodeQuery* pQuery, XnBool bExistingNode)
{
XN_VALIDATE_INPUT_PTR(pQuery);
pQuery->bExistingNodeOnly = bExistingNode;
return (XN_STATUS_OK);
}
XN_C_API XnStatus XN_C_DECL xnNodeQuerySetNonExistingNodeOnly(XnNodeQuery* pQuery, XnBool bNonExistingNode)
{
XN_VALIDATE_INPUT_PTR(pQuery);
pQuery->bNonExistingNodeOnly = bNonExistingNode;
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnNodeQueryAddNeededNode(XnNodeQuery* pQuery, const XnChar* strInstanceName)
{
XN_VALIDATE_INPUT_PTR(pQuery);
XN_VALIDATE_INPUT_PTR(strInstanceName);
pQuery->astrNeededNodes[pQuery->nNeededNodes++] = xnOSStrDup(strInstanceName);
return (XN_STATUS_OK);
}
XN_C_API XnStatus xnNodeQuerySetCreationInfo(XnNodeQuery* pQuery, const XnChar* strCreationInfo)
{
XN_VALIDATE_INPUT_PTR(pQuery);
XN_VALIDATE_INPUT_PTR(strCreationInfo);
strncpy(pQuery->strCreationInfo, strCreationInfo, sizeof(pQuery->strCreationInfo));
return (XN_STATUS_OK);
}
static XnBool xnIsInstanceInTree(XnNodeInfo* pNodeInfo, const XnChar* strInstanceName)
{
if (strcmp(xnNodeInfoGetInstanceName(pNodeInfo), strInstanceName) == 0)
{
return (TRUE);
}
XnNodeInfoList* pNeededNodes = xnNodeInfoGetNeededNodes(pNodeInfo);
for (XnNodeInfoListIterator it = xnNodeInfoListGetFirst(pNeededNodes);
xnNodeInfoListIteratorIsValid(it);
it = xnNodeInfoListGetNext(it))
{
XnNodeInfo* pChild = xnNodeInfoListGetCurrent(it);
if (xnIsInstanceInTree(pChild, strInstanceName))
{
return (TRUE);
}
}
return (FALSE);
}
static XnBool xnIsMapOutputModeSupported(XnNodeHandle hNode, const XnMapOutputMode* pQuery)
{
XnUInt32 nModes = xnGetSupportedMapOutputModesCount(hNode);
if (nModes == 0)
{
return (FALSE);
}
XnMapOutputMode* aModes = (XnMapOutputMode*)xnOSCalloc(nModes, sizeof(XnMapOutputMode));
if (aModes == NULL)
{
return (FALSE);
}
if (XN_STATUS_OK != xnGetSupportedMapOutputModes(hNode, aModes, &nModes))
{
xnOSFree(aModes);
return (FALSE);
}
for (XnUInt i = 0; i < nModes; ++i)
{
if (pQuery->nXRes != (XnUInt32)(-1) && pQuery->nXRes == aModes[i].nXRes &&
pQuery->nYRes != (XnUInt32)(-1) && pQuery->nYRes == aModes[i].nYRes &&
pQuery->nFPS != (XnUInt32)(-1) && pQuery->nFPS == aModes[i].nFPS)
{
xnOSFree(aModes);
return (TRUE);
}
}
xnOSFree(aModes);
return (FALSE);
}
static XnBool xnIsInfoQueryMatch(const XnNodeQuery* pQuery, XnNodeInfo* pNodeInfo)
{
const XnProductionNodeDescription* pDescription = xnNodeInfoGetDescription(pNodeInfo);
// vendor
if (pQuery->strVendor[0] != '\0' && strcmp(pQuery->strVendor, pDescription->strVendor) != 0)
{
return (FALSE);
}
// name
if (pQuery->strName[0] != '\0' && strcmp(pQuery->strName, pDescription->strName) != 0)
{
return (FALSE);
}
// min version
if (xnVersionCompare(&pQuery->MinVersion, &pDescription->Version) > 0)
{
return (FALSE);
}
// max version
if (xnVersionCompare(&pQuery->MaxVersion, &pDescription->Version) < 0)
{
return (FALSE);
}
// check needed nodes
for (XnUInt i = 0; i < pQuery->nNeededNodes; ++i)
{
if (!xnIsInstanceInTree(pNodeInfo, pQuery->astrNeededNodes[i]))
{
return (FALSE);
}
}
// Creation info
if (pQuery->strCreationInfo[0] != '\0' && strcmp(pQuery->strCreationInfo, xnNodeInfoGetCreationInfo(pNodeInfo)) != 0)
{
return (FALSE);
}
return (TRUE);
}
static XnBool xnIsNodeInstanceMatch(const XnNodeQuery* pQuery, XnNodeHandle hNode)
{
for (XnUInt i = 0; i < pQuery->nSupportedCapabilities; ++i)
{
if (!xnIsCapabilitySupported(hNode, pQuery->astrSupportedCapabilities[i]))
{
return (FALSE);
}
}
for (XnUInt i = 0; i < pQuery->nSupportedMapOutputModes; ++i)
{
if (!xnIsMapOutputModeSupported(hNode, &pQuery->aSupportedMapOutputModes[i]))
{
return (FALSE);
}
}
if (pQuery->nMinUserPositions > 0 && xnGetSupportedUserPositionsCount(hNode) < pQuery->nMinUserPositions)
{
return (FALSE);
}
return (TRUE);
}
static XnBool xnIsNodeMatch(XnContext* pContext, const XnNodeQuery* pQuery, XnNodeInfo* pNodeInfo)
{
// check existing node
XnNodeHandle hNode = xnNodeInfoGetRefHandle(pNodeInfo);
if (pQuery->bExistingNodeOnly && (hNode == NULL))
{
return (FALSE);
}
if (pQuery->bNonExistingNodeOnly && (hNode != NULL))
{
return (FALSE);
}
if (!xnIsInfoQueryMatch(pQuery, pNodeInfo))
{
if (hNode != NULL)
{
xnProductionNodeRelease(hNode);
}
return (FALSE);
}
// check if we need to create an instance, to check capabilities
if (pQuery->nSupportedCapabilities > 0 ||
pQuery->nSupportedMapOutputModes > 0 ||
pQuery->nMinUserPositions > 0)
{
if (hNode == NULL)
{
const XnProductionNodeDescription* pDescription = xnNodeInfoGetDescription(pNodeInfo);
xnLogVerbose(XN_MASK_OPEN_NI, "Creating node '%s' of type '%s' for querying...", pDescription->strName, xnProductionNodeTypeToString(pDescription->Type));
XnStatus nRetVal = xnCreateProductionTree(pContext, pNodeInfo, &hNode);
if (nRetVal != XN_STATUS_OK)
{
xnLogWarning(XN_MASK_OPEN_NI, "Failed to create node of type '%s' for querying: %s", xnProductionNodeTypeToString(pDescription->Type), xnGetStatusString(nRetVal));
return (FALSE);
}
}
}
XnBool bResult = xnIsNodeInstanceMatch(pQuery, hNode);
// in any case, we need to release the node. if we created it, this will cause it to be destroyed. If we just took
// a reference to it, we need to release it.
if (hNode != NULL)
{
xnProductionNodeRelease(hNode);
}
return (bResult);
}
XN_C_API XnStatus xnNodeQueryFilterList(XnContext* pContext, const XnNodeQuery* pQuery, XnNodeInfoList* pList)
{
XnNodeInfoListIterator it = xnNodeInfoListGetFirst(pList);
while (xnNodeInfoListIteratorIsValid(it))
{
// keep current
XnNodeInfoListIterator currIt = it;
XnNodeInfo* pNodeInfo = xnNodeInfoListGetCurrent(currIt);
// move to next (we might remove this one)
it = xnNodeInfoListGetNext(it);
if (!xnIsNodeMatch(pContext, pQuery, pNodeInfo))
{
xnNodeInfoListRemove(pList, currIt);
}
}
return (XN_STATUS_OK);
}
|