29 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
35 #include <Inventor/SoOffscreenRenderer.h>
41 std::string cameraFrameName,
42 visionx::ImageFormatInfo imageFormat,
44 SbColor backgroundColor,
47 float collisionModelInflationMargin)
49 VirtualRobot::init(
"MaskRobotInImage");
51 SoInteraction::init();
53 this->cameraFrameName = cameraFrameName;
55 this->imageWidth = imageFormat.dimension.width;
56 this->imageHeight = imageFormat.dimension.height;
57 this->backgroundColor = backgroundColor;
58 this->flipImages = flipImages;
59 this->useFullModel = useFullModel;
60 this->collisionModelInflationMargin = collisionModelInflationMargin;
63 renderedImage =
new CByteImage(imageWidth, imageHeight, CByteImage::eRGB24);
64 maskImage =
new CByteImage(imageWidth, imageHeight, CByteImage::eGrayScale);
69 this->robotStateComponent = robotStateComponent;
77 VirtualRobot::CoinVisualizationFactory::createOffscreenRenderer(imageWidth, imageHeight));
78 renderBuffer.resize(imageWidth * imageHeight * 3);
81 renderer->setBackgroundColor(backgroundColor);
86 if (collisionModelInflationMargin > 0.f)
89 for (
auto& model : localRobot->getCollisionModels())
91 model->setUpdateVisualization(
true);
92 model->inflateModel(collisionModelInflationMargin);
93 ARMARX_INFO <<
"MARGIN AFTER INITIALIZATION: " << model->getMargin();
99 if (collisionModelInflationMargin > 0.f)
101 ARMARX_IMPORTANT <<
"Inflating collisionModel but not rendering collisionModel!";
109 ARMARX_INFO <<
"RENDERER BACKGROUND COLOR IS: " << renderer->getBackgroundColor().getValue()[0]
110 <<
";" << renderer->getBackgroundColor().getValue()[1] <<
";"
111 << renderer->getBackgroundColor().getValue()[2] <<
"]";
126 VirtualRobot::RobotNodePtr cameraNode = localRobot->getRobotNode(cameraFrameName);
131 VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Collision;
135 visuType = VirtualRobot::SceneObject::Full;
143 SoNode* visualisationNode = NULL;
146 visualisationNode = visualization->getCoinVisualization();
152 bool renderOK =
false;
154 auto bufferPtr = renderBuffer.data();
158 renderOK = VirtualRobot::CoinVisualizationFactory::renderOffscreen(
159 renderer.get(), cameraNode, visualisationNode, &bufferPtr, 20.0f, 100000.0f, fov);
166 unsigned char* pRenderedImage = renderer->getBuffer();
169 std::uint8_t* pixelsRow = renderedImage->pixels;
170 std::uint8_t* maskPixelsRow = maskImage->pixels;
171 for (
int y = 0; y < imageHeight; y++)
173 for (
int x = 0; x < imageWidth; x++)
179 adjustedX = imageWidth - 1 - x;
180 adjustedY = imageHeight - 1 - y;
184 pixelsRow[x * 3 + 0] =
185 pRenderedImage[3 * (imageWidth * (imageHeight - adjustedY) + adjustedX) + 0];
186 pixelsRow[x * 3 + 1] =
187 pRenderedImage[3 * (imageWidth * (imageHeight - adjustedY) + adjustedX) + 1];
188 pixelsRow[x * 3 + 2] =
189 pRenderedImage[3 * (imageWidth * (imageHeight - adjustedY) + adjustedX) + 2];
192 SbColor pixelColor(((
float)pixelsRow[x * 3 + 0]) / 255.f,
193 ((
float)pixelsRow[x * 3 + 1]) / 255.f,
194 ((
float)pixelsRow[x * 3 + 2]) / 255.f);
195 if (backgroundColor.equals(pixelColor, 0.004f))
197 maskPixelsRow[x] = 0;
201 maskPixelsRow[x] = 255;
204 pixelsRow += imageWidth * 3;
205 maskPixelsRow += imageWidth;
216 backgroundColor = newBackgroundColor;
217 renderer->setBackgroundColor(newBackgroundColor);