-
Notifications
You must be signed in to change notification settings - Fork 73
Segmentation Camera #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Segmentation Camera #329
Changes from 27 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
8813531
Semantic Segmentation Camera in OGRE 2
AmrElsersy df77bf5
address CI compiler warnings
adlarkin 6dd5624
Style
AmrElsersy 53dec8d
Merge pull request #1 from AmrElsersy/SegmentationCamera
AmrElsersy ec14e66
Add Unit Test & Integration Test
AmrElsersy 05913c4
Solved Destructor Crash problem
AmrElsersy 1240859
Convert colored map to label ids map
AmrElsersy df6eda1
Merge branch 'main' into segmentation
adlarkin f4f3761
Style SegmentationType
AmrElsersy 1cef192
solve many subitems problem
AmrElsersy b8415a3
Solve Multi Link models problem
AmrElsersy 8139028
Merge branch 'main' into segmentation
adlarkin 798c0ed
Merge branch 'main' of https://github.com/ignitionrobotics/ign-render…
AmrElsersy 35f5dfd
Merge branch 'main' into segmentation
adlarkin 3d24d2c
style
AmrElsersy b18f377
Merge branch 'segmentation' of https://github.com/AmrElsersy/ign-rend…
AmrElsersy ac4b604
review feedback
adlarkin 4f0209d
use TopLevelModelVisual method instead of hardcoded string
adlarkin 2cbfe91
use iterator to avoid multiple lookups
adlarkin de63a56
Move MaterialSwitcher into separate TU
4ff40ee
Merge branch 'main' into segmentation
05da804
Update for OGRE 2.2
3dff4dc
Fix workspace creation
8f90b62
Fix readback
3c2e70b
Fix background
5e78c76
Fix order of destruction
c4e0685
fix tests
iche033 e185ecd
final edits
adlarkin 31547f9
codecheck
adlarkin 8f25b09
windows warnings
adlarkin 4ed1b40
macOS warning
adlarkin 3e3711b
add panoptic pixel order documentation to public API
adlarkin e6e7579
Merge branch 'main' into segmentation
iche033 b40fa21
fix build from recent merge
adlarkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR) | ||
| project(ignition-rendering-segmentation-camera) | ||
| find_package(ignition-rendering6 REQUIRED) | ||
|
|
||
| include_directories(SYSTEM | ||
| ${PROJECT_BINARY_DIR} | ||
| ) | ||
|
|
||
| find_package(GLUT REQUIRED) | ||
| include_directories(SYSTEM ${GLUT_INCLUDE_DIRS}) | ||
| link_directories(${GLUT_LIBRARY_DIRS}) | ||
|
|
||
| find_package(OpenGL REQUIRED) | ||
| include_directories(SYSTEM ${OpenGL_INCLUDE_DIRS}) | ||
| link_directories(${OpenGL_LIBRARY_DIRS}) | ||
|
|
||
| if (NOT APPLE) | ||
| find_package(GLEW REQUIRED) | ||
| include_directories(SYSTEM ${GLEW_INCLUDE_DIRS}) | ||
| link_directories(${GLEW_LIBRARY_DIRS}) | ||
| endif() | ||
|
|
||
| set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations") | ||
|
|
||
| configure_file (example_config.hh.in ${PROJECT_BINARY_DIR}/example_config.hh) | ||
|
|
||
| add_executable(segmentation_camera Main.cc GlutWindow.cc) | ||
|
|
||
| target_link_libraries(segmentation_camera | ||
| ${GLUT_LIBRARIES} | ||
| ${OPENGL_LIBRARIES} | ||
| ${GLEW_LIBRARIES} | ||
| ${IGNITION-RENDERING_LIBRARIES} | ||
| ) | ||
|
|
||
| add_custom_command(TARGET segmentation_camera POST_BUILD | ||
| COMMAND ${CMAKE_COMMAND} -E copy_directory | ||
| ${CMAKE_SOURCE_DIR}/media | ||
| $<TARGET_FILE_DIR:segmentation_camera>/media) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,357 @@ | ||
| /* | ||
| * Copyright (C) 2021 Open Source Robotics Foundation | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| #if __APPLE__ | ||
| #include <OpenGL/gl.h> | ||
| #include <OpenGL/OpenGL.h> | ||
| #include <GLUT/glut.h> | ||
| #else | ||
| #include <GL/glew.h> | ||
| #include <GL/gl.h> | ||
| #include <GL/glut.h> | ||
| #endif | ||
|
|
||
| #if !defined(__APPLE__) && !defined(_WIN32) | ||
| #include <GL/glx.h> | ||
| #endif | ||
|
|
||
| #include <memory> | ||
| #include <mutex> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include <ignition/common/Console.hh> | ||
| #include <ignition/math/Vector2.hh> | ||
| #include <ignition/rendering/Camera.hh> | ||
| #include <ignition/rendering/Image.hh> | ||
| #include <ignition/rendering/RayQuery.hh> | ||
| #include <ignition/rendering/Scene.hh> | ||
| #include <ignition/rendering/OrbitViewController.hh> | ||
| #include <ignition/rendering/SegmentationCamera.hh> | ||
AmrElsersy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include "GlutWindow.hh" | ||
|
|
||
| #define KEY_ESC 27 | ||
| #define UNSUPPORTED_BUTTONS 5 | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| unsigned int imgw = 0; | ||
| unsigned int imgh = 0; | ||
|
|
||
| ir::CameraPtr g_camera; | ||
| ir::ImagePtr g_image; | ||
| ignition::common::ConnectionPtr g_connection; | ||
|
|
||
| bool g_initContext = false; | ||
|
|
||
| #if __APPLE__ | ||
| CGLContextObj g_context; | ||
| CGLContextObj g_glutContext; | ||
| #elif _WIN32 | ||
| #else | ||
| GLXContext g_context; | ||
| Display *g_display; | ||
| GLXDrawable g_drawable; | ||
| GLXContext g_glutContext; | ||
| Display *g_glutDisplay; | ||
| GLXDrawable g_glutDrawable; | ||
| #endif | ||
|
|
||
| // view control variables | ||
| ir::RayQueryPtr g_rayQuery; | ||
| ir::OrbitViewController g_viewControl; | ||
| ir::RayQueryResult g_target; | ||
| struct mouseButton | ||
| { | ||
| int button = 0; | ||
| int state = GLUT_UP; | ||
| int x = 0; | ||
| int y = 0; | ||
| int motionX = 0; | ||
| int motionY = 0; | ||
| int dragX = 0; | ||
| int dragY = 0; | ||
| int scroll = 0; | ||
| bool buttonDirty = false; | ||
| bool motionDirty = false; | ||
| }; | ||
| struct mouseButton g_mouse; | ||
| std::mutex g_mouseMutex; | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| void mouseCB(int _button, int _state, int _x, int _y) | ||
| { | ||
| // ignore unknown mouse button numbers | ||
| if (_button >= UNSUPPORTED_BUTTONS) | ||
| return; | ||
|
|
||
| std::lock_guard<std::mutex> lock(g_mouseMutex); | ||
| g_mouse.button = _button; | ||
| g_mouse.state = _state; | ||
| g_mouse.x = _x; | ||
| g_mouse.y = _y; | ||
| g_mouse.motionX = _x; | ||
| g_mouse.motionY = _y; | ||
| g_mouse.buttonDirty = true; | ||
| } | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| void motionCB(int _x, int _y) | ||
| { | ||
| std::lock_guard<std::mutex> lock(g_mouseMutex); | ||
| int deltaX = _x - g_mouse.motionX; | ||
| int deltaY = _y - g_mouse.motionY; | ||
| g_mouse.motionX = _x; | ||
| g_mouse.motionY = _y; | ||
|
|
||
| if (g_mouse.motionDirty) | ||
| { | ||
| g_mouse.dragX += deltaX; | ||
| g_mouse.dragY += deltaY; | ||
| } | ||
| else | ||
| { | ||
| g_mouse.dragX = deltaX; | ||
| g_mouse.dragY = deltaY; | ||
| } | ||
| g_mouse.motionDirty = true; | ||
| } | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| void handleMouse() | ||
| { | ||
| std::lock_guard<std::mutex> lock(g_mouseMutex); | ||
| // only ogre supports ray query for now so use | ||
| // ogre camera located at camera index = 0. | ||
| ir::CameraPtr rayCamera = g_camera; | ||
| if (!g_rayQuery) | ||
| { | ||
| g_rayQuery = rayCamera->Scene()->CreateRayQuery(); | ||
| if (!g_rayQuery) | ||
| { | ||
| ignerr << "Failed to create Ray Query" << std::endl; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (g_mouse.buttonDirty) | ||
| { | ||
| g_mouse.buttonDirty = false; | ||
| double nx = | ||
| 2.0 * g_mouse.x / static_cast<double>(rayCamera->ImageWidth()) - 1.0; | ||
| double ny = 1.0 - | ||
| 2.0 * g_mouse.y / static_cast<double>(rayCamera->ImageHeight()); | ||
|
|
||
| // g_rayQuery->SetFromCamera(rayCamera, ignition::math::Vector2d(nx, ny)); | ||
| // g_target = g_rayQuery->ClosestPoint(); | ||
| // if (!g_target) | ||
| // { | ||
| // // set point to be 10m away if no intersection found | ||
| // g_target.point = g_rayQuery->Origin() + g_rayQuery->Direction() * 10; | ||
| // return; | ||
| // } | ||
adlarkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // mouse wheel scroll zoom | ||
adlarkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if ((g_mouse.button == 3 || g_mouse.button == 4) && | ||
| g_mouse.state == GLUT_UP) | ||
| { | ||
| double scroll = (g_mouse.button == 3) ? -1.0 : 1.0; | ||
| double distance = rayCamera->WorldPosition().Distance( | ||
| g_target.point); | ||
| int factor = 1; | ||
| double amount = -(scroll * factor) * (distance / 5.0); | ||
|
|
||
| g_viewControl.SetCamera(g_camera); | ||
| g_viewControl.SetTarget(g_target.point); | ||
| g_viewControl.Zoom(amount); | ||
| } | ||
| } | ||
|
|
||
AmrElsersy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (g_mouse.motionDirty) | ||
| { | ||
| g_mouse.motionDirty = false; | ||
| auto drag = ignition::math::Vector2d(g_mouse.dragX, g_mouse.dragY); | ||
|
|
||
| // left mouse button pan | ||
| if (g_mouse.button == GLUT_LEFT_BUTTON && g_mouse.state == GLUT_DOWN) | ||
| { | ||
| g_viewControl.SetCamera(g_camera); | ||
| g_viewControl.SetTarget(g_target.point); | ||
| g_viewControl.Pan(drag); | ||
| } | ||
| else if (g_mouse.button == GLUT_MIDDLE_BUTTON && g_mouse.state == GLUT_DOWN) | ||
| { | ||
| g_viewControl.SetCamera(g_camera); | ||
| g_viewControl.SetTarget(g_target.point); | ||
| g_viewControl.Orbit(drag); | ||
| } | ||
| // right mouse button zoom | ||
adlarkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else if (g_mouse.button == GLUT_RIGHT_BUTTON && g_mouse.state == GLUT_DOWN) | ||
| { | ||
| double hfov = rayCamera->HFOV().Radian(); | ||
| double vfov = 2.0f * atan(tan(hfov / 2.0f) / | ||
| rayCamera->AspectRatio()); | ||
| double distance = rayCamera->WorldPosition().Distance( | ||
| g_target.point); | ||
| double amount = ((-g_mouse.dragY / | ||
| static_cast<double>(rayCamera->ImageHeight())) | ||
| * distance * tan(vfov/2.0) * 6.0); | ||
|
|
||
| g_viewControl.SetCamera(g_camera); | ||
| g_viewControl.SetTarget(g_target.point); | ||
| g_viewControl.Zoom(amount); | ||
| } | ||
| } | ||
| } | ||
AmrElsersy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
AmrElsersy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ////////////////////////////////////////////////// | ||
| void displayCB() | ||
| { | ||
| #if __APPLE__ | ||
| CGLSetCurrentContext(g_context); | ||
| #elif _WIN32 | ||
| #else | ||
| if (g_display) | ||
| { | ||
| glXMakeCurrent(g_display, g_drawable, g_context); | ||
| } | ||
| #endif | ||
|
|
||
| g_camera->Update(); | ||
|
|
||
| handleMouse(); | ||
|
|
||
| #if __APPLE__ | ||
| CGLSetCurrentContext(g_glutContext); | ||
| #elif _WIN32 | ||
| #else | ||
| glXMakeCurrent(g_glutDisplay, g_glutDrawable, g_glutContext); | ||
| #endif | ||
|
|
||
| unsigned char *data = g_image->Data<unsigned char>(); | ||
|
|
||
| glClearColor(0.5, 0.5, 0.5, 1); | ||
| glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||
| glPixelZoom(1, -1); | ||
| glRasterPos2f(-1, 1); | ||
| glDrawPixels(imgw, imgh, GL_RGB, GL_UNSIGNED_BYTE, data); | ||
|
|
||
| glutSwapBuffers(); | ||
| } | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| void idleCB() | ||
| { | ||
| glutPostRedisplay(); | ||
| } | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| void keyboardCB(unsigned char _key, int, int) | ||
| { | ||
| if (_key == KEY_ESC || _key == 'q' || _key == 'Q') | ||
| { | ||
| exit(0); | ||
| } | ||
| } | ||
|
|
||
| ////////////////////////////////////////////////// | ||
AmrElsersy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| void OnNewSegmentationFrame(const uint8_t *_scan, | ||
| unsigned int _width, unsigned int _height, | ||
| unsigned int /*_channels*/, | ||
| const std::string &_format) | ||
| { | ||
| unsigned char *data = g_image->Data<unsigned char>(); | ||
| auto bufferSize = _width * _height * 3; | ||
| memcpy(data, _scan, bufferSize); | ||
| } | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| void initCamera(ir::CameraPtr _camera) | ||
| { | ||
| g_camera = _camera; | ||
| imgw = g_camera->ImageWidth(); | ||
| imgh = g_camera->ImageHeight(); | ||
| ir::Image image = g_camera->CreateImage(); | ||
| g_image = std::make_shared<ir::Image>(image); | ||
|
|
||
| ir::SegmentationCameraPtr camera = std::dynamic_pointer_cast<ir::SegmentationCamera>( | ||
| g_camera); | ||
|
|
||
| // callback when new segmentation frame is received. | ||
| g_connection = camera->ConnectNewSegmentationFrame( | ||
| std::bind(OnNewSegmentationFrame, | ||
| std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, | ||
| std::placeholders::_4, std::placeholders::_5)); | ||
|
|
||
| g_camera->Update(); | ||
| } | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| void initContext() | ||
| { | ||
| glutInitDisplayMode(GLUT_DOUBLE); | ||
| glutInitWindowPosition(0, 0); | ||
| glutInitWindowSize(imgw, imgh); | ||
| glutCreateWindow("Segmentation Camera"); | ||
| glutDisplayFunc(displayCB); | ||
| glutIdleFunc(idleCB); | ||
| glutKeyboardFunc(keyboardCB); | ||
|
|
||
| glutMouseFunc(mouseCB); | ||
| glutMotionFunc(motionCB); | ||
| } | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| void printUsage() | ||
| { | ||
| std::cout << "===============================" << std::endl; | ||
| std::cout << " ESC - Exit " << std::endl; | ||
| std::cout << "===============================" << std::endl; | ||
adlarkin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| void run(ir::CameraPtr _camera) | ||
| { | ||
| if (!_camera) | ||
| { | ||
| ignerr << "No camera found. Scene will not be rendered" << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| #if __APPLE__ | ||
| g_context = CGLGetCurrentContext(); | ||
| #elif _WIN32 | ||
| #else | ||
| g_context = glXGetCurrentContext(); | ||
| g_display = glXGetCurrentDisplay(); | ||
| g_drawable = glXGetCurrentDrawable(); | ||
| #endif | ||
|
|
||
| initCamera(_camera); | ||
| initContext(); | ||
| printUsage(); | ||
|
|
||
| #if __APPLE__ | ||
| g_glutContext = CGLGetCurrentContext(); | ||
| #elif _WIN32 | ||
| #else | ||
| g_glutDisplay = glXGetCurrentDisplay(); | ||
| g_glutDrawable = glXGetCurrentDrawable(); | ||
| g_glutContext = glXGetCurrentContext(); | ||
| #endif | ||
|
|
||
| glutMainLoop(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.