Skip to content

Commit

Permalink
align exmples includes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nir-Az committed Mar 3, 2021
1 parent 141ed25 commit bf7b6fc
Show file tree
Hide file tree
Showing 21 changed files with 260 additions and 252 deletions.
1 change: 1 addition & 0 deletions examples/C/color/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project(RealsenseExamples-Color)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

add_executable(rs-color rs-color.c ../example.h)
include_directories(../../common ../../third-party/imgui ../../C)
target_link_libraries(rs-color ${DEPENDENCIES})
set_target_properties (rs-color PROPERTIES
FOLDER "Examples/C"
Expand Down
2 changes: 1 addition & 1 deletion examples/C/color/rs-color.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "../example.h"
#include "example.h"


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions examples/C/depth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project(RealsenseExamples-Depth)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

add_executable(rs-depth rs-depth.c ../example.h)
include_directories(../../C)
target_link_libraries(rs-depth ${DEPENDENCIES})
set_target_properties (rs-depth PROPERTIES
FOLDER "Examples/C"
Expand Down
2 changes: 1 addition & 1 deletion examples/C/depth/rs-depth.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <librealsense2/h/rs_pipeline.h>
#include <librealsense2/h/rs_option.h>
#include <librealsense2/h/rs_frame.h>
#include "../example.h"
#include "example.h"

#include <stdlib.h>
#include <stdint.h>
Expand Down
1 change: 1 addition & 0 deletions examples/C/distance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project(RealsenseExamples-Distance)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

add_executable(rs-distance rs-distance.c ../example.h)
include_directories(../../C)
target_link_libraries(rs-distance ${DEPENDENCIES})
set_target_properties (rs-distance PROPERTIES
FOLDER "Examples/C"
Expand Down
2 changes: 1 addition & 1 deletion examples/C/distance/rs-distance.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <librealsense2/h/rs_pipeline.h>
#include <librealsense2/h/rs_option.h>
#include <librealsense2/h/rs_frame.h>
#include "../example.h"
#include "example.h"

#include <stdlib.h>
#include <stdint.h>
Expand Down
2 changes: 1 addition & 1 deletion examples/align-advanced/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if(BUILD_GRAPHICAL_EXAMPLES)
add_executable(rs-align-advanced rs-align-advanced.cpp ../../third-party/imgui/imgui.cpp ../../third-party/imgui/imgui_draw.cpp ../../third-party/imgui/imgui_impl_glfw.cpp)
set_property(TARGET rs-align-advanced PROPERTY CXX_STANDARD 11)
target_link_libraries(rs-align-advanced ${DEPENDENCIES})
include_directories(../../common ../../third-party/imgui)
include_directories(../../common ../../third-party/imgui ../../examples)
set_target_properties (rs-align-advanced PROPERTIES FOLDER Examples)
install(TARGETS rs-align-advanced RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
4 changes: 1 addition & 3 deletions examples/align-advanced/rs-align-advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.

#include <librealsense2/rs.hpp>
#include "../example.hpp"
#include <imgui.h>
#include "imgui_impl_glfw.h"
#include "example-imgui.hpp"

#include <sstream>
#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion examples/align/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if(BUILD_GRAPHICAL_EXAMPLES)
add_executable(rs-align rs-align.cpp ../../third-party/imgui/imgui.cpp ../../third-party/imgui/imgui_draw.cpp ../../third-party/imgui/imgui_impl_glfw.cpp)
set_property(TARGET rs-align PROPERTY CXX_STANDARD 11)
target_link_libraries(rs-align ${DEPENDENCIES})
include_directories(../../common ../../third-party/imgui)
include_directories(../../common ../../third-party/imgui ../../examples)
set_target_properties (rs-align PROPERTIES FOLDER Examples)
install(TARGETS rs-align RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
4 changes: 1 addition & 3 deletions examples/align/rs-align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.

#include <librealsense2/rs.hpp>
#include "../example.hpp"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "example-imgui.hpp"

/*
This example introduces the concept of spatial stream alignment.
Expand Down
2 changes: 1 addition & 1 deletion examples/ar-advanced/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ endif()

if(BUILD_GRAPHICAL_EXAMPLES)
add_executable(rs-ar-advanced rs-ar-advanced.cpp ../example.hpp)
target_include_directories(rs-ar-advanced PUBLIC ../)
target_include_directories(rs-ar-advanced PUBLIC ../../examples)
target_link_libraries(rs-ar-advanced ${DEPENDENCIES})
set_target_properties (rs-ar-advanced PROPERTIES FOLDER Examples)
install(TARGETS rs-ar-advanced RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Expand Down
10 changes: 5 additions & 5 deletions examples/ar-basic/rs-ar-basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int main(int argc, char * argv[]) try
// Create the vertices of a simple virtual object.
// This virtual object is 4 points in 3D space that describe 3 XYZ 20cm long axes.
// These vertices are relative to the object's own coordinate system.
const float length = 0.20;
const float length = 0.20f;
const object virtual_object = {{
{ 0, 0, 0 }, // origin
{ length, 0, 0 }, // X
Expand Down Expand Up @@ -139,7 +139,7 @@ int main(int argc, char * argv[]) try
for (size_t i = 1; i < object_in_sensor.size(); ++i)
{
// Discretize the virtual object line into smaller 1cm long segments
std::vector<point3d> points_in_sensor = raster_line(object_in_sensor[0], object_in_sensor[i], 0.01);
std::vector<point3d> points_in_sensor = raster_line(object_in_sensor[0], object_in_sensor[i], 0.01f);
std::vector<pixel> projected_line;
projected_line.reserve(points_in_sensor.size());
for (auto& point : points_in_sensor)
Expand All @@ -153,11 +153,11 @@ int main(int argc, char * argv[]) try
}
}
// Display the line in the image
render_line(projected_line, i);
render_line(projected_line, static_cast<int>(i));
}

// Display text in the image
render_text(app.height(), "Press spacebar to reset the pose of the virtual object. Press ESC to exit");
render_text(static_cast<int>(app.height()), "Press spacebar to reset the pose of the virtual object. Press ESC to exit");

// Check if some key is pressed
switch (key_watcher.get_key())
Expand Down Expand Up @@ -289,7 +289,7 @@ std::vector<point3d> raster_line(const point3d& a, const point3d& b, float step)
{
rs2_vector direction = { b.x() - a.x(), b.y() - a.y(), b.z() - a.z() };
float distance = std::sqrt(direction.x*direction.x + direction.y*direction.y + direction.z*direction.z);
int npoints = distance / step + 1;
int npoints = static_cast<int>(distance / step + 1);

std::vector<point3d> points;
if (npoints > 0)
Expand Down
2 changes: 1 addition & 1 deletion examples/capture/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if(BUILD_GRAPHICAL_EXAMPLES)
add_executable(rs-capture rs-capture.cpp ../example.hpp)
set_property(TARGET rs-capture PROPERTY CXX_STANDARD 11)
target_link_libraries(rs-capture ${DEPENDENCIES})
include_directories(../ ../../third-party/tclap/include)
include_directories(../ ../../third-party/tclap/include ../../examples)
set_target_properties (rs-capture PROPERTIES FOLDER "Examples")

install(TARGETS rs-capture RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
Expand Down
210 changes: 210 additions & 0 deletions examples/example-imgui.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2021 Intel Corporation. All Rights Reserved.

#pragma once

#include <string>
#include <cmath>
#include <map>

#include "example.hpp"
#include <imgui.h>
#include <imgui_impl_glfw.h>


//////////////////////////////
// ImGui Helpers //
//////////////////////////////

//slider for ImGui
class slider {
public:
slider(const char* name, int seq_id, float init_value, float min_value, float max_value, ImVec2 position, ImVec2 size) :
_name(name), _seq_id(seq_id), _value(init_value), _min_value(min_value), _max_value(max_value), _position(position), _size(size) {}

void virtual show()=0;

public:
const char* _name;
int _seq_id;
float _value;
float _max_value;
float _min_value;
ImVec2 _position;
ImVec2 _size;

};

class hdr_slider : public slider {
public:
hdr_slider(const char* name, int seq_id, float init_value, rs2::sensor& sensor,
rs2_option option, rs2::option_range range, ImVec2 position, ImVec2 size) : slider(name, seq_id, init_value, range.min, range.max, position, size),
_sensor(sensor), _option(option), _range(range){}

void show() override
{
ImGui::SetNextWindowSize(_size);
ImGui::SetNextWindowPos(_position);
//concate the name given with seq_id in order to make a unique name (uniqeness is needed for Begin())
std::string name_id = std::string(_name) + std::to_string(_seq_id);
ImGui::Begin(name_id.c_str(), nullptr, _sliders_flags);
ImGui::Text("%s",_name);
bool is_changed =
ImGui::SliderFloat("", &_value, _min_value, _max_value, "%.3f", 5.0f, false); //5.0f for logarithmic scale
if (is_changed) {
_sensor.set_option(RS2_OPTION_SEQUENCE_ID, float(_seq_id));
_sensor.set_option(_option, _value);
}
ImGui::End();
}

public:
rs2::sensor& _sensor;
rs2_option _option;
rs2::option_range _range;
//flags for the sliders
const static int _sliders_flags = ImGuiWindowFlags_NoCollapse
| ImGuiWindowFlags_NoScrollbar
| ImGuiWindowFlags_NoSavedSettings
| ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoTitleBar
| ImGuiWindowFlags_NoBringToFrontOnFocus;
};

//text box for ImGui
class text_box {
public:
text_box(const char* name, ImVec2 position, ImVec2 size) : _name(name), _position(position), _size(size) {}

void show(const char* text)
{
ImGui::SetNextWindowSize(_size);
ImGui::SetNextWindowPos(_position);
ImGui::Begin(_name, nullptr, _text_box_flags);
ImGui::Text("%s",text);

ImGui::End();
}
void remove_title_bar() {
_text_box_flags |= ImGuiWindowFlags_NoTitleBar;
}

public:
const char* _name;
ImVec2 _position;
ImVec2 _size;
// flags for displaying text box
int _text_box_flags = ImGuiWindowFlags_NoCollapse
| ImGuiWindowFlags_NoScrollbar
| ImGuiWindowFlags_NoSavedSettings
| ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoFocusOnAppearing
| ImGuiWindowFlags_AlwaysUseWindowPadding
| ImGuiWindowFlags_NoBringToFrontOnFocus
| ImGuiWindowFlags_AlwaysAutoResize;
};


class hdr_widgets {
public:
// c'tor that creats all 4 sliders and text boxes
// needed to init in an init list because no default c'tor for sliders and they are allocated inside hdr_widgets
hdr_widgets(rs2::depth_sensor& depth_sensor):
_exposure_slider_seq_1("Exposure", 1, 8000,
depth_sensor, RS2_OPTION_EXPOSURE, depth_sensor.get_option_range(RS2_OPTION_EXPOSURE), { 130, 180 }, { 350, 40 }),
_exposure_slider_seq_2("Exposure", 2, 18,
depth_sensor, RS2_OPTION_EXPOSURE, depth_sensor.get_option_range(RS2_OPTION_EXPOSURE), { 390, 180 }, { 350, 40 }),
_gain_slider_seq_1("Gain", 1, 25,
depth_sensor, RS2_OPTION_GAIN, depth_sensor.get_option_range(RS2_OPTION_GAIN), { 130, 220 }, { 350, 40 }),
_gain_slider_seq_2("Gain", 2, 16,
depth_sensor, RS2_OPTION_GAIN, depth_sensor.get_option_range(RS2_OPTION_GAIN), { 390, 220 }, { 350, 40 }),
_text_box_hdr_explain("HDR Tutorial", { 120, 20 }, { 1000, 140 }),
_text_box_first_frame("frame 1", { 200, 150 }, { 170, 40 }),
_text_box_second_frame("frame 2", { 460, 150 }, { 170, 40 }),
_text_box_hdr_frame("hdr", { 850, 280 }, { 170, 40 })
{
// init frames map
//for initilize only - an empty frame with its properties
rs2::frame frame;

//set each frame with its properties:
// { tile's x coordinate, tiles's y coordinate, tile's width (in tiles), tile's height (in tiles), priority (default value=0) }, (x=0,y=0) <-> left bottom corner
//priority sets the order of drawing frame when two frames share part of the same tile,
//meaning if there are two frames: frame1 with priority=-1 and frame2 with priority=0, both with { 0,0,1,1 } as property,
//frame2 will be drawn on top of frame1
_frames_map[IR1] = frame_and_tile_property(frame, { 0,0,1,1,Priority::high });
_frames_map[IR2] = frame_and_tile_property(frame, { 1,0,1,1,Priority::high });
_frames_map[DEPTH1] = frame_and_tile_property(frame,{ 0,1,1,1,Priority::high });
_frames_map[DEPTH2] = frame_and_tile_property(frame, { 1,1,1,1,Priority::high });
_frames_map[HDR] = frame_and_tile_property(frame, { 2,0,2,2,Priority::high });
}

//show the features of the ImGui we have created
//we need slider 2 to be showen before slider 1 (otherwise slider 1 padding is covering slider 2)
void render_widgets() {

//start a new frame of ImGui
ImGui_ImplGlfw_NewFrame(1);

_exposure_slider_seq_2.show();
_exposure_slider_seq_1.show();
_gain_slider_seq_2.show();
_gain_slider_seq_1.show();

_text_box_first_frame.remove_title_bar();
_text_box_first_frame.show("Sequence 1");

_text_box_second_frame.remove_title_bar();
_text_box_second_frame.show("Sequence 2");

_text_box_hdr_frame.remove_title_bar();
_text_box_hdr_frame.show("HDR Stream");
_text_box_hdr_explain.show("This demo provides a quick overview of the High Dynamic Range (HDR) feature.\nThe HDR configures and operates on sequences of two frames configurations, for which separate exposure and gain values are defined.\nBoth configurations are streamed and the HDR feature uses both frames in order to provide the best depth image.\nChange the values of the sliders to see the impact on the HDR Depth Image.");

//render the ImGui features: sliders and text
ImGui::Render();

}

// return a reference to frames map
frames_mosaic& get_frames_map() {
return _frames_map;
}

void update_frames_map(const rs2::video_frame& infrared_frame, const rs2::frame& depth_frame,
const rs2::frame& hdr_frame, rs2_metadata_type hdr_seq_id, rs2_metadata_type hdr_seq_size) {

// frame index in frames_map are according to hdr_seq_id and hdr_seq_size
int infrared_index = int(hdr_seq_id);
int depth_index = int(hdr_seq_id + hdr_seq_size);
int hdr_index = int(hdr_seq_id + hdr_seq_size + 1);

//work-around, 'get_frame_metadata' sometimes (after changing exposure or gain values) sets hdr_seq_size to 0 even though it 2 for few frames
//so we update the frames only if hdr_seq_size > 0. (hdr_seq_size==0 <-> frame is invalid)
if (hdr_seq_size > 0) {
_frames_map[infrared_index].first = infrared_frame;
_frames_map[depth_index].first = depth_frame;
_frames_map[hdr_index].first = hdr_frame; //HDR shall be after IR1/2 & DEPTH1/2
}
}

public:

frames_mosaic _frames_map;

hdr_slider _exposure_slider_seq_1;
hdr_slider _gain_slider_seq_1;
hdr_slider _exposure_slider_seq_2;
hdr_slider _gain_slider_seq_2;

text_box _text_box_hdr_explain;
text_box _text_box_first_frame;
text_box _text_box_second_frame;
text_box _text_box_hdr_frame;

enum frame_id { IR1, IR2, DEPTH1, DEPTH2, HDR };

};

Loading

0 comments on commit bf7b6fc

Please sign in to comment.