32 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
40 VirtualRobot::init(
"MaskRobotInImage");
42 SoInteraction::init();
44 this->cameraFrameName = cameraFrameName;
46 this->imageWidth = imageFormat.dimension.width;
47 this->imageHeight = imageFormat.dimension.height;
48 this->backgroundColor = backgroundColor;
49 this->flipImages = flipImages;
50 this->useFullModel = useFullModel;
51 this->collisionModelInflationMargin = collisionModelInflationMargin;
54 renderedImage =
new CByteImage(imageWidth, imageHeight, CByteImage::eRGB24);
55 maskImage =
new CByteImage(imageWidth, imageHeight, CByteImage::eGrayScale);
60 this->robotStateComponent = robotStateComponent;
66 renderer.reset(VirtualRobot::CoinVisualizationFactory::createOffscreenRenderer(imageWidth, imageHeight));
67 renderBuffer.resize(imageWidth * imageHeight * 3);
70 renderer->setBackgroundColor(backgroundColor);
77 if (collisionModelInflationMargin > 0.f)
80 for (
auto& model : localRobot->getCollisionModels())
82 model->setUpdateVisualization(
true);
83 model->inflateModel(collisionModelInflationMargin);
84 ARMARX_INFO <<
"MARGIN AFTER INITIALIZATION: " << model->getMargin();
90 if (collisionModelInflationMargin > 0.f)
92 ARMARX_IMPORTANT <<
"Inflating collisionModel but not rendering collisionModel!";
102 ARMARX_INFO <<
"RENDERER BACKGROUND COLOR IS: " << renderer->getBackgroundColor().getValue()[0] <<
";" << renderer->getBackgroundColor().getValue()[1] <<
";" << renderer->getBackgroundColor().getValue()[2] <<
"]";
117 VirtualRobot::RobotNodePtr cameraNode = localRobot->getRobotNode(cameraFrameName);
122 VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Collision;
126 visuType = VirtualRobot::SceneObject::Full;
135 SoNode* visualisationNode = NULL;
138 visualisationNode = visualization->getCoinVisualization();
144 bool renderOK =
false;
146 auto bufferPtr = renderBuffer.data();
153 renderOK = VirtualRobot::CoinVisualizationFactory::renderOffscreen(renderer.get(), cameraNode, visualisationNode, &bufferPtr, 20.0f, 100000.0f, fov);
160 unsigned char* pRenderedImage = renderer->getBuffer();
163 std::uint8_t* pixelsRow = renderedImage->pixels;
164 std::uint8_t* maskPixelsRow = maskImage->pixels;
165 for (
int y = 0; y < imageHeight; y++)
167 for (
int x = 0; x < imageWidth; x++)
173 adjustedX = imageWidth - 1 - x;
174 adjustedY = imageHeight - 1 - y;
178 pixelsRow[x * 3 + 0] = pRenderedImage[3 * (imageWidth * (imageHeight - adjustedY) + adjustedX) + 0];
179 pixelsRow[x * 3 + 1] = pRenderedImage[3 * (imageWidth * (imageHeight - adjustedY) + adjustedX) + 1];
180 pixelsRow[x * 3 + 2] = pRenderedImage[3 * (imageWidth * (imageHeight - adjustedY) + adjustedX) + 2];
183 SbColor pixelColor(((
float) pixelsRow[x * 3 + 0]) / 255.f, ((
float) pixelsRow[x * 3 + 1]) / 255.f, ((
float) pixelsRow[x * 3 + 2]) / 255.f);
184 if (backgroundColor.equals(pixelColor, 0.004f))
186 maskPixelsRow[x] = 0;
190 maskPixelsRow[x] = 255;
193 pixelsRow += imageWidth * 3;
194 maskPixelsRow += imageWidth;
204 backgroundColor = newBackgroundColor;
205 renderer->setBackgroundColor(newBackgroundColor);