Example #1
0
	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);
		}
	}
Example #2
0
	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++;
		}
	}
std::shared_ptr<SimulatorResource> SimulatorResourceFactory::buildResource(
    const std::shared_ptr<RAML::RamlResource> &ramlResource)
{
    // Build resource request and respone model schema
    RequestModelBuilder requestModelBuilder;
    std::unordered_map<std::string, RequestModelSP> requestModels =
        requestModelBuilder.build(ramlResource);

    // Build SimulatorResourceModel from "GET" response schema
    if (requestModels.end() == requestModels.find("GET"))
    {
        OIC_LOG(ERROR, TAG, "Resource's RAML does not have GET request model!");
        return nullptr;
    }

    RequestModelSP getRequestModel = requestModels["GET"];
    ResponseModelSP getResponseModel = getRequestModel->getResponseModel(200);
    if (!getResponseModel)
    {
        OIC_LOG(ERROR, TAG, "Resource's RAML does not have response for GET request!");
        return nullptr;
    }

    std::shared_ptr<SimulatorResourceModelSchema> responseSchema =
        getResponseModel->getSchema();
    if (!responseSchema)
    {
        OIC_LOG(ERROR, TAG, "Failed to get schema from response model!");
        return nullptr;
    }

    SimulatorResourceModel resourceModel = responseSchema->buildResourceModel();

    // Remove the common properties from  resource Model
    std::string resourceURI = ramlResource->getResourceUri();
    std::string resourceName = ramlResource->getDisplayName();
    std::string resourceType;

    // Extracting resource type.
    if (resourceModel.contains("rt"))
    {
        resourceType = resourceModel.get<std::string>("rt");
        resourceModel.remove("rt");
    }
    else if (resourceModel.contains("resourceType"))
    {
        resourceType = resourceModel.get<std::string>("resourceType");
        resourceModel.remove("resourceType");
    }

    // Construct resource type from uri
    if(resourceType.empty())
    {
        std::ostringstream rtString;
        rtString << "oic.r.";

        size_t pos = resourceURI.rfind("/");
        if (pos == std::string::npos)
            pos = -1;

        std::string rtName = resourceURI.substr(pos+1);
        std::transform(rtName.begin(), rtName.end(), rtName.begin(), ::tolower);
        rtString << rtName;
        resourceType = rtString.str();
    }

    // Extracting interface type.
    std::vector<std::string> interfaceTypes;
    if (resourceModel.contains("if"))
    {
        SimulatorResourceModel::TypeInfo typeInfo = resourceModel.getType("if");
        if(AttributeValueType::STRING == typeInfo.type())
        {
            interfaceTypes.push_back(resourceModel.get<std::string>("if"));
        }
        else if(AttributeValueType::VECTOR == typeInfo.type()
            && AttributeValueType::STRING == typeInfo.baseType()
            && typeInfo.depth() == 1)
        {
            interfaceTypes = resourceModel.get<std::vector<std::string>>("if");
            if (interfaceTypes.size() > 1)
                interfaceTypes.erase(interfaceTypes.begin()+1, interfaceTypes.end());
        }

        resourceModel.remove("if");
    }

    for (auto &requestModel : requestModels)
    {
        if (requestModel.second)
        {
            addInterfaceFromQueryParameter((requestModel.second)->getQueryParams("if"),
                interfaceTypes);
        }
    }

    // Remove properties which are not part of resource representation
    resourceModel.remove("p");
    resourceModel.remove("n");
    resourceModel.remove("id");

    // Create simple/collection resource
    std::shared_ptr<SimulatorResource> simResource;
    if (resourceModel.contains("links"))
    {
        std::shared_ptr<SimulatorCollectionResourceImpl> collectionRes(
            new SimulatorCollectionResourceImpl());

        collectionRes->setName(resourceName);
        collectionRes->setResourceType(resourceType);
        if (interfaceTypes.size() > 0)
            collectionRes->setInterface(interfaceTypes);
        collectionRes->setURI(ResourceURIFactory::getInstance()->makeUniqueURI(resourceURI));

        // Set the resource model and its schema to simulated resource
        collectionRes->setResourceModel(resourceModel);
        collectionRes->setResourceModelSchema(responseSchema);
        collectionRes->setRequestModel(requestModels);

        simResource = collectionRes;
    }
    else
    {
        std::shared_ptr<SimulatorSingleResourceImpl> singleRes(
            new SimulatorSingleResourceImpl());

        singleRes->setName(resourceName);
        singleRes->setResourceType(resourceType);
        if (interfaceTypes.size() > 0)
            singleRes->setInterface(interfaceTypes);
        singleRes->setURI(ResourceURIFactory::getInstance()->makeUniqueURI(resourceURI));

        // Set the resource model and its schema to simulated resource
        singleRes->setResourceModel(resourceModel);
        singleRes->setResourceModelSchema(responseSchema);
        singleRes->setRequestModel(requestModels);

        simResource = singleRes;
    }

    return simResource;
}
std::shared_ptr<SimulatorResource> SimulatorResourceFactory::buildResource(
    std::shared_ptr<RAML::RamlResource> ramlResource)
{
    std::string name;
    std::string uri;
    std::string resourceType, rt;
    std::vector<std::string> interfaceType, ifType;

    name = ramlResource->getDisplayName();
    uri = ramlResource->getResourceUri();
    std::map<RAML::ActionType, RAML::ActionPtr> actionType = ramlResource->getActions();

    RAML::RequestResponseBodyPtr successResponseBody = getRAMLResponseBody(
                ramlResource, RAML::ActionType::GET, "200");
    RAML::RequestResponseBodyPtr putErrorResponseBody = getRAMLResponseBody(
                ramlResource, RAML::ActionType::PUT, "403");
    RAML::RequestResponseBodyPtr postErrorResponseBody = getRAMLResponseBody(
                ramlResource, RAML::ActionType::POST, "403");

    SimulatorResourceModel successResponseModel = buildModelFromResponseBody(
                successResponseBody, resourceType, interfaceType);
    SimulatorResourceModel putErrorResponseModel = buildModelFromResponseBody(
                putErrorResponseBody, rt, ifType);
    SimulatorResourceModel postErrorResponseModel = buildModelFromResponseBody(
                postErrorResponseBody, rt, ifType);

    // Create simple/collection resource
    std::shared_ptr<SimulatorResource> simResource;
    if (successResponseModel.containsAttribute("links"))
    {
        try
        {
            std::shared_ptr<SimulatorCollectionResourceImpl> collectionRes(
                new SimulatorCollectionResourceImpl());

            collectionRes->setName(name);
            collectionRes->setResourceType(resourceType);
            collectionRes->setInterface(interfaceType);
            collectionRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri));
            collectionRes->setActionType(actionType);

            collectionRes->setResourceModel(successResponseModel);
            simResource = std::dynamic_pointer_cast<SimulatorResource>(collectionRes);
        }
        catch (InvalidArgsException &e) {}
    }
    else
    {
        try
        {
            std::shared_ptr<SimulatorSingleResourceImpl> singleRes(
                new SimulatorSingleResourceImpl());

            singleRes->setName(name);
            singleRes->setResourceType(resourceType);
            singleRes->setInterface(interfaceType);
            singleRes->setURI(ResourceURIFactory::getInstance()->constructURI(uri));
            singleRes->setActionType(actionType);

            singleRes->setResourceModel(successResponseModel);
            singleRes->setPutErrorResponseModel(putErrorResponseModel);
            singleRes->setPostErrorResponseModel(postErrorResponseModel);

            simResource = std::dynamic_pointer_cast<SimulatorResource>(singleRes);
        }
        catch (InvalidArgsException &e) {}
    }

    return simResource;
}