62 #include <OGRE/OgreColourValue.h>
68 using std::stringstream;
72 #define CL_RAY_CAST_KERNEL_FILE "/include/kernels/cast_rays.cl"
105 "ClusterLabelTool_SelectionBoxMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
true);
119 vector<cl::Platform> platforms;
120 cl::Platform::get(&platforms);
121 for (
auto const& platform : platforms)
123 ROS_DEBUG(
"Found platform: %s", platform.getInfo<CL_PLATFORM_NAME>().c_str());
124 ROS_DEBUG(
"platform version: %s", platform.getInfo<CL_PLATFORM_VERSION>().c_str());
128 vector<cl::Device> consideredDevices;
129 for (
auto const& platform : platforms)
131 ROS_DEBUG(
"Get devices of %s: ", platform.getInfo<CL_PLATFORM_NAME>().c_str());
132 cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platform)(), 0 };
133 m_clContext = cl::Context(CL_DEVICE_TYPE_ALL, properties);
134 vector<cl::Device> devices =
m_clContext.getInfo<CL_CONTEXT_DEVICES>();
135 for (
auto const& device : devices)
137 ROS_DEBUG(
"Found device: %s", device.getInfo<CL_DEVICE_NAME>().c_str());
138 ROS_DEBUG(
"Device work units: %d", device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>());
139 ROS_DEBUG(
"Device work group size: %lu", device.getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>());
141 std::string device_info = device.getInfo<CL_DEVICE_VERSION>();
144 unsigned int version = cl::detail::getVersion(std::vector<char>(device_info.begin(), device_info.end()));
147 cl_uint majorVersion = version >> 16;
149 cl_uint minorVersion = version & 0x0000FFFF;
151 ROS_INFO(
"Found a device with OpenCL version: %u.%u", majorVersion, minorVersion);
154 if (majorVersion >= 1 && minorVersion >= 2)
157 consideredDevices.push_back(device);
163 cl::Platform platform;
165 bool deviceFound =
false;
166 for (
auto const& device : consideredDevices)
168 if (device.getInfo<CL_DEVICE_TYPE>() == CL_DEVICE_TYPE_GPU)
171 platform = device.getInfo<CL_DEVICE_PLATFORM>();
176 if (!deviceFound && consideredDevices.size() > 0)
180 platform =
m_clDevice.getInfo<CL_DEVICE_PLATFORM>();
186 ROS_ERROR(
"No device with compatible OpenCL version found (minimum 2.0)");
190 cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platform)(), 0 };
191 m_clContext = cl::Context(CL_DEVICE_TYPE_ALL, properties);
193 ROS_INFO(
"Using device %s of platform %s",
m_clDevice.getInfo<CL_DEVICE_NAME>().c_str(),
194 platform.getInfo<CL_PLATFORM_NAME>().c_str());
199 std::string cast_rays_kernel(
static_cast<stringstream const&
>(stringstream() << in.rdbuf()).str());
203 m_clProgramSources = cl::Program::Sources(1, { cast_rays_kernel.c_str(), cast_rays_kernel.length() });
209 ROS_INFO(
"Successfully built program.");
211 catch (cl::Error& err)
222 catch (cl::Error err)
270 for (uint32_t faceId = 0; faceId <
m_meshGeometry->faces.size(); faceId++)
272 for (uint32_t i = 0; i < 3; i++)
274 uint32_t vertexId =
m_meshGeometry->faces[faceId].vertexIndices[i];
319 catch (cl::Error err)
330 float left, right, top, bottom;
378 std::swap(left, right);
383 std::swap(top, bottom);
386 const float BOX_SIZE_TOLERANCE = 0.0001;
387 if ((right - left) * (bottom - top) < BOX_SIZE_TOLERANCE)
393 Ogre::PlaneBoundedVolume volume =
394 event.viewport->getCamera()->getCameraToViewportBoxVolume(left, top, right, bottom,
true);
402 for (Ogre::Plane plane : volume.planes)
415 cl::NullRange,
nullptr);
422 catch (cl::Error err)
428 for (
int faceId = 0; faceId <
m_meshGeometry->faces.size(); faceId++)
440 std::vector<uint32_t> tmpFaceList;
446 tmpFaceList.push_back(faceId);
452 m_visual->setFacesInCluster(tmpFaceList);
459 Ogre::Ray ray =
event.viewport->getCamera()->getCameraToViewportRay(
460 (
float)event.
x / event.
viewport->getActualWidth(), (
float)event.
y / event.
viewport->getActualHeight());
466 m_rayData = { ray.getOrigin().x, ray.getOrigin().y, ray.getOrigin().z,
467 ray.getDirection().x, ray.getDirection().y, ray.getDirection().z };
469 std::vector<std::pair<uint32_t, float>> intersectedFaceList;
476 cl::NullRange,
nullptr);
483 catch (cl::Error err)
489 int closestFaceId = -1;
490 float minDist = std::numeric_limits<float>::max();
503 std::vector<uint32_t> tmpFaceList;
515 tmpFaceList.push_back(faceId);
519 m_visual->setFacesInCluster(tmpFaceList);
521 ROS_DEBUG(
"selectSingleFaceParallel() found face with id %d", closestFaceId);
527 Ogre::Ray ray =
event.viewport->getCamera()->getCameraToViewportRay(
528 (
float)event.
x / event.
viewport->getActualWidth(), (
float)event.
y / event.
viewport->getActualHeight());
538 Ogre::Vector3 sphereCenter = ray.getPoint(raycastResult->second);
540 m_sphereData = { sphereCenter.x, sphereCenter.y, sphereCenter.z, raycastResult->second };
547 cl::NullRange,
nullptr);
554 catch (cl::Error err)
560 for (
int faceId = 0; faceId <
m_meshGeometry->faces.size(); faceId++)
575 std::vector<uint32_t> tmpFaceList;
580 tmpFaceList.push_back(faceId);
584 m_visual->setFacesInCluster(tmpFaceList);
591 m_rayData = { ray.getOrigin().x, ray.getOrigin().y, ray.getOrigin().z,
592 ray.getDirection().x, ray.getDirection().y, ray.getDirection().z };
599 cl::NullRange,
nullptr);
606 catch (cl::Error err)
612 uint32_t closestFaceId;
613 bool faceFound =
false;
614 float minDist = std::numeric_limits<float>::max();
628 return std::make_pair(closestFaceId, minDist);
640 vector<uint32_t> faces;
711 std::vector<uint32_t> faceList;
717 faceList.push_back(faceId);
728 m_visual->setFacesInCluster(std::vector<uint32_t>());