void Mainloop() { int frameId = 1; int xdir = 1; int ydir = 1; struct {int x, y;} center = {0,0}; while (m_running) { // printf("Tick"); OniFrame* pFrame = getServices().acquireFrame(); if (pFrame == NULL) {printf("Didn't get frame...\n"); continue;} // Fill frame xnOSMemSet(pFrame->data, 0, pFrame->dataSize); OniDepthPixel* pDepth = (OniDepthPixel*)pFrame->data; for (int y1 = XN_MAX(center.y-10, 0); y1 < XN_MIN(center.y+10, OZ_RESOLUTION_Y); ++y1) for (int x1 = XN_MAX(center.x-10, 0); x1 < XN_MIN(center.x+10, OZ_RESOLUTION_X); ++x1) if ((x1-center.x)*(x1-center.x)+(y1-center.y)*(y1-center.y) < 70) pDepth[singleRes(x1, y1)] = OniDepthPixel(1000+(x1-y1)*3); // pDepth[singleRes(center.x, center.y)] = 1000; center.x += xdir; center.y += ydir; if (center.x < abs(xdir) || center.x > OZ_RESOLUTION_X-1-abs(xdir)) xdir*=-1; if (center.y < abs(ydir) || center.y > OZ_RESOLUTION_Y-1-abs(ydir)) ydir*=-1; for (int i = 0; i < OZ_RESOLUTION_X; ++i) pDepth[i] = 2000; pDepth[0] = 2000; // Fill metadata pFrame->frameIndex = frameId; pFrame->videoMode.pixelFormat = ONI_PIXEL_FORMAT_DEPTH_1_MM; pFrame->videoMode.resolutionX = OZ_RESOLUTION_X; pFrame->videoMode.resolutionY = OZ_RESOLUTION_Y; pFrame->videoMode.fps = 30; pFrame->width = OZ_RESOLUTION_X; pFrame->height = OZ_RESOLUTION_Y; pFrame->cropOriginX = pFrame->cropOriginY = 0; pFrame->croppingEnabled = FALSE; pFrame->sensorType = ONI_SENSOR_DEPTH; pFrame->stride = OZ_RESOLUTION_X*sizeof(OniDepthPixel); pFrame->timestamp = frameId*33000; raiseNewFrame(pFrame); getServices().releaseFrame(pFrame); frameId++; xnOSSleep(33); } }
XnStatus XnDeviceBase::ReadFromStreamImpl(XnDeviceStream* pStream, XnStreamData* pStreamData) { XnStatus nRetVal = XN_STATUS_OK; // read nRetVal = pStream->Read(pStreamData); XN_IS_STATUS_OK(nRetVal); if (pStreamData->bIsNew) { if (strcmp(m_PrimaryStream.GetValue(), XN_PRIMARY_STREAM_ANY) == 0 || strcmp(m_PrimaryStream.GetValue(), XN_PRIMARY_STREAM_NONE) == 0) { // when primary stream is any, any stream read makes the device advance m_nLastReadTimestamp = XN_MAX(pStreamData->nTimestamp, m_nLastReadTimestamp); m_nLastReadFrameID = XN_MAX(pStreamData->nFrameID, m_nLastReadFrameID); } else if (strcmp(m_PrimaryStream.GetValue(), pStream->GetName()) == 0) { // this is the primary stream m_nLastReadTimestamp = pStreamData->nTimestamp; m_nLastReadFrameID = pStreamData->nFrameID; } } return (XN_STATUS_OK); }
void XnDeviceBase::OnNewStreamData(XnDeviceStream* pStream, XnUInt64 nTimestamp, XnUInt32 nFrameID) { XnUInt64 nNow; xnOSGetHighResTimeStamp(&nNow); xnDumpFileWriteString(m_StreamsDataDump, "%llu,%s,%llu,%u\n", nNow, pStream->GetName(), nTimestamp, nFrameID); if (strcmp(m_PrimaryStream.GetValue(), XN_PRIMARY_STREAM_ANY) == 0 || strcmp(m_PrimaryStream.GetValue(), XN_PRIMARY_STREAM_NONE) == 0) { // any stream makes us advance m_nLastTimestamp = XN_MAX(m_nLastTimestamp, nTimestamp); m_nLastFrameID = XN_MAX(m_nLastFrameID, nFrameID); } else if (strcmp(m_PrimaryStream.GetValue(), pStream->GetName()) == 0) // this stream is the primary { m_nLastTimestamp = nTimestamp; m_nLastFrameID = nFrameID; } XnStatus nRetVal = xnOSSetEvent(m_hNewDataEvent); if (nRetVal != XN_STATUS_OK) { xnLogWarning(XN_MASK_DDK, "Failed setting the new data event: %s", xnGetStatusString(nRetVal)); } RaiseNewStreamDataEvent(pStream->GetName()); }
void mouseInputCallSelection() { if (g_MouseInput.pSelectionCallback != NULL) { UIntRect selection; selection.uBottom = XN_MIN(g_MouseInput.StartSelection.Y, g_MouseInput.LastLocation.Y); selection.uTop = XN_MAX(g_MouseInput.StartSelection.Y, g_MouseInput.LastLocation.Y); selection.uLeft = XN_MIN(g_MouseInput.StartSelection.X, g_MouseInput.LastLocation.X); selection.uRight = XN_MAX(g_MouseInput.StartSelection.X, g_MouseInput.LastLocation.X); g_MouseInput.pSelectionCallback(g_MouseInput.nSelectionState, selection); } }
void XnVSlider1D::Initialize(XnVAxis eAxis, XnBool bDraggable, const XnPoint3D& ptInitialPosition, XnFloat fSliderLength, XnFloat fInitialValue, XnFloat fMinOutput, XnFloat fMaxOutput, XnFloat fOffAxisDetectionAngle, XnFloat fOffAxisDetectionMinimumVelocity) { m_bIsDraggable = bDraggable; if (m_pPointBuffer == NULL) { m_pPointBuffer = XN_NEW(XnVPointBuffer, 100); } else { m_pPointBuffer->Reset(); } m_fOffAxisDetectionAngle = fOffAxisDetectionAngle; m_fOffAxisDetectionMinimumVelocity = fOffAxisDetectionMinimumVelocity; m_eAxis = eAxis; m_ptCurrentPosition = ptInitialPosition; m_fCurrentOutput = fInitialValue; m_fOutputMaximum = fMaxOutput; m_fOutputMinimum = fMinOutput; switch (m_eAxis) { case AXIS_X: m_fMinOutputMajorAxisPosition = ptInitialPosition.X - fSliderLength * (fInitialValue - fMinOutput) / (fMaxOutput - fMinOutput); m_fMaxOutputMajorAxisPosition = ptInitialPosition.X + fSliderLength * (fMaxOutput - fInitialValue) / (fMaxOutput - fMinOutput); break; case AXIS_Y: m_fMinOutputMajorAxisPosition = ptInitialPosition.Y - fSliderLength * (fInitialValue - fMinOutput) / (fMaxOutput - fMinOutput); m_fMaxOutputMajorAxisPosition = ptInitialPosition.Y + fSliderLength * (fMaxOutput - fInitialValue) / (fMaxOutput - fMinOutput); break; case AXIS_Z: m_fMinOutputMajorAxisPosition = ptInitialPosition.Z - fSliderLength * (fInitialValue - fMinOutput) / (fMaxOutput - fMinOutput); m_fMaxOutputMajorAxisPosition = ptInitialPosition.Z + fSliderLength * (fMaxOutput - fInitialValue) / (fMaxOutput - fMinOutput); break; default: return; } // clamp current output to valid range - it may not have been valid till now m_fCurrentOutput = XN_MIN(XN_MAX(m_fCurrentOutput, m_fOutputMinimum), m_fOutputMaximum); } // XnVSlider1D::Initialize
XnStatus XnDeviceFileReader::SeekFrame(XnUInt32 nFrameID) { XnStatus nRetVal = XN_STATUS_OK; // don't allow seeking to frame 0 nFrameID = XN_MAX(nFrameID, 1); xnLogInfo(XN_MASK_FILE, "Seeking file to frame %u...", nFrameID); if (m_nFileVersion < 4) { return BCSeekFrame(nFrameID); } nRetVal = SeekTo(0, nFrameID); XN_IS_STATUS_OK(nRetVal); return (XN_STATUS_OK); }
XnStatus PlayerNode::SeekToFrame(const XnChar* strNodeName, XnInt32 nFrameOffset, XnPlayerSeekOrigin origin) { XnStatus nRetVal = XN_STATUS_OK; XnUInt32 nNodeID = GetPlayerNodeIDByName(strNodeName); if (nNodeID == INVALID_NODE_ID) { XN_LOG_ERROR_RETURN(XN_STATUS_BAD_NODE_NAME, XN_MASK_OPEN_NI, "Bad node name '%s'", strNodeName); } PlayerNodeInfo* pPlayerNodeInfo = &m_pNodeInfoMap[nNodeID]; XnInt64 nOriginFrame = 0; switch (origin) { case XN_PLAYER_SEEK_SET: { nOriginFrame = 0; break; } case XN_PLAYER_SEEK_CUR: { nOriginFrame = pPlayerNodeInfo->nCurFrame; break; } case XN_PLAYER_SEEK_END: { nOriginFrame = pPlayerNodeInfo->nFrames; break; } default: { XN_ASSERT(FALSE); XN_LOG_ERROR_RETURN(XN_STATUS_BAD_PARAM, XN_MASK_OPEN_NI, "Invalid seek origin: %u", origin); } } XnUInt32 nDestFrame = (XnUInt32)XN_MIN(XN_MAX(1, nOriginFrame + nFrameOffset), pPlayerNodeInfo->nFrames); nRetVal = SeekToFrameAbsolute(nNodeID, nDestFrame); XN_IS_STATUS_OK(nRetVal); return XN_STATUS_OK; }
XnFloat XnVSlider1D::ValueAtPosition(const XnPoint3D& pt) { XnFloat fMajorAxisPosition; switch (m_eAxis) { case AXIS_X: fMajorAxisPosition = pt.X; break; case AXIS_Y: fMajorAxisPosition = pt.Y; break; case AXIS_Z: fMajorAxisPosition = pt.Z; break; default: return -1; } XnFloat fRelativePosition = (fMajorAxisPosition - m_fMinOutputMajorAxisPosition) / (m_fMaxOutputMajorAxisPosition - m_fMinOutputMajorAxisPosition); XnFloat fTempOutput = m_fOutputMinimum + ((m_fOutputMaximum - m_fOutputMinimum) * fRelativePosition); return XN_MAX(m_fOutputMinimum, XN_MIN(m_fOutputMaximum, fTempOutput)); } // XnVSlider1D::ValueAtPosition
XnStatus XnFileDevice::SeekToFrame(const XnChar* strNodeName, XnInt32 nFrameOffset, XnPlayerSeekOrigin origin) { XnStatus nRetVal = XN_STATUS_OK; XnNodeInfo* pNodeInfo = NULL; nRetVal = m_nodeInfoMap.Get(strNodeName, pNodeInfo); XN_IS_STATUS_OK(nRetVal); XnInt32 nFrameID = 0; switch (origin) { case XN_PLAYER_SEEK_CUR: nFrameID = pNodeInfo->nCurrFrameID + nFrameOffset; break; case XN_PLAYER_SEEK_SET: nFrameID = nFrameOffset; break; case XN_PLAYER_SEEK_END: // TODO: handle return XN_STATUS_NOT_IMPLEMENTED; } // don't allow seeking to frame 0 nFrameID = XN_MAX(nFrameID, 1); xnLogInfo(XN_MASK_FILE, "Seeking file to frameID %u of node %s...", nFrameID, strNodeName); if (m_nFileVersion < 4) { return BCSeekFrame(nFrameID); } else { nRetVal = SeekTo(0, strNodeName, nFrameID); XN_IS_STATUS_OK(nRetVal); } return (XN_STATUS_OK); }
void drawCenteredMessage(void* font, int y, const char* message, float fRed, float fGreen, float fBlue) { const XnUInt32 nMaxLines = 5; XnChar buf[512]; XnChar* aLines[nMaxLines]; XnUInt32 anLinesWidths[nMaxLines]; XnUInt32 nLine = 0; XnUInt32 nLineLengthChars = 0; XnUInt32 nLineLengthPixels = 0; XnUInt32 nMaxLineLength = 0; aLines[0] = buf; // parse message to lines const char* pChar = message; for (;;) { if (*pChar == '\n' || *pChar == '\0') { if (nLineLengthChars > 0) { aLines[nLine][nLineLengthChars++] = '\0'; aLines[nLine+1] = &aLines[nLine][nLineLengthChars]; anLinesWidths[nLine] = nLineLengthPixels; nLine++; if (nLineLengthPixels > nMaxLineLength) { nMaxLineLength = nLineLengthPixels; } nLineLengthPixels = 0; nLineLengthChars = 0; } if (nLine >= nMaxLines || *pChar == '\0') { break; } } else { aLines[nLine][nLineLengthChars++] = *pChar; nLineLengthPixels += glutBitmapWidth(font, *pChar); } pChar++; } XnUInt32 nHeight = 26; int nXLocation = XN_MAX(0, (WIN_SIZE_X - nMaxLineLength) / 2); int nYLocation = y; // Draw black background glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBegin(GL_QUADS); glColor4f(0, 0, 0, 0.6); glVertex2i(nXLocation - 5, nYLocation - nHeight - 5); glVertex2i(nXLocation + nMaxLineLength + 5, nYLocation - nHeight - 5); glVertex2i(nXLocation + nMaxLineLength + 5, nYLocation + nHeight * nLine + 5); glVertex2i(nXLocation - 5, nYLocation + nHeight * nLine + 5); glEnd(); glDisable(GL_BLEND); // show message glColor3f(fRed, fGreen, fBlue); for (XnUInt32 i = 0; i < nLine; ++i) { glRasterPos2i(nXLocation + (nMaxLineLength - anLinesWidths[i])/2, nYLocation + i * nHeight); glPrintString(font, aLines[i]); } }
static XnFloat SoftThreshold(XnFloat x, XnFloat fThreshold) { XnFloat fRes = XN_MAX(XnFloat(fabs(x) - fThreshold), 0.0f); return x > 0 ? fRes : -fRes; }
XnStatus XnVSlider1D::Update(const XnPoint3D& pt, XnFloat fTime, XnBool bCheckOffAxis) { if (m_pPointBuffer == NULL) { return XN_STATUS_NOT_INIT; } m_pPointBuffer->AddPoint(pt, fTime); if (bCheckOffAxis) { XnVDirection eDir = CheckForOffAxisMovement(pt, fTime); if (eDir != DIRECTION_ILLEGAL) { // Set position and value to "Last Valid" // TODO: Do in a non-crappy way m_ptCurrentPosition = m_pPointBuffer->GetAveragePointByTime(m_nOffAxisDetectionTime, fTime, 1); m_fCurrentOutput = ValueAtPosition(m_ptCurrentPosition); OffAxisMovement(eDir); // don't update values return XN_STATUS_OK; } } m_ptCurrentPosition = pt; // calculate the current value // normalize the on-axis movement according to this scale: //TODO: Off-axis motion detection XnFloat fMajorAxisPosition; switch (m_eAxis) { case AXIS_X: fMajorAxisPosition = pt.X; break; case AXIS_Y: fMajorAxisPosition = pt.Y; break; case AXIS_Z: fMajorAxisPosition = pt.Z; break; default: return XN_STATUS_NITE_UNEXPECTED_DIRECTION; } XnFloat fRelativePosition = (fMajorAxisPosition - m_fMinOutputMajorAxisPosition) / (m_fMaxOutputMajorAxisPosition - m_fMinOutputMajorAxisPosition); XnFloat fTempOutput = m_fOutputMinimum + ((m_fOutputMaximum - m_fOutputMinimum) * fRelativePosition); XnFloat fPreviousOutput = m_fCurrentOutput; // Handle dragging if enabled if ((fMajorAxisPosition > m_fMaxOutputMajorAxisPosition) && m_bIsDraggable) { XnFloat fPositionDelta = fMajorAxisPosition - m_fMaxOutputMajorAxisPosition; m_fMaxOutputMajorAxisPosition = fMajorAxisPosition; m_fMinOutputMajorAxisPosition += fPositionDelta; } else { if ((fMajorAxisPosition < m_fMinOutputMajorAxisPosition) && m_bIsDraggable) { XnFloat fPositionDelta = m_fMinOutputMajorAxisPosition - fMajorAxisPosition; m_fMaxOutputMajorAxisPosition = fMajorAxisPosition; m_fMaxOutputMajorAxisPosition -= fPositionDelta; } } m_fCurrentOutput = XN_MAX(m_fOutputMinimum, XN_MIN(m_fOutputMaximum, fTempOutput)); ValueChange(m_fCurrentOutput); return XN_STATUS_OK; } // XnVSlider1D::Update
XnStatus XnFileDevice::BCSeekFrame(XnUInt32 nFrameID) { XnStatus nRetVal = XN_STATUS_OK; XnDeviceFileFrameHeaderV3 FileFrameHeader; XnUInt32 nReadBytes = 0; XnUInt32 nShouldRead = 0; XnInt32 nExpectedFrameID = 1; // go back to start of file nRetVal = Rewind(); XN_IS_STATUS_OK(nRetVal); // Update the frame position to the new position (treat 0 as 1) m_pBCData->nFramePos = XN_MAX(nFrameID, 1); // Make sure we aren't trying to reach a frame that's beyond the number of frames if (m_pBCData->nFramePos > m_pBCData->StreamProperties.nNumOfFrames) { // Set the frame position to the last frame m_pBCData->nFramePos = m_pBCData->StreamProperties.nNumOfFrames; } // Set the file position to the first frame data (right after the file header) XnUInt32 nOffset = 0; switch (m_nFileVersion) { case 3: nOffset = sizeof(XnDeviceFileHeader); break; case 2: nOffset = sizeof(XnDeviceFileHeaderV2); break; case 1: nOffset = sizeof(XnDeviceFileHeaderV1); break; default: return (XN_STATUS_IO_INVALID_STREAM_HEADER); } nRetVal = m_pInputStream->Seek(nOffset); XN_IS_STATUS_OK(nRetVal); // Keep reading frames until we reach the wanted frame XnUInt32 nCurrFilePos = 1; while (nCurrFilePos < m_pBCData->nFramePos) { // Read the frame header switch (m_nFileVersion) { case 3: { nShouldRead = nReadBytes = sizeof(XnDeviceFileFrameHeaderV3); nRetVal = m_pInputStream->ReadData((XnUChar*)&FileFrameHeader, nReadBytes); XN_IS_STATUS_OK(nRetVal); nExpectedFrameID = nCurrFilePos; } break; case 2: { XnDeviceFileFrameHeaderV2 FileFrameHeaderV2; nShouldRead = nReadBytes = sizeof(XnDeviceFileFrameHeaderV2); nRetVal = m_pInputStream->ReadData((XnUChar*)&FileFrameHeaderV2, nReadBytes); XN_IS_STATUS_OK(nRetVal); nRetVal = XnDeviceFileAdjustFileFrameHeaderV2(&FileFrameHeaderV2, &FileFrameHeader); XN_IS_STATUS_OK(nRetVal); nExpectedFrameID = nCurrFilePos - 1; } break; case 1: { XnDeviceFileFrameHeaderV1 FileFrameHeaderV1; nShouldRead = nReadBytes = sizeof(XnDeviceFileFrameHeaderV1); nRetVal = m_pInputStream->ReadData((XnUChar*)&FileFrameHeaderV1, nReadBytes); XN_IS_STATUS_OK(nRetVal); nRetVal = XnDeviceFileAdjustFileFrameHeaderV1(&FileFrameHeaderV1, &FileFrameHeader); XN_IS_STATUS_OK(nRetVal); nExpectedFrameID = nCurrFilePos - 1; } break; default: return XN_STATUS_IO_INVALID_STREAM_HEADER; } // Make sure we got the right header size if (nReadBytes != nShouldRead) { return (XN_STATUS_IO_INVALID_STREAM_FRAME_HEADER); } // Skip the frame data XnUInt32 nPosition; nRetVal = m_pInputStream->Tell(&nPosition); XN_IS_STATUS_OK(nRetVal); nRetVal = m_pInputStream->Seek(FileFrameHeader.nPackedStreamSize + nPosition); XN_IS_STATUS_OK(nRetVal); // increment streams frame ID for (XnNodeInfoMap::Iterator it = m_nodeInfoMap.Begin(); it != m_nodeInfoMap.End(); ++it) { it->Value().nCurrFrameID++; } // Make sure frame ids are sequential if (FileFrameHeader.FrameProperties.nDepthFrameID != nExpectedFrameID) { return (XN_STATUS_IO_STREAM_NOT_SEQUENTIAL); } // Update the current file frame position nCurrFilePos++; } // now read last frame (the one we wanted) XnBool bWrapOccured; nRetVal = BCReadFrame(&bWrapOccured); XN_IS_STATUS_OK(nRetVal); return (XN_STATUS_OK); }
void Mainloop() { int frameId = 1; int xdir = -3; int ydir = 1; struct {int x, y;} center = {160,120}; while (m_running) { xnOSSleep(33); // printf("Tick"); OniFrame* pFrame = getServices().acquireFrame(); if (pFrame == NULL) {printf("Didn't get frame...\n"); continue;} // Fill frame xnOSMemSet(pFrame->data, 0, pFrame->dataSize); OniRGB888Pixel* pImage = (OniRGB888Pixel*)pFrame->data; for (int y = XN_MAX(center.y-10, 0); y < XN_MIN(center.y+10, OZ_RESOLUTION_Y); ++y) for (int x = XN_MAX(center.x-10, 0); x < XN_MIN(center.x+10, OZ_RESOLUTION_X); ++x) if ((x-center.x)*(x-center.x)+(y-center.y)*(y-center.y) < 70) { pImage[singleRes(x, y)].r = (char)(255*(x/(double)OZ_RESOLUTION_X)); pImage[singleRes(x, y)].g = (char)(255*(y/(double)OZ_RESOLUTION_Y)); pImage[singleRes(x, y)].b = (char)(255*((OZ_RESOLUTION_X-x)/(double)OZ_RESOLUTION_X)); } // pImage[singleRes(center.x, center.y)].r = 255; center.x += xdir; center.y += ydir; if (center.x < abs(xdir) || center.x > OZ_RESOLUTION_X-1-abs(xdir)) xdir*=-1; if (center.y < abs(ydir) || center.y > OZ_RESOLUTION_Y-1-abs(ydir)) ydir*=-1; pImage[0].b = (unsigned char)255; // for (int y = 0; y < OZ_RESOLUTION_Y; ++y) // { // pDepth[y*OZ_RESOLUTION_X+(OZ_RESOLUTION_Y-y)] = pDepth[y*OZ_RESOLUTION_X+(y)] = 500+y; // } // Fill metadata pFrame->frameIndex = frameId; pFrame->videoMode.pixelFormat = ONI_PIXEL_FORMAT_RGB888; pFrame->videoMode.resolutionX = OZ_RESOLUTION_X; pFrame->videoMode.resolutionY = OZ_RESOLUTION_Y; pFrame->videoMode.fps = 30; pFrame->width = OZ_RESOLUTION_X; pFrame->height = OZ_RESOLUTION_Y; pFrame->cropOriginX = pFrame->cropOriginY = 0; pFrame->croppingEnabled = FALSE; pFrame->sensorType = ONI_SENSOR_COLOR; pFrame->stride = OZ_RESOLUTION_X*3; pFrame->timestamp = frameId*33000; raiseNewFrame(pFrame); getServices().releaseFrame(pFrame); frameId++; } }